diff options
author | jvech <jmvalenciae@unal.edu.co> | 2022-08-29 09:12:18 -0500 |
---|---|---|
committer | jvech <jmvalenciae@unal.edu.co> | 2022-08-29 09:12:18 -0500 |
commit | d7bd9a3dbd3486a4b0615dc9f30a890ff6d04ec6 (patch) | |
tree | 4d4e2a4770fae453a6ac541931216d5e353102b9 | |
parent | 3b24a477cf566296aa758058aaca96cf0c117141 (diff) |
feat: 3D support added
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | TODO | 9 | ||||
-rw-r--r-- | shaders/fragment.fsh (renamed from shaders/fragment.vsh) | 2 | ||||
-rw-r--r-- | shaders/vertex.vsh | 4 | ||||
-rw-r--r-- | src/linear.c | 50 | ||||
-rw-r--r-- | src/linear.h | 10 | ||||
-rw-r--r-- | src/main.c | 87 | ||||
-rw-r--r-- | src/main.h | 8 | ||||
-rw-r--r-- | src/shader.c | 1 |
9 files changed, 131 insertions, 42 deletions
@@ -1,5 +1,5 @@ CC := cc -CFLAGS := -Wall -Wall -pedantic -std=c11 +CFLAGS := -Wall -pedantic -pedantic-errors -std=c11 DLIBS := -lm $(shell pkg-config --libs glfw3 opengl glew) OBJDIR = objs SRCDIR = src @@ -7,13 +7,10 @@ General - define a format for 3D objects - Render a set of vertices loaded from a file -Learning -* Make a Hello Window -* Make a Hello Triangle -* Make a Shader Loader Library +- Implement Camera movement Linear - Implement the Inverse function of mat4 - Implement the determinant function of mat4 - -Shader +* Check 3D perspective transformation +- Implement Orhographic view transformation diff --git a/shaders/fragment.vsh b/shaders/fragment.fsh index 38bc00d..2d87daa 100644 --- a/shaders/fragment.vsh +++ b/shaders/fragment.fsh @@ -5,5 +5,5 @@ out vec4 FragColor; void main() { - FragColor = vec4(vertexColor, 1.0); + FragColor = vec4(vertexColor, 0.0f); } diff --git a/shaders/vertex.vsh b/shaders/vertex.vsh index 220041d..987dc3b 100644 --- a/shaders/vertex.vsh +++ b/shaders/vertex.vsh @@ -2,12 +2,12 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; -uniform mat4 trans; +uniform mat4 model, view, proj; out vec3 vertexColor; void main() { - gl_Position = trans * vec4(aPos, 1.0f); + gl_Position = proj * view * model * vec4(aPos, 1.0f); vertexColor = aColor; } 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 <stdio.h> +#include <stdlib.h> +#include <stdarg.h> #include <math.h> 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; @@ -103,6 +106,12 @@ linearRotate(float degree, vec3 R_xyz) } mat4 +linearRotate(float degree, float Rx, float Ry, float Rz) +{ + return linearRotatev(degree, linearVec3(Rx, Ry, Rz)); +} + +mat4 linearMat4Fill(float value) { int i, j; @@ -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; } } @@ -158,6 +167,27 @@ linearMat4Mul(mat4 x1, mat4 x2) } 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) { int i, j; 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 @@ -3,7 +3,9 @@ #include <errno.h> #include <string.h> #include <signal.h> +#include <stdarg.h> +#include <math.h> #include <GL/glew.h> #include <GLFW/glfw3.h> @@ -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(); @@ -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 <stdio.h> #include <stdlib.h> -#include <stdarg.h> #include <GL/glew.h> #include "shader.h" |