metasurf

diff examples/metaballs/src/metaballs.c @ 2:9ab057fba0c5

added copyright statements, added comments, yada yada
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 08:59:07 +0300
parents dc0e882ec3f9
children 52664d3451ad
line diff
     1.1 --- a/examples/metaballs/src/metaballs.c	Tue Oct 25 07:57:07 2011 +0300
     1.2 +++ b/examples/metaballs/src/metaballs.c	Tue Oct 25 08:59:07 2011 +0300
     1.3 @@ -3,7 +3,11 @@
     1.4  #include <math.h>
     1.5  #include <assert.h>
     1.6  
     1.7 +#ifndef NO_SHADERS
     1.8  #include <GL/glew.h>
     1.9 +#include "sdr.h"
    1.10 +#endif
    1.11 +
    1.12  #ifndef __APPLE__
    1.13  #include <GL/glut.h>
    1.14  #else
    1.15 @@ -11,7 +15,6 @@
    1.16  #endif
    1.17  
    1.18  #include "cam.h"
    1.19 -#include "sdr.h"
    1.20  #include "metasurf.h"
    1.21  
    1.22  #define RES		38
    1.23 @@ -40,10 +43,14 @@
    1.24  void sball_motion(int x, int y, int z);
    1.25  int parse_args(int argc, char **argv);
    1.26  
    1.27 -int stereo;
    1.28 +int stereo, fullscreen;
    1.29 +int orig_xsz, orig_ysz;
    1.30 +
    1.31  struct metasurface *msurf;
    1.32  float threshold = 12;
    1.33 +#ifndef NO_SHADERS
    1.34  unsigned int sdr;
    1.35 +#endif
    1.36  int bidx = 1;
    1.37  
    1.38  int main(int argc, char **argv)
    1.39 @@ -60,6 +67,13 @@
    1.40  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
    1.41  	glutCreateWindow("metasurf");
    1.42  
    1.43 +	orig_xsz = glutGet(GLUT_WINDOW_WIDTH);
    1.44 +	orig_ysz = glutGet(GLUT_WINDOW_HEIGHT);
    1.45 +
    1.46 +	if(fullscreen) {
    1.47 +		glutFullScreen();
    1.48 +	}
    1.49 +
    1.50  	glutDisplayFunc(disp);
    1.51  	glutReshapeFunc(reshape);
    1.52  	glutKeyboardFunc(keyb);
    1.53 @@ -68,7 +82,12 @@
    1.54  	glutSpaceballButtonFunc(sball_button);
    1.55  	glutSpaceballMotionFunc(sball_motion);
    1.56  
    1.57 +#ifndef NO_SHADERS
    1.58  	glewInit();
    1.59 +	if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) {
    1.60 +		return 1;
    1.61 +	}
    1.62 +#endif
    1.63  
    1.64  	glEnable(GL_CULL_FACE);
    1.65  	glEnable(GL_DEPTH_TEST);
    1.66 @@ -95,10 +114,6 @@
    1.67  
    1.68  	glClearColor(0.8, 0.8, 0.8, 1.0);
    1.69  
    1.70 -	if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) {
    1.71 -		return 1;
    1.72 -	}
    1.73 -
    1.74  	glutMainLoop();
    1.75  	return 0;
    1.76  }
    1.77 @@ -143,7 +158,9 @@
    1.78  	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
    1.79  	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
    1.80  
    1.81 +#ifndef NO_SHADERS
    1.82  	bind_program(sdr);
    1.83 +#endif
    1.84  
    1.85  	glBegin(GL_TRIANGLES);
    1.86  	msurf_polygonize(msurf);
    1.87 @@ -201,6 +218,15 @@
    1.88  	case 27:
    1.89  		exit(0);
    1.90  
    1.91 +	case 'f':
    1.92 +		fullscreen = !fullscreen;
    1.93 +		if(fullscreen) {
    1.94 +			glutFullScreen();
    1.95 +		} else {
    1.96 +			glutReshapeWindow(orig_xsz, orig_ysz);
    1.97 +		}
    1.98 +		break;
    1.99 +
   1.100  	case 's':
   1.101  		stereo = !stereo;
   1.102  		glutPostRedisplay();
   1.103 @@ -254,14 +280,18 @@
   1.104  	prev_x = x;
   1.105  	prev_y = y;
   1.106  
   1.107 -	if(bnstate[GLUT_LEFT_BUTTON]) {
   1.108 -		cam_inp_rotate(dx, dy);
   1.109 -		glutPostRedisplay();
   1.110 +	if(glutGetModifiers()) {
   1.111 +		if(bnstate[GLUT_LEFT_BUTTON]) {
   1.112 +			cam_inp_rotate(dx, dy);
   1.113 +		}
   1.114 +		if(bnstate[GLUT_RIGHT_BUTTON]) {
   1.115 +			cam_inp_zoom(dy);
   1.116 +		}
   1.117 +	} else {
   1.118 +		mball[bidx].x += (float)dx * 0.005;
   1.119 +		mball[bidx].y -= (float)dy * 0.005;
   1.120  	}
   1.121 -	if(bnstate[GLUT_RIGHT_BUTTON]) {
   1.122 -		cam_inp_zoom(dy);
   1.123 -		glutPostRedisplay();
   1.124 -	}
   1.125 +	glutPostRedisplay();
   1.126  }
   1.127  
   1.128  void sball_button(int bn, int state)
   1.129 @@ -277,9 +307,9 @@
   1.130  
   1.131  void sball_motion(int x, int y, int z)
   1.132  {
   1.133 -	mball[bidx].x += (float)x / 32768.0;
   1.134 -	mball[bidx].y += (float)y / 32768.0;
   1.135 -	mball[bidx].z -= (float)z / 32768.0;
   1.136 +	mball[bidx].x += (float)x / 16384.0;
   1.137 +	mball[bidx].y += (float)y / 16384.0;
   1.138 +	mball[bidx].z -= (float)z / 16384.0;
   1.139  	glutPostRedisplay();
   1.140  }
   1.141  
   1.142 @@ -290,10 +320,22 @@
   1.143  	for(i=1; i<argc; i++) {
   1.144  		if(argv[i][0] == '-' && argv[i][2] == 0) {
   1.145  			switch(argv[i][1]) {
   1.146 +			case 'f':
   1.147 +				fullscreen = !fullscreen;
   1.148 +				break;
   1.149 +
   1.150  			case 's':
   1.151  				stereo = !stereo;
   1.152  				break;
   1.153  
   1.154 +			case 'h':
   1.155 +				printf("usage: %s [opt]\n", argv[0]);
   1.156 +				printf("options:\n");
   1.157 +				printf("  -f    start in fullscreen\n");
   1.158 +				printf("  -s    enable stereoscopic rendering\n");
   1.159 +				printf("  -h    print usage and exit\n");
   1.160 +				exit(0);
   1.161 +
   1.162  			default:
   1.163  				fprintf(stderr, "unrecognized option: %s\n", argv[i]);
   1.164  				return -1;