refmod_test

view refmod_test.c @ 1:7e911c994ef2

should work on mac now
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Feb 2016 23:21:49 +0200
parents b469e6a72636
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <GL/glew.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "sdr.h"
12 #ifdef FREEGLUT
13 #include <GL/freeglut_ext.h>
14 #define MOUSEWHEEL
15 #endif
17 void redraw(void);
18 void set_material_color(float r, float g, float b, float a);
19 void setup_lights(void);
20 void key_handler(unsigned char key, int x, int y);
21 void key_up_handler(unsigned char key, int x, int y);
22 void skey_handler(int key, int x, int y);
23 void button_handler(int bn, int state, int x, int y);
24 void mouse_handler(int x, int y);
25 void reshape(int x, int y);
26 #ifdef MOUSEWHEEL
27 void wheel_handler(int unk, int dir, int x, int y);
28 #endif
29 void menu_handler(int val);
30 void init_sdr(void);
32 int xres, yres;
34 float cam_x, cam_y = 0.6, cam_z = 4;
35 float cam_rot, cam_pitch = 35.0;
37 float pan_offs = 0.025;
39 unsigned int prog;
41 float roughness = 0.24;
42 float specularity = 0.86;
43 float ior = 3.2;
44 float spec_pow = 60.0;
46 enum {DSDR_LAMBERT, DSDR_OREN_NAYAR};
47 enum {SSDR_PHONG, SSDR_BLINN, SSDR_COOK_TORR};
49 int dif_sdr = DSDR_OREN_NAYAR;
50 int spec_sdr = SSDR_COOK_TORR;
52 int main(int argc, char **argv)
53 {
54 unsigned int vs, ps, pslib;
55 int dif_menu, spec_menu, i;
57 for(i=1; i<argc; i++) {
58 if(argv[i][0] == '-' && argv[i][2] == 0) {
59 switch(argv[i][1]) {
60 case 'd':
61 if(strcmp(argv[++i], "lambert") == 0) {
62 dif_sdr = DSDR_LAMBERT;
63 } else if(strcmp(argv[i], "oren-nayar") == 0) {
64 dif_sdr = DSDR_OREN_NAYAR;
65 } else {
66 printf("-d (diffuse model) choices:\n");
67 printf(" lambert \"Photometria sive de mensura de gratibus luminis, colorum et umbrae\", 1760\n");
68 printf(" oren-nayar \"Generalization of Lambert's Reflectance Model\", SIGGRAPH 1994\n");
69 return EXIT_FAILURE;
70 }
71 break;
73 case 's':
74 if(strcmp(argv[++i], "phong") == 0) {
75 spec_sdr = SSDR_PHONG;
76 } else if(strcmp(argv[i], "cook-torrance") == 0) {
77 spec_sdr = SSDR_COOK_TORR;
78 } else {
79 printf("-s (specular model) choices:\n");
80 printf(" phong \"Illumination for Computer Generated Pictures\", CACM 1975\n");
81 printf(" blinn \"Models of Light Reflection for Computer Synthesized Pictures\", SIGGRAPH 1977\n");
82 printf(" cook-torrance \"A Reflectance Model for Computer Graphics\", SIGGRAPH 1981\n");
83 return EXIT_FAILURE;
84 }
85 break;
87 default:
88 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
89 return EXIT_FAILURE;
90 }
91 }
92 }
94 glutInitWindowSize(800, 600);
95 glutInit(&argc, argv);
96 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
97 glutCreateWindow("1.roughness 2.specularity 3.ior 4.phong power");
98 xres = glutGet(GLUT_WINDOW_WIDTH);
99 yres = glutGet(GLUT_WINDOW_HEIGHT);
101 glutKeyboardFunc(key_handler);
102 glutKeyboardUpFunc(key_up_handler);
103 glutSpecialFunc(skey_handler);
104 glutMotionFunc(mouse_handler);
105 glutMouseFunc(button_handler);
106 glutReshapeFunc(reshape);
107 glutDisplayFunc(redraw);
108 #ifdef MOUSEWHEEL
109 glutMouseWheelFunc(wheel_handler);
110 #endif
112 /* create the menus */
113 dif_menu = glutCreateMenu(menu_handler);
114 glutAddMenuEntry("lambert", DSDR_LAMBERT);
115 glutAddMenuEntry("oren-nayar", DSDR_OREN_NAYAR);
117 spec_menu = glutCreateMenu(menu_handler);
118 glutAddMenuEntry("phong", SSDR_PHONG + 10);
119 glutAddMenuEntry("blinn", SSDR_BLINN + 10);
120 glutAddMenuEntry("cook-torrance", SSDR_COOK_TORR + 10);
122 glutCreateMenu(menu_handler);
123 glutAddSubMenu("diffuse models", dif_menu);
124 glutAddSubMenu("specular models", spec_menu);
126 glutAttachMenu(GLUT_RIGHT_BUTTON);
129 glEnable(GL_DEPTH_TEST);
130 glEnable(GL_CULL_FACE);
132 glEnable(GL_LIGHTING);
133 glEnable(GL_LIGHT0);
135 glMatrixMode(GL_PROJECTION);
136 gluPerspective(45.0, (float)xres / (float)yres, 1.0, 100.0);
137 glMatrixMode(GL_MODELVIEW);
139 init_sdr();
141 if(!(vs = load_vertex_shader("sdr.vs.glsl"))) {
142 fprintf(stderr, "failed to load vertex shader\n");
143 return EXIT_FAILURE;
144 }
146 if(!(pslib = load_pixel_shader("sdr_ref_models.glsl"))) {
147 fprintf(stderr, "failed to load reflection models shader\n");
148 return EXIT_FAILURE;
149 }
151 if(!(ps = load_pixel_shader("sdr.ps.glsl"))) {
152 fprintf(stderr, "failed to load pixel shader\n");
153 return EXIT_FAILURE;
154 }
156 if(!(prog = create_program())) {
157 fprintf(stderr, "failed to create GPU program\n");
158 return EXIT_FAILURE;
159 }
160 attach_shader(prog, vs);
161 attach_shader(prog, pslib);
162 attach_shader(prog, ps);
164 if(link_program(prog) == -1) {
165 fprintf(stderr, "failed to link GPU program\n");
166 return EXIT_FAILURE;
167 }
169 glutMainLoop();
170 return 0;
171 }
173 void setup_lights(void)
174 {
175 float lpos[] = {-6, 8, 10, 1};
176 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
177 }
179 void redraw(void)
180 {
181 glClearColor(0, 0, 0, 0);
182 glClearDepth(1.0);
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
186 glMatrixMode(GL_MODELVIEW);
187 glLoadIdentity();
188 glTranslatef(-cam_x, -cam_y, -cam_z);
189 glRotatef(cam_pitch, 1, 0, 0);
190 glRotatef(cam_rot, 0, 1, 0);
192 setup_lights();
194 glPushMatrix();
195 glTranslatef(0, 0.8, 0);
197 bind_program(prog);
198 set_uniform_float(prog, "rough", roughness);
199 set_uniform_float(prog, "specularity", specularity);
200 set_uniform_float(prog, "ior", ior);
201 set_uniform_float(prog, "dif_sdr", dif_sdr);
202 set_uniform_float(prog, "spec_sdr", spec_sdr);
203 glFrontFace(GL_CW);
204 set_material_color(0.55, 0.20, 0.10, 1.0);
205 /*set_material_color(0.87, 0.72, 0.62, 1.0);*/
206 glutSolidTeapot(1.0);
207 glFrontFace(GL_CCW);
208 bind_program(0);
210 glPopMatrix();
213 glutSwapBuffers();
214 }
216 void set_material_color(float r, float g, float b, float a)
217 {
218 float col[4];
219 col[0] = r;
220 col[1] = g;
221 col[2] = b;
222 col[3] = a;
223 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
224 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, spec_pow);
225 }
227 int mod_rough, mod_spec, mod_ior, mod_spow;
229 void key_handler(unsigned char key, int x, int y)
230 {
231 switch(key) {
232 case 27:
233 exit(0);
235 case '1':
236 mod_rough = 1;
237 break;
239 case '2':
240 mod_spec = 1;
241 break;
243 case '3':
244 mod_ior = 1;
245 break;
247 case '4':
248 mod_spow = 1;
249 break;
251 default:
252 break;
253 }
254 }
256 void key_up_handler(unsigned char key, int x, int y)
257 {
258 switch(key) {
259 case '1':
260 mod_rough = 0;
261 break;
263 case '2':
264 mod_spec = 0;
265 break;
267 case '3':
268 mod_ior = 0;
269 break;
271 case '4':
272 mod_spow = 0;
273 break;
275 default:
276 break;
277 }
278 }
280 void skey_handler(int key, int x, int y)
281 {
282 switch(key) {
283 default:
284 break;
285 }
286 }
288 static int prev_x = -1;
289 static int prev_y = -1;
290 static int pbn;
292 void button_handler(int button, int state, int x, int y)
293 {
294 if(state == GLUT_DOWN) {
295 prev_x = x;
296 prev_y = y;
297 pbn = button;
298 } else {
299 prev_x = -1;
300 prev_y = -1;
301 }
302 }
305 #define MIN(a, b) ((a) < (b) ? (a) : (b))
306 #define MAX(a, b) ((a) > (b) ? (a) : (b))
308 void mouse_handler(int x, int y)
309 {
310 if(pbn == GLUT_LEFT_BUTTON) {
311 if(mod_rough || mod_spec || mod_ior || mod_spow) {
312 float dx = (float)(x - prev_x) / (float)xres;
314 if(mod_rough) {
315 roughness = MAX(0.0, MIN(roughness + dx, 1.0));
316 printf("roughness: %.3f\n", roughness);
317 }
318 if(mod_spec) {
319 specularity = MAX(0.0, MIN(specularity + dx, 1.0));
320 printf("specularity: %.3f\n", specularity);
321 }
322 if(mod_ior) {
323 ior = MAX(ior + dx, 0.0);
324 printf("ior: %.3f\n", ior);
325 }
326 if(mod_spow) {
327 spec_pow = MAX(spec_pow + dx * 50.0, 0.01);
328 printf("specular power: %.3f\n", spec_pow);
329 }
330 } else {
331 /*
332 cam_x += (float)(prev_x - x) * pan_offs;
333 cam_y += (float)(y - prev_y) * pan_offs;
334 */
335 }
337 prev_x = x;
338 prev_y = y;
339 glutPostRedisplay();
341 } else if(pbn == GLUT_MIDDLE_BUTTON) {
342 cam_rot += (float)(x - prev_x) / 2.0f;
343 cam_pitch += (float)(y - prev_y) / 2.0f;
344 if(cam_pitch > 90.0) cam_pitch = 90.0;
345 if(cam_pitch < -90.0) cam_pitch = -90.0;
347 prev_x = x;
348 prev_y = y;
349 glutPostRedisplay();
351 } else if(pbn == GLUT_RIGHT_BUTTON) {
352 /*
353 cam_z -= (float)(prev_y - y) / 2.0f;
354 prev_y = y;
355 glutPostRedisplay();
356 */
357 }
358 }
360 void reshape(int x, int y)
361 {
362 glViewport(0, 0, x, y);
363 xres = x;
364 yres = y;
366 glMatrixMode(GL_PROJECTION);
367 glLoadIdentity();
368 gluPerspective(45.0, (float)xres / (float)yres, 1.0, 100.0);
369 glMatrixMode(GL_MODELVIEW);
370 }
372 #ifdef MOUSEWHEEL
373 void wheel_handler(int unk, int dir, int x, int y)
374 {
375 if(dir > 0) {
376 cam_z -= pan_offs * 5.0;
377 } else {
378 cam_z += pan_offs * 5.0;
379 }
380 glutPostRedisplay();
381 }
382 #endif
384 void menu_handler(int val)
385 {
386 if(val < 10) {
387 dif_sdr = val;
388 } else {
389 spec_sdr = val - 10;
390 }
391 glutPostRedisplay();
392 }