aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/renderer/renderer.c
blob: 5b8638cfe4b958e019c355bb2eced34341739634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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()
{

}