コード例 #1
0
/////////////
// Methods //
/////////////
color environmentMap::evaluate(const ray& r) const
{
  // inverse transform the ray
  ray transformedRay(r);
  transformedRay.inverseTransform(_transformation);

  // convert ray direction to texture coordinate
  float theta = acos(transformedRay.direction().z);
  float phi = atan2(transformedRay.direction().y, transformedRay.direction().x);

  if(phi < 0.0f) phi += 2.0f * M_PI;

  theta /= M_PI;
  phi /= 2.0f * M_PI;

  // get texel coordinate
  float texelU = std::min(phi * (_texture.width()-1), (float)(_texture.width()-1));
  float texelV = std::min(theta * (_texture.height()-1), (float)(_texture.height()-1));

  // compute corners
  float left = floor(texelU);
  float right = ceil(texelU);
  float top = floor(texelV);
  float bottom = ceil(texelV);

  // linear interpolate along X
  float w = texelU - left;
  color topColor = _texture(left,top)*(1.0f-w) + _texture(right,top)*w;
  color bottomColor = _texture(left,bottom)*(1.0f-w) + _texture(right,bottom)*w;

  // linear interlate along Y
  w = texelV - top;
  return (1.0f - w)*topColor + w*bottomColor;
}
コード例 #2
0
ファイル: Transform.cpp プロジェクト: alexunder/X-toys
bool Transform::intersect(const Ray &r, Hit &h, float tmin)
{
	Vec3f original = r.getOrigin();
	Vec3f dir = r.getDirection();

	mReverseMat.Transform(original);
	mReverseMat.TransformDirection(dir);


	Ray transformedRay(original, dir);

	if (mObj->intersect(transformedRay, h, tmin))
	{
        Vec3f normal = h.getNormal();
        Matrix t;
        mReverseMat.Transpose(t);
        t.TransformDirection(normal);
        h.set(h.getT(), h.getMaterial(), normal, r);
        return true;
	}

    return false;
}