2025-11-27 17:58:57 +00:00

86 lines
2.6 KiB
C#

using LearnOpenTK.Common;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace TheLabs;
public class RenderObject
{
public Mesh Mesh;
public Shader Shader;
public Texture Texture;
public Texture NormalMap;
public Material Material;
public Lighting Lighting;
public Vector3 Position = Vector3.Zero;
public Matrix4 Transform = Matrix4.Identity;
public Quaternion Rotation = Quaternion.Identity;
public Vector3 Scale = Vector3.One;
public bool UseTexture = true;
public RenderObject(Mesh mesh, Shader shader, Texture texture, Material material, Lighting lighting)
{
Mesh = mesh;
Shader = shader;
Texture = texture;
Material = material;
Lighting = lighting;
}
public RenderObject(Mesh mesh, Shader shader)
{
Mesh = mesh;
Shader = shader;
}
public void Draw(Matrix4 view, Matrix4 projection, Lighting light)
{
Vector3 pink = new Vector3(0.5f, 0.5f, 0.5f);
Material mat = new Material(pink * 0.1f, pink * 0.6f, Vector3.One, 32.0f);
Shader.Use();
if (Texture != null && UseTexture)
{
Texture.Use(TextureUnit.Texture0);
Shader.SetInt("uTexture", 0);
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 1);
}
else
{
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 0);
}
if (NormalMap != null)
{
NormalMap.Use(TextureUnit.Texture1);
Shader.SetInt("uNormalMap", 1);
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseNormalMap"), 1);
}
else
{
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseNormalMap"), 0);
}
Material.Apply(Shader);
light.Apply(Shader);
Vector3 cameraPos = view.Inverted().ExtractTranslation();
Shader.SetVector3("uViewPos", cameraPos);
Matrix4 model = Matrix4.CreateScale(Scale) * Matrix4.CreateFromQuaternion(Rotation) * Matrix4.CreateTranslation(Position);
int modelLoc = GL.GetUniformLocation(Shader.Handle, "model");
GL.UniformMatrix4(modelLoc, false, ref model); // Try TRUE here
int viewLoc = GL.GetUniformLocation(Shader.Handle, "view");
GL.UniformMatrix4(viewLoc, false, ref view); // Try TRUE here
int projLoc = GL.GetUniformLocation(Shader.Handle, "projection");
GL.UniformMatrix4(projLoc, false, ref projection); // Try TRUE here
// 5. Tell the Mesh to draw itself
Mesh.Draw();
}
}