erebus

diff liberebus/src/brdf.cc @ 46:c4d48a21bc4a

in the middle of the vmath->gph-math port
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 24 Feb 2016 00:26:50 +0200
parents d15ee526daa6
children
line diff
     1.1 --- a/liberebus/src/brdf.cc	Tue Dec 29 12:19:53 2015 +0200
     1.2 +++ b/liberebus/src/brdf.cc	Wed Feb 24 00:26:50 2016 +0200
     1.3 @@ -5,32 +5,32 @@
     1.4  #include "erebus_impl.h"
     1.5  
     1.6  // --- class SurfaceGeometry ---
     1.7 -SurfaceGeometry::SurfaceGeometry(const Vector3 &norm, VecLength st)
     1.8 -	: SurfaceGeometry(norm, Vector2(0, 0), st)
     1.9 +SurfaceGeometry::SurfaceGeometry(const Vec3 &norm, VecLength st)
    1.10 +	: SurfaceGeometry(norm, Vec2(0, 0), st)
    1.11  {
    1.12  }
    1.13  
    1.14 -SurfaceGeometry::SurfaceGeometry(const Vector3 &norm, const Vector2 &uvarg, VecLength st)
    1.15 +SurfaceGeometry::SurfaceGeometry(const Vec3 &norm, const Vec2 &uvarg, VecLength st)
    1.16  	: normal(norm), uv(uvarg)
    1.17  {
    1.18  	if(st == unknown) {
    1.19  		normal.normalize();
    1.20  	}
    1.21  
    1.22 -	tangent = Vector3(1.0f, 0.0f, 0.0f);
    1.23 +	tangent = Vec3(1.0f, 0.0f, 0.0f);
    1.24  	if(1.0 - fabs(dot_product(normal, tangent)) < 1e-4f) {
    1.25 -		tangent = Vector3(0.0f, 0.0f, 1.0f);
    1.26 +		tangent = Vec3(0.0f, 0.0f, 1.0f);
    1.27  	}
    1.28 -	Vector3 bitan = cross_product(normal, tangent);
    1.29 +	Vec3 bitan = cross_product(normal, tangent);
    1.30  	tangent = cross_product(bitan, normal);
    1.31  }
    1.32  
    1.33 -SurfaceGeometry::SurfaceGeometry(const Vector3 &norm, const Vector3 &tang, VecLength st)
    1.34 -	: SurfaceGeometry(norm, tang, Vector2(0, 0), st)
    1.35 +SurfaceGeometry::SurfaceGeometry(const Vec3 &norm, const Vec3 &tang, VecLength st)
    1.36 +	: SurfaceGeometry(norm, tang, Vec2(0, 0), st)
    1.37  {
    1.38  }
    1.39  
    1.40 -SurfaceGeometry::SurfaceGeometry(const Vector3 &norm, const Vector3 &tang, const Vector2 &uvarg, VecLength st)
    1.41 +SurfaceGeometry::SurfaceGeometry(const Vec3 &norm, const Vec3 &tang, const Vec2 &uvarg, VecLength st)
    1.42  	: normal(norm), tangent(tang), uv(uvarg)
    1.43  {
    1.44  	if(st == unknown) {
    1.45 @@ -39,26 +39,26 @@
    1.46  	}
    1.47  }
    1.48  
    1.49 -Vector3 SurfaceGeometry::spherical_to_world(float theta, float phi) const
    1.50 +Vec3 SurfaceGeometry::spherical_to_world(float theta, float phi) const
    1.51  {
    1.52  	float x = cos(theta) * sin(phi);
    1.53  	float y = sin(theta) * sin(phi);
    1.54  	float z = cos(phi);
    1.55 -	return sample_to_world(Vector3(x, y, z));
    1.56 +	return sample_to_world(Vec3(x, y, z));
    1.57  }
    1.58  
    1.59 -Vector3 SurfaceGeometry::sample_to_world(const Vector3 &v) const
    1.60 +Vec3 SurfaceGeometry::sample_to_world(const Vec3 &v) const
    1.61  {
    1.62 -	Matrix3x3 xform;
    1.63 +	Mat3x3 xform;
    1.64  	xform.set_column_vector(tangent, 0);
    1.65  	xform.set_column_vector(cross_product(normal, tangent), 1);
    1.66  	xform.set_column_vector(normal, 2);
    1.67  	return v.transformed(xform);
    1.68  }
    1.69  
    1.70 -Vector3 SurfaceGeometry::world_to_sample(const Vector3 &v) const
    1.71 +Vec3 SurfaceGeometry::world_to_sample(const Vec3 &v) const
    1.72  {
    1.73 -	Matrix3x3 xform;
    1.74 +	Mat3x3 xform;
    1.75  	xform.set_row_vector(tangent, 0);
    1.76  	xform.set_row_vector(cross_product(normal, tangent), 1);
    1.77  	xform.set_row_vector(normal, 2);
    1.78 @@ -76,7 +76,7 @@
    1.79  	this->mtl = mtl;
    1.80  }
    1.81  
    1.82 -float Reflectance::sample(const SurfaceGeometry &geom, const Vector3 &outdir, Vector3 *indir) const
    1.83 +float Reflectance::sample(const SurfaceGeometry &geom, const Vec3 &outdir, Vec3 *indir) const
    1.84  {
    1.85  	*indir = sample_dir(geom, outdir);
    1.86  	return eval(geom, outdir, *indir);
    1.87 @@ -94,7 +94,7 @@
    1.88  	valid_checked = false;
    1.89  }
    1.90  
    1.91 -int CompositeRefl::pick_brdf(const SurfaceGeometry &geom, const Vector3 &outdir) const
    1.92 +int CompositeRefl::pick_brdf(const SurfaceGeometry &geom, const Vec3 &outdir) const
    1.93  {
    1.94  	if(sub_brdf.empty()) {
    1.95  		return -1;
    1.96 @@ -158,7 +158,7 @@
    1.97  	"warning: don't call CompositeRefl's sample_dir and eval separately\n"
    1.98  	"         it'll pick different brdfs each time! use sample instead\n";
    1.99  
   1.100 -Vector3 CompositeRefl::sample_dir(const SurfaceGeometry &geom, const Vector3 &outdir) const
   1.101 +Vec3 CompositeRefl::sample_dir(const SurfaceGeometry &geom, const Vec3 &outdir) const
   1.102  {
   1.103  	if(!warned_composite_usage) {
   1.104  		fputs(composite_usage_warnstr, stderr);
   1.105 @@ -168,13 +168,13 @@
   1.106  	return sub_brdf[bidx].brdf->sample_dir(geom, outdir);
   1.107  }
   1.108  
   1.109 -float CompositeRefl::sample(const SurfaceGeometry &geom, const Vector3 &outdir, Vector3 *indir) const
   1.110 +float CompositeRefl::sample(const SurfaceGeometry &geom, const Vec3 &outdir, Vec3 *indir) const
   1.111  {
   1.112  	int bidx = pick_brdf(geom, outdir);
   1.113  	return sub_brdf[bidx].brdf->sample(geom, outdir, indir);
   1.114  }
   1.115  
   1.116 -float CompositeRefl::eval(const SurfaceGeometry &geom, const Vector3 &outdir, const Vector3 &indir) const
   1.117 +float CompositeRefl::eval(const SurfaceGeometry &geom, const Vec3 &outdir, const Vec3 &indir) const
   1.118  {
   1.119  	if(!warned_composite_usage) {
   1.120  		fputs(composite_usage_warnstr, stderr);
   1.121 @@ -185,38 +185,38 @@
   1.122  }
   1.123  
   1.124  // --- class LambertRefl ---
   1.125 -Vector3 LambertRefl::sample_dir(const SurfaceGeometry &geom, const Vector3 &outdir) const
   1.126 +Vec3 LambertRefl::sample_dir(const SurfaceGeometry &geom, const Vec3 &outdir) const
   1.127  {
   1.128  	// TODO: write a better uniform disk sampling that doesn't rely in rejection
   1.129 -	Vector3 dir;
   1.130 +	Vec3 dir;
   1.131  	do {
   1.132 -		dir = Vector3(randf(-1, 1), randf(-1, 1), 0);
   1.133 +		dir = Vec3(randf(-1, 1), randf(-1, 1), 0);
   1.134  	} while(dir.length_sq() > 1.0);
   1.135  	dir.z = sqrt(1.0 - (dir.x * dir.x + dir.y * dir.y));
   1.136  
   1.137  	return geom.sample_to_world(dir);
   1.138  }
   1.139  
   1.140 -float LambertRefl::eval(const SurfaceGeometry &geom, const Vector3 &outdir, const Vector3 &indir) const
   1.141 +float LambertRefl::eval(const SurfaceGeometry &geom, const Vec3 &outdir, const Vec3 &indir) const
   1.142  {
   1.143  	return dot_product(geom.normal, outdir);
   1.144  }
   1.145  
   1.146  // --- class MirrorRefl ---
   1.147 -Vector3 MirrorRefl::sample_dir(const SurfaceGeometry &geom, const Vector3 &outdir) const
   1.148 +Vec3 MirrorRefl::sample_dir(const SurfaceGeometry &geom, const Vec3 &outdir) const
   1.149  {
   1.150  	return outdir.reflection(geom.normal);
   1.151  }
   1.152  
   1.153 -float MirrorRefl::eval(const SurfaceGeometry &geom, const Vector3 &outdir, const Vector3 &indir) const
   1.154 +float MirrorRefl::eval(const SurfaceGeometry &geom, const Vec3 &outdir, const Vec3 &indir) const
   1.155  {
   1.156  	return 1.0f;
   1.157  }
   1.158  
   1.159  // --- class PhongRefl ---
   1.160 -Vector3 PhongRefl::sample_dir(const SurfaceGeometry &geom, const Vector3 &outdir) const
   1.161 +Vec3 PhongRefl::sample_dir(const SurfaceGeometry &geom, const Vec3 &outdir) const
   1.162  {
   1.163 -	Vector3 refl = outdir.reflection(geom.normal).normalized();
   1.164 +	Vec3 refl = outdir.reflection(geom.normal).normalized();
   1.165  	SurfaceGeometry refl_geom(refl, SurfaceGeometry::unit);
   1.166  
   1.167  	float shininess = mtl ? mtl->get_attrib_value("shininess", geom.uv.x, geom.uv.y) : 42.0;
   1.168 @@ -227,11 +227,11 @@
   1.169  	return refl_geom.spherical_to_world(theta, phi);
   1.170  }
   1.171  
   1.172 -float PhongRefl::eval(const SurfaceGeometry &geom, const Vector3 &outdir, const Vector3 &indir) const
   1.173 +float PhongRefl::eval(const SurfaceGeometry &geom, const Vec3 &outdir, const Vec3 &indir) const
   1.174  {
   1.175  	float shininess = mtl ? mtl->get_attrib_value("shininess", geom.uv.x, geom.uv.y) : 42.0;
   1.176  
   1.177 -	Vector3 refl = outdir.reflection(geom.normal);
   1.178 +	Vec3 refl = outdir.reflection(geom.normal);
   1.179  	float dot = std::max<float>(dot_product(indir, refl), 0.0f);
   1.180  	return pow(dot, shininess);
   1.181  }