Skip to content

Commit

Permalink
basically just a non msaa framebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 31, 2022
1 parent 628570e commit 5b08fc4
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 162 deletions.
1 change: 1 addition & 0 deletions FModel/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Constants
public static readonly FGuid ZERO_GUID = new(0U);

public const float SCALE_DOWN_RATIO = 0.01F;
public const uint SAMPLES_COUNT = 4;

public const string WHITE = "#DAE5F2";
public const string GRAY = "#BBBBBB";
Expand Down
189 changes: 136 additions & 53 deletions FModel/Extensions/ImGuiExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
using System.Numerics;
using FModel.Views.Snooper;
using ImGuiNET;
using Silk.NET.Input;
using Silk.NET.Maths;

namespace FModel.Extensions;

public static class ImGuiExtensions
{
public const float PADDING = 5.0f;
public static ImGuiStylePtr STYLE = ImGui.GetStyle();
public const float PADDING = 2.5f;

private static bool _viewportFocus;

public static void DrawDockSpace(Vector2D<int> size)
{
const ImGuiWindowFlags flags =
ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking |
ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize |
ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoMove |
ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoNavFocus;

ImGui.SetNextWindowPos(new Vector2(0, 0));
ImGui.SetNextWindowSize(new Vector2(size.X, size.Y));
ImGui.Begin("Snooper", flags);
ImGui.DockSpace(ImGui.GetID("Snooper"));
}

public static void DrawNavbar()
{
Expand Down Expand Up @@ -50,58 +68,123 @@ public static void DrawFPS()
ImGui.End();
}

public static void DrawViewport(FramebufferObject framebuffer, Camera camera, IMouse mouse)
{
const float lookSensitivity = 0.1f;
const ImGuiWindowFlags flags = ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse;

ImGui.Begin("Viewport", flags);

var largest = ImGui.GetContentRegionAvail();
largest.X -= ImGui.GetScrollX();
largest.Y -= ImGui.GetScrollY();

var width = largest.X;
var height = width / camera.AspectRatio;
if (height > largest.Y)
{
height = largest.Y;
width = height * camera.AspectRatio;
}

var pos = new Vector2(largest.X / 2f - width / 2f + ImGui.GetCursorPosX(), largest.Y / 2f - height / 2f + ImGui.GetCursorPosY());
ImGui.SetCursorPos(pos);
ImGui.ImageButton(framebuffer.GetPointer(), new Vector2(width, height), new Vector2(0, 1), new Vector2(1, 0), 0);

// it took me 5 hours to make it work, don't change any of the following code
// basically the Raw cursor doesn't actually freeze the mouse position
// so for ImGui, the IsItemHovered will be false if mouse leave, even in Raw mode
var io = ImGui.GetIO();
if (ImGui.IsItemHovered())
{
camera.ModifyZoom(io.MouseWheel);

// if left button down while mouse is hover viewport
if (ImGui.IsMouseDown(ImGuiMouseButton.Left) && !_viewportFocus)
_viewportFocus = true;
}

// this can't be inside IsItemHovered! read it as
// if left mouse button was pressed while hovering the viewport
// move camera until left mouse button is released
// no matter where mouse position end up
if (ImGui.IsMouseDragging(ImGuiMouseButton.Left, lookSensitivity) && _viewportFocus)
{
var delta = io.MouseDelta * lookSensitivity;
camera.ModifyDirection(delta.X, delta.Y);
mouse.Cursor.CursorMode = CursorMode.Raw;
}

// if left button up and mouse was in viewport
if (ImGui.IsMouseReleased(ImGuiMouseButton.Left) && _viewportFocus)
{
_viewportFocus = false;
mouse.Cursor.CursorMode = CursorMode.Normal;
}

ImGui.End();
}

public static void Theme()
{
STYLE.FrameRounding = 4.0f;
STYLE.GrabRounding = 4.0f;

STYLE.Colors[(int) ImGuiCol.Text] = new Vector4(0.95f, 0.96f, 0.98f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TextDisabled] = new Vector4(0.36f, 0.42f, 0.47f, 1.00f);
STYLE.Colors[(int) ImGuiCol.WindowBg] = new Vector4(0.11f, 0.15f, 0.17f, 0.35f);
STYLE.Colors[(int) ImGuiCol.ChildBg] = new Vector4(0.15f, 0.18f, 0.22f, 1.00f);
STYLE.Colors[(int) ImGuiCol.PopupBg] = new Vector4(0.08f, 0.08f, 0.08f, 0.94f);
STYLE.Colors[(int) ImGuiCol.Border] = new Vector4(0.08f, 0.10f, 0.12f, 1.00f);
STYLE.Colors[(int) ImGuiCol.BorderShadow] = new Vector4(0.00f, 0.00f, 0.00f, 0.00f);
STYLE.Colors[(int) ImGuiCol.FrameBg] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
STYLE.Colors[(int) ImGuiCol.FrameBgHovered] = new Vector4(0.12f, 0.20f, 0.28f, 1.00f);
STYLE.Colors[(int) ImGuiCol.FrameBgActive] = new Vector4(0.09f, 0.12f, 0.14f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TitleBg] = new Vector4(0.09f, 0.12f, 0.14f, 0.65f);
STYLE.Colors[(int) ImGuiCol.TitleBgActive] = new Vector4(0.08f, 0.10f, 0.12f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TitleBgCollapsed] = new Vector4(0.00f, 0.00f, 0.00f, 0.51f);
STYLE.Colors[(int) ImGuiCol.MenuBarBg] = new Vector4(0.15f, 0.18f, 0.22f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ScrollbarBg] = new Vector4(0.02f, 0.02f, 0.02f, 0.39f);
STYLE.Colors[(int) ImGuiCol.ScrollbarGrab] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ScrollbarGrabHovered] = new Vector4(0.18f, 0.22f, 0.25f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ScrollbarGrabActive] = new Vector4(0.09f, 0.21f, 0.31f, 1.00f);
STYLE.Colors[(int) ImGuiCol.CheckMark] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.SliderGrab] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.SliderGrabActive] = new Vector4(0.37f, 0.61f, 1.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.Button] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ButtonHovered] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ButtonActive] = new Vector4(0.06f, 0.53f, 0.98f, 1.00f);
STYLE.Colors[(int) ImGuiCol.Header] = new Vector4(0.20f, 0.25f, 0.29f, 0.55f);
STYLE.Colors[(int) ImGuiCol.HeaderHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.80f);
STYLE.Colors[(int) ImGuiCol.HeaderActive] = new Vector4(0.26f, 0.59f, 0.98f, 1.00f);
STYLE.Colors[(int) ImGuiCol.Separator] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
STYLE.Colors[(int) ImGuiCol.SeparatorHovered] = new Vector4(0.10f, 0.40f, 0.75f, 0.78f);
STYLE.Colors[(int) ImGuiCol.SeparatorActive] = new Vector4(0.10f, 0.40f, 0.75f, 1.00f);
STYLE.Colors[(int) ImGuiCol.ResizeGrip] = new Vector4(0.26f, 0.59f, 0.98f, 0.25f);
STYLE.Colors[(int) ImGuiCol.ResizeGripHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.67f);
STYLE.Colors[(int) ImGuiCol.ResizeGripActive] = new Vector4(0.26f, 0.59f, 0.98f, 0.95f);
STYLE.Colors[(int) ImGuiCol.Tab] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TabHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.80f);
STYLE.Colors[(int) ImGuiCol.TabActive] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TabUnfocused] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TabUnfocusedActive] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
STYLE.Colors[(int) ImGuiCol.PlotLines] = new Vector4(0.61f, 0.61f, 0.61f, 1.00f);
STYLE.Colors[(int) ImGuiCol.PlotLinesHovered] = new Vector4(1.00f, 0.43f, 0.35f, 1.00f);
STYLE.Colors[(int) ImGuiCol.PlotHistogram] = new Vector4(0.90f, 0.70f, 0.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.PlotHistogramHovered] = new Vector4(1.00f, 0.60f, 0.00f, 1.00f);
STYLE.Colors[(int) ImGuiCol.TextSelectedBg] = new Vector4(0.26f, 0.59f, 0.98f, 0.35f);
STYLE.Colors[(int) ImGuiCol.DragDropTarget] = new Vector4(1.00f, 1.00f, 0.00f, 0.90f);
STYLE.Colors[(int) ImGuiCol.NavHighlight] = new Vector4(0.26f, 0.59f, 0.98f, 1.00f);
STYLE.Colors[(int) ImGuiCol.NavWindowingHighlight] = new Vector4(1.00f, 1.00f, 1.00f, 0.70f);
STYLE.Colors[(int) ImGuiCol.NavWindowingDimBg] = new Vector4(0.80f, 0.80f, 0.80f, 0.20f);
STYLE.Colors[(int) ImGuiCol.ModalWindowDimBg] = new Vector4(0.80f, 0.80f, 0.80f, 0.35f);
var io = ImGui.GetIO();
io.ConfigFlags |= ImGuiConfigFlags.DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.ConfigWindowsMoveFromTitleBarOnly = true;
io.ConfigWindowsResizeFromEdges = false;

var style = ImGui.GetStyle();
style.FrameRounding = 2.0f;
style.GrabRounding = 2.0f;
style.WindowPadding = Vector2.Zero;

// style.Colors[(int) ImGuiCol.Text] = new Vector4(0.95f, 0.96f, 0.98f, 1.00f);
// style.Colors[(int) ImGuiCol.TextDisabled] = new Vector4(0.36f, 0.42f, 0.47f, 1.00f);
// style.Colors[(int) ImGuiCol.WindowBg] = new Vector4(0.149f, 0.149f, 0.188f, 0.35f);
// style.Colors[(int) ImGuiCol.ChildBg] = new Vector4(0.15f, 0.18f, 0.22f, 1.00f);
// style.Colors[(int) ImGuiCol.PopupBg] = new Vector4(0.08f, 0.08f, 0.08f, 0.94f);
// style.Colors[(int) ImGuiCol.Border] = new Vector4(0.08f, 0.10f, 0.12f, 1.00f);
// style.Colors[(int) ImGuiCol.BorderShadow] = new Vector4(0.00f, 0.00f, 0.00f, 0.00f);
// style.Colors[(int) ImGuiCol.FrameBg] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
// style.Colors[(int) ImGuiCol.FrameBgHovered] = new Vector4(0.12f, 0.20f, 0.28f, 1.00f);
// style.Colors[(int) ImGuiCol.FrameBgActive] = new Vector4(0.09f, 0.12f, 0.14f, 1.00f);
// style.Colors[(int) ImGuiCol.TitleBg] = new Vector4(0.09f, 0.12f, 0.14f, 0.65f);
// style.Colors[(int) ImGuiCol.TitleBgActive] = new Vector4(0.08f, 0.10f, 0.12f, 1.00f);
// style.Colors[(int) ImGuiCol.TitleBgCollapsed] = new Vector4(0.00f, 0.00f, 0.00f, 0.51f);
// style.Colors[(int) ImGuiCol.MenuBarBg] = new Vector4(0.15f, 0.18f, 0.22f, 1.00f);
// style.Colors[(int) ImGuiCol.ScrollbarBg] = new Vector4(0.02f, 0.02f, 0.02f, 0.39f);
// style.Colors[(int) ImGuiCol.ScrollbarGrab] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
// style.Colors[(int) ImGuiCol.ScrollbarGrabHovered] = new Vector4(0.18f, 0.22f, 0.25f, 1.00f);
// style.Colors[(int) ImGuiCol.ScrollbarGrabActive] = new Vector4(0.09f, 0.21f, 0.31f, 1.00f);
// style.Colors[(int) ImGuiCol.CheckMark] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
// style.Colors[(int) ImGuiCol.SliderGrab] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
// style.Colors[(int) ImGuiCol.SliderGrabActive] = new Vector4(0.37f, 0.61f, 1.00f, 1.00f);
// style.Colors[(int) ImGuiCol.Button] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
// style.Colors[(int) ImGuiCol.ButtonHovered] = new Vector4(0.28f, 0.56f, 1.00f, 1.00f);
// style.Colors[(int) ImGuiCol.ButtonActive] = new Vector4(0.06f, 0.53f, 0.98f, 1.00f);
// style.Colors[(int) ImGuiCol.Header] = new Vector4(0.20f, 0.25f, 0.29f, 0.55f);
// style.Colors[(int) ImGuiCol.HeaderHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.80f);
// style.Colors[(int) ImGuiCol.HeaderActive] = new Vector4(0.26f, 0.59f, 0.98f, 1.00f);
// style.Colors[(int) ImGuiCol.Separator] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
// style.Colors[(int) ImGuiCol.SeparatorHovered] = new Vector4(0.10f, 0.40f, 0.75f, 0.78f);
// style.Colors[(int) ImGuiCol.SeparatorActive] = new Vector4(0.10f, 0.40f, 0.75f, 1.00f);
// style.Colors[(int) ImGuiCol.ResizeGrip] = new Vector4(0.26f, 0.59f, 0.98f, 0.25f);
// style.Colors[(int) ImGuiCol.ResizeGripHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.67f);
// style.Colors[(int) ImGuiCol.ResizeGripActive] = new Vector4(0.26f, 0.59f, 0.98f, 0.95f);
// style.Colors[(int) ImGuiCol.Tab] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
// style.Colors[(int) ImGuiCol.TabHovered] = new Vector4(0.26f, 0.59f, 0.98f, 0.80f);
// style.Colors[(int) ImGuiCol.TabActive] = new Vector4(0.20f, 0.25f, 0.29f, 1.00f);
// style.Colors[(int) ImGuiCol.TabUnfocused] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
// style.Colors[(int) ImGuiCol.TabUnfocusedActive] = new Vector4(0.11f, 0.15f, 0.17f, 1.00f);
// style.Colors[(int) ImGuiCol.PlotLines] = new Vector4(0.61f, 0.61f, 0.61f, 1.00f);
// style.Colors[(int) ImGuiCol.PlotLinesHovered] = new Vector4(1.00f, 0.43f, 0.35f, 1.00f);
// style.Colors[(int) ImGuiCol.PlotHistogram] = new Vector4(0.90f, 0.70f, 0.00f, 1.00f);
// style.Colors[(int) ImGuiCol.PlotHistogramHovered] = new Vector4(1.00f, 0.60f, 0.00f, 1.00f);
// style.Colors[(int) ImGuiCol.TextSelectedBg] = new Vector4(0.26f, 0.59f, 0.98f, 0.35f);
// style.Colors[(int) ImGuiCol.DragDropTarget] = new Vector4(1.00f, 1.00f, 0.00f, 0.90f);
// style.Colors[(int) ImGuiCol.NavHighlight] = new Vector4(0.26f, 0.59f, 0.98f, 1.00f);
// style.Colors[(int) ImGuiCol.NavWindowingHighlight] = new Vector4(1.00f, 1.00f, 1.00f, 0.70f);
// style.Colors[(int) ImGuiCol.NavWindowingDimBg] = new Vector4(0.80f, 0.80f, 0.80f, 0.20f);
// style.Colors[(int) ImGuiCol.ModalWindowDimBg] = new Vector4(0.80f, 0.80f, 0.80f, 0.35f);
}
}
4 changes: 4 additions & 0 deletions FModel/FModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<None Remove="Resources\grid.vert" />
<None Remove="Resources\skybox.frag" />
<None Remove="Resources\skybox.vert" />
<None Remove="Resources\framebuffer.frag" />
<None Remove="Resources\framebuffer.vert" />
</ItemGroup>

<ItemGroup>
Expand All @@ -114,6 +116,8 @@
<EmbeddedResource Include="Resources\grid.vert" />
<EmbeddedResource Include="Resources\skybox.frag" />
<EmbeddedResource Include="Resources\skybox.vert" />
<EmbeddedResource Include="Resources\framebuffer.frag" />
<EmbeddedResource Include="Resources\framebuffer.vert" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions FModel/Resources/framebuffer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 330 core

in vec2 fTexCoords;

uniform sampler2D screenTexture;

out vec4 FragColor;

void main()
{
FragColor = texture(screenTexture, fTexCoords);
}
12 changes: 12 additions & 0 deletions FModel/Resources/framebuffer.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 330 core

layout (location = 0) in vec2 vPos;
layout (location = 1) in vec2 vTexCoords;

out vec2 fTexCoords;

void main()
{
gl_Position = vec4(vPos.x, vPos.y, 0.0, 1.0);
fTexCoords = vTexCoords;
}
104 changes: 104 additions & 0 deletions FModel/Views/Snooper/FramebufferObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using Silk.NET.Maths;
using Silk.NET.OpenGL;

namespace FModel.Views.Snooper;

public class FramebufferObject : IDisposable
{
private uint _handle;
private GL _gl;

private readonly int _width;
private readonly int _height;
private readonly RenderbufferObject _renderbuffer;

private BufferObject<uint> _ebo;
private BufferObject<float> _vbo;
private VertexArrayObject<float, uint> _vao;

private Shader _shader;
private Texture _texture;

public readonly uint[] Indices = { 0, 1, 2, 3, 4, 5 };
public readonly float[] Vertices = {
// Coords // texCoords
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,

1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};

public FramebufferObject(Vector2D<int> size)
{
_width = size.X;
_height = size.Y;
_renderbuffer = new RenderbufferObject((uint) _width, (uint) _height);
}

public void Setup(GL gl)
{
_gl = gl;
_handle = _gl.GenFramebuffer();
Bind();

_texture = new Texture(_gl, (uint) _width, (uint) _height);

_renderbuffer.Setup(gl);

_shader = new Shader(_gl, "framebuffer.vert", "framebuffer.frag");
_shader.Use();
_shader.SetUniform("screenTexture", 0);

_ebo = new BufferObject<uint>(_gl, Indices, BufferTargetARB.ElementArrayBuffer);
_vbo = new BufferObject<float>(_gl, Vertices, BufferTargetARB.ArrayBuffer);
_vao = new VertexArrayObject<float, uint>(_gl, _vbo, _ebo);

_vao.VertexAttributePointer(0, 2, VertexAttribPointerType.Float, 4, 0); // position
_vao.VertexAttributePointer(1, 2, VertexAttribPointerType.Float, 4, 2); // uv

var status = _gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (status != GLEnum.FramebufferComplete)
{
throw new Exception($"Framebuffer failed to bind with error: {_gl.GetProgramInfoLog(_handle)}");
}
}

public void Bind()
{
_gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, _handle);
}

public void UnBind()
{
_gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
}

public void BindStuff()
{
_gl.DepthMask(false);

_shader.Use();
_vao.Bind();

_texture.Bind(TextureUnit.Texture0);

_gl.DrawArrays(PrimitiveType.Triangles, 0, (uint) Indices.Length);

_gl.DepthMask(true);
}

public IntPtr GetPointer() => _texture.GetPointer();

public void Dispose()
{
_vao.Dispose();
_shader.Dispose();
_texture.Dispose();
_renderbuffer.Dispose();
_gl.DeleteFramebuffer(_handle);
}
}
Loading

0 comments on commit 5b08fc4

Please sign in to comment.