]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
bd4456bd55637388e578991d5288c79510303527
[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 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
42 void renderFrame() {
43         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
44
45         glEnable(GL_NORMALIZE);
46         glEnable(GL_CULL_FACE);
47         glEnable(GL_DEPTH_TEST);
48
49         setupCamera();
50         moveCameraTo(cameraAnchor);
51
52         renderScene(currentScene, identity());
53
54         glFlush();
55         glutSwapBuffers();
56         frameRendered();
57         glutPostRedisplay();
58 }
59
60 static void renderScene(const Scene* scene, const Transform baseTransform) {
61         if (!scene) {
62                 return;
63         }
64
65         Transform transform = multiply(scene->transform, baseTransform);
66
67         glMatrixMode(GL_MODELVIEW);
68         glLoadTransposeMatrixf((const GLfloat*) &transform);
69
70         glDisable(GL_LIGHTING);
71 #if RENDER_DEBUG_
72         drawAxes();
73 #endif // RENDER_DEBUG_
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         Vector3D 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 void drawSolid(const Solid* solid) {
131         if (solid == NULL) {
132                 return;
133         }
134         
135         glMatrixMode(GL_MODELVIEW);
136         glColor3f(0.5f, 1.0f, 0.0f);
137         
138         for (size_t meshIndex = 0; meshIndex < solid->numMeshes; ++meshIndex) {
139                 const Mesh mesh = solid->meshes[meshIndex];
140                 glBindTexture(GL_TEXTURE_2D,
141                               solid->materials[mesh.materialIndex].textureId);
142                 bool hasNormals = mesh.normals != NULL;
143                 bool hasTextureCoords = mesh.textureCoords != NULL;
144                 
145                 for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
146                         const Face face = mesh.faces[faceIndex];
147                         
148                         GLenum faceMode;
149                         switch (face.numIndices) {
150                                 case 1: faceMode = GL_POINTS; break;
151                                 case 2: faceMode = GL_LINES; break;
152                                 case 3: faceMode = GL_TRIANGLES; break;
153                                 default: faceMode = GL_POLYGON; break;
154                         }
155                         
156                         glBegin(faceMode);
157                         
158                         for (size_t i = 0; i < face.numIndices; ++i) {
159                                 size_t vertIndex = face.indices[i];
160                                 if (hasNormals) {
161                                         if (hasTextureCoords) {
162                                                 Vector3D coords = mesh.textureCoords[vertIndex];
163                                                 glTexCoord2f(coords.x, coords.y);
164                                         }
165                                         Vector3D normal = mesh.normals[vertIndex];
166                                         glNormal3f(normal.x, normal.y, normal.z);
167                                 }
168                                 Vector3D vertex = mesh.vertices[vertIndex];
169                                 glVertex3f(vertex.x, vertex.y, vertex.z);
170                         }
171                         
172                         glEnd();
173                 }
174         }
175         glBindTexture(GL_TEXTURE_2D, 0);
176 }