getting uniform locations once, at the renderer creation, not everytime
[demo] / src / renderer.cc
index 353d0db..a223fa7 100644 (file)
@@ -1,11 +1,15 @@
+#include <GL/glew.h>
+
 #include "global.h"
 
 #include "camera.h"
+#include "mesh.h"
 #include "object.h"
 #include "renderer.h"
 #include "scene.h"
 #include "shader.h"
 #include "shader_manager.h"
+#include "texture.h"
 
 Renderer::Renderer()
 {
@@ -19,10 +23,44 @@ Renderer::~Renderer()
 }
 
 bool Renderer::create()
-{      
+{
        if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
                return false;
        }
+
+       if(!(sprog->link())) {
+               return false;
+       }
+
+       /* getting material uniform locations: diffuse, specular, specular exponent (strength) */
+
+       if((diff_loc = sprog->get_uniform_location("diffuse")) == -1) {
+               fprintf(stderr, "Invalid uniform location: can't find \"diffuse\".\n");
+               return false;
+       }
+
+       if((spec_loc = sprog->get_uniform_location("specular")) == -1) {
+               fprintf(stderr, "Invalid uniform location: can't find \"specular\".\n");
+               return false;
+       }
+
+       if((shin_loc = sprog->get_uniform_location("shininess")) == -1) {
+               fprintf(stderr, "Invalid uniform location: can't find \"shininess\".\n");
+               return false;
+       }
+
+       /* uniform locations for matrices */
+
+       if((mmviewproj_loc = sprog->get_uniform_location("mmviewproj")) == -1) {
+               fprintf(stderr, "Invalid uniform location: can't find \"mmviewproj\".\n");
+               return false;
+       }
+
+       if((mview_loc = sprog->get_uniform_location("mview")) == -1) {
+               fprintf(stderr, "Invalid uniform location: can't find \"mview\".\n");
+               return false;
+       }
+
        return true;
 }
 
@@ -31,9 +69,42 @@ void Renderer::draw() const
        if(!camera || !scene)
                return;
 
-       // sprog->set_uniform_matrix(mview_loc, camera->get_view_matrix());
+       glClearColor(0.5, 0.5, 0.5, 1);
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+       if(!sprog->link())
+               return;
+
+       sprog->use();
+
+       for(size_t i=0; i<scene->objects.size(); i++) {
+               draw_object(scene->objects[i]);
+       }
 }
 
 void Renderer::draw_object(Object *object) const
 {
+       Material *m = object->material;
+
+       /* setting uniforms for material */
+
+       sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
+       sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
+       sprog->set_uniformf(shin_loc, m->shininess);
+
+       /* texture */
+
+       if (m->dtex)
+               m->dtex->bind();
+
+       /* setting uniforms for matrices */
+
+       Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
+       sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
+
+       Mat4 mview = object->transform * camera->get_view_matrix();
+       sprog->set_uniform_matrix(mview_loc, mview);
+
+       object->mesh->update_vertex_data();
+       object->mesh->draw();
 }
\ No newline at end of file