refmod_test
diff refmod_test.c @ 0:b469e6a72636
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 18 Feb 2016 23:15:43 +0200 |
parents | |
children | 7e911c994ef2 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/refmod_test.c Thu Feb 18 23:15:43 2016 +0200 1.3 @@ -0,0 +1,387 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <GL/glut.h> 1.8 +#include "sdr.h" 1.9 + 1.10 +#ifdef FREEGLUT 1.11 +#include <GL/freeglut_ext.h> 1.12 +#define MOUSEWHEEL 1.13 +#endif 1.14 + 1.15 +void redraw(void); 1.16 +void set_material_color(float r, float g, float b, float a); 1.17 +void setup_lights(void); 1.18 +void key_handler(unsigned char key, int x, int y); 1.19 +void key_up_handler(unsigned char key, int x, int y); 1.20 +void skey_handler(int key, int x, int y); 1.21 +void button_handler(int bn, int state, int x, int y); 1.22 +void mouse_handler(int x, int y); 1.23 +void reshape(int x, int y); 1.24 +#ifdef MOUSEWHEEL 1.25 +void wheel_handler(int unk, int dir, int x, int y); 1.26 +#endif 1.27 +void menu_handler(int val); 1.28 +void init_sdr(void); 1.29 + 1.30 +int xres, yres; 1.31 + 1.32 +float cam_x, cam_y = 0.6, cam_z = 4; 1.33 +float cam_rot, cam_pitch = 35.0; 1.34 + 1.35 +float pan_offs = 0.025; 1.36 + 1.37 +unsigned int prog; 1.38 + 1.39 +float roughness = 0.24; 1.40 +float specularity = 0.86; 1.41 +float ior = 3.2; 1.42 +float spec_pow = 60.0; 1.43 + 1.44 +enum {DSDR_LAMBERT, DSDR_OREN_NAYAR}; 1.45 +enum {SSDR_PHONG, SSDR_BLINN, SSDR_COOK_TORR}; 1.46 + 1.47 +int dif_sdr = DSDR_OREN_NAYAR; 1.48 +int spec_sdr = SSDR_COOK_TORR; 1.49 + 1.50 +int main(int argc, char **argv) 1.51 +{ 1.52 + unsigned int vs, ps, pslib; 1.53 + int dif_menu, spec_menu, i; 1.54 + 1.55 + for(i=1; i<argc; i++) { 1.56 + if(argv[i][0] == '-' && argv[i][2] == 0) { 1.57 + switch(argv[i][1]) { 1.58 + case 'd': 1.59 + if(strcmp(argv[++i], "lambert") == 0) { 1.60 + dif_sdr = DSDR_LAMBERT; 1.61 + } else if(strcmp(argv[i], "oren-nayar") == 0) { 1.62 + dif_sdr = DSDR_OREN_NAYAR; 1.63 + } else { 1.64 + printf("-d (diffuse model) choices:\n"); 1.65 + printf(" lambert \"Photometria sive de mensura de gratibus luminis, colorum et umbrae\", 1760\n"); 1.66 + printf(" oren-nayar \"Generalization of Lambert's Reflectance Model\", SIGGRAPH 1994\n"); 1.67 + return EXIT_FAILURE; 1.68 + } 1.69 + break; 1.70 + 1.71 + case 's': 1.72 + if(strcmp(argv[++i], "phong") == 0) { 1.73 + spec_sdr = SSDR_PHONG; 1.74 + } else if(strcmp(argv[i], "cook-torrance") == 0) { 1.75 + spec_sdr = SSDR_COOK_TORR; 1.76 + } else { 1.77 + printf("-s (specular model) choices:\n"); 1.78 + printf(" phong \"Illumination for Computer Generated Pictures\", CACM 1975\n"); 1.79 + printf(" blinn \"Models of Light Reflection for Computer Synthesized Pictures\", SIGGRAPH 1977\n"); 1.80 + printf(" cook-torrance \"A Reflectance Model for Computer Graphics\", SIGGRAPH 1981\n"); 1.81 + return EXIT_FAILURE; 1.82 + } 1.83 + break; 1.84 + 1.85 + default: 1.86 + fprintf(stderr, "unrecognized argument: %s\n", argv[i]); 1.87 + return EXIT_FAILURE; 1.88 + } 1.89 + } 1.90 + } 1.91 + 1.92 + glutInitWindowSize(800, 600); 1.93 + glutInit(&argc, argv); 1.94 + glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 1.95 + glutCreateWindow("1.roughness 2.specularity 3.ior 4.phong power"); 1.96 + xres = glutGet(GLUT_WINDOW_WIDTH); 1.97 + yres = glutGet(GLUT_WINDOW_HEIGHT); 1.98 + 1.99 + glutKeyboardFunc(key_handler); 1.100 + glutKeyboardUpFunc(key_up_handler); 1.101 + glutSpecialFunc(skey_handler); 1.102 + glutMotionFunc(mouse_handler); 1.103 + glutMouseFunc(button_handler); 1.104 + glutReshapeFunc(reshape); 1.105 + glutDisplayFunc(redraw); 1.106 +#ifdef MOUSEWHEEL 1.107 + glutMouseWheelFunc(wheel_handler); 1.108 +#endif 1.109 + 1.110 + /* create the menus */ 1.111 + dif_menu = glutCreateMenu(menu_handler); 1.112 + glutAddMenuEntry("lambert", DSDR_LAMBERT); 1.113 + glutAddMenuEntry("oren-nayar", DSDR_OREN_NAYAR); 1.114 + 1.115 + spec_menu = glutCreateMenu(menu_handler); 1.116 + glutAddMenuEntry("phong", SSDR_PHONG + 10); 1.117 + glutAddMenuEntry("blinn", SSDR_BLINN + 10); 1.118 + glutAddMenuEntry("cook-torrance", SSDR_COOK_TORR + 10); 1.119 + 1.120 + glutCreateMenu(menu_handler); 1.121 + glutAddSubMenu("diffuse models", dif_menu); 1.122 + glutAddSubMenu("specular models", spec_menu); 1.123 + 1.124 + glutAttachMenu(GLUT_RIGHT_BUTTON); 1.125 + 1.126 + 1.127 + glEnable(GL_DEPTH_TEST); 1.128 + glEnable(GL_CULL_FACE); 1.129 + 1.130 + glEnable(GL_LIGHTING); 1.131 + glEnable(GL_LIGHT0); 1.132 + 1.133 + glMatrixMode(GL_PROJECTION); 1.134 + gluPerspective(45.0, (float)xres / (float)yres, 1.0, 100.0); 1.135 + glMatrixMode(GL_MODELVIEW); 1.136 + 1.137 + init_sdr(); 1.138 + 1.139 + if(!(vs = load_vertex_shader("sdr.vs.glsl"))) { 1.140 + fprintf(stderr, "failed to load vertex shader\n"); 1.141 + return EXIT_FAILURE; 1.142 + } 1.143 + 1.144 + if(!(pslib = load_pixel_shader("sdr_ref_models.glsl"))) { 1.145 + fprintf(stderr, "failed to load reflection models shader\n"); 1.146 + return EXIT_FAILURE; 1.147 + } 1.148 + 1.149 + if(!(ps = load_pixel_shader("sdr.ps.glsl"))) { 1.150 + fprintf(stderr, "failed to load pixel shader\n"); 1.151 + return EXIT_FAILURE; 1.152 + } 1.153 + 1.154 + if(!(prog = create_program())) { 1.155 + fprintf(stderr, "failed to create GPU program\n"); 1.156 + return EXIT_FAILURE; 1.157 + } 1.158 + attach_shader(prog, vs); 1.159 + attach_shader(prog, pslib); 1.160 + attach_shader(prog, ps); 1.161 + 1.162 + if(link_program(prog) == -1) { 1.163 + fprintf(stderr, "failed to link GPU program\n"); 1.164 + return EXIT_FAILURE; 1.165 + } 1.166 + 1.167 + glutMainLoop(); 1.168 + return 0; 1.169 +} 1.170 + 1.171 +void setup_lights(void) 1.172 +{ 1.173 + float lpos[] = {-6, 8, 10, 1}; 1.174 + glLightfv(GL_LIGHT0, GL_POSITION, lpos); 1.175 +} 1.176 + 1.177 +void redraw(void) 1.178 +{ 1.179 + glClearColor(0, 0, 0, 0); 1.180 + glClearDepth(1.0); 1.181 + 1.182 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.183 + 1.184 + glMatrixMode(GL_MODELVIEW); 1.185 + glLoadIdentity(); 1.186 + glTranslatef(-cam_x, -cam_y, -cam_z); 1.187 + glRotatef(cam_pitch, 1, 0, 0); 1.188 + glRotatef(cam_rot, 0, 1, 0); 1.189 + 1.190 + setup_lights(); 1.191 + 1.192 + glPushMatrix(); 1.193 + glTranslatef(0, 0.8, 0); 1.194 + 1.195 + bind_program(prog); 1.196 + set_uniform_float(prog, "rough", roughness); 1.197 + set_uniform_float(prog, "specularity", specularity); 1.198 + set_uniform_float(prog, "ior", ior); 1.199 + set_uniform_float(prog, "dif_sdr", dif_sdr); 1.200 + set_uniform_float(prog, "spec_sdr", spec_sdr); 1.201 + glFrontFace(GL_CW); 1.202 + set_material_color(0.55, 0.20, 0.10, 1.0); 1.203 + /*set_material_color(0.87, 0.72, 0.62, 1.0);*/ 1.204 + glutSolidTeapot(1.0); 1.205 + glFrontFace(GL_CCW); 1.206 + bind_program(0); 1.207 + 1.208 + glPopMatrix(); 1.209 + 1.210 + 1.211 + glutSwapBuffers(); 1.212 +} 1.213 + 1.214 +void set_material_color(float r, float g, float b, float a) 1.215 +{ 1.216 + float col[4]; 1.217 + col[0] = r; 1.218 + col[1] = g; 1.219 + col[2] = b; 1.220 + col[3] = a; 1.221 + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col); 1.222 + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, spec_pow); 1.223 +} 1.224 + 1.225 +int mod_rough, mod_spec, mod_ior, mod_spow; 1.226 + 1.227 +void key_handler(unsigned char key, int x, int y) 1.228 +{ 1.229 + switch(key) { 1.230 + case 27: 1.231 + exit(0); 1.232 + 1.233 + case '1': 1.234 + mod_rough = 1; 1.235 + break; 1.236 + 1.237 + case '2': 1.238 + mod_spec = 1; 1.239 + break; 1.240 + 1.241 + case '3': 1.242 + mod_ior = 1; 1.243 + break; 1.244 + 1.245 + case '4': 1.246 + mod_spow = 1; 1.247 + break; 1.248 + 1.249 + default: 1.250 + break; 1.251 + } 1.252 +} 1.253 + 1.254 +void key_up_handler(unsigned char key, int x, int y) 1.255 +{ 1.256 + switch(key) { 1.257 + case '1': 1.258 + mod_rough = 0; 1.259 + break; 1.260 + 1.261 + case '2': 1.262 + mod_spec = 0; 1.263 + break; 1.264 + 1.265 + case '3': 1.266 + mod_ior = 0; 1.267 + break; 1.268 + 1.269 + case '4': 1.270 + mod_spow = 0; 1.271 + break; 1.272 + 1.273 + default: 1.274 + break; 1.275 + } 1.276 +} 1.277 + 1.278 +void skey_handler(int key, int x, int y) 1.279 +{ 1.280 + switch(key) { 1.281 + default: 1.282 + break; 1.283 + } 1.284 +} 1.285 + 1.286 +static int prev_x = -1; 1.287 +static int prev_y = -1; 1.288 +static int pbn; 1.289 + 1.290 +void button_handler(int button, int state, int x, int y) 1.291 +{ 1.292 + if(state == GLUT_DOWN) { 1.293 + prev_x = x; 1.294 + prev_y = y; 1.295 + pbn = button; 1.296 + } else { 1.297 + prev_x = -1; 1.298 + prev_y = -1; 1.299 + } 1.300 +} 1.301 + 1.302 + 1.303 +#define MIN(a, b) ((a) < (b) ? (a) : (b)) 1.304 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) 1.305 + 1.306 +void mouse_handler(int x, int y) 1.307 +{ 1.308 + if(pbn == GLUT_LEFT_BUTTON) { 1.309 + if(mod_rough || mod_spec || mod_ior || mod_spow) { 1.310 + float dx = (float)(x - prev_x) / (float)xres; 1.311 + 1.312 + if(mod_rough) { 1.313 + roughness = MAX(0.0, MIN(roughness + dx, 1.0)); 1.314 + printf("roughness: %.3f\n", roughness); 1.315 + } 1.316 + if(mod_spec) { 1.317 + specularity = MAX(0.0, MIN(specularity + dx, 1.0)); 1.318 + printf("specularity: %.3f\n", specularity); 1.319 + } 1.320 + if(mod_ior) { 1.321 + ior = MAX(ior + dx, 0.0); 1.322 + printf("ior: %.3f\n", ior); 1.323 + } 1.324 + if(mod_spow) { 1.325 + spec_pow = MAX(spec_pow + dx * 50.0, 0.01); 1.326 + printf("specular power: %.3f\n", spec_pow); 1.327 + } 1.328 + } else { 1.329 + /* 1.330 + cam_x += (float)(prev_x - x) * pan_offs; 1.331 + cam_y += (float)(y - prev_y) * pan_offs; 1.332 + */ 1.333 + } 1.334 + 1.335 + prev_x = x; 1.336 + prev_y = y; 1.337 + glutPostRedisplay(); 1.338 + 1.339 + } else if(pbn == GLUT_MIDDLE_BUTTON) { 1.340 + cam_rot += (float)(x - prev_x) / 2.0f; 1.341 + cam_pitch += (float)(y - prev_y) / 2.0f; 1.342 + if(cam_pitch > 90.0) cam_pitch = 90.0; 1.343 + if(cam_pitch < -90.0) cam_pitch = -90.0; 1.344 + 1.345 + prev_x = x; 1.346 + prev_y = y; 1.347 + glutPostRedisplay(); 1.348 + 1.349 + } else if(pbn == GLUT_RIGHT_BUTTON) { 1.350 + /* 1.351 + cam_z -= (float)(prev_y - y) / 2.0f; 1.352 + prev_y = y; 1.353 + glutPostRedisplay(); 1.354 + */ 1.355 + } 1.356 +} 1.357 + 1.358 +void reshape(int x, int y) 1.359 +{ 1.360 + glViewport(0, 0, x, y); 1.361 + xres = x; 1.362 + yres = y; 1.363 + 1.364 + glMatrixMode(GL_PROJECTION); 1.365 + glLoadIdentity(); 1.366 + gluPerspective(45.0, (float)xres / (float)yres, 1.0, 100.0); 1.367 + glMatrixMode(GL_MODELVIEW); 1.368 +} 1.369 + 1.370 +#ifdef MOUSEWHEEL 1.371 +void wheel_handler(int unk, int dir, int x, int y) 1.372 +{ 1.373 + if(dir > 0) { 1.374 + cam_z -= pan_offs * 5.0; 1.375 + } else { 1.376 + cam_z += pan_offs * 5.0; 1.377 + } 1.378 + glutPostRedisplay(); 1.379 +} 1.380 +#endif 1.381 + 1.382 +void menu_handler(int val) 1.383 +{ 1.384 + if(val < 10) { 1.385 + dif_sdr = val; 1.386 + } else { 1.387 + spec_sdr = val - 10; 1.388 + } 1.389 + glutPostRedisplay(); 1.390 +}