]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
Some debug stuff to figure out normals and switch to flat shading
[shadowclad.git] / src / engine / render.c
1 #include "render.h"
2
3 #include <stdbool.h>
4 #include <GL/glut.h>
5
6 #include "geometry.h"
7 #include "performance.h"
8
9 #define SCENE_DEBUG_ 0
10 #define RENDER_DEBUG_ 1
11
12 float viewportAspectRatio = 1.0f;
13 const Scene* cameraAnchor;
14
15 static const float AXIS_RADIUS = 5.0f;
16
17 static void renderScene(const Scene*, const Transform baseTransform);
18 static void setupCamera();
19 static void moveCameraTo(const Scene* scene);
20 static void drawAxes();
21 static void drawSolid(const Solid* solid);
22
23
24
25 void initRender() {
26         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
27         
28         GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
29         GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
30         GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
31         GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
32         
33         glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
34         glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
35         glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
36         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
37         
38         glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
39         glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
40         glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
41         
42         glShadeModel(GL_FLAT);
43 }
44
45 void renderFrame() {
46         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
47
48         glEnable(GL_NORMALIZE);
49         glEnable(GL_CULL_FACE);
50         glEnable(GL_DEPTH_TEST);
51
52         setupCamera();
53         moveCameraTo(cameraAnchor);
54
55         renderScene(currentScene, identity());
56
57         glFlush();
58         glutSwapBuffers();
59         frameRendered();
60         glutPostRedisplay();
61 }
62
63 static void renderScene(const Scene* scene, const Transform baseTransform) {
64         if (!scene) {
65                 return;
66         }
67
68         Transform transform = multiply(scene->transform, baseTransform);
69
70         glMatrixMode(GL_MODELVIEW);
71         glLoadTransposeMatrixf((const GLfloat*) &transform);
72
73         glDisable(GL_LIGHTING);
74 #if SCENE_DEBUG_
75         drawAxes();
76 #endif // SCENE_DEBUG_
77         glEnable(GL_LIGHTING);
78
79         if (scene->solid) {
80                 glEnable(GL_LIGHT0);
81                 glEnable(GL_TEXTURE_2D);
82                 drawSolid(scene->solid);
83                 glDisable(GL_TEXTURE_2D);
84                 glDisable(GL_LIGHT0);
85         }
86
87         for (size_t i = 0; i < scene->numChildren; ++i) {
88                 renderScene(scene->children[i], transform);
89         }
90 }
91
92 static void setupCamera() {
93         glMatrixMode(GL_PROJECTION);
94         glLoadIdentity();
95         glOrtho(-16.0,
96                 16.0,
97                 -16.0/viewportAspectRatio,
98                 16.0/viewportAspectRatio,
99                 -128.0,
100                 128.0);
101         glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
102         glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
103 }
104
105 static void moveCameraTo(const Scene* anchor) {
106         glMatrixMode(GL_PROJECTION);
107         Vector3D pos = translationOf(worldTransform(anchor));
108         glTranslatef(-pos.x, -pos.y, -pos.z);
109 }
110
111 static void drawAxes() {
112         glMatrixMode(GL_MODELVIEW);
113         // X axis
114         glColor3f(1.0f, 0.0f, 0.0f);
115         glBegin(GL_LINES);
116         glVertex3f(0.0f, 0.0f, 0.0f);
117         glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
118         glEnd();
119         // Y axis
120         glColor3f(0.0f, 1.0f, 0.0f);
121         glBegin(GL_LINES);
122         glVertex3f(0.0f, 0.0f, 0.0f);
123         glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
124         glEnd();
125         // Z axis
126         glColor3f(0.0f, 0.0f, 1.0f);
127         glBegin(GL_LINES);
128         glVertex3f(0.0f, 0.0f, 0.0f);
129         glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
130         glEnd();
131 }
132
133 #if RENDER_DEBUG_
134 static GLfloat ab(GLfloat a) {
135         return a < 0 ? -a : a;
136 }
137 #endif // RENDER_DEBUG_
138
139 static void drawSolid(const Solid* solid) {
140         if (solid == NULL) {
141                 return;
142         }
143         
144         glMatrixMode(GL_MODELVIEW);
145         glColor3f(0.5f, 1.0f, 0.0f);
146         
147         for (size_t meshIndex = 0; meshIndex < solid->numMeshes; ++meshIndex) {
148                 const Mesh mesh = solid->meshes[meshIndex];
149                 glBindTexture(GL_TEXTURE_2D,
150                               solid->materials[mesh.materialIndex].textureId);
151                 bool hasNormals = mesh.normals != NULL;
152                 bool hasTextureCoords = mesh.textureCoords != NULL;
153                 
154                 for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
155                         const Face face = mesh.faces[faceIndex];
156                         
157 #if RENDER_DEBUG_
158                         if (hasNormals) {
159                                 glDisable(GL_LIGHTING);
160                                 glDisable(GL_TEXTURE_2D);
161                                 glBegin(GL_LINES);
162                                 for (size_t i = 0; i < face.numIndices; ++i) {
163                                         size_t vertIndex = face.indices[i];
164                                         Vector3D vertex = mesh.vertices[vertIndex];
165                                         Vector3D normal = mesh.normals[vertIndex];
166                                         glColor3f(ab(normal.x), ab(normal.y), ab(normal.z));
167                                         glVertex3f(vertex.x, vertex.y, vertex.z);
168                                         glVertex3f(vertex.x + normal.x, vertex.y + normal.y, vertex.z + normal.z);
169                                 }
170                                 glEnd();
171                                 glEnable(GL_TEXTURE_2D);
172                                 glEnable(GL_LIGHTING);
173                         }
174 #endif // RENDER_DEBUG_
175                         
176                         GLenum faceMode;
177                         switch (face.numIndices) {
178                                 case 1: faceMode = GL_POINTS; break;
179                                 case 2: faceMode = GL_LINES; break;
180                                 case 3: faceMode = GL_TRIANGLES; break;
181                                 default: faceMode = GL_POLYGON; break;
182                         }
183                         
184                         glBegin(faceMode);
185                         
186                         for (size_t i = 0; i < face.numIndices; ++i) {
187                                 size_t vertIndex = face.indices[i];
188                                 if (hasNormals) {
189                                         if (hasTextureCoords) {
190                                                 Vector3D coords = mesh.textureCoords[vertIndex];
191                                                 glTexCoord2f(coords.x, coords.y);
192                                         }
193                                         Vector3D normal = mesh.normals[vertIndex];
194                                         glNormal3f(normal.x, normal.y, normal.z);
195                                 }
196                                 Vector3D vertex = mesh.vertices[vertIndex];
197                                 glVertex3f(vertex.x, vertex.y, vertex.z);
198                         }
199                         
200                         glEnd();
201                 }
202         }
203         glBindTexture(GL_TEXTURE_2D, 0);
204 }