Render a triangle for testing purposes
This commit is contained in:
parent
578d0bf66e
commit
e45344c189
@ -8,21 +8,123 @@ namespace GLTFViewer.Rendering;
|
|||||||
|
|
||||||
public class Renderer
|
public class Renderer
|
||||||
{
|
{
|
||||||
|
private int _vao;
|
||||||
|
private int _vbo;
|
||||||
|
private int _shaderProgram;
|
||||||
|
|
||||||
public void Initialize(GlInterface gl)
|
public void Initialize(GlInterface gl)
|
||||||
{
|
{
|
||||||
GLLoader.LoadBindings(new AvaloniaBindingsContext(gl));
|
GLLoader.LoadBindings(new AvaloniaBindingsContext(gl));
|
||||||
GL.ClearColor(1,1,1,1);
|
GL.ClearColor(1, 1, 1, 1);
|
||||||
|
|
||||||
|
// Vertex data for a triangle
|
||||||
|
float[] vertices = {
|
||||||
|
0.0f, 0.5f, 0.0f, // Top vertex
|
||||||
|
-0.5f, -0.5f, 0.0f, // Bottom left vertex
|
||||||
|
0.5f, -0.5f, 0.0f // Bottom right vertex
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create and bind the VAO
|
||||||
|
_vao = GL.GenVertexArray();
|
||||||
|
GL.BindVertexArray(_vao);
|
||||||
|
|
||||||
|
// Create and bind the VBO
|
||||||
|
_vbo = GL.GenBuffer();
|
||||||
|
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo);
|
||||||
|
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsage.StaticDraw);
|
||||||
|
|
||||||
|
// Vertex shader source code
|
||||||
|
string vertexShaderSource = @"
|
||||||
|
#version 330 core
|
||||||
|
layout(location = 0) in vec3 aPos;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
}";
|
||||||
|
|
||||||
|
// Fragment shader source code
|
||||||
|
string fragmentShaderSource = @"
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(0.0, 0.0, 1.0, 1.0); // Blue color
|
||||||
|
}";
|
||||||
|
|
||||||
|
// Compile vertex shader
|
||||||
|
int vertexShader = GL.CreateShader(ShaderType.VertexShader);
|
||||||
|
GL.ShaderSource(vertexShader, vertexShaderSource);
|
||||||
|
GL.CompileShader(vertexShader);
|
||||||
|
CheckShaderCompileStatus(vertexShader);
|
||||||
|
|
||||||
|
// Compile fragment shader
|
||||||
|
int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
|
||||||
|
GL.ShaderSource(fragmentShader, fragmentShaderSource);
|
||||||
|
GL.CompileShader(fragmentShader);
|
||||||
|
CheckShaderCompileStatus(fragmentShader);
|
||||||
|
|
||||||
|
// Link shaders into a program
|
||||||
|
_shaderProgram = GL.CreateProgram();
|
||||||
|
GL.AttachShader(_shaderProgram, vertexShader);
|
||||||
|
GL.AttachShader(_shaderProgram, fragmentShader);
|
||||||
|
GL.LinkProgram(_shaderProgram);
|
||||||
|
CheckProgramLinkStatus(_shaderProgram);
|
||||||
|
|
||||||
|
// Clean up shaders as they are no longer needed
|
||||||
|
GL.DeleteShader(vertexShader);
|
||||||
|
GL.DeleteShader(fragmentShader);
|
||||||
|
|
||||||
|
// Specify the layout of the vertex data
|
||||||
|
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
|
||||||
|
GL.EnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
// Unbind the VAO
|
||||||
|
GL.BindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Render(GlInterface gl, int width, int height, int fbo)
|
public void Render(GlInterface gl, int width, int height, int fbo)
|
||||||
{
|
{
|
||||||
GL.Viewport(0, 0, width, height);
|
GL.Viewport(0, 0, width, height);
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
|
|
||||||
|
// Use the shader program
|
||||||
|
GL.UseProgram(_shaderProgram);
|
||||||
|
|
||||||
|
// Bind the VAO
|
||||||
|
GL.BindVertexArray(_vao);
|
||||||
|
|
||||||
|
// Draw the triangle
|
||||||
|
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
|
||||||
|
|
||||||
|
// Unbind the VAO
|
||||||
|
GL.BindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Deinitialize(GlInterface gl)
|
public void Deinitialize(GlInterface gl)
|
||||||
{
|
{
|
||||||
|
GL.DeleteVertexArray(_vao);
|
||||||
|
GL.DeleteBuffer(_vbo);
|
||||||
|
GL.DeleteProgram(_shaderProgram);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckShaderCompileStatus(int shader)
|
||||||
|
{
|
||||||
|
GL.GetShaderi(shader, ShaderParameterName.CompileStatus, out int status);
|
||||||
|
if (status == (int)All.False)
|
||||||
|
{
|
||||||
|
GL.GetShaderInfoLog(shader, out var infoLog);
|
||||||
|
throw new Exception($"Shader compilation failed: {infoLog}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckProgramLinkStatus(int program)
|
||||||
|
{
|
||||||
|
GL.GetProgrami(program, ProgramProperty.LinkStatus, out int status);
|
||||||
|
if (status == (int)All.False)
|
||||||
|
{
|
||||||
|
GL.GetProgramInfoLog(program, out var infoLog);
|
||||||
|
throw new Exception($"Program linking failed: {infoLog}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AvaloniaBindingsContext : IBindingsContext
|
private class AvaloniaBindingsContext : IBindingsContext
|
||||||
@ -39,5 +141,4 @@ public class Renderer
|
|||||||
return _gl.GetProcAddress(procName);
|
return _gl.GetProcAddress(procName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user