symmetry

annotate src/main.cc @ 1:46fe847bba08

using goat3dgfx
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Feb 2014 23:47:20 +0200
parents a90a71a74f0b
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <assert.h>
nuclear@0 4 #include <vector>
nuclear@1 5 #include <goat3dgfx/goat3dgfx.h>
nuclear@1 6
nuclear@1 7 using namespace goatgfx;
nuclear@0 8
nuclear@0 9 static bool init();
nuclear@0 10 static void cleanup();
nuclear@1 11 static void disp();
nuclear@1 12 static void draw_scene(int which);
nuclear@0 13 static void handle_input(float dt);
nuclear@0 14 static void idle();
nuclear@0 15 static void reshape(int x, int y);
nuclear@0 16 static void keypress(unsigned char key, int x, int y);
nuclear@0 17 static void keyrelease(unsigned char key, int x, int y);
nuclear@0 18 static void mouse(int bn, int st, int x, int y);
nuclear@0 19 static void motion(int x, int y);
nuclear@0 20
nuclear@1 21 static int win_width, win_height;
nuclear@1 22 static float split_pos = 0.5;
nuclear@1 23 static VRFpsCamera cam;
nuclear@0 24 static std::vector<bool> keystate(256);
nuclear@0 25
nuclear@0 26 int main(int argc, char **argv)
nuclear@0 27 {
nuclear@1 28 glutInitWindowSize(1024, 768);
nuclear@0 29 glutInit(&argc, argv);
nuclear@0 30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 31 glutCreateWindow("symmetry");
nuclear@0 32
nuclear@0 33 glutDisplayFunc(disp);
nuclear@0 34 glutIdleFunc(idle);
nuclear@0 35 glutReshapeFunc(reshape);
nuclear@0 36 glutKeyboardFunc(keypress);
nuclear@0 37 glutKeyboardUpFunc(keyrelease);
nuclear@0 38 glutMouseFunc(mouse);
nuclear@0 39 glutMotionFunc(motion);
nuclear@0 40
nuclear@0 41 if(!init()) {
nuclear@0 42 return 1;
nuclear@0 43 }
nuclear@0 44 atexit(cleanup);
nuclear@0 45
nuclear@0 46 glutMainLoop();
nuclear@0 47 return 0;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50
nuclear@0 51 static bool init()
nuclear@0 52 {
nuclear@0 53 glewInit();
nuclear@0 54
nuclear@0 55 glEnable(GL_DEPTH_TEST);
nuclear@0 56 glEnable(GL_CULL_FACE);
nuclear@0 57
nuclear@0 58 glEnable(GL_LIGHTING);
nuclear@0 59 glEnable(GL_LIGHT0);
nuclear@0 60
nuclear@0 61 cam.input_move(0, 1.65, 0);
nuclear@0 62
nuclear@0 63 return true;
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 static void cleanup()
nuclear@0 67 {
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 static void disp()
nuclear@0 71 {
nuclear@0 72 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@0 73 static unsigned int prev_msec;
nuclear@0 74 float dt = (float)(msec - prev_msec) / 1000.0f;
nuclear@1 75 prev_msec = msec;
nuclear@0 76
nuclear@0 77 handle_input(dt);
nuclear@0 78
nuclear@0 79 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 80
nuclear@0 81 cam.use_inverse();
nuclear@1 82 setup_gl_matrices();
nuclear@0 83
nuclear@1 84 float left_pixels = split_pos * win_width;
nuclear@0 85
nuclear@1 86 glEnable(GL_SCISSOR_TEST);
nuclear@1 87
nuclear@1 88 // draw left viewport
nuclear@1 89 if(left_pixels > 0) {
nuclear@1 90 glScissor(0, 0, left_pixels, win_height);
nuclear@1 91 draw_scene(0);
nuclear@1 92 }
nuclear@1 93
nuclear@1 94 // draw right viewport
nuclear@1 95 if(left_pixels < win_width) {
nuclear@1 96 glScissor(left_pixels, 0, win_width - left_pixels, win_height);
nuclear@1 97 draw_scene(1);
nuclear@1 98 }
nuclear@1 99
nuclear@1 100 glDisable(GL_SCISSOR_TEST);
nuclear@0 101
nuclear@0 102 glutSwapBuffers();
nuclear@0 103 assert(glGetError() == GL_NO_ERROR);
nuclear@0 104 }
nuclear@0 105
nuclear@1 106 static void draw_scene(int which)
nuclear@1 107 {
nuclear@1 108 glMatrixMode(GL_MODELVIEW);
nuclear@1 109
nuclear@1 110 float lpos[] = {-5, 20, 5, 1};
nuclear@1 111 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@1 112
nuclear@1 113 float color[][4] = {
nuclear@1 114 {1.0, 0.3, 0.2, 1.0},
nuclear@1 115 {0.2, 0.3, 1.0, 1.0}
nuclear@1 116 };
nuclear@1 117
nuclear@1 118 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color[which]);
nuclear@1 119
nuclear@1 120 glBegin(GL_QUADS);
nuclear@1 121 glNormal3f(0, 1, 0);
nuclear@1 122 glVertex3f(-20, 0, 20);
nuclear@1 123 glVertex3f(20, 0, 20);
nuclear@1 124 glVertex3f(20, 0, -20);
nuclear@1 125 glVertex3f(-20, 0, -20);
nuclear@1 126 glEnd();
nuclear@1 127
nuclear@1 128 for(int i=0; i<8; i++) {
nuclear@1 129 float theta = 360.0 * (float)i / 8.0;
nuclear@1 130 glPushMatrix();
nuclear@1 131 glRotatef(theta, 0, 1, 0);
nuclear@1 132 glTranslatef(0, 0, 10);
nuclear@1 133
nuclear@1 134 glTranslatef(0, 1, 0);
nuclear@1 135
nuclear@1 136 glFrontFace(GL_CW);
nuclear@1 137 glutSolidTeapot(1.0);
nuclear@1 138 glFrontFace(GL_CCW);
nuclear@1 139 glPopMatrix();
nuclear@1 140 }
nuclear@1 141 }
nuclear@0 142
nuclear@0 143 static void handle_input(float dt)
nuclear@0 144 {
nuclear@0 145 Vector3 inpv;
nuclear@0 146 float offs = dt * 2.0;
nuclear@0 147
nuclear@0 148 if(keystate['w'] || keystate['W']) {
nuclear@0 149 inpv.z -= offs;
nuclear@0 150 }
nuclear@0 151 if(keystate['s'] || keystate['S']) {
nuclear@0 152 inpv.z += offs;
nuclear@0 153 }
nuclear@0 154 if(keystate['d'] || keystate['D']) {
nuclear@0 155 inpv.x += offs;
nuclear@0 156 }
nuclear@0 157 if(keystate['a'] || keystate['A']) {
nuclear@0 158 inpv.x -= offs;
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 cam.input_move(inpv.x, inpv.y, inpv.z);
nuclear@0 162 }
nuclear@0 163
nuclear@0 164 static void idle()
nuclear@0 165 {
nuclear@0 166 glutPostRedisplay();
nuclear@0 167 }
nuclear@0 168
nuclear@0 169 static void reshape(int x, int y)
nuclear@0 170 {
nuclear@0 171 glViewport(0, 0, x, y);
nuclear@0 172
nuclear@1 173 Matrix4x4 proj;
nuclear@1 174 proj.set_perspective(DEG_TO_RAD(50), (float)x / (float)y, 0.5, 500.0);
nuclear@1 175 set_projection_matrix(proj);
nuclear@1 176
nuclear@1 177 win_width = x;
nuclear@1 178 win_height = y;
nuclear@0 179 }
nuclear@0 180
nuclear@0 181 static void keypress(unsigned char key, int x, int y)
nuclear@0 182 {
nuclear@0 183 keystate[key] = true;
nuclear@0 184
nuclear@0 185 switch(key) {
nuclear@0 186 case 27:
nuclear@0 187 exit(0);
nuclear@1 188
nuclear@1 189 case ' ':
nuclear@1 190 split_pos = 0.5;
nuclear@1 191 break;
nuclear@0 192 }
nuclear@0 193 }
nuclear@0 194
nuclear@0 195 static void keyrelease(unsigned char key, int x, int y)
nuclear@0 196 {
nuclear@0 197 keystate[key] = false;
nuclear@0 198 }
nuclear@0 199
nuclear@0 200 static int prev_x, prev_y;
nuclear@0 201 static bool bnstate[32];
nuclear@0 202
nuclear@0 203 static void mouse(int bn, int st, int x, int y)
nuclear@0 204 {
nuclear@0 205 prev_x = x;
nuclear@0 206 prev_y = y;
nuclear@0 207 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
nuclear@0 208 }
nuclear@0 209
nuclear@0 210 static void motion(int x, int y)
nuclear@0 211 {
nuclear@0 212 int dx = x - prev_x;
nuclear@0 213 int dy = y - prev_y;
nuclear@0 214 prev_x = x;
nuclear@0 215 prev_y = y;
nuclear@0 216
nuclear@0 217 if(!dx && !dy) {
nuclear@0 218 return;
nuclear@0 219 }
nuclear@0 220
nuclear@1 221 if(keystate[(int)'\b']) {
nuclear@1 222 split_pos = (float)x / win_width;
nuclear@1 223 return;
nuclear@1 224 }
nuclear@1 225
nuclear@0 226 if(bnstate[0]) {
nuclear@0 227 float dtheta_deg = dy * 0.5;
nuclear@0 228 float dphi_deg = dx * 0.5;
nuclear@0 229
nuclear@0 230 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
nuclear@0 231 }
nuclear@0 232 }