symmetry
view src/main.cc @ 0:a90a71a74f0b
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Feb 2014 19:53:34 +0200 |
parents | |
children | 46fe847bba08 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
5 #include "opengl.h"
6 #include "camera.h"
8 static bool init();
9 static void cleanup();
10 static void handle_input(float dt);
11 static void disp();
12 static void idle();
13 static void reshape(int x, int y);
14 static void keypress(unsigned char key, int x, int y);
15 static void keyrelease(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);
19 static goatgfx::VRFpsCamera cam;
20 static std::vector<bool> keystate(256);
22 int main(int argc, char **argv)
23 {
24 glutInitWindowSize(800, 600);
25 glutInit(&argc, argv);
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
27 glutCreateWindow("symmetry");
29 glutDisplayFunc(disp);
30 glutIdleFunc(idle);
31 glutReshapeFunc(reshape);
32 glutKeyboardFunc(keypress);
33 glutKeyboardUpFunc(keyrelease);
34 glutMouseFunc(mouse);
35 glutMotionFunc(motion);
37 if(!init()) {
38 return 1;
39 }
40 atexit(cleanup);
42 glutMainLoop();
43 return 0;
44 }
47 static bool init()
48 {
49 glewInit();
51 glEnable(GL_DEPTH_TEST);
52 glEnable(GL_CULL_FACE);
54 glEnable(GL_LIGHTING);
55 glEnable(GL_LIGHT0);
57 cam.input_move(0, 1.65, 0);
59 return true;
60 }
62 static void cleanup()
63 {
64 }
66 static void disp()
67 {
68 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
69 static unsigned int prev_msec;
70 float dt = (float)(msec - prev_msec) / 1000.0f;
72 handle_input(dt);
74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76 glMatrixMode(GL_MODELVIEW);
77 glLoadIdentity();
78 cam.use_inverse();
80 glBegin(GL_QUADS);
81 glNormal3f(0, 1, 0);
82 glVertex3f(-5, 0, 5);
83 glVertex3f(5, 0, 5);
84 glVertex3f(5, 0, -5);
85 glVertex3f(-5, 0, -5);
86 glEnd();
88 glFrontFace(GL_CW);
89 glutSolidTeapot(1.0);
90 glFrontFace(GL_CCW);
92 glutSwapBuffers();
93 assert(glGetError() == GL_NO_ERROR);
94 }
97 static void handle_input(float dt)
98 {
99 Vector3 inpv;
100 float offs = dt * 2.0;
102 if(keystate['w'] || keystate['W']) {
103 inpv.z -= offs;
104 }
105 if(keystate['s'] || keystate['S']) {
106 inpv.z += offs;
107 }
108 if(keystate['d'] || keystate['D']) {
109 inpv.x += offs;
110 }
111 if(keystate['a'] || keystate['A']) {
112 inpv.x -= offs;
113 }
115 cam.input_move(inpv.x, inpv.y, inpv.z);
116 }
118 static void idle()
119 {
120 glutPostRedisplay();
121 }
123 static void reshape(int x, int y)
124 {
125 glViewport(0, 0, x, y);
127 glMatrixMode(GL_PROJECTION);
128 glLoadIdentity();
129 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
130 }
132 static void keypress(unsigned char key, int x, int y)
133 {
134 keystate[key] = true;
136 switch(key) {
137 case 27:
138 exit(0);
139 }
140 }
142 static void keyrelease(unsigned char key, int x, int y)
143 {
144 keystate[key] = false;
145 }
147 static int prev_x, prev_y;
148 static bool bnstate[32];
150 static void mouse(int bn, int st, int x, int y)
151 {
152 prev_x = x;
153 prev_y = y;
154 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
155 }
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;
164 if(!dx && !dy) {
165 return;
166 }
168 if(bnstate[0]) {
169 float dtheta_deg = dy * 0.5;
170 float dphi_deg = dx * 0.5;
172 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
173 }
174 }