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