symmetry

diff 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
line diff
     1.1 --- a/src/main.cc	Tue Feb 25 19:53:34 2014 +0200
     1.2 +++ b/src/main.cc	Tue Feb 25 23:47:20 2014 +0200
     1.3 @@ -2,13 +2,15 @@
     1.4  #include <stdlib.h>
     1.5  #include <assert.h>
     1.6  #include <vector>
     1.7 -#include "opengl.h"
     1.8 -#include "camera.h"
     1.9 +#include <goat3dgfx/goat3dgfx.h>
    1.10 +
    1.11 +using namespace goatgfx;
    1.12  
    1.13  static bool init();
    1.14  static void cleanup();
    1.15 +static void disp();
    1.16 +static void draw_scene(int which);
    1.17  static void handle_input(float dt);
    1.18 -static void disp();
    1.19  static void idle();
    1.20  static void reshape(int x, int y);
    1.21  static void keypress(unsigned char key, int x, int y);
    1.22 @@ -16,12 +18,14 @@
    1.23  static void mouse(int bn, int st, int x, int y);
    1.24  static void motion(int x, int y);
    1.25  
    1.26 -static goatgfx::VRFpsCamera cam;
    1.27 +static int win_width, win_height;
    1.28 +static float split_pos = 0.5;
    1.29 +static VRFpsCamera cam;
    1.30  static std::vector<bool> keystate(256);
    1.31  
    1.32  int main(int argc, char **argv)
    1.33  {
    1.34 -	glutInitWindowSize(800, 600);
    1.35 +	glutInitWindowSize(1024, 768);
    1.36  	glutInit(&argc, argv);
    1.37  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.38  	glutCreateWindow("symmetry");
    1.39 @@ -68,31 +72,73 @@
    1.40  	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
    1.41  	static unsigned int prev_msec;
    1.42  	float dt = (float)(msec - prev_msec) / 1000.0f;
    1.43 +	prev_msec = msec;
    1.44  
    1.45  	handle_input(dt);
    1.46  
    1.47  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.48  
    1.49 -	glMatrixMode(GL_MODELVIEW);
    1.50 -	glLoadIdentity();
    1.51  	cam.use_inverse();
    1.52 +	setup_gl_matrices();
    1.53  
    1.54 -	glBegin(GL_QUADS);
    1.55 -	glNormal3f(0, 1, 0);
    1.56 -	glVertex3f(-5, 0, 5);
    1.57 -	glVertex3f(5, 0, 5);
    1.58 -	glVertex3f(5, 0, -5);
    1.59 -	glVertex3f(-5, 0, -5);
    1.60 -	glEnd();
    1.61 +	float left_pixels = split_pos * win_width;
    1.62  
    1.63 -	glFrontFace(GL_CW);
    1.64 -	glutSolidTeapot(1.0);
    1.65 -	glFrontFace(GL_CCW);
    1.66 +	glEnable(GL_SCISSOR_TEST);
    1.67 +
    1.68 +	// draw left viewport
    1.69 +	if(left_pixels > 0) {
    1.70 +		glScissor(0, 0, left_pixels, win_height);
    1.71 +		draw_scene(0);
    1.72 +	}
    1.73 +
    1.74 +	// draw right viewport
    1.75 +	if(left_pixels < win_width) {
    1.76 +		glScissor(left_pixels, 0, win_width - left_pixels, win_height);
    1.77 +		draw_scene(1);
    1.78 +	}
    1.79 +
    1.80 +	glDisable(GL_SCISSOR_TEST);
    1.81  
    1.82  	glutSwapBuffers();
    1.83  	assert(glGetError() == GL_NO_ERROR);
    1.84  }
    1.85  
    1.86 +static void draw_scene(int which)
    1.87 +{
    1.88 +	glMatrixMode(GL_MODELVIEW);
    1.89 +
    1.90 +	float lpos[] = {-5, 20, 5, 1};
    1.91 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.92 +
    1.93 +	float color[][4] = {
    1.94 +		{1.0, 0.3, 0.2, 1.0},
    1.95 +		{0.2, 0.3, 1.0, 1.0}
    1.96 +	};
    1.97 +
    1.98 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color[which]);
    1.99 +
   1.100 +	glBegin(GL_QUADS);
   1.101 +	glNormal3f(0, 1, 0);
   1.102 +	glVertex3f(-20, 0, 20);
   1.103 +	glVertex3f(20, 0, 20);
   1.104 +	glVertex3f(20, 0, -20);
   1.105 +	glVertex3f(-20, 0, -20);
   1.106 +	glEnd();
   1.107 +
   1.108 +	for(int i=0; i<8; i++) {
   1.109 +		float theta = 360.0 * (float)i / 8.0;
   1.110 +		glPushMatrix();
   1.111 +		glRotatef(theta, 0, 1, 0);
   1.112 +		glTranslatef(0, 0, 10);
   1.113 +
   1.114 +		glTranslatef(0, 1, 0);
   1.115 +
   1.116 +		glFrontFace(GL_CW);
   1.117 +		glutSolidTeapot(1.0);
   1.118 +		glFrontFace(GL_CCW);
   1.119 +		glPopMatrix();
   1.120 +	}
   1.121 +}
   1.122  
   1.123  static void handle_input(float dt)
   1.124  {
   1.125 @@ -124,9 +170,12 @@
   1.126  {
   1.127  	glViewport(0, 0, x, y);
   1.128  
   1.129 -	glMatrixMode(GL_PROJECTION);
   1.130 -	glLoadIdentity();
   1.131 -	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
   1.132 +	Matrix4x4 proj;
   1.133 +	proj.set_perspective(DEG_TO_RAD(50), (float)x / (float)y, 0.5, 500.0);
   1.134 +	set_projection_matrix(proj);
   1.135 +
   1.136 +	win_width = x;
   1.137 +	win_height = y;
   1.138  }
   1.139  
   1.140  static void keypress(unsigned char key, int x, int y)
   1.141 @@ -136,6 +185,10 @@
   1.142  	switch(key) {
   1.143  	case 27:
   1.144  		exit(0);
   1.145 +
   1.146 +	case ' ':
   1.147 +		split_pos = 0.5;
   1.148 +		break;
   1.149  	}
   1.150  }
   1.151  
   1.152 @@ -165,6 +218,11 @@
   1.153  		return;
   1.154  	}
   1.155  
   1.156 +	if(keystate[(int)'\b']) {
   1.157 +		split_pos = (float)x / win_width;
   1.158 +		return;
   1.159 +	}
   1.160 +
   1.161  	if(bnstate[0]) {
   1.162  		float dtheta_deg = dy * 0.5;
   1.163  		float dphi_deg = dx * 0.5;