]> git.lizzy.rs Git - shadowclad.git/blob - render.c
Render loaded aiScene primitively
[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 render_scene() {
10         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
11         glLoadIdentity();
12         
13         draw_axes();
14         draw_model_recursive(model, (*model).mRootNode);
15         
16         glFlush();
17         glutSwapBuffers();
18 }
19
20 void draw_axes() {
21         point3f x_axis_start = { 0.0f, 0.0f, 0.0f };
22         point3f x_axis_end = { AXIS_RADIUS, 0.0f, 0.0f };
23         point3f y_axis_start = { 0.0f, 0.0f, 0.0f };
24         point3f y_axis_end = { 0.0f, AXIS_RADIUS, 0.0f };
25         point3f z_axis_start = { 0.0f, 0.0f, 0.0f };
26         point3f z_axis_end = { 0.0f, 0.0f, AXIS_RADIUS };
27         
28         glColor3f(1.0f, 0.0f, 0.0f);
29         glBegin(GL_LINES);
30         glVertex3fv(x_axis_start);
31         glVertex3fv(x_axis_end);
32         glEnd();
33         
34         glColor3f(0.0f, 1.0f, 0.0f);
35         glBegin(GL_LINES);
36         glVertex3fv(y_axis_start);
37         glVertex3fv(y_axis_end);
38         glEnd();
39         
40         glColor3f(0.0f, 0.0f, 1.0f);
41         glBegin(GL_LINES);
42         glVertex3fv(z_axis_start);
43         glVertex3fv(z_axis_end);
44         glEnd();
45 }
46
47 void draw_model_recursive(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 face_mode;
58                         switch(face.mNumIndices) {
59                                 case 1: face_mode = GL_POINTS; break;
60                                 case 2: face_mode = GL_LINES; break;
61                                 case 3: face_mode = GL_TRIANGLES; break;
62                                 default: face_mode = GL_POLYGON; break;
63                         }
64                         
65                         glBegin(face_mode);
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                 draw_model_recursive(model, (*node).mChildren[i]);
78         }
79 }