From bc5ca68189db1818557c683bfac952ab39b9c970 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 23 Oct 2023 22:31:48 +0700 Subject: [PATCH] Finish Coordinate Systems --- .gitignore | 1 + lib.h | 58 +++++++++++++++++++++++ main.c | 116 +++++++++++++++++++++++++++------------------- shaders/main.frag | 5 +- shaders/main.vert | 15 +++--- 5 files changed, 135 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 3ccef61..0b2101a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /opengl /game/main /game/texturepacker +/.idea/ diff --git a/lib.h b/lib.h index 1dfff99..e630cb2 100644 --- a/lib.h +++ b/lib.h @@ -112,3 +112,61 @@ unsigned int compileShaderProgram(char* vertSrcPath, char* fragSrcPath, char* ge return program; } + + +float quadVerts[] = { + // Position // Color // UV + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f +}; + +unsigned int quadIndices[] = { + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle +}; + +float cubeVerts[] = { + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f +}; diff --git a/main.c b/main.c index 66ffcfd..64cc002 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,8 @@ int SCREEN_WIDTH = 1024; int SCREEN_HEIGHT = 768; +vec2 direction = {0}; + void framebuffer_size_callback(GLFWwindow* window, int width, int height) { (void)window; @@ -26,6 +28,20 @@ void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); + direction[0] = 0; + direction[1] = 0; + if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) { + direction[0] = 1; + } + if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) { + direction[0] = -1; + } + if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { + direction[1] = -1; + } + if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { + direction[1] = 1; + } } int main(void) { @@ -55,7 +71,7 @@ int main(void) { GLuint texture1, texture2; glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -70,7 +86,7 @@ int main(void) { glGenTextures(1, &texture2); glBindTexture(GL_TEXTURE_2D, texture2); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -86,39 +102,28 @@ int main(void) { stbi_image_free(container); stbi_image_free(smiley); - float v1[] = { - // Position // Color // UV - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f - }; - - unsigned int indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - }; + glEnable(GL_DEPTH_TEST); GLuint vaos[2]; GLuint vbos[2]; - unsigned int EBO; + // unsigned int EBO; glGenVertexArrays(2, vaos); - glGenBuffers(1, &EBO); + // glGenBuffers(1, &EBO); glGenBuffers(2, vbos); glBindVertexArray(vaos[0]); glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVerts), cubeVerts, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float))); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float))); - glEnableVertexAttribArray(2); + // glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float))); + // glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -129,32 +134,46 @@ int main(void) { int t2Loc = glGetUniformLocation(shaderProgram, "t2"); glUniform1i(t2Loc, 1); - mat4 trans; - glm_mat4_identity(trans); - // glm_rotate(trans, GLM_PI_2, (vec3){0.0f, 0.0f, 1.0f}); - glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f}); + mat4 view; + glm_mat4_identity(view); + glm_translate(view, (vec3){0.0f, 0.0f, -0.6f}); + mat4 projection; + glm_mat4_identity(projection); + float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT; + glm_perspective(45.0f, aspectRatio, 0.1f, 100.0f, projection); - mat4 trans2; - glm_mat4_identity(trans2); - glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f}); - glm_translate(trans2, (vec3){-5.1f, 1.1f, 0.0f}); + int cubeCount = 10; + vec3 cubePositions[cubeCount]; + srand(arc4random()); + for (int i = 0; i < cubeCount; i++) { + float x = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f; + float y = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f; + float z = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f; + cubePositions[i][0] = x; + cubePositions[i][1] = y; + cubePositions[i][2] = z; + } float lastFrame = 0.0f; while (!glfwWindowShouldClose(window)) { float currentFrame = (float)glfwGetTime(); - float deltaTime = currentFrame - lastFrame; + float dt = currentFrame - lastFrame; lastFrame = currentFrame; processInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // float spin = (sin(timeValue) / 2.0f) + 0.5f; - printf("%f\n", deltaTime); - glm_rotate(trans, (float)deltaTime * -1.0f, (vec3){0.0f, 0.0f, 1.0f}); + // float spin = (sin(currentFrame) / 2.0f) + 0.5f; + // printf("%f\n", deltaTime); - int transLoc = glGetUniformLocation(shaderProgram, "transform"); - glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans[0]); + float speed = 0.5 * dt; + glm_translate(view, (vec3){direction[0] * speed, 0.0f, direction[1] * speed}); + int matrixLocation; + matrixLocation = glGetUniformLocation(shaderProgram, "view"); + glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, view[0]); + matrixLocation = glGetUniformLocation(shaderProgram, "projection"); + glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, projection[0]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); @@ -162,16 +181,19 @@ int main(void) { glBindTexture(GL_TEXTURE_2D, texture2); glBindVertexArray(vaos[0]); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + for (int i = 0; i < cubeCount; i++) { + mat4 model; + glm_mat4_identity(model); + glm_scale(model, (vec3){0.1f, 0.1f, 0.1f}); + glm_translate(model, cubePositions[i]); + glm_rotate(model, (i+1) * currentFrame, (vec3){1.0f, 1.0f, 0.0f}); - mat4 trans2; - glm_mat4_identity(trans2); - glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f}); - glm_translate(trans2, (vec3){cos(currentFrame * 4.0f) * 5.0f, - sin(currentFrame * 4.0f) * 5.0f, - 0.0f}); - glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans2[0]); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + matrixLocation = glGetUniformLocation(shaderProgram, "model"); + glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, model[0]); + + glDrawArrays(GL_TRIANGLES, 0, 36); + } glfwSwapBuffers(window); glfwPollEvents(); diff --git a/shaders/main.frag b/shaders/main.frag index e8b04e8..546e78f 100644 --- a/shaders/main.frag +++ b/shaders/main.frag @@ -1,15 +1,12 @@ #version 330 core -in vec3 vPos; -in vec3 vCol; in vec2 tCoord; out vec4 FragColor; uniform sampler2D t1; uniform sampler2D t2; -uniform float mixAmount; void main() { - vec2 flipped = vec2(-tCoord.x, tCoord.y); + // vec2 flipped = vec2(-tCoord.x, tCoord.y); FragColor = mix(texture(t1, tCoord), texture(t2, tCoord), 0.3); } diff --git a/shaders/main.vert b/shaders/main.vert index 737dcab..42e27bb 100644 --- a/shaders/main.vert +++ b/shaders/main.vert @@ -1,17 +1,14 @@ #version 330 core layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -layout (location = 2) in vec2 aTexCoord; +layout (location = 1) in vec2 aTexCoord; -out vec3 vPos; -out vec3 vCol; out vec2 tCoord; -uniform float offset; -uniform mat4 transform; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; void main() { - gl_Position = transform * vec4(aPos, 1.0); - vPos = aPos; - vCol = aColor; + gl_Position = projection * view * model * vec4(aPos, 1.0); tCoord = aTexCoord; }