Permalink
Browse files

Implemented material dynamic choose

  • Loading branch information...
Ninputer committed Apr 8, 2012
1 parent d491cd7 commit 2ac6e474520639830081e6c82f59c50b1942f688
Showing with 242 additions and 115 deletions.
  1. +2 −2 ampdemo/RayTracing/RenderArea.cpp
  2. +26 −7 ampdemo/RayTracing/ampmathhelper.h
  3. +155 −0 ampdemo/RayTracing/raycommon.h
  4. +59 −106 ampdemo/RayTracing/render.h
@@ -108,8 +108,8 @@ HRESULT RenderAreaMessageHandler::OnRender()
std::vector<unsigned int> data(width * height);
array_view<unsigned int, 2> arrayview(height, width, data);
render_depth<float>(arrayview);
arrayview.discard_data();
render_material<float>(arrayview);
arrayview.synchronize();
@@ -11,16 +11,15 @@ class math_helper_cpu
static fp_t sqrt(fp_t x) restrict(cpu) { return ::sqrt(x); }
static fp_t rsqrt(fp_t x) restrict(cpu) { return 1 / ::sqrt(x); }
static fp_t tan(fp_t x) restrict(cpu) { return ::tan(x); }
static fp_t fabs(fp_t x) restrict(cpu) { return ::fabs(x); }
static fp_t floor(fp_t x) restrict(cpu) { return ::floor(x); }
static fp_t fmin(fp_t x, fp_t y) restrict(cpu) { return std::min(x, y); }
static fp_t fmax(fp_t x, fp_t y) restrict(cpu) { return std::max(x, y); }
static fp_t pow(fp_t x, fp_t y) restrict(cpu) { return ::pow(x, y); }
};
template<typename fp_t>
class math_helper
{
public:
static fp_t sqrt(fp_t x) restrict(cpu, amp);
static fp_t rsqrt(fp_t x) restrict(cpu, amp);
static fp_t tan(fp_t x) restrict(cpu, amp);
};
class math_helper;
template<>
class math_helper<float> : public math_helper_cpu<float>
@@ -29,10 +28,20 @@ class math_helper<float> : public math_helper_cpu<float>
using math_helper_cpu<float>::sqrt;
using math_helper_cpu<float>::rsqrt;
using math_helper_cpu<float>::tan;
using math_helper_cpu<float>::fabs;
using math_helper_cpu<float>::floor;
using math_helper_cpu<float>::fmin;
using math_helper_cpu<float>::fmax;
using math_helper_cpu<float>::pow;
static float sqrt(float x) restrict(amp) { return Concurrency::fast_math::sqrt(x); }
static float rsqrt(float x) restrict(amp) { return Concurrency::fast_math::rsqrt(x); }
static float tan(float x) restrict(amp) { return Concurrency::fast_math::tan(x); }
static float fabs(float x) restrict(amp) { return Concurrency::fast_math::fabs(x); }
static float floor(float x) restrict(amp) { return Concurrency::fast_math::floor(x); }
static float fmin(float x, float y) restrict(amp) { return Concurrency::fast_math::fmin(x, y); }
static float fmax(float x, float y) restrict(amp) { return Concurrency::fast_math::fmax(x, y); }
static float pow(float x, float y) restrict(amp) { return Concurrency::fast_math::pow(x, y); }
};
template<>
@@ -42,8 +51,18 @@ class math_helper<double> : public math_helper_cpu<double>
using math_helper_cpu<double>::sqrt;
using math_helper_cpu<double>::rsqrt;
using math_helper_cpu<double>::tan;
using math_helper_cpu<double>::fabs;
using math_helper_cpu<double>::floor;
using math_helper_cpu<double>::fmin;
using math_helper_cpu<double>::fmax;
using math_helper_cpu<double>::pow;
static double sqrt(double x) restrict(amp) { return Concurrency::precise_math::sqrt(x); }
static double rsqrt(double x) restrict(amp) { return Concurrency::precise_math::rsqrt(x); }
static double tan(double x) restrict(amp) { return Concurrency::precise_math::tan(x); }
static double fabs(double x) restrict(amp) { return Concurrency::precise_math::fabs(x); }
static double floor(double x) restrict(amp) { return Concurrency::precise_math::floor(x); }
static double fmin(double x, double y) restrict(amp) { return Concurrency::precise_math::fmin(x, y); }
static double fmax(double x, double y) restrict(amp) { return Concurrency::precise_math::fmax(x, y); }
static double pow(double x, double y) restrict(amp) { return Concurrency::precise_math::pow(x, y); }
};
@@ -63,4 +63,159 @@ class perspective_camera
return ray<fp_t>(eye, (front + r + u).normalize());
}
};
template <typename fp_t>
class color
{
public:
fp_t r;
fp_t g;
fp_t b;
explicit color(fp_t r, fp_t g, fp_t b) restrict(cpu, amp) : r(r), g(g), b(b) {}
color(const color& other) restrict(cpu, amp) : r(other.r), g(other.g), b(other.b) {}
color operator+(const color& c) const restrict(cpu, amp)
{
return color(r + c.r, g + c.g, b + c.b);
}
color operator*(fp_t s) const restrict(cpu, amp)
{
return color(r * s, g * s, b * s);
}
color operator*(const color& c) const restrict(cpu, amp)
{
return color(r * c.r, g * c.g, b * c.b);
}
static color black() restrict(cpu, amp) { return color(0.0f, 0.0f, 0.0f); }
static color white() restrict(cpu, amp) { return color(1.0f, 1.0f, 1.0f); }
static color red() restrict(cpu, amp) { return color(1.0f, 0.0f, 0.0f); }
static color green() restrict(cpu, amp) { return color(0.0f, 1.0f, 0.0f); }
static color blue() restrict(cpu, amp) { return color(0.0f, 0.0f, 1.0f); }
};
class material
{
public:
enum material_type
{
material_checker,
material_phong
};
template<typename fp_t>
color<fp_t> sample(ray<fp_t> ray, vector3<fp_t> position, vector3<fp_t> normal) const restrict(cpu, amp)
{
switch (type)
{
case material_checker:
return static_cast<const checker<fp_t>*>(this)->sample_impl(ray, position, normal);
case material_phong:
return static_cast<const phong<fp_t>*>(this)->sample_impl(ray, position, normal);
default:
return color<fp_t>::black();
}
}
protected:
explicit material(int type) restrict(cpu, amp) : type(type) {}
private:
int type;
};
template <typename fp_t>
class checker : public material
{
public:
checker(fp_t scale, fp_t reflectiveness) restrict(cpu, amp) : material(material_checker), scale(scale), reflectiveness(reflectiveness) {}
color<fp_t> sample_impl(ray<fp_t> ray, vector3<fp_t> position, vector3<fp_t> normal)const restrict(cpu, amp)
{
fp_t r = math_helper<fp_t>::fabs(math_helper<fp_t>::floor(position.x * scale) + math_helper<fp_t>::floor(position.z * scale));
return (static_cast<int>(r) % 2) < 1 ? color<fp_t>::black() : color<fp_t>::white();
}
private:
fp_t scale;
fp_t reflectiveness;
};
template <typename fp_t>
class phong : public material
{
public:
phong(color<fp_t> diffuse, color<fp_t> specular, fp_t shininess, fp_t reflectiveness)
: material(material_phong), diffuse(diffuse), specular(specular), shininess(shininess), reflectiveness(reflectiveness) {}
color<fp_t> sample_impl(ray<fp_t> ray, vector3<fp_t> position, vector3<fp_t> normal)const restrict(cpu, amp)
{
vector3<fp_t> light_dir(0.5773503f, 0.5773503f, 0.5773503f);
color<fp_t> light_color(color<fp_t>::white());
fp_t n_dot_l = normal.dot(light_dir);
vector3<fp_t> h((light_dir - ray.direction).normalize());
fp_t n_dot_h = normal.dot(h);
color<fp_t> diffuse_term = diffuse * math_helper<fp_t>::fmax(n_dot_l, 0.0f);
color<fp_t> specular_term = specular * math_helper<fp_t>::pow(math_helper<fp_t>::fmax(n_dot_h, 0.0f), shininess);
return light_color * (diffuse_term + specular_term);
}
private:
color<fp_t> diffuse;
color<fp_t> specular;
fp_t shininess;
fp_t reflectiveness;
};
template <typename fp_t>
class sphere
{
public:
vector3<fp_t> center;
fp_t radius;
explicit sphere(const vector3<fp_t>& center, fp_t radius) restrict(cpu, amp) : center(center), radius(radius) { init(); }
sphere(const sphere& other) restrict(cpu, amp) : center(other.center), radius(other.radius) { init(); }
intersect_result<fp_t> intersect(const ray<fp_t>& ray) const restrict(amp)
{
vector3<fp_t> v = ray.origin - center;
fp_t a0 = v.sqr_length() - sqr_radius;
fp_t d_dot_v = ray.direction.dot(v);
if (d_dot_v <= 0 )
{
fp_t discr = d_dot_v * d_dot_v - a0;
fp_t distance = -d_dot_v - math_helper<fp_t>::sqrt(discr);
vector3<fp_t> position(ray.get_point(distance));
if (discr >= 0)
{
return intersect_result<fp_t>(
true,
0,
distance,
position,
(position - center).normalize()
);
}
}
return intersect_result<fp_t>();
}
private:
fp_t sqr_radius;
void init()
{
sqr_radius = radius * radius;
}
};
Oops, something went wrong.

0 comments on commit 2ac6e47

Please sign in to comment.