fixed issue

This commit is contained in:
zyb3rwolfi 2025-10-07 10:28:44 +01:00
parent 5dd7aef809
commit 83d2311ff5
3 changed files with 19 additions and 69 deletions

View File

@ -16,51 +16,8 @@ namespace TheLabs
private readonly float[] _vertices = private readonly float[] _vertices =
{ {
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-left vertex -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-left vertex
0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-right vertex 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // Bottom-right vertex
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top vertex 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top vertex
};
private readonly float[] _quadvertices =
{
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-left vertex
0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-right vertex
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top vertex
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-left vertex
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-right vertex
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top vertex
};
private readonly float[] _cubevertices =
{
// Positions
// Front face
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, // Bottom-left
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, // Bottom-right
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, // Top-right
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, // Top-left
// Back face
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f, // Bottom-left
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, // Top-right
-0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 1.0f, // Top-left
};
private readonly uint[] _cubeIndices =
{
// Front face
0, 1, 2, 2, 3, 0,
// Back face
4, 5, 6, 6, 7, 4,
// Left face
4, 0, 3, 3, 7, 4,
// Right face
1, 5, 6, 6, 2, 1,
// Top face
3, 2, 6, 6, 7, 3,
// Bottom face
4, 5, 1, 1, 0, 4
}; };
// These are the handles to OpenGL objects. A handle is an integer representing where the object lives on the // These are the handles to OpenGL objects. A handle is an integer representing where the object lives on the
@ -69,7 +26,7 @@ namespace TheLabs
// What these objects are will be explained in OnLoad. // What these objects are will be explained in OnLoad.
private int _vertexBufferObject; private int _vertexBufferObject;
private int _vao, _vbo, _ebo;
private int _vertexArrayObject; private int _vertexArrayObject;
// This class is a wrapper around a shader, which helps us manage it. // This class is a wrapper around a shader, which helps us manage it.
@ -92,7 +49,6 @@ namespace TheLabs
// the largest possible value for that channel. // the largest possible value for that channel.
// This is a deep green. // This is a deep green.
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
GL.Enable(EnableCap.DepthTest);
// We need to send our vertices over to the graphics card so OpenGL can use them. // We need to send our vertices over to the graphics card so OpenGL can use them.
// To do this, we need to create what's called a Vertex Buffer Object (VBO). // To do this, we need to create what's called a Vertex Buffer Object (VBO).
@ -121,7 +77,8 @@ namespace TheLabs
// StreamDraw: This buffer will change on every frame. // StreamDraw: This buffer will change on every frame.
// Writing to the proper memory space is important! Generally, you'll only want StaticDraw, // Writing to the proper memory space is important! Generally, you'll only want StaticDraw,
// but be sure to use the right one for your use case. // but be sure to use the right one for your use case.
GL.BufferData(BufferTarget.ArrayBuffer, _cubevertices.Length * sizeof(float), _cubevertices, BufferUsageHint.StaticDraw); GL.BufferData(BufferTarget.ArrayBuffer, _vertices.Length * sizeof(float), _vertices, BufferUsageHint.StaticDraw);
// One notable thing about the buffer we just loaded data into is that it doesn't have any structure to it. It's just a bunch of floats (which are actaully just bytes). // One notable thing about the buffer we just loaded data into is that it doesn't have any structure to it. It's just a bunch of floats (which are actaully just bytes).
// The opengl driver doesn't know how this data should be interpreted or how it should be divided up into vertices. To do this opengl introduces the idea of a // The opengl driver doesn't know how this data should be interpreted or how it should be divided up into vertices. To do this opengl introduces the idea of a
// Vertex Array Obejct (VAO) which has the job of keeping track of what parts or what buffers correspond to what data. In this example we want to set our VAO up so that // Vertex Array Obejct (VAO) which has the job of keeping track of what parts or what buffers correspond to what data. In this example we want to set our VAO up so that
@ -132,10 +89,6 @@ namespace TheLabs
// Now, we need to setup how the vertex shader will interpret the VBO data; you can send almost any C datatype (and a few non-C ones too) to it. // Now, we need to setup how the vertex shader will interpret the VBO data; you can send almost any C datatype (and a few non-C ones too) to it.
// While this makes them incredibly flexible, it means we have to specify how that data will be mapped to the shader's input variables. // While this makes them incredibly flexible, it means we have to specify how that data will be mapped to the shader's input variables.
// --- ELEMENT BUFFER (for indices) ---
_ebo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _ebo);
GL.BufferData(BufferTarget.ElementArrayBuffer, _cubeIndices.Length * sizeof(uint), _cubeIndices, BufferUsageHint.StaticDraw);
// To do this, we use the GL.VertexAttribPointer function // To do this, we use the GL.VertexAttribPointer function
// This function has two jobs, to tell opengl about the format of the data, but also to associate the current array buffer with the VAO. // This function has two jobs, to tell opengl about the format of the data, but also to associate the current array buffer with the VAO.
@ -179,7 +132,7 @@ namespace TheLabs
// OpenGL provides several different types of data that can be rendered. // OpenGL provides several different types of data that can be rendered.
// You can clear multiple buffers by using multiple bit flags. // You can clear multiple buffers by using multiple bit flags.
// However, we only modify the color, so ColorBufferBit is all we need to clear. // However, we only modify the color, so ColorBufferBit is all we need to clear.
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);
// To draw an object in OpenGL, it's typically as simple as binding your shader, // To draw an object in OpenGL, it's typically as simple as binding your shader,
// setting shader uniforms (not done here, will be shown in a future tutorial) // setting shader uniforms (not done here, will be shown in a future tutorial)
@ -200,7 +153,9 @@ namespace TheLabs
// is some variant of a triangle. Since we just want a single triangle, we use Triangles. // is some variant of a triangle. Since we just want a single triangle, we use Triangles.
// Starting index; this is just the start of the data you want to draw. 0 here. // Starting index; this is just the start of the data you want to draw. 0 here.
// How many vertices you want to draw. 3 for a triangle. // How many vertices you want to draw. 3 for a triangle.
GL.DrawElements(PrimitiveType.Triangles, _cubeIndices.Length, DrawElementsType.UnsignedInt, 0); // OpenTK windows are what's known as "double-buffered". In essence, the window manages two buffers. GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
// OpenTK windows are what's known as "double-buffered". In essence, the window manages two buffers.
// One is rendered to while the other is currently displayed by the window. // One is rendered to while the other is currently displayed by the window.
// This avoids screen tearing, a visual artifact that can happen if the buffer is modified while being displayed. // This avoids screen tearing, a visual artifact that can happen if the buffer is modified while being displayed.
// After drawing, call this function to swap the buffers. If you don't, it won't display what you've rendered. // After drawing, call this function to swap the buffers. If you don't, it won't display what you've rendered.

View File

@ -1,8 +1,9 @@
#version 330 #version 330
in vec4 vertexColor; in vec4 outColour;
out vec4 FragColor; out vec4 outputColor;
void main() void main()
{ {
FragColor = vertexColor; outputColor = outColour;
} }

View File

@ -25,15 +25,9 @@
// Next, the keyword "in" defines this as an input variable. We'll have an example of the "out" keyword in the next tutorial. // Next, the keyword "in" defines this as an input variable. We'll have an example of the "out" keyword in the next tutorial.
// Then, the keyword "vec3" means this is a vector with 3 floats inside. // Then, the keyword "vec3" means this is a vector with 3 floats inside.
layout(location = 0) in vec3 aPosition; layout(location = 0) in vec3 aPosition;
layout (location = 1) in vec4 aColor; layout(location = 1) in vec4 aColour;
out vec4 outColour;
out vec4 vertexColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
// Like C, we have an entrypoint function. In this case, it takes void and returns void, and must be named main. // Like C, we have an entrypoint function. In this case, it takes void and returns void, and must be named main.
@ -43,8 +37,8 @@ uniform mat4 projection;
// It's only used in some more advanced OpenGL functions; it's not needed here. // It's only used in some more advanced OpenGL functions; it's not needed here.
// So with a call to the vec4 function, we just give it a constant value of 1.0. // So with a call to the vec4 function, we just give it a constant value of 1.0.
void main() void main(void)
{ {
gl_Position = projection * view * model * vec4(aPosition, 1.0); outColour = aColour;
vertexColor = aColor; gl_Position = vec4(aPosition, 1.0);
} }