# Problem with reflection in raytracing

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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<triangle>::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

##### Share on other sites
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; 		}

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632955
• Total Posts
3009450
• ### Who's Online (See full list)

There are no registered users currently online

×