opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 16625bd18dee43051f5f2fa1b87e8f21b1428d69
parent 524c505d628c280f14aa75ac90dad65967d72c12
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 28 Jun 2023 08:04:12 +0900

fix bug

Diffstat:
Mglm.c | 12+++++++-----
Mmain.c | 21+++++++++++++--------
Mshaders/fragment.sl | 2++
Mshaders/vertex.sl | 2++
4 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/glm.c b/glm.c @@ -158,9 +158,8 @@ float mat * matDot(mat *m, const mat *n) // m_ij = m->data[i + m->size*j] { - if (m->size != n->size) { + if (m->size != n->size) assert(!"mat sizes don't match"); - } float *data = (float *)malloc(sizeof(float)*m->size*m->size); for (int i = 0; i < m->size; i++) { for (int j = 0; j < m->size; j++) { @@ -182,9 +181,12 @@ translate(mat *m, const vec *v) { if (m->size != 4 || v->size != 3) assert(!"mat size must be 4 and vec size must be 3"); - m->data[12] += v->data[0]; - m->data[13] += v->data[1]; - m->data[14] += v->data[2]; + mat *t = matMakeIdent(4); + t->data[12] = v->data[0]; + t->data[13] = v->data[1]; + t->data[14] = v->data[2]; + matDot(m, t); + matFree(t); return m; } diff --git a/main.c b/main.c @@ -66,7 +66,8 @@ main(void) 0, 3, 5, 0, 4, 5, }; - vec *axis = vecMake((float[]){1.41421356/2, 1.41421356/2, 0.0}, 3); + vec *axis0 = vecMake((float[]){1.41421356/2, 1.41421356/2, 0.0}, 3); + vec *axis1 = vecMake((float[]){0.0, 0.0, 1.0}, 3); vec *transvec = vecMake((float[]){0.5, -0.5, 0.0}, 3); // initialize glfw @@ -156,25 +157,29 @@ main(void) glClearColor(1.0, 1.0, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); - mat *trans = matMakeIdent(4); + mat *transform = matMakeIdent(4); if (rotating) { - rotate(trans, (float)glfwGetTime(), axis); + rotate(transform, (float)glfwGetTime(), axis0); + } + translate(transform, transvec); + if (rotating) { + rotate(transform, (float)glfwGetTime(), axis1); } - translate(trans, transvec); ShaderSetFloat(shader, "alpha", alpha); glUniform1i(glGetUniformLocation(shader->ID, "texture0"), 0); glUniform1i(glGetUniformLocation(shader->ID, "texture1"), 1); unsigned int tloc = glGetUniformLocation(shader->ID, "transform"); - glUniformMatrix4fv(tloc, 1, GL_FALSE, matDataPtr(trans)); + glUniformMatrix4fv(tloc, 1, GL_FALSE, matDataPtr(transform)); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); - matFree(trans); + matFree(transform); glfwPollEvents(); glfwSwapBuffers(window); } - vecFree(axis); - + vecFree(axis0); + vecFree(axis1); + glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); ShaderDelete(shader); diff --git a/shaders/fragment.sl b/shaders/fragment.sl @@ -1,5 +1,6 @@ #version 330 core in vec2 texCoord; +in float depth; out vec4 FragColor; uniform sampler2D texture0; uniform sampler2D texture1; @@ -10,4 +11,5 @@ main() { FragColor = mix(texture(texture0, texCoord), texture(texture1, vec2(1, 1)-texCoord), alpha); + FragColor *= vec4(depth, depth, depth, 1.0); } diff --git a/shaders/vertex.sl b/shaders/vertex.sl @@ -2,6 +2,7 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 texCoord; +out float depth; uniform mat4 transform; void @@ -9,4 +10,5 @@ main() { gl_Position = transform * vec4(aPos, 1.0); texCoord = aTexCoord; + depth = gl_Position.z / 2 + 1.0; }