diff --git a/src/main.c b/src/main.c index fd763cb..3f26d01 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,8 @@ GLuint program; GLuint vertexArrayObject; +GLuint indexBuffer; + GLuint initialWindowWidth = 800; GLuint initialWindowHeight = 600; @@ -112,98 +114,52 @@ void init(void) { * | -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[] = { // X // Y - - // left 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, - - // 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.35f, 0.6f, + 0.35f, -0.6f, + 0.2f, -0.6f, 0.2f, -0.1f, - - // bottom bar - -0.35f, -0.7f, - -0.35f, -0.9f, - 0.35f, -0.9f, + -0.2f, -0.1f, + -0.2f, -0.6f, + -0.35f, -0.6f, -0.35f, -0.7f, + 0.35f, -0.7f, 0.35f, -0.9f, - 0.35f, -0.7f + -0.35f, -0.9f }; - GLfloat colors[] = { - // R // G // B - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.3f, - 1.0f, 0.3f, 0.0f, + GLuint restart = 128; + glEnable(GL_PRIMITIVE_RESTART); + glPrimitiveRestartIndex(restart); - 1.0f, 0.3f, 0.3f, - 1.0f, 0.3f, 0.6f, - 1.0f, 0.6f, 0.3f, - - - 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 + GLuint indices[] = { + 1, 0, 10, 11, restart, + 2, 3, 9, 8, restart, + 5, 4, 6, 7, restart, + 12, 13, 15, 14, restart }; DEBUG("Creating vertext buffer"); @@ -213,13 +169,6 @@ void init(void) { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 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"); // create vertex array object glGenVertexArrays(1, &vertexArrayObject); @@ -237,12 +186,12 @@ void init(void) { ); glEnableVertexAttribArray(0); - // vertex color data - glBindBuffer(GL_ARRAY_BUFFER, colorBuffer); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(1); - + DEBUG("Creating index buffer"); + glGenBuffers(1, &indexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 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); 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) { diff --git a/src/shaders/vertex.glsl b/src/shaders/vertex.glsl index eb6ebd9..b7e01b1 100644 --- a/src/shaders/vertex.glsl +++ b/src/shaders/vertex.glsl @@ -1,11 +1,10 @@ #version 330 core layout (location = 0) in vec2 aPosition; -layout (location = 1) in vec3 aColor; uniform vec2 uPosition; out vec3 vertexColor; void main() { - vertexColor = aColor; + vertexColor = vec3(1.0, 0.1, 0.1); gl_Position = vec4(uPosition + aPosition, 0.0, 1.0); } \ No newline at end of file