]> git.lizzy.rs Git - shadowclad.git/blob - render.c
Refactor code style
[shadowclad.git] / render.c
1 #include <GL/glut.h>
2 #include <assimp/scene.h>
3
4 #include "render.h"
5 #include "typedefs.h"
6
7 const float AXIS_RADIUS = 5.0f;
8
9 void renderScene() {
10         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
11         glLoadIdentity();
12         
13         drawAxes();
14         drawModelRecursive(model, (*model).mRootNode);
15         
16         glFlush();
17         glutSwapBuffers();
18 }
19
20 void drawAxes() {
21         point3f xAxisStart = { 0.0f, 0.0f, 0.0f };
22         point3f xAxisEnd = { AXIS_RADIUS, 0.0f, 0.0f };
23         point3f yAxisStart = { 0.0f, 0.0f, 0.0f };
24         point3f yAxisEnd = { 0.0f, AXIS_RADIUS, 0.0f };
25         point3f zAxisStart = { 0.0f, 0.0f, 0.0f };
26         point3f zAxisEnd = { 0.0f, 0.0f, AXIS_RADIUS };
27         
28         glColor3f(1.0f, 0.0f, 0.0f);
29         glBegin(GL_LINES);
30         glVertex3fv(xAxisStart);
31         glVertex3fv(xAxisEnd);
32         glEnd();
33         
34         glColor3f(0.0f, 1.0f, 0.0f);
35         glBegin(GL_LINES);
36         glVertex3fv(yAxisStart);
37         glVertex3fv(yAxisEnd);
38         glEnd();
39         
40         glColor3f(0.0f, 0.0f, 1.0f);
41         glBegin(GL_LINES);
42         glVertex3fv(zAxisStart);
43         glVertex3fv(zAxisEnd);
44         glEnd();
45 }
46
47 void drawModelRecursive(const struct aiScene* model, const struct aiNode* node) {
48         if (((*model).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
49                 return;
50         }
51         
52         for (int i = 0; i < (*node).mNumMeshes; ++i) {
53                 const struct aiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
54                 for (int k = 0; k < (*mesh).mNumFaces; ++k) {
55                         const struct aiFace face = (*mesh).mFaces[k];
56                         
57                         GLenum faceMode;
58                         switch (face.mNumIndices) {
59                                 case 1: faceMode = GL_POINTS; break;
60                                 case 2: faceMode = GL_LINES; break;
61                                 case 3: faceMode = GL_TRIANGLES; break;
62                                 default: faceMode = GL_POLYGON; break;
63                         }
64                         
65                         glBegin(faceMode);
66                         
67                         glColor3f(1.0f, 1.0f, 1.0f);
68                         for (int l = 0; l < face.mNumIndices; ++l) {
69                                 glVertex3fv((const GLfloat*) &(*mesh).mVertices[face.mIndices[l]]);
70                         }
71                         
72                         glEnd();
73                 }
74         }
75         
76         for (int i = 0; i < (*node).mNumChildren; ++i) {
77                 drawModelRecursive(model, (*node).mChildren[i]);
78         }
79 }