From 9467d044337e43eba950cca0ecc82ca3480b1605 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 3 Apr 2021 22:59:43 +1100 Subject: [PATCH] Added model positioning and rotation. --- assets/bruh.png | Bin 209 -> 145 bytes assets/shaders/test.frag | 4 ++-- assets/shaders/test.vert | 3 ++- src/engine/display/shader.c | 31 +++++++++++++++++++++++++++++++ src/engine/display/shader.h | 11 ++++++++++- src/engine/engine.c | 11 ++++++++++- 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/assets/bruh.png b/assets/bruh.png index 131ebded2f86c3c7d60dcb77912b326f77eabd11..716ab8cdaa266255e8c56502b296cc986b2ef35c 100644 GIT binary patch delta 104 zcmcb}IFV7YGr-TCmrII^fq{Y7)59eQNHc>l2NRII>812yqN2jYU{Px|PZ!4!iSXov zfZy@K!9V}AF*6^Y@}K9wsJY>v^grhpus<;}FkoQV&cVfSAb!SLpk@Y7S3j3^P6x3`sveQBi@PEx;$l^*<0WFuaa> zK6#>@L}rMmi(`n!`Lh!o`4|*=m`?LQ_&@*dJ1ODr-geuQYnSlcdf_sg^%v6_foz}5 zl%8xBZK>G{d3cl}{ diff --git a/assets/shaders/test.frag b/assets/shaders/test.frag index dc3182c1..d7100dec 100644 --- a/assets/shaders/test.frag +++ b/assets/shaders/test.frag @@ -8,6 +8,6 @@ out vec4 FragColor; void main() { vec4 color = texture(u_Text, TexCoord); - // FragColor = color; - FragColor = vec4(1, 1, 1, 1); + FragColor = color; + // FragColor = vec4(1, 1, 1, 1); } \ No newline at end of file diff --git a/assets/shaders/test.vert b/assets/shaders/test.vert index c0b76c18..3c4e9a28 100644 --- a/assets/shaders/test.vert +++ b/assets/shaders/test.vert @@ -6,10 +6,11 @@ layout (location = 1) in vec2 aTexCoord; uniform mat4 u_Proj; uniform mat4 u_View; +uniform mat4 u_Modl; out vec2 TexCoord; void main() { - gl_Position = u_Proj * u_View * gl_Vertex; + gl_Position = u_Proj * u_View * u_Modl * gl_Vertex; TexCoord = vec2(aTexCoord.x, aTexCoord.y); } \ No newline at end of file diff --git a/src/engine/display/shader.c b/src/engine/display/shader.c index b43db529..23a0cb9d 100644 --- a/src/engine/display/shader.c +++ b/src/engine/display/shader.c @@ -78,10 +78,14 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) { shader->uniProj = glGetUniformLocation(shader->shaderProgram, SHADER_UNI_PROJ); shader->uniView = glGetUniformLocation(shader->shaderProgram, SHADER_UNI_VIEW); shader->uniText = glGetUniformLocation(shader->shaderProgram, SHADER_UNI_TEXT); + shader->uniModl = glGetUniformLocation(shader->shaderProgram, SHADER_UNI_MODL); // Bind the shader shaderUse(shader); + // Reset position + shaderUsePosition(shader, 0, 0, 0, 0, 0, 0); + // Fetch the uniforms. return shader; } @@ -102,9 +106,36 @@ void shaderUseCamera(shader_t *shader, camera_t *camera) { glUniformMatrix4fv(shader->uniView, 1, GL_FALSE, (float*)camera->view); glUniformMatrix4fv(shader->uniProj, 1, GL_FALSE, (float*)camera->projection); } + void shaderUseTexture(shader_t *shader, texture_t *texture) { // OpenGL requires us to set the active texure. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture->id); glUniform1i(shader->uniText, 0); +} + +void shaderUsePosition(shader_t *shader, + float x, float y, float z, + float pitch, float yaw, float roll +) { + mat4 MATRIX_POSITION; + vec3 axis; + + // Identify mat. + glm_mat4_identity(MATRIX_POSITION); + + //Position + axis[0] = x, axis[1] = y, axis[2] = z; + glm_translate(MATRIX_POSITION, axis); + + //Rotation, we do each axis individually + axis[0] = 1, axis[1] = 0, axis[2] = 0; + glm_rotate(MATRIX_POSITION, pitch, axis); + axis[0] = 0, axis[1] = 1; + glm_rotate(MATRIX_POSITION, yaw, axis); + axis[1] = 0, axis[2] = 1; + glm_rotate(MATRIX_POSITION, roll, axis); + + //Send to the shader. + glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, (float*)MATRIX_POSITION); } \ No newline at end of file diff --git a/src/engine/display/shader.h b/src/engine/display/shader.h index 427ead4c..a7644fbf 100644 --- a/src/engine/display/shader.h +++ b/src/engine/display/shader.h @@ -17,6 +17,7 @@ #define SHADER_UNI_VIEW "u_View" #define SHADER_UNI_PROJ "u_Proj" #define SHADER_UNI_TEXT "u_Text" +#define SHADER_UNI_MODL "u_Modl" /** * Structure containing information about an OpenGL Shader. For simplicity sake @@ -40,6 +41,9 @@ typedef struct { /** Uniform for the current texture */ GLint uniText; + + /** Uniform for the current model world position */ + GLint uniModl; } shader_t; /** @@ -78,4 +82,9 @@ void shaderUseCamera(shader_t *shader, camera_t *camera); * @param shader Shader to attach to. * @param texture Texture to attach. */ -void shaderUseTexture(shader_t *shader, texture_t *texture); \ No newline at end of file +void shaderUseTexture(shader_t *shader, texture_t *texture); + +void shaderUsePosition(shader_t *shader, + float x, float y, float z, + float pitch, float yaw, float roll +); \ No newline at end of file diff --git a/src/engine/engine.c b/src/engine/engine.c index 2540b6db..ffa1e9f1 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -10,6 +10,9 @@ camera_t *camera; shader_t *shader; primitive_t *cube; +texture_t *texture; + +float bruh; engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { // Create the engine instance. @@ -43,9 +46,13 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 0.5f, 100.0f); shaderUseCamera(shader, camera); + texture = assetTextureLoad("bruh.png"); + shaderUseTexture(shader, texture); + // cube = quadCreate(0, 0, 0, 0, 1, 1, 1, 1); cube = cubeCreate(1, 1, 1); - + bruh = 0; + return engine; } @@ -53,6 +60,8 @@ uint32_t engineUpdate(engine_t *engine) { shaderUse(shader); renderFrame(engine->render); + shaderUsePosition(shader, 0, 0, 0, bruh, bruh / 2.0f, bruh / 3.0f); + bruh += 0.0001f; primitiveDraw(cube, 0, cube->indiceCount); inputUpdate(engine->input);