From d7bd9a3dbd3486a4b0615dc9f30a890ff6d04ec6 Mon Sep 17 00:00:00 2001 From: jvech Date: Mon, 29 Aug 2022 09:12:18 -0500 Subject: feat: 3D support added --- src/linear.c | 50 +++++++++++++++++++++++++++------- src/linear.h | 10 ++++--- src/main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- src/main.h | 8 +++--- src/shader.c | 1 - 5 files changed, 124 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/linear.c b/src/linear.c index 06c2e33..4a4c958 100644 --- a/src/linear.c +++ b/src/linear.c @@ -1,10 +1,13 @@ #include "linear.h" +#include +#include +#include #include mat4 linearLookAt(vec3 position, vec3 target, vec3 world_up) { - mat4 out = linearMat4Identity(); + mat4 out = linearMat4Identity(1.0); mat4 translate; vec3 cam_dir, cam_right, cam_up; /* position - target */ @@ -33,8 +36,8 @@ linearPerspective(float FoV, float ratio, float near, float far) { mat4 out = linearMat4Fill(0.0); float FoV_radians = FoV * M_PI / 180; - float width = near * tanf(FoV_radians) * ratio; - float height = near * tanf(FoV_radians); + float width = near * tanf(FoV_radians); + float height = near * tanf(FoV_radians) * ratio; out.matrix[0][0] = near / width; out.matrix[1][1] = near / height; @@ -47,7 +50,7 @@ linearPerspective(float FoV, float ratio, float near, float far) mat4 linearTranslate(float T_x, float T_y, float T_z) { - mat4 out = linearMat4Identity(); + mat4 out = linearMat4Identity(1.0); out.matrix[0][3] = T_x; out.matrix[1][3] = T_y; out.matrix[2][3] = T_z; @@ -63,7 +66,7 @@ linearTranslatev(vec3 T) mat4 linearScale(float S_x, float S_y, float S_z) { - mat4 out = linearMat4Identity(); + mat4 out = linearMat4Identity(1.0); out.matrix[0][0] = S_x; out.matrix[1][1] = S_y; out.matrix[2][2] = S_z; @@ -77,9 +80,9 @@ linearScalev(vec3 S) } mat4 -linearRotate(float degree, vec3 R_xyz) +linearRotatev(float degree, vec3 R_xyz) { - mat4 out = linearMat4Identity(); + mat4 out = linearMat4Identity(1.0); vec3 R_xyz_normalized = linearVec3Normalize(R_xyz); float radians = degree * M_PI/180.0; float Rx = R_xyz_normalized.vector[0]; @@ -88,7 +91,7 @@ linearRotate(float degree, vec3 R_xyz) float rcos = cosf(radians); float rsin = sinf(radians); - out.matrix[0][0] = rcos + pow(Rx, 2) * ( 1 - rcos); + out.matrix[0][0] = rcos + pow(Rx, 2) * (1 - rcos); out.matrix[0][1] = Rx * Ry * (1 - rcos) - Rz * rsin; out.matrix[0][2] = Rx * Rz * (1 - rcos) + Ry * rsin; @@ -102,6 +105,12 @@ linearRotate(float degree, vec3 R_xyz) return out; } +mat4 +linearRotate(float degree, float Rx, float Ry, float Rz) +{ + return linearRotatev(degree, linearVec3(Rx, Ry, Rz)); +} + mat4 linearMat4Fill(float value) { @@ -116,13 +125,13 @@ linearMat4Fill(float value) } mat4 -linearMat4Identity() +linearMat4Identity(float value) { int i, j; mat4 out; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { - if (i == j) out.matrix[i][j] = 1.0; + if (i == j) out.matrix[i][j] = value; else out.matrix[i][j] = 0.0; } } @@ -157,6 +166,27 @@ linearMat4Mul(mat4 x1, mat4 x2) return out; } +mat4 +linearMat4Muln(int n, ...) +{ + mat4 out; + + if (n <= 0) { + fprintf(stderr, "linearMat4Muln() Error: the specified number of args must be a positive integer greater than 0\n"); + } + + va_list(ap); + va_start(ap, n); + out = va_arg(ap, mat4); + + int i; + for (i = 1; i < n; i++) { + out = linearMat4Mul(out, va_arg(ap, mat4)); + } + va_end(ap); + return out; +} + mat4 linearMat4Add(mat4 x1, mat4 x2) { diff --git a/src/linear.h b/src/linear.h index 86b93d7..c5f40aa 100644 --- a/src/linear.h +++ b/src/linear.h @@ -13,17 +13,19 @@ typedef struct { float vector[3]; } vec3; -mat4 linearTranslatev(vec3 translate_vector); //TODO -mat4 linearScalev(vec3 scale_vector); //TODO +mat4 linearTranslatev(vec3 translate_vector); +mat4 linearRotatev(float degree, vec3 rotation_axis); +mat4 linearScalev(vec3 scale_vector); mat4 linearTranslate(float translate_x, float translate_y, float translate_z); mat4 linearScale(float scale_x, float scale_y, float scale_z); -mat4 linearRotate(float degree, vec3 rotation_axis); +mat4 linearRotate(float degree, float rotate_x, float rotate_y, float rotate_z); mat4 linearPerspective(float FoV, float ratio, float near, float far); mat4 linearLookAt(vec3 position, vec3 target, vec3 up); mat4 linearMat4Fill(float value); -mat4 linearMat4Identity(); +mat4 linearMat4Identity(float value); mat4 linearMat4Mul(mat4 x1, mat4 x2); +mat4 linearMat4Muln(int n, ...); mat4 linearMat4Transpose(mat4 x); mat4 linearMat4Inv(mat4 x); //TODO mat4 linearMat4Add(mat4 x1, mat4 x2); //TODO diff --git a/src/main.c b/src/main.c index 27181ef..7cded68 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,9 @@ #include #include #include +#include +#include #include #include @@ -12,21 +14,42 @@ #include "shader.h" float vertices[] = { - -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.0f, 1.0, 1.0f, 0.0f, - 0.5f, 0.5f, 0.0f, 1.0, 1.0f, 0.0f, + -0.5,-0.5,-0.5, 1.0, 1.0, 1.0, + 0.5,-0.5,-0.5, 1.0, 1.0, 0.0, + -0.5, 0.5,-0.5, 1.0, 0.0, 1.0, + 0.5, 0.5,-0.5, 1.0, 0.0, 0.0, + -0.5,-0.5, 0.5, 0.0, 1.0, 1.0, + 0.5,-0.5, 0.5, 0.0, 1.0, 0.0, + -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, + 0.5, 0.5, 0.5, 0.0, 0.0, 0.0, }; unsigned int indices[] = { 0, 1, 2, - 2, 3, 1 + 2, 3, 1, + + 4, 5, 7, + 7, 6, 4, + + 1, 3, 5, + 3, 7, 5, + + 6, 0, 2, + 6, 0, 4, + + 2, 7, 6, + 2, 7, 3, + + 0, 1, 5, + 0, 4, 5 }; static void userError(const char *msg, const char *detail); static void glfw_size_callback(GLFWwindow *window, int width, int height); static void processInput(GLFWwindow *window); +static void linearPrintMat4(mat4 x); + void userError(const char *msg, const char *detail) { @@ -34,7 +57,7 @@ userError(const char *msg, const char *detail) exit(1); } -static void +void glfw_size_callback(GLFWwindow *window, int width, int height) { glViewport(0, 0, width, height); @@ -55,6 +78,19 @@ processInput(GLFWwindow *window) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); } +void +linearPrintMat4(mat4 x) +{ + int i, j; + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + printf("%f ", x.matrix[i][j]); + } + printf("\n"); + } + printf("\n"); +} + int main() { GLFWwindow *window; @@ -81,6 +117,8 @@ int main() userError("glewInit() failed", (const char *)glewGetErrorString(glewErrno)); } + glEnable(GL_DEPTH_TEST); + unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); @@ -103,25 +141,46 @@ int main() glBindVertexArray(0); unsigned int shaderProgram = shaderCreateProgram("shaders/vertex.vsh", - "shaders/fragment.vsh"); + "shaders/fragment.fsh"); - unsigned int transformLoc; - mat4 transform; + unsigned int modelLoc, viewLoc, projLoc; + mat4 model, view, proj; + mat4 T, S, R; + float t; while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(0.2f, 0.2f, 0.2f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + t = (float)glfwGetTime(); + T = linearTranslate(0.0, 0.0, 0.0); + R = linearRotate(180 * t / M_PI, 0.0, 1.0, 0.0); + S = linearScale(1.0, 1.0, 1.0); + model = linearMat4Muln(3, T, R, S); + + T = linearTranslate(0.0, -0.0, -80); + R = linearRotate(60, 1, 0, 0); + + view = linearMat4Muln(2, T, R); + + proj = linearPerspective(35, 4 / 3.0, 0.1, 100); glUseProgram(shaderProgram); glBindVertexArray(VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - transformLoc = glGetUniformLocation(shaderProgram, "trans"); - transform = linearRotate(45.0, linearVec3(0, 0, 1)); - glUniformMatrix4fv(transformLoc, 1, GL_FALSE, transform.matrix[0]); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + modelLoc = glGetUniformLocation(shaderProgram, "model"); + viewLoc = glGetUniformLocation(shaderProgram, "view"); + projLoc = glGetUniformLocation(shaderProgram, "proj"); + + glUniformMatrix4fv(modelLoc, 1, GL_TRUE, model.matrix[0]); + glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(int), GL_UNSIGNED_INT, 0); + glUniformMatrix4fv(viewLoc, 1, GL_TRUE, view.matrix[0]); + glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(int), GL_UNSIGNED_INT, 0); + glUniformMatrix4fv(projLoc, 1, GL_TRUE, proj.matrix[0]); + glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(int), GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/main.h b/src/main.h index e4fc1ee..d93f941 100644 --- a/src/main.h +++ b/src/main.h @@ -1,4 +1,6 @@ -struct Background { - float R, G, B, A; -}; +typedef struct { + float pos[3]; + float color[3]; +} vertex; +enum AXIS {X_AXIS, Y_AXIS, Z_AXIS}; diff --git a/src/shader.c b/src/shader.c index e21277f..700628f 100644 --- a/src/shader.c +++ b/src/shader.c @@ -1,6 +1,5 @@ #include #include -#include #include #include "shader.h" -- cgit v1.2.3-70-g09d2