]> git.lizzy.rs Git - shadowclad.git/blob - render.c
Move assets into assets/ directory
[shadowclad.git] / render.c
1 #include <GL/glut.h>
2 #include <stdbool.h>
3
4 #include "assimp_types.h"
5
6 #include "level.h"
7 #include "performance.h"
8 #include "typedefs.h"
9
10 const float AXIS_RADIUS = 5.0f;
11
12 static void drawAxes();
13 static void renderBlockGrid(const BlockGrid grid);
14 static void drawBlock(const Block* block);
15
16
17
18 void initRender() {
19         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
20         
21         GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
22         GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
23         GLfloat light0_specular[] = {0.96f, 0.98f, 1.0f, 1.0f};
24         GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 0.0f}; // (w == 0.0f) == directional
25         
26         glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
27         glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
28         glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
29         glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
30         
31         glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
32         glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
33         glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
34 }
35
36 void renderScene() {
37         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
38         glLoadIdentity();
39         
40         glEnable(GL_NORMALIZE);
41         glEnable(GL_CULL_FACE);
42         glEnable(GL_DEPTH_TEST);
43         
44         glDisable(GL_LIGHTING);
45         drawAxes();
46         glEnable(GL_LIGHTING);
47         
48         glEnable(GL_LIGHT0);
49         glEnable(GL_TEXTURE_2D);
50         renderBlockGrid(levelGrid);
51         glDisable(GL_TEXTURE_2D);
52         glDisable(GL_LIGHT0);
53         
54         glFlush();
55         glutSwapBuffers();
56         frameRendered();
57         glutPostRedisplay();
58 }
59
60 static void drawAxes() {
61         point3f xAxisStart = { 0.0f, 0.0f, 0.0f };
62         point3f xAxisEnd = { AXIS_RADIUS, 0.0f, 0.0f };
63         point3f yAxisStart = { 0.0f, 0.0f, 0.0f };
64         point3f yAxisEnd = { 0.0f, AXIS_RADIUS, 0.0f };
65         point3f zAxisStart = { 0.0f, 0.0f, 0.0f };
66         point3f zAxisEnd = { 0.0f, 0.0f, AXIS_RADIUS };
67         
68         glColor3f(1.0f, 0.0f, 0.0f);
69         glBegin(GL_LINES);
70         glVertex3fv(xAxisStart);
71         glVertex3fv(xAxisEnd);
72         glEnd();
73         
74         glColor3f(0.0f, 1.0f, 0.0f);
75         glBegin(GL_LINES);
76         glVertex3fv(yAxisStart);
77         glVertex3fv(yAxisEnd);
78         glEnd();
79         
80         glColor3f(0.0f, 0.0f, 1.0f);
81         glBegin(GL_LINES);
82         glVertex3fv(zAxisStart);
83         glVertex3fv(zAxisEnd);
84         glEnd();
85 }
86
87 static void renderBlockGrid(const BlockGrid grid) {
88         glMatrixMode(GL_MODELVIEW);
89         for (int z = 0; z < grid.depth; ++z) {
90                 glLoadIdentity();
91                 glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
92                 for (int x = 0; x < grid.width; ++x) {
93                         drawBlock(getBlockFromGrid(grid, x, z));
94                         glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
95                 }
96         }
97         glLoadIdentity();
98 }
99
100 static void drawBlock(const Block* block) {
101         if (block->sceneData == NULL) {
102                 return;
103         }
104         
105         glColor3f(0.5f, 1.0f, 0.0f);
106         
107         for (int i = 0; i < block->sceneData->mNumMeshes; ++i) {
108                 glBindTexture(GL_TEXTURE_2D, block->textureIds[i]);
109                 const AiMesh* mesh = block->sceneData->mMeshes[i];
110                 bool hasNormals = mesh->mNormals != NULL;
111                 bool hasTextureCoords = mesh->mTextureCoords[0] != NULL;
112                 
113                 for (int k = 0; k < mesh->mNumFaces; ++k) {
114                         const AiFace face = mesh->mFaces[k];
115                         
116                         GLenum faceMode;
117                         switch (face.mNumIndices) {
118                                 case 1: faceMode = GL_POINTS; break;
119                                 case 2: faceMode = GL_LINES; break;
120                                 case 3: faceMode = GL_TRIANGLES; break;
121                                 default: faceMode = GL_POLYGON; break;
122                         }
123                         
124                         glBegin(faceMode);
125                         
126                         for (int l = 0; l < face.mNumIndices; ++l) {
127                                 unsigned int vertexIndex = face.mIndices[l];
128                                 if (hasNormals) {
129                                         if (hasTextureCoords) {
130                                                 AiVector3D coords = mesh->mTextureCoords[0][vertexIndex];
131                                                 glTexCoord2f(coords.x, coords.y);
132                                         }
133                                         glNormal3fv(&mesh->mNormals[vertexIndex].x);
134                                 }
135                                 glVertex3fv((const GLfloat*) &mesh->mVertices[vertexIndex]);
136                         }
137                         
138                         glEnd();
139                 }
140         }
141         glBindTexture(GL_TEXTURE_2D, 0);
142 }