]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/render.c
Move code into game and engine directories
[shadowclad.git] / src / engine / render.c
1 #include <GL/glut.h>
2 #include <stdbool.h>
3
4 #include "level.h"
5 #include "performance.h"
6 #include "player.h"
7 #include "typedefs.h"
8
9 const float AXIS_RADIUS = 5.0f;
10
11 static void setupCamera();
12 static void moveCameraTo(const Vector3D pos);
13 static void drawAxes();
14 static void renderBlockGrid(const BlockGrid grid);
15 static void renderCharacter(const Character* character, const Vector3D pos);
16 static void drawAsset3D(const Asset3D* asset3D);
17
18 float viewportAspectRatio = 1.0f;
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 renderScene() {
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(playerPos);
49         
50         glDisable(GL_LIGHTING);
51         drawAxes();
52         glEnable(GL_LIGHTING);
53         
54         glEnable(GL_LIGHT0);
55         glEnable(GL_TEXTURE_2D);
56         renderBlockGrid(levelGrid);
57         renderCharacter(&playerCharacter, playerPos);
58         glDisable(GL_TEXTURE_2D);
59         glDisable(GL_LIGHT0);
60         
61         glFlush();
62         glutSwapBuffers();
63         frameRendered();
64         glutPostRedisplay();
65 }
66
67 static void setupCamera() {
68         glMatrixMode(GL_PROJECTION);
69         glLoadIdentity();
70         glOrtho(-16.0,
71                 16.0,
72                 -16.0/viewportAspectRatio,
73                 16.0/viewportAspectRatio,
74                 -128.0,
75                 128.0);
76         glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
77         glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
78 }
79
80 static void moveCameraTo(const Vector3D pos) {
81         glMatrixMode(GL_PROJECTION);
82         glTranslatef(-pos.x, -pos.y, -pos.z);
83 }
84
85 static void drawAxes() {
86         glMatrixMode(GL_MODELVIEW);
87         // X axis
88         glColor3f(1.0f, 0.0f, 0.0f);
89         glBegin(GL_LINES);
90         glVertex3f(0.0f, 0.0f, 0.0f);
91         glVertex3f(AXIS_RADIUS, 0.0f, 0.0f);
92         glEnd();
93         // Y axis
94         glColor3f(0.0f, 1.0f, 0.0f);
95         glBegin(GL_LINES);
96         glVertex3f(0.0f, 0.0f, 0.0f);
97         glVertex3f(0.0f, AXIS_RADIUS, 0.0f);
98         glEnd();
99         // Z axis
100         glColor3f(0.0f, 0.0f, 1.0f);
101         glBegin(GL_LINES);
102         glVertex3f(0.0f, 0.0f, 0.0f);
103         glVertex3f(0.0f, 0.0f, AXIS_RADIUS);
104         glEnd();
105 }
106
107 static void renderBlockGrid(const BlockGrid grid) {
108         glMatrixMode(GL_MODELVIEW);
109         for (int z = 0; z < grid.depth; ++z) {
110                 glLoadIdentity();
111                 glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
112                 for (int x = 0; x < grid.width; ++x) {
113                         drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
114                         glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
115                 }
116         }
117         glLoadIdentity();
118 }
119
120 static void renderCharacter(const Character* character, const Vector3D pos) {
121         glMatrixMode(GL_MODELVIEW);
122         glTranslatef(pos.x, pos.y, pos.z);
123         drawAsset3D(character->asset3D);
124         glLoadIdentity();
125 }
126
127 static void drawAsset3D(const Asset3D* asset3D) {
128         if (asset3D == 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 < asset3D->numMeshes; ++meshIndex) {
136                 const Mesh mesh = asset3D->meshes[meshIndex];
137                 glBindTexture(GL_TEXTURE_2D,
138                               asset3D->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                                 unsigned int 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 }