This commit is contained in:
Luca Conte 2025-03-11 12:06:49 +01:00
parent 4cc7e98998
commit e8b713db85
2 changed files with 33 additions and 84 deletions

View File

@ -16,6 +16,8 @@ GLuint program;
GLuint vertexArrayObject; GLuint vertexArrayObject;
GLuint indexBuffer;
GLuint initialWindowWidth = 800; GLuint initialWindowWidth = 800;
GLuint initialWindowHeight = 600; GLuint initialWindowHeight = 600;
@ -112,98 +114,52 @@ void init(void) {
* | -0.2 0.2 | * | -0.2 0.2 |
* | | | | * | | | |
* *
* +----+ +----+ --- 0.6 * 0----1 4----5 --- 0.6
* | | | | * | | | |
* | | | | * | | | |
* | +--------+ | --- 0.1 * | 2--------3 | --- 0.1
* | | * | |
* | +--------+ | --- -0.1 * | 9--------8 | --- -0.1
* | | | | * | | | |
* | | | | * | | | |
* +----+ +----+ --- -0.6 * 11---10 7----6 --- -0.6
* *
* +------------------+ --- -0.7 * 12-----------------13 --- -0.7
* | | * | |
* +------------------+ --- -0.9 * 15-----------------14 --- -0.9
* *
*/ */
GLfloat vertices[] = { GLfloat vertices[] = {
// X // Y // X // Y
// left vertical bar
-0.35f, -0.6f,
-0.35f, 0.6f, -0.35f, 0.6f,
-0.2f, 0.6f, -0.2f, 0.6f,
-0.35f, -0.6f,
-0.2f, 0.6f,
-0.2f, -0.6f,
// right vertical bar
0.35f, -0.6f,
0.35f, 0.6f,
0.2f, 0.6f,
0.35f, -0.6f,
0.2f, 0.6f,
0.2f, -0.6f,
// middle bar
-0.2f, -0.1f,
-0.2f, 0.1f, -0.2f, 0.1f,
0.2f, 0.1f, 0.2f, 0.1f,
0.2f, 0.6f,
-0.2f, -0.1f, 0.35f, 0.6f,
0.2f, 0.1f, 0.35f, -0.6f,
0.2f, -0.6f,
0.2f, -0.1f, 0.2f, -0.1f,
-0.2f, -0.1f,
// bottom bar -0.2f, -0.6f,
-0.35f, -0.7f, -0.35f, -0.6f,
-0.35f, -0.9f,
0.35f, -0.9f,
-0.35f, -0.7f, -0.35f, -0.7f,
0.35f, -0.7f,
0.35f, -0.9f, 0.35f, -0.9f,
0.35f, -0.7f -0.35f, -0.9f
}; };
GLfloat colors[] = { GLuint restart = 128;
// R // G // B glEnable(GL_PRIMITIVE_RESTART);
1.0f, 0.0f, 0.0f, glPrimitiveRestartIndex(restart);
1.0f, 0.0f, 0.3f,
1.0f, 0.3f, 0.0f,
1.0f, 0.3f, 0.3f, GLuint indices[] = {
1.0f, 0.3f, 0.6f, 1, 0, 10, 11, restart,
1.0f, 0.6f, 0.3f, 2, 3, 9, 8, restart,
5, 4, 6, 7, restart,
12, 13, 15, 14, restart
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.3f,
0.3f, 1.0f, 0.0f,
0.3f, 1.0f, 0.3f,
0.3f, 1.0f, 0.6f,
0.6f, 1.0f, 0.3f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.1f,
1.0f, 1.0f, 0.2f,
1.0f, 1.0f, 0.7f,
1.0f, 1.0f, 0.8f,
1.0f, 1.0f, 0.9f
}; };
DEBUG("Creating vertext buffer"); DEBUG("Creating vertext buffer");
@ -213,13 +169,6 @@ void init(void) {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating colour buffer");
GLuint colorBuffer;
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating vertex array object"); DEBUG("Creating vertex array object");
// create vertex array object // create vertex array object
glGenVertexArrays(1, &vertexArrayObject); glGenVertexArrays(1, &vertexArrayObject);
@ -237,12 +186,12 @@ void init(void) {
); );
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// vertex color data DEBUG("Creating index buffer");
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer); glGenBuffers(1, &indexBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glEnableVertexAttribArray(1); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
@ -269,7 +218,8 @@ void draw(void) {
glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f); glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f);
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, 48); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_INT, 0);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {

View File

@ -1,11 +1,10 @@
#version 330 core #version 330 core
layout (location = 0) in vec2 aPosition; layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec3 aColor;
uniform vec2 uPosition; uniform vec2 uPosition;
out vec3 vertexColor; out vec3 vertexColor;
void main() { void main() {
vertexColor = aColor; vertexColor = vec3(1.0, 0.1, 0.1);
gl_Position = vec4(uPosition + aPosition, 0.0, 1.0); gl_Position = vec4(uPosition + aPosition, 0.0, 1.0);
} }