]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
0cfc49f1f57af34cc913d99a37c424f6a1bdebef
[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 float viewportAspectRatio = 1.0f;
10 const Scene* cameraAnchor;
11
12 static const float AXIS_RADIUS = 5.0f;
13
14 static void renderScene(const Scene*, const Transform baseTransform);
15 static void setupCamera();
16 static void moveCameraTo(const Scene* scene);
17 static void drawAxes();
18 static void drawSolid(const Solid* solid);
19
20
21
22 void initRender() {
23         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
24         
25         GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
26         GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
27         GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
28         GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
29         
30         glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
31         glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
32         glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
33         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
34         
35         glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
36         glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
37         glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
38 }
39
40 void renderFrame() {
41         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
42
43         glEnable(GL_NORMALIZE);
44         glEnable(GL_CULL_FACE);
45         glEnable(GL_DEPTH_TEST);
46
47         setupCamera();
48         moveCameraTo(cameraAnchor);
49
50         renderScene(currentScene, identity());
51
52         glFlush();
53         glutSwapBuffers();
54         frameRendered();
55         glutPostRedisplay();
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         drawAxes();
70         glEnable(GL_LIGHTING);
71
72         if (scene->solid) {
73                 glEnable(GL_LIGHT0);
74                 glEnable(GL_TEXTURE_2D);
75                 drawSolid(scene->solid);
76                 glDisable(GL_TEXTURE_2D);
77                 glDisable(GL_LIGHT0);
78         }
79
80         for (size_t i = 0; i < scene->numChildren; ++i) {
81                 renderScene(scene->children[i], transform);
82         }
83 }
84
85 static void setupCamera() {
86         glMatrixMode(GL_PROJECTION);
87         glLoadIdentity();
88         glOrtho(-16.0,
89                 16.0,
90                 -16.0/viewportAspectRatio,
91                 16.0/viewportAspectRatio,
92                 -128.0,
93                 128.0);
94         glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
95         glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
96 }
97
98 static void moveCameraTo(const Scene* anchor) {
99         glMatrixMode(GL_PROJECTION);
100         // TODO This needs to account for parent nodes as well
101         Vector3D pos = translationOf(worldTransform(anchor));
102         glTranslatef(-pos.x, -pos.y, -pos.z);
103 }
104
105 static void drawAxes() {
106         glMatrixMode(GL_MODELVIEW);
107         // X axis
108         glColor3f(1.0f, 0.0f, 0.0f);
109         glBegin(GL_LINES);
110         glVertex3f(0.0f, 0.0f, 0.0f);
111         glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
112         glEnd();
113         // Y axis
114         glColor3f(0.0f, 1.0f, 0.0f);
115         glBegin(GL_LINES);
116         glVertex3f(0.0f, 0.0f, 0.0f);
117         glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
118         glEnd();
119         // Z axis
120         glColor3f(0.0f, 0.0f, 1.0f);
121         glBegin(GL_LINES);
122         glVertex3f(0.0f, 0.0f, 0.0f);
123         glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
124         glEnd();
125 }
126
127 static void drawSolid(const Solid* solid) {
128         if (solid == NULL) {
129                 return;
130         }
131         
132         glMatrixMode(GL_MODELVIEW);
133         glColor3f(0.5f, 1.0f, 0.0f);
134         
135         for (size_t meshIndex = 0; meshIndex < solid->numMeshes; ++meshIndex) {
136                 const Mesh mesh = solid->meshes[meshIndex];
137                 glBindTexture(GL_TEXTURE_2D,
138                               solid->materials[mesh.materialIndex].textureId);
139                 bool hasNormals = mesh.normals != NULL;
140                 bool hasTextureCoords = mesh.textureCoords != NULL;
141                 
142                 for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
143                         const Face face = mesh.faces[faceIndex];
144                         
145                         GLenum faceMode;
146                         switch (face.numIndices) {
147                                 case 1: faceMode = GL_POINTS; break;
148                                 case 2: faceMode = GL_LINES; break;
149                                 case 3: faceMode = GL_TRIANGLES; break;
150                                 default: faceMode = GL_POLYGON; break;
151                         }
152                         
153                         glBegin(faceMode);
154                         
155                         for (size_t i = 0; i < face.numIndices; ++i) {
156                                 size_t vertIndex = face.indices[i];
157                                 if (hasNormals) {
158                                         if (hasTextureCoords) {
159                                                 Vector3D coords = mesh.textureCoords[vertIndex];
160                                                 glTexCoord2f(coords.x, coords.y);
161                                         }
162                                         Vector3D normal = mesh.normals[vertIndex];
163                                         glNormal3f(normal.x, normal.y, normal.z);
164                                 }
165                                 Vector3D vertex = mesh.vertices[vertIndex];
166                                 glVertex3f(vertex.x, vertex.y, vertex.z);
167                         }
168                         
169                         glEnd();
170                 }
171         }
172         glBindTexture(GL_TEXTURE_2D, 0);
173 }