18 static void cleanup();
19 static void display();
21 static void draw_scene();
22 static void reshape(int x, int y);
23 static void keyb(unsigned char key, int x, int y);
24 static void mouse(int bn, int st, int x, int y);
25 static void motion(int x, int y);
26 static void passive_motion(int x, int y);
28 static int win_width, win_height;
30 static float cam_dist = 0.25;
31 static float cam_theta, cam_phi = 20;
32 static int prev_mx, prev_my;
33 static bool bnstate[8];
35 static Mat4 view_matrix;
37 static unsigned int start_time, prev_msec;
39 static TextureSet texman;
42 int main(int argc, char **argv)
44 glutInitWindowSize(1024, 768);
45 glutInit(&argc, argv);
46 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
47 glutCreateWindow("demo");
49 glutDisplayFunc(display);
51 glutReshapeFunc(reshape);
52 glutKeyboardFunc(keyb);
54 glutMotionFunc(motion);
55 glutPassiveMotionFunc(passive_motion);
70 glEnable(GL_MULTISAMPLE);
71 glEnable(GL_DEPTH_TEST);
72 glEnable(GL_CULL_FACE);
73 glEnable(GL_LIGHTING);
74 glEnable(GL_NORMALIZE);
76 Mesh::use_custom_sdr_attr = false;
78 float ambient[] = {0.1, 0.1, 0.1, 0.0};
79 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
83 start_time = glutGet(GLUT_ELAPSED_TIME);
92 static void update(float dt)
97 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
99 unsigned int lt = GL_LIGHT0 + idx;
100 float posv[] = { pos.x, pos.y, pos.z, 1 };
101 float colv[] = { color.x, color.y, color.z, 1 };
104 glLightfv(lt, GL_POSITION, posv);
105 glLightfv(lt, GL_DIFFUSE, colv);
106 glLightfv(lt, GL_SPECULAR, colv);
109 static void display()
111 unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
112 float dt = (float)(msec - prev_msec) / 1000.0f;
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117 view_matrix = Mat4::identity;
118 view_matrix.pre_translate(0, 0, -cam_dist);
119 view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
120 view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
122 glMatrixMode(GL_MODELVIEW);
123 glLoadMatrixf(view_matrix[0]);
125 static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
126 set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
127 set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
128 set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
135 assert(glGetError() == GL_NO_ERROR);
143 static void draw_scene()
147 glVertex3f(-30, -10, 30);
148 glVertex3f(30, -10, 30);
149 glVertex3f(30, -10, -30);
150 glVertex3f(-30, -10, -30);
154 static void reshape(int x, int y)
159 glViewport(0, 0, x, y);
161 glMatrixMode(GL_PROJECTION);
163 gluPerspective(50.0, (float)x / (float)y, 0.01, 50.0);
166 static void keyb(unsigned char key, int x, int y)
174 static void mouse(int bn, int st, int x, int y)
176 int bidx = bn - GLUT_LEFT_BUTTON;
177 bool down = st == GLUT_DOWN;
181 bnstate[bidx] = down;
184 static void motion(int x, int y)
186 int dx = x - prev_mx;
187 int dy = y - prev_my;
191 if(!dx && !dy) return;
194 cam_theta += dx * 0.5;
197 if(cam_phi < -90) cam_phi = -90;
198 if(cam_phi > 90) cam_phi = 90;
202 cam_dist += dy * 0.01;
203 if(cam_dist < 0.0) cam_dist = 0.0;
208 static void passive_motion(int x, int y)