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