]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
Finish moving to scene tree implementation; clean up unused code
[shadowclad.git] / src / engine / render.c
1 #include <GL/glut.h>
2 #include <stdbool.h>
3
4 #include "geometry.h"
5 #include "performance.h"
6 #include "scene.h"
7
8 float viewportAspectRatio = 1.0f;
9 const Scene* cameraAnchor;
10
11 static const float AXIS_RADIUS = 5.0f;
12
13 static void renderScene(const Scene*);
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
39 void renderFrame() {
40         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
41
42         glEnable(GL_NORMALIZE);
43         glEnable(GL_CULL_FACE);
44         glEnable(GL_DEPTH_TEST);
45
46         setupCamera();
47         moveCameraTo(cameraAnchor);
48
49         renderScene(currentScene);
50
51         glFlush();
52         glutSwapBuffers();
53         frameRendered();
54         glutPostRedisplay();
55 }
56
57 void renderScene(const Scene* scene) {
58         if (!scene) {
59                 return;
60         }
61
62         glMatrixMode(GL_MODELVIEW);
63         glLoadTransposeMatrixf((const GLfloat*) &scene->transform);
64
65         glDisable(GL_LIGHTING);
66         drawAxes();
67         glEnable(GL_LIGHTING);
68
69         if (scene->solid) {
70                 glEnable(GL_LIGHT0);
71                 glEnable(GL_TEXTURE_2D);
72                 drawSolid(scene->solid);
73                 glDisable(GL_TEXTURE_2D);
74                 glDisable(GL_LIGHT0);
75         }
76
77         for (size_t i = 0; i < scene->numChildren; ++i) {
78                 renderScene(scene->children[i]);
79         }
80 }
81
82 static void setupCamera() {
83         glMatrixMode(GL_PROJECTION);
84         glLoadIdentity();
85         glOrtho(-16.0,
86                 16.0,
87                 -16.0/viewportAspectRatio,
88                 16.0/viewportAspectRatio,
89                 -128.0,
90                 128.0);
91         glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
92         glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
93 }
94
95 static void moveCameraTo(const Scene* anchor) {
96         glMatrixMode(GL_PROJECTION);
97         // TODO This needs to account for parent nodes as well
98         Vector3D pos = translationOf(anchor->transform);
99         glTranslatef(-pos.x, -pos.y, -pos.z);
100 }
101
102 static void drawAxes() {
103         glMatrixMode(GL_MODELVIEW);
104         // X axis
105         glColor3f(1.0f, 0.0f, 0.0f);
106         glBegin(GL_LINES);
107         glVertex3f(0.0f, 0.0f, 0.0f);
108         glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
109         glEnd();
110         // Y axis
111         glColor3f(0.0f, 1.0f, 0.0f);
112         glBegin(GL_LINES);
113         glVertex3f(0.0f, 0.0f, 0.0f);
114         glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
115         glEnd();
116         // Z axis
117         glColor3f(0.0f, 0.0f, 1.0f);
118         glBegin(GL_LINES);
119         glVertex3f(0.0f, 0.0f, 0.0f);
120         glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
121         glEnd();
122 }
123
124 static void drawSolid(const Solid* solid) {
125         if (solid == NULL) {
126                 return;
127         }
128         
129         glMatrixMode(GL_MODELVIEW);
130         glColor3f(0.5f, 1.0f, 0.0f);
131         
132         for (size_t meshIndex = 0; meshIndex < solid->numMeshes; ++meshIndex) {
133                 const Mesh mesh = solid->meshes[meshIndex];
134                 glBindTexture(GL_TEXTURE_2D,
135                               solid->materials[mesh.materialIndex].textureId);
136                 bool hasNormals = mesh.normals != NULL;
137                 bool hasTextureCoords = mesh.textureCoords != NULL;
138                 
139                 for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
140                         const Face face = mesh.faces[faceIndex];
141                         
142                         GLenum faceMode;
143                         switch (face.numIndices) {
144                                 case 1: faceMode = GL_POINTS; break;
145                                 case 2: faceMode = GL_LINES; break;
146                                 case 3: faceMode = GL_TRIANGLES; break;
147                                 default: faceMode = GL_POLYGON; break;
148                         }
149                         
150                         glBegin(faceMode);
151                         
152                         for (size_t i = 0; i < face.numIndices; ++i) {
153                                 size_t vertIndex = face.indices[i];
154                                 if (hasNormals) {
155                                         if (hasTextureCoords) {
156                                                 Vector3D coords = mesh.textureCoords[vertIndex];
157                                                 glTexCoord2f(coords.x, coords.y);
158                                         }
159                                         Vector3D normal = mesh.normals[vertIndex];
160                                         glNormal3f(normal.x, normal.y, normal.z);
161                                 }
162                                 Vector3D vertex = mesh.vertices[vertIndex];
163                                 glVertex3f(vertex.x, vertex.y, vertex.z);
164                         }
165                         
166                         glEnd();
167                 }
168         }
169         glBindTexture(GL_TEXTURE_2D, 0);
170 }