Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with reflection in raytracing

Started by Icebraker Jul 19, 2008 at 11:50 PM 0 replies 800+ views
Original Post
Icebraker
Icebraker
Hi, I'm currently writing a triangle-raytracer using the triangle intersection by Moeller & Trumbore. I can raytrace a normal scene perfectly, but I don't get reflections working properly. Obviously I misunderstood something about reflections... However, here is my code: Vec3f dir=-originOfOriginalRay+pointOfIntersection; dir.Normalize(); //! Compute new ray Vec3f reflect = 2.0*dir.Dot3(normal)*normal-dir; reflect.Normalize(); for(vector::iterator itReflect = triangleList->begin(); itReflect != triangleList->end(); itReflect++) { Vec3f vertex0(itReflect->getVertices(0,0),itReflect->getVertices(0,1),itReflect->getVertices(0,2)); Vec3f vertex1(itReflect->getVertices(1,0),itReflect->getVertices(1,1),itReflect->getVertices(1,2)); Vec3f vertex2(itReflect->getVertices(2,0),itReflect->getVertices(2,1),itReflect->getVertices(2,2)); Vec3f edge1=vertex1-vertex0; Vec3f edge2=vertex2-vertex0; if(intersect_triangle(pointOfIntersection, reflect,vertex0,edge1,edge2,&t,&u,&v)==1) { //! Enters this part all the time... } } I know that the normals and my pointOfIntersection are OK as phong lighting works perfectly, but I always detect an intersection of my reflection ray, even if it should just reflect out of the scene. Does anybodt spot a mistake in my code? Icebraker
cignox1
cignox1
When you create a new ray after an intersection occourred, you should always offset the origin point of the new raz, to avoid the famous self intersection problem due to floating point precision limits:

vector reflection_origin = intersection_point + surface_normal * EPSILON

where epsilon is a small value (i.e 0.001).

Have you already implemented shadow rays? Because you should get this error with them too...

By the way, this is the code I use to reflect a vector:
		const vector4<T> Reflect(const vector4<T> &n) const 		{ 			return *this - (T(2) * (this->Dot(n))) * n; 		}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.