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