getting uniform locations once, at the renderer creation, not everytime
[demo] / src / renderer.cc
1 #include <GL/glew.h>
2
3 #include "global.h"
4
5 #include "camera.h"
6 #include "mesh.h"
7 #include "object.h"
8 #include "renderer.h"
9 #include "scene.h"
10 #include "shader.h"
11 #include "shader_manager.h"
12 #include "texture.h"
13
14 Renderer::Renderer()
15 {
16         scene = 0;
17         camera = 0;
18         sprog = 0;
19 }
20
21 Renderer::~Renderer()
22 {
23 }
24
25 bool Renderer::create()
26 {
27         if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
28                 return false;
29         }
30
31         if(!(sprog->link())) {
32                 return false;
33         }
34
35         /* getting material uniform locations: diffuse, specular, specular exponent (strength) */
36
37         if((diff_loc = sprog->get_uniform_location("diffuse")) == -1) {
38                 fprintf(stderr, "Invalid uniform location: can't find \"diffuse\".\n");
39                 return false;
40         }
41
42         if((spec_loc = sprog->get_uniform_location("specular")) == -1) {
43                 fprintf(stderr, "Invalid uniform location: can't find \"specular\".\n");
44                 return false;
45         }
46
47         if((shin_loc = sprog->get_uniform_location("shininess")) == -1) {
48                 fprintf(stderr, "Invalid uniform location: can't find \"shininess\".\n");
49                 return false;
50         }
51
52         /* uniform locations for matrices */
53
54         if((mmviewproj_loc = sprog->get_uniform_location("mmviewproj")) == -1) {
55                 fprintf(stderr, "Invalid uniform location: can't find \"mmviewproj\".\n");
56                 return false;
57         }
58
59         if((mview_loc = sprog->get_uniform_location("mview")) == -1) {
60                 fprintf(stderr, "Invalid uniform location: can't find \"mview\".\n");
61                 return false;
62         }
63
64         return true;
65 }
66
67 void Renderer::draw() const
68 {
69         if(!camera || !scene)
70                 return;
71
72         glClearColor(0.5, 0.5, 0.5, 1);
73         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75         if(!sprog->link())
76                 return;
77
78         sprog->use();
79
80         for(size_t i=0; i<scene->objects.size(); i++) {
81                 draw_object(scene->objects[i]);
82         }
83 }
84
85 void Renderer::draw_object(Object *object) const
86 {
87         Material *m = object->material;
88
89         /* setting uniforms for material */
90
91         sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
92         sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
93         sprog->set_uniformf(shin_loc, m->shininess);
94
95         /* texture */
96
97         if (m->dtex)
98                 m->dtex->bind();
99
100         /* setting uniforms for matrices */
101
102         Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
103         sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
104
105         Mat4 mview = object->transform * camera->get_view_matrix();
106         sprog->set_uniform_matrix(mview_loc, mview);
107
108         object->mesh->update_vertex_data();
109         object->mesh->draw();
110 }