symmetry
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.cc Tue Feb 25 19:53:34 2014 +0200 1.3 @@ -0,0 +1,174 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <assert.h> 1.7 +#include <vector> 1.8 +#include "opengl.h" 1.9 +#include "camera.h" 1.10 + 1.11 +static bool init(); 1.12 +static void cleanup(); 1.13 +static void handle_input(float dt); 1.14 +static void disp(); 1.15 +static void idle(); 1.16 +static void reshape(int x, int y); 1.17 +static void keypress(unsigned char key, int x, int y); 1.18 +static void keyrelease(unsigned char key, int x, int y); 1.19 +static void mouse(int bn, int st, int x, int y); 1.20 +static void motion(int x, int y); 1.21 + 1.22 +static goatgfx::VRFpsCamera cam; 1.23 +static std::vector<bool> keystate(256); 1.24 + 1.25 +int main(int argc, char **argv) 1.26 +{ 1.27 + glutInitWindowSize(800, 600); 1.28 + glutInit(&argc, argv); 1.29 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 1.30 + glutCreateWindow("symmetry"); 1.31 + 1.32 + glutDisplayFunc(disp); 1.33 + glutIdleFunc(idle); 1.34 + glutReshapeFunc(reshape); 1.35 + glutKeyboardFunc(keypress); 1.36 + glutKeyboardUpFunc(keyrelease); 1.37 + glutMouseFunc(mouse); 1.38 + glutMotionFunc(motion); 1.39 + 1.40 + if(!init()) { 1.41 + return 1; 1.42 + } 1.43 + atexit(cleanup); 1.44 + 1.45 + glutMainLoop(); 1.46 + return 0; 1.47 +} 1.48 + 1.49 + 1.50 +static bool init() 1.51 +{ 1.52 + glewInit(); 1.53 + 1.54 + glEnable(GL_DEPTH_TEST); 1.55 + glEnable(GL_CULL_FACE); 1.56 + 1.57 + glEnable(GL_LIGHTING); 1.58 + glEnable(GL_LIGHT0); 1.59 + 1.60 + cam.input_move(0, 1.65, 0); 1.61 + 1.62 + return true; 1.63 +} 1.64 + 1.65 +static void cleanup() 1.66 +{ 1.67 +} 1.68 + 1.69 +static void disp() 1.70 +{ 1.71 + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 1.72 + static unsigned int prev_msec; 1.73 + float dt = (float)(msec - prev_msec) / 1000.0f; 1.74 + 1.75 + handle_input(dt); 1.76 + 1.77 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.78 + 1.79 + glMatrixMode(GL_MODELVIEW); 1.80 + glLoadIdentity(); 1.81 + cam.use_inverse(); 1.82 + 1.83 + glBegin(GL_QUADS); 1.84 + glNormal3f(0, 1, 0); 1.85 + glVertex3f(-5, 0, 5); 1.86 + glVertex3f(5, 0, 5); 1.87 + glVertex3f(5, 0, -5); 1.88 + glVertex3f(-5, 0, -5); 1.89 + glEnd(); 1.90 + 1.91 + glFrontFace(GL_CW); 1.92 + glutSolidTeapot(1.0); 1.93 + glFrontFace(GL_CCW); 1.94 + 1.95 + glutSwapBuffers(); 1.96 + assert(glGetError() == GL_NO_ERROR); 1.97 +} 1.98 + 1.99 + 1.100 +static void handle_input(float dt) 1.101 +{ 1.102 + Vector3 inpv; 1.103 + float offs = dt * 2.0; 1.104 + 1.105 + if(keystate['w'] || keystate['W']) { 1.106 + inpv.z -= offs; 1.107 + } 1.108 + if(keystate['s'] || keystate['S']) { 1.109 + inpv.z += offs; 1.110 + } 1.111 + if(keystate['d'] || keystate['D']) { 1.112 + inpv.x += offs; 1.113 + } 1.114 + if(keystate['a'] || keystate['A']) { 1.115 + inpv.x -= offs; 1.116 + } 1.117 + 1.118 + cam.input_move(inpv.x, inpv.y, inpv.z); 1.119 +} 1.120 + 1.121 +static void idle() 1.122 +{ 1.123 + glutPostRedisplay(); 1.124 +} 1.125 + 1.126 +static void reshape(int x, int y) 1.127 +{ 1.128 + glViewport(0, 0, x, y); 1.129 + 1.130 + glMatrixMode(GL_PROJECTION); 1.131 + glLoadIdentity(); 1.132 + gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0); 1.133 +} 1.134 + 1.135 +static void keypress(unsigned char key, int x, int y) 1.136 +{ 1.137 + keystate[key] = true; 1.138 + 1.139 + switch(key) { 1.140 + case 27: 1.141 + exit(0); 1.142 + } 1.143 +} 1.144 + 1.145 +static void keyrelease(unsigned char key, int x, int y) 1.146 +{ 1.147 + keystate[key] = false; 1.148 +} 1.149 + 1.150 +static int prev_x, prev_y; 1.151 +static bool bnstate[32]; 1.152 + 1.153 +static void mouse(int bn, int st, int x, int y) 1.154 +{ 1.155 + prev_x = x; 1.156 + prev_y = y; 1.157 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.158 +} 1.159 + 1.160 +static void motion(int x, int y) 1.161 +{ 1.162 + int dx = x - prev_x; 1.163 + int dy = y - prev_y; 1.164 + prev_x = x; 1.165 + prev_y = y; 1.166 + 1.167 + if(!dx && !dy) { 1.168 + return; 1.169 + } 1.170 + 1.171 + if(bnstate[0]) { 1.172 + float dtheta_deg = dy * 0.5; 1.173 + float dphi_deg = dx * 0.5; 1.174 + 1.175 + cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0); 1.176 + } 1.177 +}