]> git.lizzy.rs Git - shadowclad.git/blob - render.c
63578dffac98cf652021373e5f18d8d0aa498927
[shadowclad.git] / render.c
1 #include <GL/glut.h>
2
3 #include "level.h"
4 #include "typedefs.h"
5 #include "performance.h"
6 #include "render.h"
7
8 const float AXIS_RADIUS = 5.0f;
9
10 void renderScene() {
11         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
12         glLoadIdentity();
13         
14         glEnable(GL_NORMALIZE);
15         glEnable(GL_CULL_FACE);
16         
17         glDisable(GL_LIGHTING);
18         drawAxes();
19         glEnable(GL_LIGHTING);
20         
21         glEnable(GL_LIGHT0);
22         drawSceneRecursive(levelScene, levelScene->mRootNode);
23         glDisable(GL_LIGHT0);
24         
25         glFlush();
26         glutSwapBuffers();
27         frameRendered();
28         glutPostRedisplay();
29 }
30
31 void drawAxes() {
32         point3f xAxisStart = { 0.0f, 0.0f, 0.0f };
33         point3f xAxisEnd = { AXIS_RADIUS, 0.0f, 0.0f };
34         point3f yAxisStart = { 0.0f, 0.0f, 0.0f };
35         point3f yAxisEnd = { 0.0f, AXIS_RADIUS, 0.0f };
36         point3f zAxisStart = { 0.0f, 0.0f, 0.0f };
37         point3f zAxisEnd = { 0.0f, 0.0f, AXIS_RADIUS };
38         
39         glColor3f(1.0f, 0.0f, 0.0f);
40         glBegin(GL_LINES);
41         glVertex3fv(xAxisStart);
42         glVertex3fv(xAxisEnd);
43         glEnd();
44         
45         glColor3f(0.0f, 1.0f, 0.0f);
46         glBegin(GL_LINES);
47         glVertex3fv(yAxisStart);
48         glVertex3fv(yAxisEnd);
49         glEnd();
50         
51         glColor3f(0.0f, 0.0f, 1.0f);
52         glBegin(GL_LINES);
53         glVertex3fv(zAxisStart);
54         glVertex3fv(zAxisEnd);
55         glEnd();
56 }
57
58 void drawSceneRecursive(const AiScene* scene, const AiNode* node) {
59         if (((*scene).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
60                 return;
61         }
62         
63         for (int i = 0; i < (*node).mNumMeshes; ++i) {
64                 const AiMesh* mesh = (*scene).mMeshes[(*node).mMeshes[i]];
65                 for (int k = 0; k < (*mesh).mNumFaces; ++k) {
66                         const AiFace face = (*mesh).mFaces[k];
67                         
68                         GLenum faceMode;
69                         switch (face.mNumIndices) {
70                                 case 1: faceMode = GL_POINTS; break;
71                                 case 2: faceMode = GL_LINES; break;
72                                 case 3: faceMode = GL_TRIANGLES; break;
73                                 default: faceMode = GL_POLYGON; break;
74                         }
75                         
76                         glBegin(faceMode);
77                         
78                         glColor3f(1.0f, 1.0f, 1.0f);
79                         for (int l = 0; l < face.mNumIndices; ++l) {
80                                 glVertex3fv((const GLfloat*) &(*mesh).mVertices[face.mIndices[l]]);
81                         }
82                         
83                         glEnd();
84                 }
85         }
86         
87         for (int i = 0; i < (*node).mNumChildren; ++i) {
88                 drawSceneRecursive(scene, (*node).mChildren[i]);
89         }
90 }