metasurf

annotate examples/metaballs/src/test.c @ 0:7aa4627e492b

first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 07:34:31 +0300
parents
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <math.h>
nuclear@0 4 #include <assert.h>
nuclear@0 5
nuclear@0 6 #include <GL/glew.h>
nuclear@0 7 #ifndef __APPLE__
nuclear@0 8 #include <GL/glut.h>
nuclear@0 9 #else
nuclear@0 10 #include <GLUT/glut.h>
nuclear@0 11 #endif
nuclear@0 12
nuclear@0 13 #include "cam.h"
nuclear@0 14 #include "sdr.h"
nuclear@0 15 #include "metasurf.h"
nuclear@0 16
nuclear@0 17 #define RES 38
nuclear@0 18
nuclear@0 19 struct metaball {
nuclear@0 20 float energy;
nuclear@0 21 float x, y, z;
nuclear@0 22 } mball[] = {
nuclear@0 23 {1.0, 0, 0, 0},
nuclear@0 24 {0.25, 0.45, 0, 0.25},
nuclear@0 25 {0.15, -0.3, 0.2, 0.1}
nuclear@0 26 };
nuclear@0 27
nuclear@0 28 int num_mballs = sizeof mball / sizeof *mball;
nuclear@0 29
nuclear@0 30 float eval(float x, float y, float z);
nuclear@0 31 float eval_cached(float x, float y, float z);
nuclear@0 32 void vertex(float x, float y, float z);
nuclear@0 33 void render(void);
nuclear@0 34 void disp(void);
nuclear@0 35 void reshape(int x, int y);
nuclear@0 36 void keyb(unsigned char key, int x, int y);
nuclear@0 37 void mouse(int bn, int state, int x, int y);
nuclear@0 38 void motion(int x, int y);
nuclear@0 39 void sball_button(int bn, int state);
nuclear@0 40 void sball_motion(int x, int y, int z);
nuclear@0 41 int parse_args(int argc, char **argv);
nuclear@0 42
nuclear@0 43 int stereo;
nuclear@0 44 struct metasurface *msurf;
nuclear@0 45 float threshold = 12;
nuclear@0 46 unsigned int sdr;
nuclear@0 47 int bidx = 1;
nuclear@0 48
nuclear@0 49 int main(int argc, char **argv)
nuclear@0 50 {
nuclear@0 51 float amb[] = {0, 0, 0, 0};
nuclear@0 52 float lpos[] = {-0.2, 0.2, 1, 0};
nuclear@0 53
nuclear@0 54 glutInitWindowSize(1280, 720);
nuclear@0 55 glutInit(&argc, argv);
nuclear@0 56
nuclear@0 57 if(parse_args(argc, argv) == -1) {
nuclear@0 58 return 1;
nuclear@0 59 }
nuclear@0 60 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
nuclear@0 61 glutCreateWindow("metasurf");
nuclear@0 62
nuclear@0 63 glutDisplayFunc(disp);
nuclear@0 64 glutReshapeFunc(reshape);
nuclear@0 65 glutKeyboardFunc(keyb);
nuclear@0 66 glutMouseFunc(mouse);
nuclear@0 67 glutMotionFunc(motion);
nuclear@0 68 glutSpaceballButtonFunc(sball_button);
nuclear@0 69 glutSpaceballMotionFunc(sball_motion);
nuclear@0 70
nuclear@0 71 glewInit();
nuclear@0 72
nuclear@0 73 glEnable(GL_CULL_FACE);
nuclear@0 74 glEnable(GL_DEPTH_TEST);
nuclear@0 75
nuclear@0 76 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
nuclear@0 77
nuclear@0 78 glEnable(GL_LIGHTING);
nuclear@0 79 glEnable(GL_LIGHT0);
nuclear@0 80 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 81
nuclear@0 82 glEnable(GL_NORMALIZE);
nuclear@0 83
nuclear@0 84 cam_focus_dist(2.0);
nuclear@0 85 cam_clip(0.1, 200.0);
nuclear@0 86 cam_rotate(0, 0);
nuclear@0 87 cam_dolly(2);
nuclear@0 88
nuclear@0 89 msurf = msurf_create();
nuclear@0 90 msurf_eval_func(msurf, eval);
nuclear@0 91 msurf_vertex_func(msurf, vertex);
nuclear@0 92 msurf_threshold(msurf, threshold);
nuclear@0 93 msurf_resolution(msurf, RES, RES, RES);
nuclear@0 94 msurf_bounds(msurf, -1, -1, -1, 1, 1, 1);
nuclear@0 95
nuclear@0 96 glClearColor(0.8, 0.8, 0.8, 1.0);
nuclear@0 97
nuclear@0 98 if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) {
nuclear@0 99 return 1;
nuclear@0 100 }
nuclear@0 101
nuclear@0 102 glutMainLoop();
nuclear@0 103 return 0;
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 float eval(float x, float y, float z)
nuclear@0 107 {
nuclear@0 108 int i;
nuclear@0 109 float val = 0.0f;
nuclear@0 110
nuclear@0 111 for(i=0; i<num_mballs; i++) {
nuclear@0 112 float dx = mball[i].x - x;
nuclear@0 113 float dy = mball[i].y - y;
nuclear@0 114 float dz = mball[i].z - z;
nuclear@0 115 float dist_sq = dx * dx + dy * dy + dz * dz;
nuclear@0 116
nuclear@0 117 if(dist_sq < 1e-6) {
nuclear@0 118 val += 100.0;
nuclear@0 119 } else {
nuclear@0 120 val += mball[i].energy / dist_sq;
nuclear@0 121 }
nuclear@0 122 }
nuclear@0 123 return val;
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 void vertex(float x, float y, float z)
nuclear@0 127 {
nuclear@0 128 const float dt = 0.001;
nuclear@0 129 float dfdx = eval(x - dt, y, z) - eval(x + dt, y, z);
nuclear@0 130 float dfdy = eval(x, y - dt, z) - eval(x, y + dt, z);
nuclear@0 131 float dfdz = eval(x, y, z - dt) - eval(x, y, z + dt);
nuclear@0 132
nuclear@0 133 glNormal3f(dfdx, dfdy, dfdz);
nuclear@0 134 glVertex3f(x, y, z);
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 void render(void)
nuclear@0 138 {
nuclear@0 139 float kd[] = {0.7, 0.28, 0.2, 1.0};
nuclear@0 140 float ks[] = {0.9, 0.9, 0.9, 1.0};
nuclear@0 141
nuclear@0 142 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, kd);
nuclear@0 143 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
nuclear@0 144 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@0 145
nuclear@0 146 bind_program(sdr);
nuclear@0 147
nuclear@0 148 glBegin(GL_TRIANGLES);
nuclear@0 149 msurf_polygonize(msurf);
nuclear@0 150 glEnd();
nuclear@0 151
nuclear@0 152 assert(glGetError() == GL_NO_ERROR);
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 void disp(void)
nuclear@0 156 {
nuclear@0 157 if(stereo) {
nuclear@0 158 glDrawBuffer(GL_BACK_LEFT);
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 162
nuclear@0 163 glMatrixMode(GL_PROJECTION);
nuclear@0 164 glLoadIdentity();
nuclear@0 165 cam_stereo_proj_matrix(stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 166
nuclear@0 167 glMatrixMode(GL_MODELVIEW);
nuclear@0 168 glLoadIdentity();
nuclear@0 169 cam_stereo_view_matrix(stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 170
nuclear@0 171 render();
nuclear@0 172
nuclear@0 173 if(stereo) {
nuclear@0 174 glDrawBuffer(GL_BACK_RIGHT);
nuclear@0 175 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 176
nuclear@0 177 glMatrixMode(GL_PROJECTION);
nuclear@0 178 glLoadIdentity();
nuclear@0 179 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@0 180
nuclear@0 181 glMatrixMode(GL_MODELVIEW);
nuclear@0 182 glLoadIdentity();
nuclear@0 183 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@0 184
nuclear@0 185 render();
nuclear@0 186 }
nuclear@0 187 glutSwapBuffers();
nuclear@0 188 }
nuclear@0 189
nuclear@0 190 void reshape(int x, int y)
nuclear@0 191 {
nuclear@0 192 glViewport(0, 0, x, y);
nuclear@0 193 cam_aspect((float)x / (float)y);
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 void keyb(unsigned char key, int x, int y)
nuclear@0 197 {
nuclear@0 198 static int wire;
nuclear@0 199
nuclear@0 200 switch(key) {
nuclear@0 201 case 27:
nuclear@0 202 exit(0);
nuclear@0 203
nuclear@0 204 case 's':
nuclear@0 205 stereo = !stereo;
nuclear@0 206 glutPostRedisplay();
nuclear@0 207 break;
nuclear@0 208
nuclear@0 209 case 'w':
nuclear@0 210 wire = !wire;
nuclear@0 211 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
nuclear@0 212 glutPostRedisplay();
nuclear@0 213 break;
nuclear@0 214
nuclear@0 215 case '=':
nuclear@0 216 threshold += 0.05;
nuclear@0 217 msurf_threshold(msurf, threshold);
nuclear@0 218 printf("threshold: %f\n", threshold);
nuclear@0 219 glutPostRedisplay();
nuclear@0 220 break;
nuclear@0 221
nuclear@0 222 case '-':
nuclear@0 223 threshold -= 0.05;
nuclear@0 224 msurf_threshold(msurf, threshold);
nuclear@0 225 printf("threshold: %f\n", threshold);
nuclear@0 226 glutPostRedisplay();
nuclear@0 227 break;
nuclear@0 228
nuclear@0 229 case ' ':
nuclear@0 230 bidx = (bidx + 1) % num_mballs;
nuclear@0 231 break;
nuclear@0 232
nuclear@0 233 default:
nuclear@0 234 break;
nuclear@0 235 }
nuclear@0 236 }
nuclear@0 237
nuclear@0 238 int bnstate[32];
nuclear@0 239 int prev_x, prev_y;
nuclear@0 240
nuclear@0 241 void mouse(int bn, int state, int x, int y)
nuclear@0 242 {
nuclear@0 243 bnstate[bn] = state == GLUT_DOWN;
nuclear@0 244 prev_x = x;
nuclear@0 245 prev_y = y;
nuclear@0 246 }
nuclear@0 247
nuclear@0 248 void motion(int x, int y)
nuclear@0 249 {
nuclear@0 250 int dx, dy;
nuclear@0 251
nuclear@0 252 dx = x - prev_x;
nuclear@0 253 dy = y - prev_y;
nuclear@0 254 prev_x = x;
nuclear@0 255 prev_y = y;
nuclear@0 256
nuclear@0 257 if(bnstate[GLUT_LEFT_BUTTON]) {
nuclear@0 258 cam_inp_rotate(dx, dy);
nuclear@0 259 glutPostRedisplay();
nuclear@0 260 }
nuclear@0 261 if(bnstate[GLUT_RIGHT_BUTTON]) {
nuclear@0 262 cam_inp_zoom(dy);
nuclear@0 263 glutPostRedisplay();
nuclear@0 264 }
nuclear@0 265 }
nuclear@0 266
nuclear@0 267 void sball_button(int bn, int state)
nuclear@0 268 {
nuclear@0 269 if(state) return;
nuclear@0 270
nuclear@0 271 if(bn < num_mballs) {
nuclear@0 272 bidx = bn;
nuclear@0 273 } else {
nuclear@0 274 bidx = (bidx + 1) % num_mballs;
nuclear@0 275 }
nuclear@0 276 }
nuclear@0 277
nuclear@0 278 void sball_motion(int x, int y, int z)
nuclear@0 279 {
nuclear@0 280 mball[bidx].x += (float)x / 32768.0;
nuclear@0 281 mball[bidx].y += (float)y / 32768.0;
nuclear@0 282 mball[bidx].z -= (float)z / 32768.0;
nuclear@0 283 glutPostRedisplay();
nuclear@0 284 }
nuclear@0 285
nuclear@0 286 int parse_args(int argc, char **argv)
nuclear@0 287 {
nuclear@0 288 int i;
nuclear@0 289
nuclear@0 290 for(i=1; i<argc; i++) {
nuclear@0 291 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@0 292 switch(argv[i][1]) {
nuclear@0 293 case 's':
nuclear@0 294 stereo = !stereo;
nuclear@0 295 break;
nuclear@0 296
nuclear@0 297 default:
nuclear@0 298 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
nuclear@0 299 return -1;
nuclear@0 300 }
nuclear@0 301 } else {
nuclear@0 302 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@0 303 return -1;
nuclear@0 304 }
nuclear@0 305 }
nuclear@0 306 return 0;
nuclear@0 307 }