#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() { }