comparison gldragon.cpp @ 37:73a785323e8a

Rendering cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Dec 2019 23:52:19 +0200
parents d640f2a34031
children 372fa249ce26
comparison
equal deleted inserted replaced
36:d640f2a34031 37:73a785323e8a
160 160
161 return true; 161 return true;
162 } 162 }
163 163
164 164
165 void dmDrawModel(const DMModel &model) 165 void dmDrawModel(const DMSimpleScene &scene, const DMModel &model)
166 { 166 {
167 int maxIndices; 167 int maxIndices;
168 168
169 if (optUseShaders)
170 {
171 // Enable shader program
172 glUseProgram(model.id_prog);
173 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
174 }
175
176 // Set the material of the model
169 glEnable(GL_COLOR_MATERIAL); 177 glEnable(GL_COLOR_MATERIAL);
170 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 178 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
171 glMateriali(GL_FRONT, GL_SHININESS, model.shininess); 179 glMateriali(GL_FRONT, GL_SHININESS, model.shininess);
172 glMaterialfv(GL_FRONT, GL_SPECULAR, model.specular.values); 180 glMaterialfv(GL_FRONT, GL_SPECULAR, model.specular.values);
181 glColor4fv(model.diffuse.values);
173 182
174 // Render the model 183 // Render the model
175 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); 184 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
176 185
177 // Add transforms 186 // Add transforms
193 202
194 for (int n = 0; n < model.nfaces; n += maxIndices) 203 for (int n = 0; n < model.nfaces; n += maxIndices)
195 { 204 {
196 const int count = std::min(maxIndices, model.nfaces - n); 205 const int count = std::min(maxIndices, model.nfaces - n);
197 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]); 206 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]);
207 }
208
209 // Restore
210 if (optUseShaders)
211 {
212 glUseProgram(0);
198 } 213 }
199 } 214 }
200 215
201 216
202 void dmDrawScene(const DMSimpleScene &scene) 217 void dmDrawScene(const DMSimpleScene &scene)
226 glVertex2f(1.0f, 1.0f); 241 glVertex2f(1.0f, 1.0f);
227 glVertex2f(0.0f, 1.0f); 242 glVertex2f(0.0f, 1.0f);
228 } 243 }
229 glEnd(); 244 glEnd();
230 245
231
232 // Restore the 3D projection 246 // Restore the 3D projection
233 glMatrixMode(GL_PROJECTION); 247 glMatrixMode(GL_PROJECTION);
234 glPopMatrix(); 248 glPopMatrix();
235 249
236 glMatrixMode(GL_MODELVIEW); 250 glMatrixMode(GL_MODELVIEW);
237 glPopMatrix(); 251 glPopMatrix();
238 252
239 glEnable(GL_DEPTH_TEST); 253 glEnable(GL_DEPTH_TEST);
254 glEnable(GL_LIGHTING);
240 255
241 // Draw models 256 // Draw models
242 for (const DMModel &model : scene.models) 257 for (const DMModel &model : scene.models)
243 { 258 {
244 if (optUseShaders)
245 {
246 // Enable shader program
247 glUseProgram(model.id_prog);
248 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
249 }
250 else
251 {
252 // Set the color of the model
253 glEnable(GL_LIGHTING);
254 glColor3ub(0x90, 0x80, 0x90);
255 }
256
257 glPushMatrix(); 259 glPushMatrix();
258 dmDrawModel(model); 260 dmDrawModel(scene, model);
259 glPopMatrix(); 261 glPopMatrix();
260
261 // Restore
262 if (optUseShaders)
263 {
264 glUseProgram(0);
265 }
266 } 262 }
267 } 263 }
268 264
269 265
270 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader) 266 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader)