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