rayfract

diff src/rayfract.cc @ 6:8a9aa21b32cf

added spaceball support through GLUT
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 18 Jun 2011 01:04:25 +0300
parents 48e0e7d33d9e
children dfe7c98cf373
line diff
     1.1 --- a/src/rayfract.cc	Sat May 28 22:31:07 2011 +0300
     1.2 +++ b/src/rayfract.cc	Sat Jun 18 01:04:25 2011 +0300
     1.3 @@ -8,6 +8,8 @@
     1.4  #include "gui.h"
     1.5  #include "vmath.h"
     1.6  
     1.7 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
     1.8 +
     1.9  void disp();
    1.10  void reshape(int x, int y);
    1.11  void keyb(unsigned char key, int x, int y);
    1.12 @@ -15,6 +17,10 @@
    1.13  void mouse(int bn, int state, int x, int y);
    1.14  void motion(int x, int y);
    1.15  void passive_motion(int x, int y);
    1.16 +void sball_motion(int x, int y, int z);
    1.17 +void sball_rot(int x, int y, int z);
    1.18 +void sball_button(int bn, int state);
    1.19 +
    1.20  
    1.21  int load_shader();
    1.22  unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
    1.23 @@ -22,7 +28,7 @@
    1.24  static int round_pow2(int x);
    1.25  
    1.26  float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    1.27 -float cam_y = 0;
    1.28 +float cam_x, cam_y, cam_z;
    1.29  
    1.30  unsigned int sdr;
    1.31  unsigned int ray_tex;
    1.32 @@ -53,6 +59,9 @@
    1.33  	glutMouseFunc(mouse);
    1.34  	glutMotionFunc(motion);
    1.35  	glutPassiveMotionFunc(passive_motion);
    1.36 +	glutSpaceballMotionFunc(sball_motion);
    1.37 +	glutSpaceballRotateFunc(sball_rot);
    1.38 +	glutSpaceballButtonFunc(sball_button);
    1.39  
    1.40  	glEnable(GL_DEPTH_TEST);
    1.41  	glEnable(GL_LIGHTING);
    1.42 @@ -97,6 +106,7 @@
    1.43  
    1.44  	glMatrixMode(GL_MODELVIEW);
    1.45  	glLoadIdentity();
    1.46 +	glTranslatef(cam_x, cam_y, -cam_z);
    1.47  	glRotatef(cam_theta, 0, 1, 0);
    1.48  	glRotatef(cam_phi, 1, 0, 0);
    1.49  	glTranslatef(0, 0, -cam_dist);
    1.50 @@ -279,6 +289,7 @@
    1.51  
    1.52  	if(bnstate[2]) {
    1.53  		cam_dist += (y - prev_y) * 0.1;
    1.54 +		if(cam_dist < 0.0) cam_dist = 0.0;
    1.55  		glutPostRedisplay();
    1.56  	}
    1.57  
    1.58 @@ -295,6 +306,42 @@
    1.59  	glutPostRedisplay();
    1.60  }
    1.61  
    1.62 +void sball_motion(int x, int y, int z)
    1.63 +{
    1.64 +	float dx = (float)x * 0.0015f;
    1.65 +	float dy = (float)y * 0.0015f;
    1.66 +	float dz = -(float)z * 0.001f;
    1.67 +	float angle = -DEG_TO_RAD(cam_theta);
    1.68 +
    1.69 +	cam_x += cos(angle) * dx + sin(angle) * dz;
    1.70 +	cam_z += -sin(angle) * dx + cos(angle) * dz;
    1.71 +	cam_y += dy;
    1.72 +
    1.73 +	glutPostRedisplay();
    1.74 +}
    1.75 +
    1.76 +void sball_rot(int x, int y, int z)
    1.77 +{
    1.78 +	cam_theta += -y / 15.0;
    1.79 +	cam_phi += -x / 15.0;
    1.80 +	glutPostRedisplay();
    1.81 +}
    1.82 +
    1.83 +void sball_button(int bn, int state)
    1.84 +{
    1.85 +	if(state == GLUT_DOWN) {
    1.86 +		switch(bn) {
    1.87 +		case 0:
    1.88 +			/* TODO reset */
    1.89 +			break;
    1.90 +
    1.91 +		default:
    1.92 +			break;
    1.93 +		}
    1.94 +	}
    1.95 +}
    1.96 +
    1.97 +
    1.98  unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
    1.99  {
   1.100  	unsigned int tex;