clray

diff src/clray.cc @ 8:deaf85acf6af

interactive spheres
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 Jul 2010 19:48:43 +0100
parents 88ac4eb2d18a
children 85fd61f374d9
line diff
     1.1 --- a/src/clray.cc	Fri Jul 23 01:22:03 2010 +0100
     1.2 +++ b/src/clray.cc	Fri Jul 23 19:48:43 2010 +0100
     1.3 @@ -21,6 +21,9 @@
     1.4  static int xsz, ysz;
     1.5  static bool need_update = true;
     1.6  
     1.7 +static float cam_theta, cam_phi = 25.0;
     1.8 +static float cam_dist = 10.0;
     1.9 +
    1.10  int main(int argc, char **argv)
    1.11  {
    1.12  	glutInitWindowSize(800, 600);
    1.13 @@ -63,7 +66,21 @@
    1.14  
    1.15  void disp()
    1.16  {
    1.17 +	glMatrixMode(GL_MODELVIEW);
    1.18 +	glLoadIdentity();
    1.19 +
    1.20  	if(need_update) {
    1.21 +		float mat[16];
    1.22 +
    1.23 +		glPushMatrix();
    1.24 +		glRotatef(cam_theta, 0, 1, 0);
    1.25 +		glRotatef(cam_phi, 1, 0, 0);
    1.26 +		glTranslatef(0, 0, -cam_dist);
    1.27 +
    1.28 +		glGetFloatv(GL_MODELVIEW_MATRIX, mat);
    1.29 +		set_xform(mat);
    1.30 +		glPopMatrix();
    1.31 +
    1.32  		render();
    1.33  		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGBA, GL_FLOAT, fb);
    1.34  		need_update = false;
    1.35 @@ -115,12 +132,47 @@
    1.36  	}
    1.37  }
    1.38  
    1.39 +static bool bnstate[32];
    1.40 +static int prev_x, prev_y;
    1.41 +
    1.42  void mouse(int bn, int state, int x, int y)
    1.43  {
    1.44 +	if(state == GLUT_DOWN) {
    1.45 +		prev_x = x;
    1.46 +		prev_y = y;
    1.47 +		bnstate[bn] = true;
    1.48 +	} else {
    1.49 +		bnstate[bn] = false;
    1.50 +	}
    1.51  }
    1.52  
    1.53 +#define ROT_SCALE	0.5
    1.54 +#define PAN_SCALE	0.1
    1.55 +
    1.56  void motion(int x, int y)
    1.57  {
    1.58 +	int dx = x - prev_x;
    1.59 +	int dy = y - prev_y;
    1.60 +	prev_x = x;
    1.61 +	prev_y = y;
    1.62 +
    1.63 +	if(bnstate[0]) {
    1.64 +		cam_theta += dx * ROT_SCALE;
    1.65 +		cam_phi += dy * ROT_SCALE;
    1.66 +
    1.67 +		if(cam_phi < -89) cam_phi = 89;
    1.68 +		if(cam_phi > 89) cam_phi = 89;
    1.69 +
    1.70 +		need_update = true;
    1.71 +		glutPostRedisplay();
    1.72 +	}
    1.73 +	if(bnstate[2]) {
    1.74 +		cam_dist += dy * PAN_SCALE;
    1.75 +		if(cam_dist < 0) cam_dist = 0;
    1.76 +
    1.77 +		need_update = true;
    1.78 +		glutPostRedisplay();
    1.79 +	}
    1.80  }
    1.81  
    1.82  bool write_ppm(const char *fname, float *fb, int xsz, int ysz)