Skip to content

Commit

Permalink
Added a frame buffer and anti-aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelBilek committed Jul 21, 2024
1 parent 9830b48 commit c1f19e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions GraphicsApp/GraphicsApp/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public class Game : GameWindow
-100.0f, 0.0f, 100.0f,
};

private int _frameBuffer;
private int _colorBuffer;
private int _depthBuffer;

private int _aaSamples = 4;

private int _vboMainObj;
private int _vaoMainObj;

Expand Down Expand Up @@ -125,6 +131,8 @@ protected override void OnLoad()

_camera.LookAt(Vector3.Zero);

GL.Enable(EnableCap.Multisample);

GL.Enable(EnableCap.DepthTest);
GL.ClearColor(0.15f, 0.15f, 0.15f, 1.0f);

Expand Down Expand Up @@ -180,6 +188,9 @@ protected override void OnLoad()

GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);

// Framebuffer
PrepareFrameBuffer();
}

protected override void OnUnload()
Expand All @@ -189,8 +200,36 @@ protected override void OnUnload()
_shaderMainObj.Dispose();
}

private void PrepareFrameBuffer()
{
_frameBuffer = GL.GenFramebuffer();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _frameBuffer);

// Create and bind the color renderbuffer
_colorBuffer = GL.GenRenderbuffer();
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _colorBuffer);
GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, _aaSamples, RenderbufferStorage.Rgba8, _width, _height);
GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, _colorBuffer);

// Create and bind the depth renderbuffer
_depthBuffer = GL.GenRenderbuffer();
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _depthBuffer);
GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, _aaSamples, RenderbufferStorage.DepthComponent, _width, _height);
GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, _depthBuffer);

// Check framebuffer completeness
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
{
throw new Exception("Framebuffer is not complete.");
}

// Unbind the framebuffer
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}

protected override void OnRenderFrame(FrameEventArgs e)
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _frameBuffer);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

Matrix4 view = _camera.GetViewMatrix();
Expand Down Expand Up @@ -255,6 +294,13 @@ protected override void OnRenderFrame(FrameEventArgs e)

GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);

// Copy the multisampled buffer to the default framebuffer
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _frameBuffer);
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); // Write to default framebuffer

// Blit the multisampled buffer to the default framebuffer
GL.BlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);

Context.SwapBuffers();

base.OnRenderFrame(e);
Expand All @@ -265,6 +311,9 @@ protected override void OnFramebufferResize(FramebufferResizeEventArgs e)
base.OnFramebufferResize(e);

GL.Viewport(0, 0, e.Width, e.Height);
_width = e.Width;
_height = e.Height;
PrepareFrameBuffer();
}

protected override void OnMouseDown(MouseButtonEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions GraphicsApp/GraphicsApp/Shaders/grid_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ out vec4 FragColor;
void main()
{
float gridSpacing = 1.0; // The spacing of the grid lines
float lineThickness = 0.01; // The thickness of the grid lines
float lineThickness = 0.015; // The thickness of the grid lines

// Calculate the grid lines
vec2 grid = abs(fract(fragPos.xz / gridSpacing - 0.5) - 0.5) / fwidth(fragPos.xz / gridSpacing);
Expand All @@ -29,7 +29,7 @@ void main()
float alpha = 1.0 - smoothstep(0.0, thickness, line);

// Define the threshold for the axis lines
float axisThreshold = lineThickness * 2.0; // Make axis lines slightly thicker
float axisThreshold = lineThickness * 1.25; // Make axis lines slightly thicker

// Colors for the X and Z axes
vec3 xAxisColor = vec3(0.5, 0.0, 0.0); // Red for X axis
Expand Down

0 comments on commit c1f19e6

Please sign in to comment.