]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
Rename typedefs.h to geometry.h
[shadowclad.git] / src / engine / render.c
1 #include <GL/glut.h>
2 #include <stdbool.h>
3
4 #include "game/level.h"
5 #include "game/player.h"
6
7 #include "geometry.h"
8 #include "performance.h"
9
10 const float AXIS_RADIUS = 5.0f;
11
12 static void setupCamera();
13 static void moveCameraTo(const Vector3D pos);
14 static void drawAxes();
15 static void renderBlockGrid(const BlockGrid grid);
16 static void renderCharacter(const Character* character, const Vector3D pos);
17 static void drawAsset3D(const Asset3D* asset3D);
18
19 float viewportAspectRatio = 1.0f;
20
21
22
23 void initRender() {
24         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
25         
26         GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
27         GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
28         GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
29         GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
30         
31         glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
32         glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
33         glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
34         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
35         
36         glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
37         glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
38         glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
39 }
40
41 void renderScene() {
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(playerPos);
50         
51         glDisable(GL_LIGHTING);
52         drawAxes();
53         glEnable(GL_LIGHTING);
54         
55         glEnable(GL_LIGHT0);
56         glEnable(GL_TEXTURE_2D);
57         renderBlockGrid(levelGrid);
58         renderCharacter(&playerCharacter, playerPos);
59         glDisable(GL_TEXTURE_2D);
60         glDisable(GL_LIGHT0);
61         
62         glFlush();
63         glutSwapBuffers();
64         frameRendered();
65         glutPostRedisplay();
66 }
67
68 static void setupCamera() {
69         glMatrixMode(GL_PROJECTION);
70         glLoadIdentity();
71         glOrtho(-16.0,
72                 16.0,
73                 -16.0/viewportAspectRatio,
74                 16.0/viewportAspectRatio,
75                 -128.0,
76                 128.0);
77         glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
78         glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
79 }
80
81 static void moveCameraTo(const Vector3D pos) {
82         glMatrixMode(GL_PROJECTION);
83         glTranslatef(-pos.x, -pos.y, -pos.z);
84 }
85
86 static void drawAxes() {
87         glMatrixMode(GL_MODELVIEW);
88         // X axis
89         glColor3f(1.0f, 0.0f, 0.0f);
90         glBegin(GL_LINES);
91         glVertex3f(0.0f, 0.0f, 0.0f);
92         glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
93         glEnd();
94         // Y axis
95         glColor3f(0.0f, 1.0f, 0.0f);
96         glBegin(GL_LINES);
97         glVertex3f(0.0f, 0.0f, 0.0f);
98         glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
99         glEnd();
100         // Z axis
101         glColor3f(0.0f, 0.0f, 1.0f);
102         glBegin(GL_LINES);
103         glVertex3f(0.0f, 0.0f, 0.0f);
104         glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
105         glEnd();
106 }
107
108 static void renderBlockGrid(const BlockGrid grid) {
109         glMatrixMode(GL_MODELVIEW);
110         for (int z = 0; z < grid.depth; ++z) {
111                 glLoadIdentity();
112                 glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
113                 for (int x = 0; x < grid.width; ++x) {
114                         drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
115                         glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
116                 }
117         }
118         glLoadIdentity();
119 }
120
121 static void renderCharacter(const Character* character, const Vector3D pos) {
122         glMatrixMode(GL_MODELVIEW);
123         glTranslatef(pos.x, pos.y, pos.z);
124         drawAsset3D(character->asset3D);
125         glLoadIdentity();
126 }
127
128 static void drawAsset3D(const Asset3D* asset3D) {
129         if (asset3D == NULL) {
130                 return;
131         }
132         
133         glMatrixMode(GL_MODELVIEW);
134         glColor3f(0.5f, 1.0f, 0.0f);
135         
136         for (size_t meshIndex = 0; meshIndex < asset3D->numMeshes; ++meshIndex) {
137                 const Mesh mesh = asset3D->meshes[meshIndex];
138                 glBindTexture(GL_TEXTURE_2D,
139                               asset3D->materials[mesh.materialIndex].textureId);
140                 bool hasNormals = mesh.normals != NULL;
141                 bool hasTextureCoords = mesh.textureCoords != NULL;
142                 
143                 for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
144                         const Face face = mesh.faces[faceIndex];
145                         
146                         GLenum faceMode;
147                         switch (face.numIndices) {
148                                 case 1: faceMode = GL_POINTS; break;
149                                 case 2: faceMode = GL_LINES; break;
150                                 case 3: faceMode = GL_TRIANGLES; break;
151                                 default: faceMode = GL_POLYGON; break;
152                         }
153                         
154                         glBegin(faceMode);
155                         
156                         for (size_t i = 0; i < face.numIndices; ++i) {
157                                 unsigned int vertIndex = face.indices[i];
158                                 if (hasNormals) {
159                                         if (hasTextureCoords) {
160                                                 Vector3D coords = mesh.textureCoords[vertIndex];
161                                                 glTexCoord2f(coords.x, coords.y);
162                                         }
163                                         Vector3D normal = mesh.normals[vertIndex];
164                                         glNormal3f(normal.x, normal.y, normal.z);
165                                 }
166                                 Vector3D vertex = mesh.vertices[vertIndex];
167                                 glVertex3f(vertex.x, vertex.y, vertex.z);
168                         }
169                         
170                         glEnd();
171                 }
172         }
173         glBindTexture(GL_TEXTURE_2D, 0);
174 }