mirror of
https://github.com/UOH-CS-Level5/551455-graphics-programming-2526-the-repo-Zyb3rWolfi.git
synced 2025-11-29 00:43:08 +00:00
84 lines
2.5 KiB
C#
84 lines
2.5 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)
|
|
{
|
|
|
|
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();
|
|
}
|
|
|
|
} |