From 37b5989378b3089530de917a945b1871a8859d5b Mon Sep 17 00:00:00 2001 From: 4sval Date: Tue, 30 Aug 2022 19:40:27 +0200 Subject: [PATCH] display vertex colors --- FModel/Resources/shader.frag | 8 +++++- FModel/Resources/shader.vert | 3 ++ FModel/Views/Snooper/Model.cs | 34 ++++++++++++++++++++-- FModel/Views/Snooper/Section.cs | 50 +++++++++++++-------------------- 4 files changed, 61 insertions(+), 34 deletions(-) diff --git a/FModel/Resources/shader.frag b/FModel/Resources/shader.frag index 129ac22b..d2fea11d 100644 --- a/FModel/Resources/shader.frag +++ b/FModel/Resources/shader.frag @@ -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; } diff --git a/FModel/Resources/shader.vert b/FModel/Resources/shader.vert index d2b4ab93..28918dd4 100644 --- a/FModel/Resources/shader.vert +++ b/FModel/Resources/shader.vert @@ -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; } diff --git a/FModel/Views/Snooper/Model.cs b/FModel/Views/Snooper/Model.cs index dee34493..e0d9e327 100644 --- a/FModel/Views/Snooper/Model.cs +++ b/FModel/Views/Snooper/Model.cs @@ -12,22 +12,29 @@ public class Model : IDisposable private uint _handle; private GL _gl; + private Shader _shader; + private BufferObject _ebo; private BufferObject _vbo; private VertexArrayObject _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(_gl, Indices, BufferTargetARB.ElementArrayBuffer); _vbo = new BufferObject(_gl, Vertices, BufferTargetARB.ArrayBuffer); _vao = new VertexArrayObject(_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(); diff --git a/FModel/Views/Snooper/Section.cs b/FModel/Views/Snooper/Section.cs index 78177d8a..6653ad59 100644 --- a/FModel/Views/Snooper/Section.cs +++ b/FModel/Views/Snooper/Section.cs @@ -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();