#include "renderer.h"
#include <string.h>
static const int MAX_LIGHTS = 4;
void Render_Init()
{
}
void Render_LoadLights(Shader_Layout *layout, light_t *lights, int n)
{
vec3_t light_positions[MAX_LIGHTS];
color_t light_colors[MAX_LIGHTS];
vec3_t attenuation[MAX_LIGHTS];
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);
Texture_Bind(entity->texture);
glDrawElements(GL_TRIANGLES, entity->shape->num_indices, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
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);
glUniform1i(layout->Texture_Background, 0);
glUniform1i(layout->Texture_R, 1);
glUniform1i(layout->Texture_G, 2);
glUniform1i(layout->Texture_B, 3);
glUniform1i(layout->Texture_BlendMap, 4);
glActiveTexture(GL_TEXTURE0);
Texture_Bind(terrain->textures.texture[0]);
glActiveTexture(GL_TEXTURE1);
Texture_Bind(terrain->textures.texture[1]);
glActiveTexture(GL_TEXTURE2);
Texture_Bind(terrain->textures.texture[2]);
glActiveTexture(GL_TEXTURE3);
Texture_Bind(terrain->textures.texture[3]);
glActiveTexture(GL_TEXTURE4);
Texture_Bind(terrain->blendmap);
glDrawElements(GL_TRIANGLES, terrain->shape->num_indices, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
void Render_Quit()
{
}