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/main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 14 deletions(-) (limited to 'src/main.c') 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(); -- cgit v1.2.3-70-g09d2