From 46f5c48493bd7473572f2f53b33b44f1d93ca7be Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 24 Apr 2024 10:43:08 +0200 Subject: [PATCH] u05-2 but boring --- u05-2/fragmentShader.glsl | 2 +- u05-2/main.c | 106 ++++++++++++++++++++++++++++---------- u05-2/vertexShader.glsl | 3 +- 3 files changed, 81 insertions(+), 30 deletions(-) diff --git a/u05-2/fragmentShader.glsl b/u05-2/fragmentShader.glsl index 4064d63..20348df 100644 --- a/u05-2/fragmentShader.glsl +++ b/u05-2/fragmentShader.glsl @@ -1,5 +1,5 @@ #version 330 core -in vec3 color; +uniform vec3 color; void main() { gl_FragColor = vec4(color, 1.0); } \ No newline at end of file diff --git a/u05-2/main.c b/u05-2/main.c index 18debb0..817ea81 100644 --- a/u05-2/main.c +++ b/u05-2/main.c @@ -12,6 +12,7 @@ #include #include #include +#include #define RESTART 345678 @@ -19,11 +20,14 @@ GLuint program; GLuint vao; GLuint cubeIndicesBufferObject; +bool exitRequested = false; + GLFWwindow* window; GLfloat aspectRatio = 1.0f; GLfloat step = 0.0f; +const GLfloat pi = 3.14159f; GLfloat cameraPosition[3] = {0.0f, 0.0f, 2.0f}; @@ -77,18 +81,20 @@ void handleInputs(void) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { cameraPosition[2] -= 0.02f; } + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + cameraPosition[1] += 0.02f; + } + if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + cameraPosition[1] -= 0.02f; + } } void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) { - // if (action == GLFW_PRESS) { - // if (key == GLFW_KEY_W) { - // cameraVelocity[0] += 0.1f; - // } - // if (key == GLFW_KEY_S) { - // cameraVelocity[0] -= 0.1f; - // } - // if (key == GLFW_KEY_S) - // } + if (action == GLFW_PRESS) { + if (key == GLFW_KEY_ESCAPE) { + exitRequested = true; + } + } } void init(void) { @@ -191,9 +197,6 @@ void init(void) { glEnable(GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex(RESTART); - // ENABLE DEPTH TESTING - // glEnable(GL_DEPTH_TEST); - // DEFINE INDEX ARRAY FOR ELEMENT DRAWING glGenBuffers(1, &cubeIndicesBufferObject); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject); @@ -203,6 +206,41 @@ void init(void) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); } +void drawCube(GLfloat* position, GLfloat* scaleVec, GLfloat* rotateVec) { + GLfloat modelTransformation[16]; + + identity(modelTransformation); + + scale(modelTransformation, modelTransformation, scaleVec); + + rotateX(modelTransformation, modelTransformation, rotateVec[0]); + rotateY(modelTransformation, modelTransformation, rotateVec[1]); + rotateZ(modelTransformation, modelTransformation, rotateVec[2]); + + translate(modelTransformation, modelTransformation, position); + + + glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, modelTransformation); + + glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 0.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(0 * sizeof(GLuint))); + + glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 0.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6 * sizeof(GLuint))); + + glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 1.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(12 * sizeof(GLuint))); + + glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 1.0f, 1.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(18 * sizeof(GLuint))); + + glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 0.0f, 1.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(24 * sizeof(GLuint))); + + glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 1.0f); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(30 * sizeof(GLuint))); +} + void draw(void) { handleInputs(); @@ -214,26 +252,24 @@ void draw(void) { step += 0.002f; if (step > 1.0f) step -= 1.0f; - GLfloat stepi = step * 3.14159 * 2; + GLfloat stepi = step * pi * 2; // ------------- MODEL TRANSFORMATION --------------------- // SCALE -> ROTATE -> TRANSLATE - GLfloat scaleFactor = 0.6f; - // GLfloat cubePosition[3] = {0.0f, sin(3.14159f * 2 * step), 0.0f}; - GLfloat cubePosition[3] = {0.0f, 0.0f, 0.0f}; - GLfloat cubeScale[3] = {scaleFactor, scaleFactor, scaleFactor}; + // GLfloat cubePosition[3] = {0.0f, -2.0f, 0.0f}; + // GLfloat cubeScale[3] = {4.0f, 0.2f, 4.0f}; - GLfloat modelTransformation[16]; - identity(modelTransformation); - scale(modelTransformation, modelTransformation, cubeScale); + // GLfloat modelTransformation[16]; + // identity(modelTransformation); + // scale(modelTransformation, modelTransformation, cubeScale); - rotateY(modelTransformation, modelTransformation, stepi); - rotateX(modelTransformation, modelTransformation, stepi + 1.0f); - rotateZ(modelTransformation, modelTransformation, stepi + 0.5f); + // rotateY(modelTransformation, modelTransformation, stepi); + // rotateX(modelTransformation, modelTransformation, stepi + 1.0f); + // rotateZ(modelTransformation, modelTransformation, stepi + 0.5f); - translate(modelTransformation, modelTransformation, cubePosition); + // translate(modelTransformation, modelTransformation, cubePosition); @@ -279,8 +315,24 @@ void draw(void) { glUniformMatrix4fv(glGetUniformLocation(program, "globalTransformation"), 1, GL_FALSE, globalTransformation); - glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, modelTransformation); - glDrawElements(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(GLuint), GL_UNSIGNED_INT, NULL); + GLfloat position[3] = {0.0f, -3.0f, 0.0f}; + GLfloat scaleVec[3] = {3.0f, 0.1f, 3.0f}; + GLfloat rotateVec[3] = {0.0f, 0.0f, 0.0f}; + drawCube(position, scaleVec, rotateVec); + + position[1] = -2.8f; + scaleVec[0] = 2.0f; + scaleVec[2] = 2.0f; + rotateVec[0] = pi; + drawCube(position, scaleVec, rotateVec); + + position[1] = -2.4f; + scaleVec[0] = 1.0f; + scaleVec[1] = 0.3f; + scaleVec[2] = 1.0f; + rotateVec[0] = 0.0f; + rotateVec[1] = pi / 4; + drawCube(position, scaleVec, rotateVec); } void framebuffer_size_callback(GLFWwindow *window, int width, int height) { @@ -338,7 +390,7 @@ int main(void) { init(); - while (!glfwWindowShouldClose(window)) { + while (!glfwWindowShouldClose(window) && !exitRequested) { draw(); glfwSwapBuffers(window); diff --git a/u05-2/vertexShader.glsl b/u05-2/vertexShader.glsl index 58b4193..6fcf2c8 100644 --- a/u05-2/vertexShader.glsl +++ b/u05-2/vertexShader.glsl @@ -2,8 +2,7 @@ layout (location = 0) in vec3 aPosition; uniform mat4 globalTransformation; uniform mat4 modelTransformation; -out vec3 color; void main() { - color = aPosition / 2 + vec3(0.5, 0.5, 0.5); + // color = aPosition / 2 + vec3(0.5, 0.5, 0.5); gl_Position = globalTransformation * modelTransformation * vec4(aPosition, 1.0); } \ No newline at end of file