From f1e489610f14b193f9321b04c845518b219a2a2b Mon Sep 17 00:00:00 2001 From: kdx Date: Wed, 10 May 2023 02:24:18 +0200 Subject: Coordinate Systems --- src/fragment.glsl | 4 +-- src/main.c | 100 +++++++++++++++++++++++++++++++++++++++--------------- src/vertex.glsl | 10 +++--- 3 files changed, 79 insertions(+), 35 deletions(-) diff --git a/src/fragment.glsl b/src/fragment.glsl index a6f7ed3..1e1d5cf 100644 --- a/src/fragment.glsl +++ b/src/fragment.glsl @@ -1,8 +1,6 @@ #version 330 core out vec4 FragColor; -uniform vec4 ourColor; -in vec4 vertexColor; in vec2 texCoord; uniform sampler2D texture0; @@ -12,5 +10,5 @@ void main() { FragColor = mix(texture(texture0, texCoord), - texture(texture1, texCoord), 0.2) * vertexColor * ourColor; + texture(texture1, texCoord), 0.2); } diff --git a/src/main.c b/src/main.c index a899cbb..05ae9da 100644 --- a/src/main.c +++ b/src/main.c @@ -45,18 +45,52 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) return 1; } + glEnable(GL_DEPTH_TEST); glViewport(0, 0, 800, 600); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); static const float verts[] = { - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left - }; - static const GLuint indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle + -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 }; GLuint VBO; glGenBuffers(1, &VBO); @@ -69,14 +103,12 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, 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); + //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + 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); Shader shader; if (shader_init(&shader, @@ -99,21 +131,15 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) return 1; } - const GLint vertex_color_location = - shader_uniform_location(&shader, "ourColor"); - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GL_LINEAR); while (!glfwWindowShouldClose(window)) { - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader_use(&shader); - const int r = 0;//rand(); - glUniform4f(vertex_color_location, - !(r&1), !(r&2), !(r&4), !(r&8)); glUniform1i(shader_uniform_location(&shader, "texture0"), 0); glUniform1i(shader_uniform_location(&shader, "texture1"), 1); glActiveTexture(GL_TEXTURE0); @@ -122,14 +148,34 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) glBindTexture(GL_TEXTURE_2D, texture1.id); glBindVertexArray(VAO); + mat4 model = GLM_MAT4_IDENTITY_INIT; + glm_rotate(model, radians(-55.0f), (vec3){1.0f, 0.0f, 0.0f}); + + mat4 view = GLM_MAT4_IDENTITY_INIT; + glm_translate(view, (vec3){0.0f, 0.0f, -3.0f}); + + mat4 projection = GLM_MAT4_IDENTITY_INIT; + glm_perspective(radians(45.0f), 1.0f, 0.1f, 100.0f, projection); + + glUniformMatrix4fv(shader_uniform_location(&shader, + "model"), + 1, GL_FALSE, model[0]); + glUniformMatrix4fv(shader_uniform_location(&shader, + "view"), + 1, GL_FALSE, view[0]); + glUniformMatrix4fv(shader_uniform_location(&shader, + "projection"), + 1, GL_FALSE, projection[0]); + mat4 trans = GLM_MAT4_IDENTITY_INIT; - glm_translate(trans, (vec3){0.0f, 0.5f, 0.2f}); - glm_rotate(trans, glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f}); - glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f}); + //glm_translate(trans, (vec3){0.0f, 0.5f, 0.2f}); + //glm_rotate(trans, glfwGetTime(), (vec3){0.0f, 1.0f, 1.0f}); + //glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f}); + glUniformMatrix4fv(shader_uniform_location(&shader, "transform"), 1, GL_FALSE, trans[0]); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, 36); glm_mat4_identity(trans); glm_rotate(trans, glfwGetTime() * 2, (vec3){0.0f, 0.0f, 1.0f}); @@ -137,7 +183,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) glUniformMatrix4fv(shader_uniform_location(&shader, "transform"), 1, GL_FALSE, trans[0]); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, 36); glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/vertex.glsl b/src/vertex.glsl index bad790f..0a6f24e 100644 --- a/src/vertex.glsl +++ b/src/vertex.glsl @@ -1,17 +1,17 @@ #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 vec4 vertexColor; out vec2 texCoord; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); - vertexColor = vec4(aColor, 1.0); + gl_Position = projection * view * model * transform * vec4(aPos, 1.0); texCoord = aTexCoord; } -- cgit v1.2.3