finally what I was looking for

This commit is contained in:
4sval 2022-09-07 22:44:42 +02:00
parent 807029d211
commit 29983c23e3
8 changed files with 64 additions and 43 deletions

View File

@ -4,5 +4,5 @@ out vec4 FragColor;
void main()
{
FragColor = vec4(0.929, 0.588, 0.196, 0.2);
FragColor = vec4(0.929, 0.588, 0.196, 1.0);
}

View File

@ -6,10 +6,11 @@ layout (location = 6) in mat4 vInstanceMatrix;
uniform mat4 uView;
uniform mat4 uProjection;
uniform vec3 viewPos;
void main()
{
float scaleFactor = 0.005;
float scaleFactor = distance(vPos, viewPos) * 0.0025;
vec3 scaleVertex = vPos + vNormal * scaleFactor;
gl_Position = uProjection * uView * vInstanceMatrix * vec4(scaleVertex, 1.0);
}

View File

@ -11,7 +11,7 @@ public class Camera
public float Yaw { get; set; } = -90f;
public float Pitch { get; set; } = 0f;
public float Zoom { get; set; } = 45f;
public float Zoom { get; set; } = 60f;
public float Speed { get; set; } = 1f;
public float Near { get; } = 0.01f;
public float Far { get; } = 100f;
@ -34,7 +34,7 @@ public class Camera
public void ModifyZoom(float zoomAmount)
{
//We don't want to be able to zoom in too close or too far away so clamp to these values
Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 45f);
Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 60f);
}
public void ModifyDirection(float xOffset, float yOffset)

View File

@ -96,7 +96,7 @@ public class FramebufferObject : IDisposable
public void BindStuff()
{
_gl.DepthMask(false);
_gl.Disable(EnableCap.DepthTest);
_shader.Use();
_vao.Bind();
@ -104,8 +104,7 @@ public class FramebufferObject : IDisposable
_postProcessingTexture.Bind(TextureUnit.Texture0);
_gl.DrawArrays(PrimitiveType.Triangles, 0, (uint) Indices.Length);
_gl.DepthMask(true);
_gl.Enable(EnableCap.DepthTest);
}
public IntPtr GetPointer() => _postProcessingTexture.GetPointer();

View File

@ -43,8 +43,7 @@ public class Grid : IDisposable
public void Bind(Camera camera)
{
_gl.DepthMask(false);
_gl.Disable(EnableCap.DepthTest);
_vao.Bind();
_shader.Use();
@ -55,8 +54,7 @@ public class Grid : IDisposable
_shader.SetUniform("uFar", camera.Far);
_gl.DrawArrays(PrimitiveType.Triangles, 0, (uint) Indices.Length);
_gl.DepthMask(true);
_gl.Enable(EnableCap.DepthTest);
}
public void Dispose()

View File

@ -30,7 +30,6 @@ public class Model : IDisposable
public Section[] Sections;
public readonly List<CSkelMeshBone> Skeleton;
public int InstanceIndex;
public int TransformsCount;
public readonly List<Transform> Transforms;
public readonly string[] TransformsLabels = {
@ -39,6 +38,7 @@ public class Model : IDisposable
"X Scale", "Y", "Z"
};
public bool Show;
public bool IsSelected;
public bool DisplayVertexColors;
public bool DisplayBones;
@ -47,8 +47,8 @@ public class Model : IDisposable
{
Name = name;
Type = type;
InstanceIndex = 0;
Transforms = new List<Transform>();
Show = true;
}
public Model(string name, string type, CBaseMeshLod lod, CMeshVertex[] vertices, List<CSkelMeshBone> skeleton = null, Transform transform = null) : this(name, type)
@ -165,35 +165,47 @@ public class Model : IDisposable
}
}
public void Bind(Shader shader, Shader outline)
public void Bind(Shader shader)
{
_vao.Bind();
if (IsSelected)
{
_gl.Enable(EnableCap.StencilTest);
_gl.StencilFunc(StencilFunction.Always, 1, 0xFF);
}
_vao.Bind();
shader.SetUniform("display_vertex_colors", DisplayVertexColors);
for (int section = 0; section < Sections.Length; section++)
{
Sections[section].Bind(shader, (uint) TransformsCount);
}
_vao.Unbind();
if (IsSelected)
{
outline.Use();
_gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
_gl.StencilFunc(StencilFunction.Notequal, 1, 0xFF);
_gl.Disable(EnableCap.DepthTest);
_gl.StencilMask(0x00);
for (int section = 0; section < Sections.Length; section++)
{
_gl.DrawArraysInstanced(PrimitiveType.Triangles, Sections[section].FirstFaceIndex, Sections[section].FacesCount, (uint) InstanceIndex + 1);
}
_gl.StencilMask(0xFF);
_gl.Enable(EnableCap.DepthTest);
_gl.StencilFunc(StencilFunction.Always, 0, 0xFF);
_gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
shader.Use();
_gl.Disable(EnableCap.StencilTest);
}
}
public void Outline(Shader shader)
{
_gl.StencilMask(0x00);
_gl.Disable(EnableCap.DepthTest);
_gl.StencilFunc(StencilFunction.Notequal, 1, 0xFF);
_vao.Bind();
shader.Use();
for (int section = 0; section < Sections.Length; section++)
{
if (!Sections[section].Show) continue;
_gl.DrawArraysInstanced(PrimitiveType.Triangles, Sections[section].FirstFaceIndex, Sections[section].FacesCount, (uint) TransformsCount);
}
_vao.Unbind();
_gl.StencilFunc(StencilFunction.Always, 0, 0xFF);
_gl.Enable(EnableCap.DepthTest);
_gl.StencilMask(0xFF);
}
public void Dispose()

View File

@ -29,6 +29,7 @@ public class SnimGui : IDisposable
private readonly Vector2 _texturePosition;
private bool _viewportFocus;
private FGuid _selectedModel;
private int _selectedInstance;
private int _selectedSection;
private const ImGuiWindowFlags _noResize = ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove; // delete once we have a proper docking branch
@ -55,6 +56,7 @@ public class SnimGui : IDisposable
_textureSize = _viewportSize with { Y = viewport.WorkSize.Y - _viewportSize.Y - titleBarHeight };
_texturePosition = new Vector2(0, _viewportPosition.Y + _viewportSize.Y);
_selectedModel = new FGuid();
_selectedInstance = 0;
_selectedSection = 0;
Theme(style);
@ -137,6 +139,7 @@ public class SnimGui : IDisposable
if (ImGui.Selectable(model.Name, model.IsSelected))
{
_selectedModel = guid;
_selectedInstance = 0;
_selectedSection = 0;
}
if (ImGui.BeginPopupContextItem())
@ -183,11 +186,12 @@ public class SnimGui : IDisposable
ImGui.Text($"Entity: {model.Name}");
ImGui.Separator();
if (ImGui.Button("Focus"))
camera.Position = model.Transforms[model.InstanceIndex].Position;
camera.Position = model.Transforms[_selectedInstance].Position;
ImGui.SameLine();
ImGui.BeginDisabled(model.TransformsCount < 2);
ImGui.SliderInt("Instance", ref model.InstanceIndex, 0, model.TransformsCount - 1, "%i", ImGuiSliderFlags.AlwaysClamp);
ImGui.SliderInt("Instance", ref _selectedInstance, 0, model.TransformsCount - 1, "%i", ImGuiSliderFlags.AlwaysClamp);
ImGui.EndDisabled();
ImGui.Checkbox("Show", ref model.Show);
ImGui.BeginDisabled(!model.HasVertexColors);
ImGui.Checkbox("Vertex Colors", ref model.DisplayVertexColors);
ImGui.EndDisabled();
@ -202,46 +206,46 @@ public class SnimGui : IDisposable
var index = 0;
ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Position.X, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Position.X, speed, 0f, 0f, "%.2f m");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Position.Y, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Position.Y, speed, 0f, 0f, "%.2f m");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Position.Z, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Position.Z, speed, 0f, 0f, "%.2f m");
ImGui.PopID();
ImGui.Spacing();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Rotation.Pitch, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Rotation.Pitch, .5f, 0f, 0f, "%.1f°");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Rotation.Roll, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Rotation.Roll, .5f, 0f, 0f, "%.1f°");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Rotation.Yaw, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Rotation.Yaw, .5f, 0f, 0f, "%.1f°");
ImGui.PopID();
ImGui.Spacing();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Scale.X, speed, 0f, 0f, "%.3f");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Scale.X, speed, 0f, 0f, "%.3f");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Scale.Y, speed, 0f, 0f, "%.3f");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Scale.Y, speed, 0f, 0f, "%.3f");
ImGui.PopID();
index++; ImGui.SetNextItemWidth(width); ImGui.PushID(index);
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[model.InstanceIndex].Scale.Z, speed, 0f, 0f, "%.3f");
ImGui.DragFloat(model.TransformsLabels[index], ref model.Transforms[_selectedInstance].Scale.Z, speed, 0f, 0f, "%.3f");
ImGui.PopID();
model.UpdateMatrix(model.InstanceIndex);
model.UpdateMatrix(_selectedInstance);
ImGui.TreePop();
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Windows;
@ -259,7 +260,7 @@ public class Snooper
_gl.Enable(EnableCap.Blend);
_gl.Enable(EnableCap.DepthTest);
_gl.Enable(EnableCap.Multisample);
_gl.Enable(EnableCap.StencilTest);
_gl.StencilOp(StencilOp.Keep, StencilOp.Replace, StencilOp.Replace);
_gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
_imGui = new SnimGui(_gl, _window, input);
@ -300,6 +301,7 @@ public class Snooper
_outline.Use();
_outline.SetUniform("uView", viewMatrix);
_outline.SetUniform("uProjection", projMatrix);
_outline.SetUniform("viewPos", _camera.Position);
_shader.Use();
_shader.SetUniform("uView", viewMatrix);
@ -315,9 +317,14 @@ public class Snooper
_shader.SetUniform("light.diffuse", _diffuseLight);
_shader.SetUniform("light.specular", _specularLight);
foreach (var model in _models.Values)
foreach (var model in _models.Values.Where(model => model.Show))
{
model.Bind(_shader, _outline);
model.Bind(_shader);
}
_gl.Enable(EnableCap.StencilTest); // I don't get why this must be here but it works now so...
foreach (var model in _models.Values.Where(model => model.IsSelected && model.Show))
{
model.Outline(_outline);
}
_imGui.Construct(_size, _framebuffer, _camera, _mouse, _models);