From 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 Mon Sep 17 00:00:00 2001 From: Thomas Guillermo Albers Raviola Date: Fri, 16 Jan 2026 23:02:32 +0100 Subject: Initial commit --- 08-august/src/renderer/renderer.c | 147 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 08-august/src/renderer/renderer.c (limited to '08-august/src/renderer/renderer.c') diff --git a/08-august/src/renderer/renderer.c b/08-august/src/renderer/renderer.c new file mode 100644 index 0000000..2ea4426 --- /dev/null +++ b/08-august/src/renderer/renderer.c @@ -0,0 +1,147 @@ +#include "renderer.h" +#include "../util/util_time.h" + +#include + +#define MAX_LIGHTS 4 + +void Render_Init() +{ + +} + +void Render_LoadLights(Shader_Layout *layout, const light_t *lights, int n) +{ + vec3_t light_positions[MAX_LIGHTS]; + vec4_t light_colors[MAX_LIGHTS]; + vec3_t attenuation[MAX_LIGHTS]; + + /* Default light in case we are not given enough lights (n < 4) */ + const light_t defaultLight = { {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} }; + + int i; + for(i = 0; i < MAX_LIGHTS; i++) + { + if(i < MAX_LIGHTS) + { + light_positions[i] = lights[i].position; + light_colors[i] = lights[i].color; + attenuation[i] = lights[i].attenuation; + } + else + { + light_positions[i] = defaultLight.position; + light_colors[i] = defaultLight.color; + attenuation[i] = defaultLight.attenuation; + } + + } + + glUniform3fv(layout->lightPosition, MAX_LIGHTS, (float*)light_positions); + glUniform4fv(layout->lightColor, MAX_LIGHTS, (float*)light_colors); + glUniform3fv(layout->lightAttenuation, MAX_LIGHTS, (float*)attenuation); + +} + +void Render_DrawEntity(Shader_Layout *layout, mat4_t *projectedViewMatrix, entity_t *entity) +{ + glBindVertexArray(entity->shape->vao); + + /*We need the model to world matrix in our shader in order to rotate the normals*/ + mat4_t modelTransform = Entity_GetModelTransform(entity); + glUniformMatrix4fv(layout->modelToWorld, 1, GL_FALSE, modelTransform.data); + + mat4_t totalMatrix = mat4_mul(projectedViewMatrix, &modelTransform); + glUniformMatrix4fv(layout->totalTransform, 1, GL_FALSE, totalMatrix.data); + + glActiveTexture(GL_TEXTURE0); + glUniform1i(layout->Texture, 0); + glBindTexture(GL_TEXTURE_2D, entity->texture); + + glDrawElements(GL_TRIANGLES, entity->shape->num_indices, GL_UNSIGNED_SHORT, NULL); + glBindVertexArray(0); +} + +/****************************************************************************** +* * +* Function Name: Render_DrawTerrain * +* * +* Specific shader layout * +* -> extra0 Texture_Background * +* -> extra1 Texture_R * +* -> extra2 Texture_G * +* -> extra3 Texture_B * +* -> extra4 Texture_BlendMap * +* * +*******************************************************************************/ + +void Render_DrawTerrain(Shader_Layout *layout, mat4_t *projectedViewMatrix, terrain_t *terrain) +{ + glBindVertexArray(terrain->shape->vao); + + /* We need the model to world matrix in our shader in order to rotate the normals */ + mat4_t modelTransform = mat4_translate(&terrain->position); + glUniformMatrix4fv(layout->modelToWorld, 1, GL_FALSE, modelTransform.data); + + mat4_t totalMatrix = mat4_mul(projectedViewMatrix, &modelTransform); + glUniformMatrix4fv(layout->totalTransform, 1, GL_FALSE, totalMatrix.data); + + /** Set textures for the terrain **/ + + glUniform1i(layout->extra0, 0); + glUniform1i(layout->extra1, 1); + glUniform1i(layout->extra2, 2); + glUniform1i(layout->extra3, 3); + glUniform1i(layout->extra4, 4); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, terrain->textures.texture[0]); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, terrain->textures.texture[1]); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, terrain->textures.texture[2]); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, terrain->textures.texture[3]); + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_2D, terrain->blendmap); + + /************************************************************/ + + glDrawElements(GL_TRIANGLES, terrain->shape->num_indices, GL_UNSIGNED_SHORT, NULL); + + glBindVertexArray(0); +} + +void Render_DrawSky(Shader_Layout *layout, mat4_t *viewMatrix, mat4_t *projectionMatrix, skybox_t *sky) +{ + glBindVertexArray(sky->cube->vao); + + mat4_t myViewMatrix = *viewMatrix; + sky->rotation += SKYBOX_ROTATION_SPEED * Time_GetFrameTime(); + mat4_t rotateMatrix = mat4_rotate_y(sky->rotation); + + /* We don't want to move the skybox around (We want it to stay with the camera in the midle), + just rotate it with the camera so we remove the translations components of the matrix */ + + myViewMatrix.data[0 + 3 * 4] = 0.0f; + myViewMatrix.data[1 + 3 * 4] = 0.0f; + myViewMatrix.data[2 + 3 * 4] = 0.0f; + + myViewMatrix = mat4_mul(&myViewMatrix, &rotateMatrix); + mat4_t totalTransform = mat4_mul(projectionMatrix, &myViewMatrix); + + glUniformMatrix4fv(layout->totalTransform, 1, GL_FALSE, totalTransform.data ); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_CUBE_MAP, sky->texture); + glUniform1i(layout->Texture, 0); + + glDrawArrays(GL_TRIANGLES, 0, 36); + + glBindVertexArray(0); +} + +void Render_Quit() +{ + +} -- cgit v1.2.3