b20ef19c6397e27384a013a495c251774226cb9e
[demo] / src / renderer.cc
1 #include <GL/glew.h>
2 #include <gmath/gmath.h>
3
4 #include "global.h"
5 #include "gfxapi.h"
6
7 #include "camera.h"
8 #include "mesh.h"
9 #include "meshgen.h"
10 #include "object.h"
11 #include "renderer.h"
12 #include "scene.h"
13 #include "shader.h"
14 #include "shader_manager.h"
15 #include "texture.h"
16
17 #undef DRAW_NORMALS
18
19 /* skybox */
20
21 static Mesh *skymesh; /* it will be used by many renderers => no destroy */
22 static ShaderProgram *skyprog;
23 static int viewproj_loc;
24
25 Renderer::Renderer()
26 {
27         scene = 0;
28         camera = 0;
29         sprog = 0;
30
31         skytex = 0;
32         dskytex = 0;
33
34         fog_density = 0;
35 }
36
37 Renderer::~Renderer()
38 {
39 }
40
41 bool Renderer::create()
42 {
43         //debug
44         if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) {
45                 fprintf(stderr, "Failed to load debug shaders.\n");
46         }
47
48         if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
49                 return false;
50         }
51
52         /* getting material uniform locations: diffuse, specular, specular exponent (strength) */
53
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");
58
59         /* uniform locations for matrices */
60
61         mmviewproj_loc = sprog->get_uniform_location("mmviewproj");
62         mview_loc = sprog->get_uniform_location("mview");
63         mmod_loc = sprog->get_uniform_location("mmod");
64
65         /* skybox, irradiance map uniforms */
66
67         stex_loc = sprog->get_uniform_location("stex");
68         dstex_loc = sprog->get_uniform_location("dstex");
69
70         if(stex_loc != -1)
71                 sprog->set_uniformi(stex_loc, 0);
72
73         if(dstex_loc != -1)
74                 sprog->set_uniformi(dstex_loc, 1);
75
76         return true;
77 }
78
79 void Renderer::draw() const
80 {
81         if(!camera || !scene)
82                 return;
83
84         if(skytex) {
85                 draw_skybox();
86
87                 if(dskytex) {
88                         dskytex->bind(1);
89                 }
90         }
91
92         if(!sprog->link())
93                 return;
94
95         sprog->use();
96
97         for(size_t i=0; i<scene->objects.size(); i++) {
98                 draw_object(scene->objects[i]);
99         }
100 }
101
102 void Renderer::draw_object(Object *object) const
103 {
104         Material *m = object->material;
105
106         /* setting uniforms for material */
107
108         if(diff_loc != -1)
109                 sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
110
111         if(spec_loc != -1)
112                 sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
113
114         if(shin_loc != -1)
115                 sprog->set_uniformf(shin_loc, m->shininess);
116
117         if(fog_loc != -1)
118                 sprog->set_uniformf(fog_loc, fog_density);
119
120         /* texture */
121
122         if(m->dtex)
123                 m->dtex->bind(0);
124
125         /* setting uniforms for matrices */
126
127         Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
128         if(mmviewproj_loc != -1)
129                 sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
130
131         Mat4 mview = object->transform * camera->get_view_matrix();
132         if(mview_loc != -1)
133                 sprog->set_uniform_matrix(mview_loc, mview);
134
135         Mat4 mmod = object->transform.upper3x3();
136         if(mmod_loc != -1)
137                 sprog->set_uniform_matrix(mmod_loc, mmod);
138
139         object->mesh->draw();
140
141         // debug
142 #ifdef DRAW_NORMALS
143         if(nprog) {
144                 int loc = nprog->get_uniform_location("mmviewproj");
145                 if(loc != -1) {
146                         nprog->set_uniform_matrix(loc, mmviewproj);
147                 }
148                 nprog->use();
149                 object->mesh->draw_normals(1.0);
150         }
151 #endif
152 }
153
154 void Renderer::set_sky_tex(Texture *stex)
155 {
156         if(!skymesh) {
157                 skymesh = gfx_create_mesh();
158                 gen_geosphere(skymesh, 1, 0);
159         }
160         if(!skyprog) {
161                 if(!(skyprog = sdr_man->create_shader_program("sky.v.glsl", "sky.f.glsl")))
162                         return;
163                 if((viewproj_loc = skyprog->get_uniform_location("mviewproj") == -1))
164                         return;
165         }
166         skytex = stex;
167 }
168
169 void Renderer::set_diffuse_sky_tex(Texture *dstex)
170 {
171         dskytex = dstex;
172 }
173
174 void Renderer::draw_skybox() const
175 {
176         if(!skymesh || !skytex)
177                 return;
178
179         gfx_zbuffer(false);
180         gfx_cull_face(GFX_CULL_NONE);
181
182         skytex->bind();
183         skyprog->use();
184
185         Mat4 mviewproj;
186         mviewproj = camera->get_view_matrix().upper3x3() * mprojection;
187         skyprog->set_uniform_matrix(viewproj_loc, mviewproj);
188
189         skymesh->draw();
190
191         gfx_cull_face(GFX_CULL_BACK);
192         gfx_zbuffer(true);
193 }