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
89 for(size_t i=0; i<scene->objects.size(); i++) {
90 draw_object(scene->objects[i]);
94 void Renderer::draw_object(Object *object) const
96 Material *m = object->material;
98 /* setting uniforms for material */
100 sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
101 sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
102 sprog->set_uniformf(shin_loc, m->shininess);
109 /* setting uniforms for matrices */
111 Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
112 sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
114 Mat4 mview = object->transform * camera->get_view_matrix();
115 sprog->set_uniform_matrix(mview_loc, mview);
117 object->mesh->update_vertex_data();
118 object->mesh->draw();
121 void Renderer::set_sky_tex(Texture *stex)
124 skymesh = gfx_create_mesh();
125 gen_geosphere(skymesh, 1, 0);
128 if(!(skyprog = sdr_man->create_shader_program("sky.v.glsl", "sky.f.glsl")))
130 if((viewproj_loc = skyprog->get_uniform_location("mviewproj") == -1))
136 void Renderer::set_diffuse_sky_tex(Texture *dstex)
141 void Renderer::draw_skybox() const
143 if(!skymesh || !skytex)
147 gfx_cull_face(GFX_CULL_NONE);
153 mviewproj = camera->get_view_matrix().upper3x3() * mprojection;
154 skyprog->set_uniform_matrix(viewproj_loc, mviewproj);
158 gfx_cull_face(GFX_CULL_BACK);