From e990d28a1ff7cfd258ea167bca7d1a295f5cf17f Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 12 Jul 2021 10:22:33 -0700 Subject: [PATCH] Added matrix --- include/dawn/dawn.h | 1 + include/dawn/display/camera.h | 5 +- include/dawn/display/matrix.h | 18 ++++++ include/dawn/display/shader.h | 17 ++--- src/display/camera.c | 26 +++----- src/display/camera.h | 1 + src/display/matrix.c | 62 ++++++++++++++++++ src/display/matrix.h | 114 ++++++++++++++++++++++++++++++++++ src/display/shader.c | 58 +++++++---------- src/display/shader.h | 10 +++ src/util/array.c | 2 + 11 files changed, 250 insertions(+), 64 deletions(-) create mode 100644 include/dawn/display/matrix.h create mode 100644 src/display/matrix.c create mode 100644 src/display/matrix.h diff --git a/include/dawn/dawn.h b/include/dawn/dawn.h index 22c7967f..3b6a2224 100644 --- a/include/dawn/dawn.h +++ b/include/dawn/dawn.h @@ -14,6 +14,7 @@ #include "display/camera.h" #include "display/framebuffer.h" +#include "display/matrix.h" #include "display/primitive.h" #include "display/render.h" #include "display/shader.h" diff --git a/include/dawn/display/camera.h b/include/dawn/display/camera.h index 18e489b8..18a80ae8 100644 --- a/include/dawn/display/camera.h +++ b/include/dawn/display/camera.h @@ -7,12 +7,13 @@ #pragma once #include "../libs.h" +#include "matrix.h" /** The math for the camera is stored here. */ typedef struct { /** View Matrix (Where the camera looks) */ - mat4 view; + matrix_t view; /** Projection Matrix (How the camera looks) */ - mat4 projection; + matrix_t projection; } camera_t; \ No newline at end of file diff --git a/include/dawn/display/matrix.h b/include/dawn/display/matrix.h new file mode 100644 index 00000000..8d6c25c2 --- /dev/null +++ b/include/dawn/display/matrix.h @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../libs.h" + +/** + * Representation for a matrix. Used as a highlevel wrapper for the math + * functions that sit underneath this API. + */ +typedef struct { + /** Internal Matrix API */ + mat4 internalMatrix; +} matrix_t; \ No newline at end of file diff --git a/include/dawn/display/shader.h b/include/dawn/display/shader.h index 35759669..85671495 100644 --- a/include/dawn/display/shader.h +++ b/include/dawn/display/shader.h @@ -13,29 +13,32 @@ #define SHADER_UNI_TEXT "u_Text" #define SHADER_UNI_MODL "u_Modl" +/** Representation of a shader uniform */ +typedef GLuint shaderuniform_t; + /** * Structure containing information about an OpenGL Shader. For simplicity sake * we demand certain uninforms to be present on the shader target. */ typedef struct { /** Pointer to an uploaded vertex shader program */ - GLuint shaderVertex; + shaderuniform_t shaderVertex; /** Pointer to an uploaded fragment shader program */ - GLuint shaderFrag; + shaderuniform_t shaderFrag; /** Pointer to an uploaded shader program linked */ - GLuint shaderProgram; + shaderuniform_t shaderProgram; /** Matrix for the view matrix */ - GLint uniView; + shaderuniform_t uniView; /** Matrix for the projection matrix */ - GLint uniProj; + shaderuniform_t uniProj; /** Uniform for the current texture */ - GLint uniText; + shaderuniform_t uniText; /** Uniform for the current model world position */ - GLint uniModl; + shaderuniform_t uniModl; } shader_t; \ No newline at end of file diff --git a/src/display/camera.c b/src/display/camera.c index 7117e033..e6d86736 100644 --- a/src/display/camera.c +++ b/src/display/camera.c @@ -11,38 +11,28 @@ void cameraLookAt(camera_t *camera, float x, float y, float z, float targetX, float targetY, float targetZ ) { - glm_mat4_identity(camera->view); - glm_lookat( - (vec3){ x, y, z }, - (vec3){ targetX, targetY, targetZ }, - (vec3){ 0,1,0 }, - camera->view - ); + matrixIdentity(&camera->view); + matrixLookAt(&camera->view, x, y, z, targetX, targetY, targetZ, 0, 1, 0); } void cameraLook(camera_t *camera, float x, float y, float z, float pitch, float yaw, float roll ) { - glm_mat4_identity(camera->view); - glm_look( - (vec3){ x, y, z }, - (vec3){ pitch, yaw, roll }, - (vec3){ 0,1,0 }, - camera->view - ); + matrixIdentity(&camera->view); + matrixLook(&camera->view, x, y, z, pitch, yaw, roll, 0, 1, 0); } void cameraPerspective(camera_t *camera, float fov, float aspect, float near, float far ) { - glm_mat4_identity(camera->projection); - glm_perspective(mathDeg2Rad(fov), aspect, near, far, camera->projection); + matrixIdentity(&camera->projection); + matrixPerspective(&camera->projection, mathDeg2Rad(fov), aspect, near, far); } void cameraOrtho(camera_t *camera, float left, float right, float bottom, float top, float near, float far ) { - glm_mat4_identity(camera->projection); - glm_ortho(left, right, bottom, top, near, far, camera->projection); + matrixIdentity(&camera->projection); + matrixOrtho(&camera->projection, left, right, bottom, top, near, far); } \ No newline at end of file diff --git a/src/display/camera.h b/src/display/camera.h index 120303bc..7e248927 100644 --- a/src/display/camera.h +++ b/src/display/camera.h @@ -5,6 +5,7 @@ #pragma once #include +#include "matrix.h" /** * Make a camera look at a position in world space while itself being positioned diff --git a/src/display/matrix.c b/src/display/matrix.c new file mode 100644 index 00000000..8e5b1a2a --- /dev/null +++ b/src/display/matrix.c @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "matrix.h" + +void matrixIdentity(matrix_t *matrix) { + glm_mat4_identity(matrix->internalMatrix); +} + +void matrixLookAt(matrix_t *matrix, + float x,float y,float z, + float tx,float ty, float tz, + float ux, float uy, float uz +) { + glm_lookat( + (vec3){ x, y, z }, + (vec3){ tx, ty, tz }, + (vec3){ ux, uy, uz }, + matrix->internalMatrix + ); +} + +void matrixLook(matrix_t *matrix, + float x, float y, float z, + float pitch, float yaw, float roll, + float ux, float uy, float uz +) { + glm_look( + (vec3){ x, y, z }, + (vec3){ pitch, yaw, roll }, + (vec3){ ux, uy, uz }, + matrix->internalMatrix + ); +} + +void matrixPerspective(matrix_t *matrix, + float fov,float aspect,float near,float far +) { + glm_perspective(fov, aspect, near, far, matrix->internalMatrix); +} + +void matrixOrtho(matrix_t *matrix, + float left, float right, float bottom, float top, float near, float far +) { + glm_ortho(left, right, bottom, top, near, far, matrix->internalMatrix); +} + +void matrixTranslate(matrix_t *matrix, float x, float y, float z) { + glm_translate(matrix->internalMatrix, (vec3){ x, y, z }); +} + +void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z) { + glm_rotate(matrix->internalMatrix, angle, (vec3){ x, y, z }); +} + +void matrixScale(matrix_t *matrix, float x, float y, float z) { + glm_scale(MATRIX_POSITION, (vec3){ x, y, z }); +} \ No newline at end of file diff --git a/src/display/matrix.h b/src/display/matrix.h new file mode 100644 index 00000000..775b6535 --- /dev/null +++ b/src/display/matrix.h @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include + +/** + * Makes matrix identity. + * @param matrix Matrix to identify. + */ +void matrixIdentity(matrix_t *matrix); + +/** + * Applies a look at point vector to a matrix. + * + * @param matrix Matrix to apply to. + * @param x Camera source X. + * @param y Camera source Y. + * @param z Camera source Z. + * @param tx Camera target X. + * @param ty Camera target Y. + * @param tz Camera target Z. + * @param ux Camera up vector X. + * @param uy Camera up vector Y. + * @param uz Camera up vector Z. + */ +void matrixLookAt(matrix_t *matrix, + float x,float y,float z, + float tx,float ty, float tz, + float ux, float uy, float uz +); + +/** + * Applies a look vector to a matrix. + * + * @param matrix Matrix to apply to. + * @param x Camera source X. + * @param y Camera source Y. + * @param z Camera source Z. + * @param pitch Camera pitch + * @param yaw Camera yaw. + * @param roll Camera roll. + * @param ux Camera up vector X. + * @param uy Camera up vector Y. + * @param uz Camera up vector Z. + */ +void matrixLook(matrix_t *matrix, + float x, float y, float z, + float pitch, float yaw, float roll, + float ux, float uy, float uz +); + +/** + * Applies a perspective projection to a matrix. + * + * @param matrix Matrix to apply to. + * @param fov Field of View (in radians) to use. + * @param aspect Aspect ratio (w/h) of the viewport. + * @param near Near vector, > 0. + * @param far Far view vector. + */ +void matrixPerspective(matrix_t *matrix, + float fov,float aspect,float near,float far +); + +/** + * Applies an orthogonal projection to a matrix. + * + * @param matrix Matrix to apply to. + * @param left Left view position. + * @param right Right view position. + * @param bottom Bottom view position. + * @param top Top view position. + * @param near Near vector, > 0. + * @param far Far view vector. + */ +void matrixOrtho(matrix_t *matrix, + float left, float right, float bottom, float top, float near, float far +); + +/** + * Performs a matrix translation. + * + * @param matrix Matrix to translate. + * @param x X coordinate to translate on. + * @param y Y coordinate to translate on. + * @param z Z coordinate to translate on. + */ +void matrixTranslate(matrix_t *matrix, float x, float y, float z); + +/** + * Applies a rotation vector to a matrix. + * + * @param matrix Matrix to rotate. + * @param angle Angle (in radians) to rotate. + * @param x X vector to rotate on. + * @param y Y vector to rotate on. + * @param z Z vector to rotate on. + */ +void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z); + +/** + * Scales a matrix. + * + * @param matrix Matrix to scale + * @param x X vector to scale. + * @param y Y vector to scale. + * @param z Z vector to scale. + */ +void matrixScale(matrix_t *matrix, float x, float y, float z); \ No newline at end of file diff --git a/src/display/shader.c b/src/display/shader.c index d7549b92..2b157a6e 100644 --- a/src/display/shader.c +++ b/src/display/shader.c @@ -92,8 +92,8 @@ void shaderUse(shader_t *shader) { } void shaderUseCamera(shader_t *shader, camera_t *camera) { - glUniformMatrix4fv(shader->uniView, 1, GL_FALSE, camera->view[0]); - glUniformMatrix4fv(shader->uniProj, 1, GL_FALSE, camera->projection[0]); + shaderUseMatrix(shader, shader->uniView, &camera->view); + shaderUseMatrix(shader, shader->uniProj, &camera->projection); } void shaderUseTexture(shader_t *shader, texture_t *texture) { @@ -103,30 +103,24 @@ void shaderUseTexture(shader_t *shader, texture_t *texture) { glUniform1i(shader->uniText, 0); } +void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix){ + glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]); +} + void shaderUsePosition(shader_t *shader, float x, float y, float z, float pitch, float yaw, float roll ) { - mat4 MATRIX_POSITION; - vec3 axis; + matrix_t matrix; - // Identify mat. - glm_mat4_identity(MATRIX_POSITION); - - //Position - axis[0] = x, axis[1] = y, axis[2] = z; - glm_translate_make(MATRIX_POSITION, axis); + matrixIdentity(&matrix); + matrixTranslate(&matrix, x, y, z); // Rotation (YZX order) - axis[0] = 0, axis[1] = 1, axis[2] = 0; - glm_rotate(MATRIX_POSITION, yaw, axis); - axis[1] = 0, axis[2] = 1; - glm_rotate(MATRIX_POSITION, roll, axis); - axis[0] = 1, axis[2] = 0; - glm_rotate(MATRIX_POSITION, pitch, axis); - - //Send to the shader. - glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, MATRIX_POSITION[0]); + matrixRotate(&matrix, yaw, 0, 1, 0); + matrixRotate(&matrix, roll, 0, 0, 1); + matrixRotate(&matrix, pitch, 1, 0, 0); + shaderUseMatrix(shader, shader->uniModl, &matrix); } void shaderUsePositionAndScale(shader_t *shader, @@ -134,27 +128,17 @@ void shaderUsePositionAndScale(shader_t *shader, float pitch, float yaw, float roll, float scaleX, float scaleY, float scaleZ ) { - mat4 MATRIX_POSITION; - vec3 axis; + matrix_t matrix; - // Identify mat. - glm_mat4_identity(MATRIX_POSITION); - - //Position - axis[0] = x, axis[1] = y, axis[2] = z; - glm_translate_make(MATRIX_POSITION, axis); + matrixIdentity(&matrix); + matrixTranslate(&matrix, x, y, z); // Rotation (YZX order) - axis[0] = 0, axis[1] = 1, axis[2] = 0; - glm_rotate(MATRIX_POSITION, yaw, axis); - axis[1] = 0, axis[2] = 1; - glm_rotate(MATRIX_POSITION, roll, axis); - axis[0] = 1, axis[2] = 0; - glm_rotate(MATRIX_POSITION, pitch, axis); + matrixRotate(&matrix, yaw, 0, 1, 0); + matrixRotate(&matrix, roll, 0, 0, 1); + matrixRotate(&matrix, pitch, 1, 0, 0); - // Scale - axis[0] = scaleX, axis[1] = scaleY, axis[2] = scaleZ; - glm_scale(MATRIX_POSITION, axis); + matrixScale(&matrix, scaleX, scaleY, scaleZ); - glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, MATRIX_POSITION[0]); + shaderUseMatrix(shader, shader->uniModl, &matrix); } \ No newline at end of file diff --git a/src/display/shader.h b/src/display/shader.h index aa392534..25eadef5 100644 --- a/src/display/shader.h +++ b/src/display/shader.h @@ -7,6 +7,7 @@ #pragma once #include +#include "matrix.h" /** * Compiles a shader from vertex and fragment shader code. @@ -45,6 +46,15 @@ void shaderUseCamera(shader_t *shader, camera_t *camera); */ void shaderUseTexture(shader_t *shader, texture_t *texture); +/** + * Set's a specific shader uniform to a matrix. + * + * @param shader Shader to apply to. + * @param uniform Uniform on the shader to set. + * @param matrix Matrix to apply. + */ +void shaderUseMatrix(shader_t *shader, shaderuniform_t uniform,matrix_t *matrix); + /** * Set's the current translation matrix onto the shader for the next * render to use. Rotation order is set to YZX. diff --git a/src/util/array.c b/src/util/array.c index 9c8546cb..4470f3cb 100644 --- a/src/util/array.c +++ b/src/util/array.c @@ -64,6 +64,8 @@ void arraySort(size_t size, void *array, int32_t length, arraysort_t *sort) { qsort(array, length, size, (_CoreCrtNonSecureSearchSortCompareFunction)sort); } +// Common Sorters: + void arraySortInt32(int32_t *array, int32_t length) { arraySort(sizeof(int32_t), array, length, &_arraySorterInt32); }