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