2 #include <gmath/gmath.h>
14 #include "shader_manager.h"
21 static Mesh *skymesh; /* it will be used by many renderers => no destroy */
22 static ShaderProgram *skyprog;
23 static int viewproj_loc;
41 bool Renderer::create()
44 // if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) {
45 // fprintf(stderr, "Failed to load debug shaders.\n");
48 if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
52 /* getting material uniform locations: diffuse, specular, specular exponent (strength) */
54 diff_loc = sprog->get_uniform_location("diffuse");
55 spec_loc = sprog->get_uniform_location("specular");
56 shin_loc = sprog->get_uniform_location("shininess");
57 fog_loc = sprog->get_uniform_location("fog_density");
59 /* uniform locations for matrices */
61 mmviewproj_loc = sprog->get_uniform_location("mmviewproj");
62 mview_loc = sprog->get_uniform_location("mview");
63 mmod_loc = sprog->get_uniform_location("mmod");
65 /* skybox, irradiance map uniforms */
67 stex_loc = sprog->get_uniform_location("stex");
68 dstex_loc = sprog->get_uniform_location("dstex");
71 sprog->set_uniformi(stex_loc, 0);
74 sprog->set_uniformi(dstex_loc, 1);
79 void Renderer::draw() const
97 for(size_t i=0; i<scene->objects.size(); i++) {
98 draw_object(scene->objects[i]);
102 void Renderer::draw_object(Object *object) const
104 Material *m = object->material;
106 /* setting uniforms for material */
109 sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
112 sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
115 sprog->set_uniformf(shin_loc, m->shininess);
118 sprog->set_uniformf(fog_loc, fog_density);
125 /* setting uniforms for matrices */
127 Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
128 if(mmviewproj_loc != -1)
129 sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
131 Mat4 mview = object->transform * camera->get_view_matrix();
133 sprog->set_uniform_matrix(mview_loc, mview);
135 Mat4 mmod = object->transform.upper3x3();
137 sprog->set_uniform_matrix(mmod_loc, mmod);
139 object->mesh->draw();
144 int loc = nprog->get_uniform_location("mmviewproj");
146 nprog->set_uniform_matrix(loc, mmviewproj);
149 object->mesh->draw_normals(1.0);
154 void Renderer::set_sky_tex(Texture *stex)
157 skymesh = gfx_create_mesh();
158 gen_geosphere(skymesh, 1, 0);
161 if(!(skyprog = sdr_man->create_shader_program("sky.v.glsl", "sky.f.glsl")))
163 if((viewproj_loc = skyprog->get_uniform_location("mviewproj") == -1))
169 void Renderer::set_diffuse_sky_tex(Texture *dstex)
174 void Renderer::draw_skybox() const
176 if(!skymesh || !skytex)
180 gfx_cull_face(GFX_CULL_NONE);
186 mviewproj = camera->get_view_matrix().upper3x3() * mprojection;
187 skyprog->set_uniform_matrix(viewproj_loc, mviewproj);
191 gfx_cull_face(GFX_CULL_BACK);