diff --git a/src/main.c b/src/main.c index 377c46b..59da158 100644 --- a/src/main.c +++ b/src/main.c @@ -228,47 +228,87 @@ void init(void) { GLfloat currentHue = 0.0f; +void drawCube(vec3 position, mat4 initialModelMatrix, mat4 viewMatrix) { + mat4 modelMatrix; + mat4 modelViewMatrix; + + mat4Translate(modelMatrix, initialModelMatrix, position); + + // combine model and view matrix to model-view matrix + mat4Multiply(modelViewMatrix, viewMatrix, modelMatrix); + + // send modelView and projection matrix to shader + GLuint modelViewLocation = glGetUniformLocation(program, "uModelView"); + glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix); + + // draw cube + glBindVertexArray(vertexArrayObject); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); + glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_INT, 0); +} + void draw(void) { updateStatusDisplay(); + // counter for animation currentHue += 0.0005f; if (currentHue > 1.0) currentHue = 0.0f; + // clear colour and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // select program glUseProgram(program); - mat4 transformMatrix; - vec3 scale = {0.2f, 0.2f, 0.2f}; - vec3 position = {0.0f, 0.0f, -1.0f}; - mat4Identity(transformMatrix); - mat4Scale(transformMatrix, transformMatrix, scale); - - mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2); - mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2); - - mat4Translate(transformMatrix, transformMatrix, position); - + // build view matrix mat4 viewMatrix; - mat4Identity(viewMatrix); - vec3 cameraPos = {0.0f, 0.0f, 0.0f}; + + // mat4Identity(viewMatrix); + vec3 cameraPos = {0.0f, 0.0f, 2.0f}; vec3 cameraLookAt = {0.0f, 0.0f, -1.0f}; vec3 cameraUp = {0.0f, 1.0f, 0.0f}; mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp); - mat4 modelViewMatrix; - mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix); - - - GLuint modelViewLocation = glGetUniformLocation(program, "uModelView"); - glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix); - GLuint projectionLocation = glGetUniformLocation(program, "uProjection"); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix); - glBindVertexArray(vertexArrayObject); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); - glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_INT, 0); + // build model Matrix + mat4 modelMatrix; + vec3 scale; + vec3 position; + mat4 scaledMatrix; + + mat4Identity(scaledMatrix); + + vec3Set(scale, 0.2f, 0.2f, 0.2f); + mat4Scale(scaledMatrix, scaledMatrix, scale); + + + // draw cubes + // cube 1 + vec3Set(position, -1.0f, 0.0f, 0.0f); + + mat4RotateZ(modelMatrix, scaledMatrix, currentHue * PI * 4); + mat4RotateY(modelMatrix, modelMatrix, currentHue * PI * 2); + + drawCube(position, modelMatrix, viewMatrix); + + // cube 2 + vec3Set(position, 0.0f, 0.0f, 0.0f); + + mat4RotateY(modelMatrix, scaledMatrix, currentHue * PI * 4); + mat4RotateX(modelMatrix, modelMatrix, currentHue * PI * 2); + + drawCube(position, modelMatrix, viewMatrix); + + // cube 3 + vec3Set(position, 1.0f, 0.0f, 0.0f); + + mat4RotateX(modelMatrix, scaledMatrix, currentHue * PI * 4); + mat4RotateZ(modelMatrix, modelMatrix, currentHue * PI * 2); + + drawCube(position, modelMatrix, viewMatrix); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { diff --git a/src/shaders/fragment.glsl b/src/shaders/fragment.glsl index f569c18..4d85599 100644 --- a/src/shaders/fragment.glsl +++ b/src/shaders/fragment.glsl @@ -1,5 +1,15 @@ #version 330 core -in vec3 vertexColor; +vec3 colors[6] = vec3[6]( + vec3(1.0, 0.5, 0.5), + vec3(0.5, 1.0, 0.5), + vec3(0.5, 0.5, 1.0), + vec3(1.0, 1.0, 0.5), + vec3(1.0, 0.5, 1.0), + vec3(0.5, 1.0, 1.0) +); + void main() { - gl_FragColor = vec4(vertexColor, 1.0); + // hacky solution to give each side a different colour + int side = gl_PrimitiveID / 2; + gl_FragColor = vec4(colors[side], 1.0); } \ No newline at end of file diff --git a/src/shaders/vertex.glsl b/src/shaders/vertex.glsl index 9b02b75..4fc2fdb 100644 --- a/src/shaders/vertex.glsl +++ b/src/shaders/vertex.glsl @@ -3,9 +3,6 @@ layout (location = 0) in vec3 aPosition; uniform mat4 uModelView; uniform mat4 uProjection; -out vec3 vertexColor; - void main() { - vertexColor = (aPosition + vec3(1)) / 2; gl_Position = uProjection * uModelView * vec4(aPosition, 1.0); } \ No newline at end of file