opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 524c505d628c280f14aa75ac90dad65967d72c12
parent c4f54ac8aff3b3c3590f61052940dbf529aca328
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 28 Jun 2023 07:02:15 +0900

implement translation

Diffstat:
Mglm.c | 39++++++++++++++++++++++++++++++++++++---
Mglm.h | 3+++
Mmain.c | 14+++++++-------
3 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/glm.c b/glm.c @@ -101,6 +101,23 @@ vecCross(vec *v, const vec *w) return v; } +char * +matStr(char *s, mat *m) +{ + int cur = 0; + for (int i = 0; i < m->size; i++) { + for (int j = 0; j < m->size; j++) { + cur += sprintf(&s[cur], "%2.2f%s", m->data[i+m->size*j], + j == m->size-1 ? "" : " "); + } + if (i != m->size - 1) { + cur += sprintf(&s[cur], "\n"); + } + } + sprintf(&s[cur], "\n\0"); + return s; +} + mat * matMake(float *data, int size) { @@ -113,6 +130,18 @@ matMake(float *data, int size) return m; } +mat * +matMakeIdent(int size) +{ + mat *m = (mat *)malloc(sizeof(mat)); + m->data = (float *)calloc(sizeof(float), size*size); + for (int i = 0; i < size; i++) { + m->data[i + size*i] = 1; + } + m->size = size; + return m; +} + void matFree(mat *m) { @@ -151,15 +180,19 @@ matDot(mat *m, const mat *n) // m_ij = m->data[i + m->size*j] mat * translate(mat *m, const vec *v) { - return NULL; + 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]; + return m; } mat * rotate(mat *m, float rad, vec *axis) { - if (axis->size != 3) { + if (axis->size != 3) assert(!"axis size must be 3"); - } float x = axis->data[0]; float y = axis->data[1]; float z = axis->data[2]; diff --git a/glm.h b/glm.h @@ -25,9 +25,12 @@ vec *vecSub(vec *v, const vec *w); float vecDot(const vec *v, const vec *w); vec *vecCross(vec *v, const vec *w); +char *matStr(char *s, mat *m); mat *matMake(float *data, int size); +mat *matMakeIdent(int size); void matFree(mat *m); float *matDataPtr(mat *m); mat *translate(mat *m, const vec *v); +// axis must be an unit vector mat *rotate(mat *m, float rad, vec *axis); diff --git a/main.c b/main.c @@ -66,11 +66,8 @@ main(void) 0, 3, 5, 0, 4, 5, }; - mat *trans = matMake((float[]){1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0,}, 4); - vec *axis = vecMake((float[]){1.0, 1.0, 0.0}, 3); + vec *axis = vecMake((float[]){1.41421356/2, 1.41421356/2, 0.0}, 3); + vec *transvec = vecMake((float[]){0.5, -0.5, 0.0}, 3); // initialize glfw glfwInit(); @@ -152,27 +149,30 @@ main(void) stbi_image_free(data); } + while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(1.0, 1.0, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); + mat *trans = matMakeIdent(4); if (rotating) { - rotate(trans, delta, axis); + rotate(trans, (float)glfwGetTime(), axis); } + 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)); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); + matFree(trans); glfwPollEvents(); glfwSwapBuffers(window); } - matFree(trans); vecFree(axis); glDeleteVertexArrays(1, &VAO);