comparison glxdragon.cpp @ 17:0fa9302e324d

Cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 05 Nov 2019 10:48:22 +0200
parents c134a186912f
children b1e75c65016d
comparison
equal deleted inserted replaced
16:c134a186912f 17:0fa9302e324d
197 197
198 void dmDrawModelVA(const Mesh &mesh) 198 void dmDrawModelVA(const Mesh &mesh)
199 { 199 {
200 int maxIndices; 200 int maxIndices;
201 201
202 if (optUseShaders)
203 {
204 // Enable shader program
205 glUseProgram(mesh.id_prog);
206 }
207 else
208 {
209 // Set the color of the model
210 glEnable(GL_LIGHTING);
211 glColor3ub(0x90, 0x80, 0x90);
212 }
213
214 // Render the model
202 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); 215 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
203 216
204 glVertexPointer(3, GL_FLOAT, 3 * 4 * 2, &mesh.vertices[0]); 217 glVertexPointer(3, GL_FLOAT, 3 * 4 * 2, &mesh.vertices[0]);
205 glNormalPointer( GL_FLOAT, 3 * 4 * 2, &mesh.vertices[3]); 218 glNormalPointer( GL_FLOAT, 3 * 4 * 2, &mesh.vertices[3]);
206 219
207 for (int n = 0; n < mesh.nfaces; n += maxIndices) 220 for (int n = 0; n < mesh.nfaces; n += maxIndices)
208 { 221 {
209 const int count = std::min(maxIndices, mesh.nfaces - n); 222 const int count = std::min(maxIndices, mesh.nfaces - n);
210 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]); 223 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]);
211 } 224 }
212 } 225
213 226 // Restore
214 227 if (optUseShaders)
215 void dmPaintGL(Mesh &mesh) 228 {
229 glUseProgram(0);
230 }
231 }
232
233
234 void dmDrawBackground()
216 { 235 {
217 glClear(GL_DEPTH_BUFFER_BIT); 236 glClear(GL_DEPTH_BUFFER_BIT);
218 237
219 glMatrixMode(GL_PROJECTION); 238 glMatrixMode(GL_PROJECTION);
220 glPushMatrix(); 239 glPushMatrix();
224 243
225 glMatrixMode(GL_MODELVIEW); 244 glMatrixMode(GL_MODELVIEW);
226 glPushMatrix(); 245 glPushMatrix();
227 glLoadIdentity(); 246 glLoadIdentity();
228 247
229 248 // Draw the background gradient
230 glDisable(GL_DEPTH_TEST); 249 glDisable(GL_DEPTH_TEST);
231 glDisable(GL_LIGHTING); 250 glDisable(GL_LIGHTING);
232
233 // Draw the background gradient
234 glBegin(GL_QUADS); 251 glBegin(GL_QUADS);
235 { 252 {
236 glColor3ub(0x3B, 0x3B, 0x75); 253 glColor3ub(0x3B, 0x3B, 0x75);
237 glVertex2f(0.0f, 0.0f); 254 glVertex2f(0.0f, 0.0f);
238 glVertex2f(1.0f, 0.0f); 255 glVertex2f(1.0f, 0.0f);
250 267
251 glMatrixMode(GL_MODELVIEW); 268 glMatrixMode(GL_MODELVIEW);
252 glPopMatrix(); 269 glPopMatrix();
253 270
254 glEnable(GL_DEPTH_TEST); 271 glEnable(GL_DEPTH_TEST);
255
256 // Render the model
257 if (optUseShaders)
258 {
259 // Enable shader program
260 glUseProgram(mesh.id_prog);
261 dmDrawModelVA(mesh);
262 glUseProgram(0);
263 }
264 else
265 {
266 // Set the color of the model
267 glEnable(GL_LIGHTING);
268 glColor3ub(0x90, 0x80, 0x90);
269 dmDrawModelVA(mesh);
270 }
271 } 272 }
272 273
273 274
274 bool dmReadText(const std::string &filename, std::string &tstr) 275 bool dmReadText(const std::string &filename, std::string &tstr)
275 { 276 {
415 416
416 417
417 int main(int argc, char *argv[]) 418 int main(int argc, char *argv[])
418 { 419 {
419 std::string modelVertStr, modelFragStr; 420 std::string modelVertStr, modelFragStr;
420 struct Mesh modelMesh; 421 Mesh modelMesh;
421 bool exitFlag = false, optShowHelp = false; 422 bool exitFlag = false, optShowHelp = false;
422 int startTime, cycleStart, cycleFrames = 0, totalFrames = 0; 423 int startTime, cycleStart, cycleFrames = 0, totalFrames = 0;
423 double totalTime; 424 double totalTime;
424 425
425 // Check commandline argument for enabling shaders 426 // Check commandline argument for enabling shaders
580 break; 581 break;
581 } 582 }
582 } 583 }
583 584
584 // Render the next frame 585 // Render the next frame
585 dmPaintGL(modelMesh); 586 dmDrawBackground();
587 dmDrawModelVA(modelMesh);
586 588
587 // Draw the current frame 589 // Draw the current frame
588 SDL_GL_SwapWindow(dmWindow); 590 SDL_GL_SwapWindow(dmWindow);
589 591
590 // Rotate for 2 degrees 592 // Rotate for 2 degrees