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