2 #include <gmath/gmath.h>
14 #include "shader_manager.h"
19 static Mesh *skymesh; /* it will be used by many renderers => no destroy */
20 static ShaderProgram *skyprog;
21 static int viewproj_loc;
37 bool Renderer::create()
39 if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
43 /* getting material uniform locations: diffuse, specular, specular exponent (strength) */
45 if((diff_loc = sprog->get_uniform_location("diffuse")) == -1) {
46 fprintf(stderr, "Invalid uniform location: can't find \"diffuse\".\n");
50 if((spec_loc = sprog->get_uniform_location("specular")) == -1) {
51 fprintf(stderr, "Invalid uniform location: can't find \"specular\".\n");
55 if((shin_loc = sprog->get_uniform_location("shininess")) == -1) {
56 fprintf(stderr, "Invalid uniform location: can't find \"shininess\".\n");
60 /* uniform locations for matrices */
62 if((mmviewproj_loc = sprog->get_uniform_location("mmviewproj")) == -1) {
63 fprintf(stderr, "Invalid uniform location: can't find \"mmviewproj\".\n");
67 if((mview_loc = sprog->get_uniform_location("mview")) == -1) {
68 fprintf(stderr, "Invalid uniform location: can't find \"mview\".\n");
75 void Renderer::draw() const
85 for(size_t i=0; i<scene->objects.size(); i++) {
86 draw_object(scene->objects[i]);
90 void Renderer::draw_object(Object *object) const
92 Material *m = object->material;
94 /* setting uniforms for material */
96 sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
97 sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
98 sprog->set_uniformf(shin_loc, m->shininess);
105 /* setting uniforms for matrices */
107 Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
108 sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
110 Mat4 mview = object->transform * camera->get_view_matrix();
111 sprog->set_uniform_matrix(mview_loc, mview);
113 object->mesh->update_vertex_data();
114 object->mesh->draw();
117 void Renderer::set_sky_tex(Texture *stex)
120 skymesh = gfx_create_mesh();
121 gen_geosphere(skymesh, 1, 0);
124 if(!(skyprog = sdr_man->create_shader_program("sky.v.glsl", "sky.f.glsl")))
126 if((viewproj_loc = skyprog->get_uniform_location("viewproj") == -1))
132 void Renderer::set_diffuse_sky_tex(Texture *dstex)
137 void Renderer::draw_skybox()
139 if(!skymesh || !skytex)
146 mviewproj = camera->get_view_matrix().upper3x3() * mprojection;
147 skyprog->set_uniform_matrix(viewproj_loc, mviewproj);