display vertex colors

This commit is contained in:
4sval 2022-08-30 19:40:27 +02:00
parent 009ad9dc55
commit 37b5989378
4 changed files with 61 additions and 34 deletions

View File

@ -3,6 +3,7 @@
in vec3 fPos;
in vec3 fNormal;
in vec2 fTexCoords;
in vec4 fColor;
struct Material {
sampler2D diffuseMap;
@ -30,6 +31,7 @@ struct Light {
uniform Material material;
uniform Light light;
uniform vec3 viewPos;
uniform bool display_vertex_colors;
out vec4 FragColor;
@ -52,7 +54,11 @@ vec3 getNormalFromMap()
void main()
{
if (material.hasDiffuseColor)
if (display_vertex_colors)
{
FragColor = fColor;
}
else if (material.hasDiffuseColor)
{
FragColor = material.diffuseColor;
}

View File

@ -3,6 +3,7 @@
layout (location = 0) in vec3 vPos;
layout (location = 1) in vec3 vNormal;
layout (location = 2) in vec2 vTexCoords;
layout (location = 3) in vec4 vColor;
uniform mat4 uModel;
uniform mat4 uView;
@ -11,6 +12,7 @@ uniform mat4 uProjection;
out vec3 fPos;
out vec3 fNormal;
out vec2 fTexCoords;
out vec4 fColor;
void main()
{
@ -19,4 +21,5 @@ void main()
fPos = vec3(uModel * vec4(vPos, 1.0));
fNormal = mat3(transpose(inverse(uModel))) * vNormal;
fTexCoords = vTexCoords;
fColor = vColor;
}

View File

@ -12,22 +12,29 @@ public class Model : IDisposable
private uint _handle;
private GL _gl;
private Shader _shader;
private BufferObject<uint> _ebo;
private BufferObject<float> _vbo;
private VertexArrayObject<float, uint> _vao;
private const int _vertexSize = 8; // Position + Normal + UV
private uint _vertexSize = 8; // Position + Normal + UV
private const uint _faceSize = 3; // just so we don't have to do .Length
private readonly uint[] _facesIndex = { 1, 0, 2 };
public readonly string Name;
public readonly bool HasVertexColors;
public readonly uint[] Indices;
public readonly float[] Vertices;
public readonly Section[] Sections;
private bool _display_vertex_colors;
public Model(string name, CBaseMeshLod lod, CMeshVertex[] vertices)
{
Name = name;
HasVertexColors = lod.VertexColors != null;
if (HasVertexColors) _vertexSize += 4; // + Color
var sections = lod.Sections.Value;
Sections = new Section[sections.Length];
@ -56,6 +63,15 @@ public class Model : IDisposable
Vertices[index * _vertexSize + 6] = vert.UV.U;
Vertices[index * _vertexSize + 7] = vert.UV.V;
if (HasVertexColors)
{
var color = lod.VertexColors[indice];
Vertices[index * _vertexSize + 8] = color.R;
Vertices[index * _vertexSize + 9] = color.G;
Vertices[index * _vertexSize + 10] = color.B;
Vertices[index * _vertexSize + 11] = color.A;
}
Indices[index] = i;
}
}
@ -68,6 +84,8 @@ public class Model : IDisposable
_handle = _gl.CreateProgram();
_shader = new Shader(_gl);
_ebo = new BufferObject<uint>(_gl, Indices, BufferTargetARB.ElementArrayBuffer);
_vbo = new BufferObject<float>(_gl, Vertices, BufferTargetARB.ArrayBuffer);
_vao = new VertexArrayObject<float, uint>(_gl, _vbo, _ebo);
@ -75,6 +93,7 @@ public class Model : IDisposable
_vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, _vertexSize, 0); // position
_vao.VertexAttributePointer(1, 3, VertexAttribPointerType.Float, _vertexSize, 3); // normal
_vao.VertexAttributePointer(2, 2, VertexAttribPointerType.Float, _vertexSize, 6); // uv
_vao.VertexAttributePointer(3, 4, VertexAttribPointerType.Float, _vertexSize, 8); // color
for (int section = 0; section < Sections.Length; section++)
{
@ -85,16 +104,26 @@ public class Model : IDisposable
public void Bind(Camera camera)
{
ImGui.Text($"Entity: {Name}");
if (HasVertexColors)
ImGui.Checkbox("Display Vertex Colors", ref _display_vertex_colors);
_vao.Bind();
_shader.Use();
_shader.SetUniform("uModel", Matrix4x4.Identity);
_shader.SetUniform("uView", camera.GetViewMatrix());
_shader.SetUniform("uProjection", camera.GetProjectionMatrix());
_shader.SetUniform("viewPos", camera.Position);
_shader.SetUniform("display_vertex_colors", _display_vertex_colors);
ImGui.BeginTable("Sections", 2, ImGuiTableFlags.RowBg);
ImGui.TableSetupColumn("Index", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableSetupColumn("Material", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableHeadersRow();
for (int section = 0; section < Sections.Length; section++)
{
Sections[section].Bind(camera, Indices.Length);
Sections[section].Bind(_shader, camera, Indices.Length);
// if (!Sections[section].Show) continue;
_gl.DrawArrays(PrimitiveType.Triangles, Sections[section].FirstFaceIndex, Sections[section].FacesCount);
}
@ -105,6 +134,7 @@ public class Model : IDisposable
public void Dispose()
{
_shader.Dispose();
_ebo.Dispose();
_vbo.Dispose();
_vao.Dispose();

View File

@ -31,8 +31,6 @@ public class Section : IDisposable
private Vector3 _diffuseLight;
private Vector3 _specularLight;
private Shader _shader;
public readonly string Name;
public readonly int Index;
public readonly uint FacesCount;
@ -68,8 +66,6 @@ public class Section : IDisposable
_handle = _gl.CreateProgram();
_shader = new Shader(_gl);
if (Parameters.IsNull)
{
_diffuseColor = new Vector4(1, 0, 0, 1);
@ -226,7 +222,7 @@ public class Section : IDisposable
}
}
public void Bind(Camera camera, float indices)
public void Bind(Shader shader, Camera camera, float indices)
{
ImGui.TableNextRow();
@ -256,40 +252,32 @@ public class Section : IDisposable
ImGui.EndTooltip();
}
_shader.Use();
_shader.SetUniform("uModel", Matrix4x4.Identity);
_shader.SetUniform("uView", camera.GetViewMatrix());
_shader.SetUniform("uProjection", camera.GetProjectionMatrix());
_shader.SetUniform("viewPos", camera.Position);
_shader.SetUniform("material.diffuseMap", 0);
_shader.SetUniform("material.normalMap", 1);
_shader.SetUniform("material.specularMap", 2);
_shader.SetUniform("material.useSpecularMap", _hasSpecularMap);
_shader.SetUniform("material.hasDiffuseColor", _hasDiffuseColor);
_shader.SetUniform("material.diffuseColor", _diffuseColor);
_shader.SetUniform("material.emissionMap", 4);
_shader.SetUniform("material.emissionColor", _emissionColor);
_shader.SetUniform("material.shininess", Parameters.MetallicValue);
shader.SetUniform("material.diffuseMap", 0);
shader.SetUniform("material.normalMap", 1);
shader.SetUniform("material.specularMap", 2);
shader.SetUniform("material.emissionMap", 3);
_diffuseMap?.Bind(TextureUnit.Texture0);
_normalMap?.Bind(TextureUnit.Texture1);
_specularMap?.Bind(TextureUnit.Texture2);
_emissionMap?.Bind(TextureUnit.Texture4);
_emissionMap?.Bind(TextureUnit.Texture3);
_shader.SetUniform("light.ambient", _ambientLight);
_shader.SetUniform("light.diffuse", _diffuseLight);
_shader.SetUniform("light.specular", _specularLight);
_shader.SetUniform("light.position", camera.Position);
shader.SetUniform("material.useSpecularMap", _hasSpecularMap);
shader.SetUniform("material.hasDiffuseColor", _hasDiffuseColor);
shader.SetUniform("material.diffuseColor", _diffuseColor);
shader.SetUniform("material.emissionColor", _emissionColor);
shader.SetUniform("material.shininess", Parameters.MetallicValue);
shader.SetUniform("light.ambient", _ambientLight);
shader.SetUniform("light.diffuse", _diffuseLight);
shader.SetUniform("light.specular", _specularLight);
shader.SetUniform("light.position", camera.Position);
}
public void Dispose()
{
_shader.Dispose();
_diffuseMap?.Dispose();
_normalMap?.Dispose();
_specularMap?.Dispose();