750a2d893f17899e861fea4a1be70aaf0acd14ad
[hair] / src / main.cc
1 #include <GL/glew.h>
2 #include <GL/glut.h>
3
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string>
8
9 #include "mesh.h"
10 #include "hair.h"
11
12 #define MAX_NUM_SPAWNS 500
13
14 static bool init();
15 static void cleanup();
16 static void display();
17 static void reshape(int x, int y);
18 static void keydown(unsigned char key, int x, int y);
19 static void mouse(int bn, int st, int x, int y);
20 static void motion(int x, int y);
21
22 static std::vector<Mesh*> meshes;
23 static Mesh *mesh_head;
24 static Hair hair;
25
26 int win_width, win_height;
27 float cam_theta, cam_phi = 25, cam_dist = 8;
28
29 int main(int argc, char **argv)
30 {
31         glutInit(&argc, argv);
32         glutInitWindowSize(800, 600);
33         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
34         glutCreateWindow("hair test");
35
36         glutDisplayFunc(display);
37         glutReshapeFunc(reshape);
38         glutKeyboardFunc(keydown);
39         glutMouseFunc(mouse);
40         glutMotionFunc(motion);
41
42         if(!init()) {
43                 return 1;
44         }
45         atexit(cleanup);
46
47         glutMainLoop();
48         return 0;
49 }
50
51 static bool init()
52 {
53         glewInit();
54
55         glEnable(GL_DEPTH_TEST);
56         glEnable(GL_CULL_FACE);
57         glEnable(GL_COLOR_MATERIAL);
58
59         glEnable(GL_LIGHTING);
60         glEnable(GL_LIGHT0);
61
62         glClearColor(0.5, 0.5, 0.5, 1);
63         meshes = load_meshes("data/head.fbx");
64         if (meshes.empty()) {
65                 fprintf(stderr, "Failed to load mesh.\n");
66                 return false;
67         }
68
69         for(size_t i=0; i<meshes.size(); i++) {
70                 meshes[i]->calc_bbox();
71
72                 Vec3 v0 = meshes[i]->bbox.v0;
73                 Vec3 v1 = meshes[i]->bbox.v1;
74
75                 printf("mesh: %s\n", meshes[i]->name.c_str());
76                 printf("AABB mesh %d: v0: (%f, %f, %f) v1: (%f, %f, %f)\n",
77                                 (int)i, v0.x, v0.y, v0.z, v1.x, v1.y, v1.z);
78
79                 meshes[i]->update_vbo(MESH_ALL);
80                 printf("num vertices: %d num triangles: %d\n",
81                                 (int)meshes[i]->vertices.size(),
82                                 (int)meshes[i]->indices.size() / 3);
83
84                 if(meshes[i]->name == "head") {
85                         mesh_head = meshes[i];
86                 }
87         }
88         if(!mesh_head) {
89                 fprintf(stderr, "Failed to find the head mesh.\n");
90                 return false;
91         }
92
93         if(!hair.init(mesh_head, MAX_NUM_SPAWNS, 0.1)) {
94                 fprintf(stderr, "Failed to initialize hair\n");
95                 return false;
96         }
97
98         return true;
99 }
100
101 static void cleanup()
102 {
103         for(size_t i=0; i<meshes.size(); i++) {
104                 delete meshes[i];
105         }
106 }
107
108 static void display()
109 {
110         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111
112         glMatrixMode(GL_MODELVIEW);
113         glLoadIdentity();
114         glTranslatef(0, 0, -cam_dist);
115         glRotatef(cam_phi, 1, 0, 0);
116         glRotatef(cam_theta, 0, 1, 0);
117
118         for(size_t i=0; i<meshes.size(); i++) {
119                 meshes[i]->draw();
120         }
121
122         hair.draw();
123
124         glutSwapBuffers();
125         assert(glGetError() == GL_NO_ERROR);
126 }
127
128 static void reshape(int x, int y)
129 {
130         glViewport(0, 0, x, y);
131         win_width = x;
132         win_height = y;
133
134         glMatrixMode(GL_PROJECTION);
135         glLoadIdentity();
136         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
137 }
138
139 static void keydown(unsigned char key, int /*x*/, int /*y*/)
140 {
141         switch(key) {
142         case 27:
143                 exit(0);
144         }
145 }
146
147 bool bnstate[8];
148 int prev_x, prev_y;
149
150 static void mouse(int bn, int st, int x, int y)
151 {
152         bnstate[bn] = st == GLUT_DOWN;
153         prev_x = x;
154         prev_y = y;
155 }
156
157 static void motion(int x, int y)
158 {
159         int dx = x - prev_x;
160         int dy = y - prev_y;
161         prev_x = x;
162         prev_y = y;
163
164         if(!dx && !dy) return;
165
166         if(bnstate[0]) {
167                 cam_theta += dx * 0.5;
168                 cam_phi += dy * 0.5;
169
170                 if(cam_phi < -90) cam_phi = -90;
171                 if(cam_phi > 90) cam_phi = 90;
172                 glutPostRedisplay();
173         }
174         if(bnstate[2]) {
175                 cam_dist += dy * 0.1;
176                 if(cam_dist < 0) cam_dist = 0;
177                 glutPostRedisplay();
178         }
179 }