rayfract

changeset 1:03022062c464

lalal
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 26 Oct 2010 08:45:37 +0300
parents 09bb67c000bc
children 87b6a11c920b
files Makefile sdr/julia.p.glsl src/rayfract.cc
diffstat 3 files changed, 134 insertions(+), 31 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Oct 21 23:39:26 2010 +0300
     1.2 +++ b/Makefile	Tue Oct 26 08:45:37 2010 +0300
     1.3 @@ -11,9 +11,9 @@
     1.4  
     1.5  CC = gcc
     1.6  CXX = g++
     1.7 -CFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath`
     1.8 -CXXFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath`
     1.9 -LDFLAGS = $(libgl) `pkg-config --libs vmath`
    1.10 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath freetype2`
    1.11 +CXXFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath freetype2` -I/usr/local/include/utk
    1.12 +LDFLAGS = $(libgl) `pkg-config --libs vmath freetype2` -lutk
    1.13  
    1.14  $(bin): $(obj)
    1.15  	$(CXX) -o $@ $(obj) $(LDFLAGS)
     2.1 --- a/sdr/julia.p.glsl	Thu Oct 21 23:39:26 2010 +0300
     2.2 +++ b/sdr/julia.p.glsl	Tue Oct 26 08:45:37 2010 +0300
     2.3 @@ -3,6 +3,8 @@
     2.4  uniform sampler2D ray_tex;
     2.5  uniform float err_thres;
     2.6  uniform int iter;
     2.7 +uniform float reflectivity;
     2.8 +uniform vec3 diffuse_color;
     2.9  
    2.10  #define quat(s, x, y, z)	vec4(x, y, z, s)
    2.11  #define quat_identity()		vec4(0.0, 0.0, 0.0, 1.0)
    2.12 @@ -20,13 +22,18 @@
    2.13  	vec4 qprime;
    2.14  };
    2.15  
    2.16 +struct Material {
    2.17 +	vec3 kd, ks;
    2.18 +	float spow;
    2.19 +	float kr;
    2.20 +};
    2.21 +
    2.22  struct ISect {
    2.23  	bool hit;
    2.24  	float t;
    2.25  	vec3 pos;
    2.26  	vec3 normal;
    2.27 -	vec3 color;
    2.28 -	float kr;
    2.29 +	Material mat;
    2.30  };
    2.31  
    2.32  ISect find_intersection(Ray ray);
    2.33 @@ -44,6 +51,7 @@
    2.34  ISect ray_floor(Ray ray);
    2.35  Ray get_primary_ray();
    2.36  
    2.37 +vec3 steps_color(int steps);
    2.38  
    2.39  void main()
    2.40  {
    2.41 @@ -57,7 +65,7 @@
    2.42  
    2.43  		if(res.hit) {
    2.44  			color += shade(ray, res) * energy;
    2.45 -			energy *= res.kr;
    2.46 +			energy *= res.mat.kr;
    2.47  
    2.48  			ray.origin = res.pos;
    2.49  			ray.dir = reflect(ray.dir, res.normal);
    2.50 @@ -97,10 +105,10 @@
    2.51  	float ndotl = dot(ldir, isect.normal);
    2.52  	float ndoth = dot(hdir, isect.normal);
    2.53  
    2.54 -	vec3 dcol = /*isect.color * max(ndotl, 0.0) */ amboc(isect);
    2.55 -	vec3 scol = vec3(1.0, 1.0, 1.0) * pow(max(ndoth, 0.0), 40.0);
    2.56 +	vec3 dcol = isect.mat.kd;// * abs(ndotl);
    2.57 +	vec3 scol = isect.mat.ks * pow(abs(ndoth), isect.mat.spow);
    2.58  
    2.59 -	return /*vec3(0.05, 0.05, 0.05) + */dcol;// + scol;
    2.60 +	return vec3(0.05, 0.05, 0.05) + dcol + scol;
    2.61  }
    2.62  
    2.63  #define AO_STEP		0.04
    2.64 @@ -117,7 +125,8 @@
    2.65  		sum += 1.0 / pow(2.0, fi) * (sample_dist - jdist);
    2.66  	}
    2.67  	
    2.68 -	return 1.0 - AO_MAGIC * sum;
    2.69 +	float res = 1.0 - AO_MAGIC * sum;
    2.70 +	return clamp(res, 0.0, 1.0);
    2.71  }
    2.72  
    2.73  vec3 sky(Ray ray)
    2.74 @@ -185,16 +194,19 @@
    2.75  	return res;
    2.76  }
    2.77  
    2.78 +#define MIN_STEP	0.001
    2.79  ISect ray_julia(Ray inray)
    2.80  {
    2.81  	float dist_acc = 0.0;
    2.82  	Ray ray = inray;
    2.83  	ISect res;
    2.84  
    2.85 +	int i = 0;
    2.86  	for(float fi=0.0; ; fi+=0.1) {
    2.87 +		i++;
    2.88  		vec4 q = quat(ray.origin.x, ray.origin.y, ray.origin.z, 0.0);
    2.89  		
    2.90 -		float dist = julia_dist(q);
    2.91 +		float dist = max(julia_dist(q), MIN_STEP);
    2.92  
    2.93  		ray.origin += ray.dir * dist;
    2.94  		dist_acc += dist;
    2.95 @@ -204,9 +216,10 @@
    2.96  			res.t = dist_acc;
    2.97  			res.pos = ray.origin;
    2.98  			res.normal = normalize(julia_grad(quat(res.pos.x, res.pos.y, res.pos.z, 0.0)));
    2.99 -			res.color = vec3(0.75, 0.8, 0.9);//abs(res.normal) * 0.2;
   2.100 -			//res.kr = 0.6;
   2.101 -			res.kr = 0.0;
   2.102 +			res.mat.kr = reflectivity;
   2.103 +			res.mat.kd = diffuse_color * amboc(res) * (1.0 - res.mat.kr);
   2.104 +			res.mat.ks = vec3(0.4, 0.4, 0.4);//vec3(res.mat.kr, res.mat.kr, res.mat.kr);
   2.105 +			res.mat.spow = 50.0;
   2.106  			break;
   2.107  		}
   2.108  
   2.109 @@ -242,8 +255,8 @@
   2.110  		res.hit = true;
   2.111  		res.t = min(t1, t2);
   2.112  		res.pos = ray.origin + ray.dir * res.t;
   2.113 -		res.color = vec3(1.0, 0.3, 0.2);
   2.114 -		res.normal = res.pos / rad;
   2.115 +		//res.mat.kd = vec3(1.0, 0.3, 0.2);
   2.116 +		//res.normal = res.pos / rad;
   2.117  	}
   2.118  
   2.119  	return res;
   2.120 @@ -272,8 +285,10 @@
   2.121  		res.hit = true;
   2.122  	
   2.123  		float chess = mod(floor(res.pos.x) + floor(res.pos.z), 2.0);
   2.124 -		res.color = mix(vec3(0.498, 0.165, 0.149), vec3(0.776, 0.851, 0.847), chess);
   2.125 -		res.kr = 0.0;
   2.126 +		res.mat.kd = mix(vec3(0.498, 0.165, 0.149), vec3(0.776, 0.851, 0.847), chess);
   2.127 +		res.mat.ks = vec3(0.0, 0.0, 0.0);
   2.128 +		res.mat.spow = 1.0;
   2.129 +		res.mat.kr = 0.0;
   2.130  	}
   2.131  	return res;
   2.132  }
   2.133 @@ -287,3 +302,27 @@
   2.134  	return ray;
   2.135  }
   2.136  
   2.137 +
   2.138 +vec3 steps_color(int steps)
   2.139 +{
   2.140 +	if(steps <= 1) {
   2.141 +		return vec3(0.0, 0.5, 0.0);
   2.142 +	} else if(steps == 2) {
   2.143 +		return vec3(0.0, 1.0, 0.0);
   2.144 +	} else if(steps == 3) {
   2.145 +		return vec3(0.0, 0.0, 0.5);
   2.146 +	} else if(steps == 4) {
   2.147 +		return vec3(0.0, 0.0, 1.0);
   2.148 +	} else if(steps == 5) {
   2.149 +		return vec3(0.0, 0.5, 0.5);
   2.150 +	} else if(steps == 6) {
   2.151 +		return vec3(0.0, 1.0, 1.0);
   2.152 +	} else if(steps == 7) {
   2.153 +		return vec3(0.5, 0.0, 0.5);
   2.154 +	} else if(steps == 8) {
   2.155 +		return vec3(1.0, 0.0, 1.0);
   2.156 +	} else if(steps == 9) {
   2.157 +		return vec3(0.5, 0.0, 0.0);
   2.158 +	}
   2.159 +	return vec3(0.5 + float(steps - 9) / 10.0, 0.0, 0.0);
   2.160 +}
     3.1 --- a/src/rayfract.cc	Thu Oct 21 23:39:26 2010 +0300
     3.2 +++ b/src/rayfract.cc	Tue Oct 26 08:45:37 2010 +0300
     3.3 @@ -5,12 +5,15 @@
     3.4  #include <GL/glut.h>
     3.5  #include <vmath.h>
     3.6  #include "sdr.h"
     3.7 +#include "gui.h"
     3.8  
     3.9  void disp();
    3.10  void reshape(int x, int y);
    3.11  void keyb(unsigned char key, int x, int y);
    3.12 +void keyb_up(unsigned char key, int x, int y);
    3.13  void mouse(int bn, int state, int x, int y);
    3.14  void motion(int x, int y);
    3.15 +void passive_motion(int x, int y);
    3.16  
    3.17  int load_shader();
    3.18  unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
    3.19 @@ -26,6 +29,8 @@
    3.20  Vector4 seed;
    3.21  float err_thres = 0.0075;
    3.22  int iter = 10;
    3.23 +float reflectivity = 0.2;
    3.24 +Vector3 color(0.9, 0.95, 1.0);
    3.25  
    3.26  int main(int argc, char **argv)
    3.27  {
    3.28 @@ -43,8 +48,10 @@
    3.29  	glutDisplayFunc(disp);
    3.30  	glutReshapeFunc(reshape);
    3.31  	glutKeyboardFunc(keyb);
    3.32 +	glutKeyboardUpFunc(keyb_up);
    3.33  	glutMouseFunc(mouse);
    3.34  	glutMotionFunc(motion);
    3.35 +	glutPassiveMotionFunc(passive_motion);
    3.36  
    3.37  	glEnable(GL_DEPTH_TEST);
    3.38  	glEnable(GL_LIGHTING);
    3.39 @@ -57,6 +64,10 @@
    3.40  		return 1;
    3.41  	}
    3.42  
    3.43 +	if(gui_init(xsz, ysz) == -1) {
    3.44 +		return 1;
    3.45 +	}
    3.46 +
    3.47  	glutMainLoop();
    3.48  	return 0;
    3.49  }
    3.50 @@ -73,6 +84,9 @@
    3.51  	set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
    3.52  	set_uniform_float(sdr, "err_thres", err_thres);
    3.53  	set_uniform_int(sdr, "iter", iter);
    3.54 +	set_uniform_float(sdr, "reflectivity", reflectivity);
    3.55 +	set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
    3.56 +
    3.57  	return 0;
    3.58  }
    3.59  
    3.60 @@ -112,6 +126,8 @@
    3.61  	glMatrixMode(GL_TEXTURE);
    3.62  	glPopMatrix();
    3.63  
    3.64 +	gui_draw();
    3.65 +
    3.66  	glutSwapBuffers();
    3.67  	assert(glGetError() == GL_NO_ERROR);
    3.68  }
    3.69 @@ -170,7 +186,35 @@
    3.70  		load_shader();
    3.71  		glutPostRedisplay();
    3.72  		break;
    3.73 +
    3.74 +	case 'r':
    3.75 +		reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
    3.76 +		set_uniform_float(sdr, "reflectivity", reflectivity);
    3.77 +		glutPostRedisplay();
    3.78 +		break;
    3.79 +
    3.80 +	case '`':
    3.81 +		{
    3.82 +			static bool vis = true;
    3.83 +			vis = !vis;
    3.84 +			gui_set_visible(vis);
    3.85 +			glutPostRedisplay();
    3.86 +		}
    3.87 +		break;
    3.88  	}
    3.89 +
    3.90 +	utk::KeyboardEvent ev(key);
    3.91 +	ev.pressed = true;
    3.92 +	utk::event(&ev);
    3.93 +	glutPostRedisplay();
    3.94 +}
    3.95 +
    3.96 +void keyb_up(unsigned char key, int x, int y)
    3.97 +{
    3.98 +	utk::KeyboardEvent ev(key);
    3.99 +	ev.pressed = false;
   3.100 +	utk::event(&ev);
   3.101 +	glutPostRedisplay();
   3.102  }
   3.103  
   3.104  int bnstate[16];
   3.105 @@ -178,22 +222,31 @@
   3.106  int prev_x = -1, prev_y;
   3.107  void mouse(int bn, int state, int x, int y)
   3.108  {
   3.109 -	bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
   3.110 -	if(state == GLUT_DOWN) {
   3.111 -		if(bn == 3) {
   3.112 -			cam_dist -= 0.1;
   3.113 -			glutPostRedisplay();
   3.114 -			if(cam_dist < 0) cam_dist = 0;
   3.115 -		} else if(bn == 4) {
   3.116 -			cam_dist += 0.1;
   3.117 -			glutPostRedisplay();
   3.118 +	utk::Container *utkroot = utk::get_root_widget();
   3.119 +
   3.120 +	if(utkroot->get_child_at(x, y) == utkroot) {
   3.121 +		bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
   3.122 +		if(state == GLUT_DOWN) {
   3.123 +			if(bn == 3) {
   3.124 +				cam_dist -= 0.1;
   3.125 +				glutPostRedisplay();
   3.126 +				if(cam_dist < 0) cam_dist = 0;
   3.127 +			} else if(bn == 4) {
   3.128 +				cam_dist += 0.1;
   3.129 +				glutPostRedisplay();
   3.130 +			} else {
   3.131 +				prev_x = x;
   3.132 +				prev_y = y;
   3.133 +			}
   3.134  		} else {
   3.135 -			prev_x = x;
   3.136 -			prev_y = y;
   3.137 +			prev_x = -1;
   3.138  		}
   3.139 -	} else {
   3.140 -		prev_x = -1;
   3.141  	}
   3.142 +
   3.143 +	utk::MButtonEvent ev(bn, x, y);
   3.144 +	ev.pressed = state == GLUT_DOWN;
   3.145 +	utk::event(&ev);
   3.146 +	glutPostRedisplay();
   3.147  }
   3.148  
   3.149  void motion(int x, int y)
   3.150 @@ -220,6 +273,17 @@
   3.151  
   3.152  	prev_x = x;
   3.153  	prev_y = y;
   3.154 +
   3.155 +	utk::MMotionEvent ev(x, y);
   3.156 +	utk::event(&ev);
   3.157 +	glutPostRedisplay();
   3.158 +}
   3.159 +
   3.160 +void passive_motion(int x, int y)
   3.161 +{
   3.162 +	utk::MMotionEvent ev(x, y);
   3.163 +	utk::event(&ev);
   3.164 +	//glutPostRedisplay();
   3.165  }
   3.166  
   3.167  unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)