# Ray tracing - draw polygon (square/bounded plane)

I'm going on with my own ray tracer as an iPad app for a school project. This is the result with soft shadow, antialiasing, pure reflective and pure transparent object:

Now i want to change the skybox, implemented with the method contained here http://www.ics.uci.edu/~gopi/CS211B/RayTracing%20tutorial.pdf with a real cube that wrap all the scene. In this way I can display the soft shadow projected on the floor. I read a lot of documentation about ray tracing polygon and I understood how to check if the ray intersect the polygon plane. Now my question is: if I want to draw a square, one for each side of my cube that will wrap the scene, how do I check if the point of intersecton is inside the square/polygon (so that i can shade it)? All the documentation seems so vague and incomplete. I can't find a complete explanation with some example, and maybe with some pseudo code, that really explain how to draw a square (bounded plane) in a ray tracer.

Thanks for helping me.

• You probably will have better luck doing something like a cube map lookup. If your Ray misses all geometry, you use the direction of the Ray as the parameters to a texture lookup. No geometry test needed! – Alan Wolfe Aug 5 '15 at 21:46
• Hi @AlanWolfe cube map look up is what i'm doing at the moment :) any other suggestion? – Fabrizio Duroni Aug 5 '15 at 21:49
• If you already have a cube map, what benefit do you gain by changing that to a cube test? Seems like you already have what you want for cheaper! – Alan Wolfe Aug 5 '15 at 21:51
• The problem is that with cube mapping i'm not able to project the shadow of the spheres on the floor, as you can see from the picture above. Do you have any suggestion about how i can test if part of the cube map is in shadow? – Fabrizio Duroni Aug 5 '15 at 21:55
• I dont mean to be rude, but what have you tried so far? Just compute intersection with the plane t=normal.Dot(origin-posR)/normal.Dot(direction) and then just determine whether it lies within using (u,v) coordinates. – wondra Aug 6 '15 at 15:48

I finally managed it. I choose one of the most simple method, that could be found here https://sites.google.com/site/justinscsstuff/object-intersection but could also be found in some of the documents linked by wondra in the comments above.

This method work for convex polygon (for other kind odd/even rule, winding number rule or other method must be used). The point is simple: a generalization of the test used for triangle.

Check that the point is always on the left of an edge, by checking the dot product between the normal of the polygon and the result of the cross product between and edge and a vector from the current vertex to the intersection point. Some optimisation could be useful (for example: precalculate the edge list and avoid to calculate them every time). Here is my code in Objective-c.

-(NSMutableDictionary *)intersect:(Ray *)ray {

//Check intersection of ray with polygon ray.
NSMutableDictionary *intersectionData = [self intersectWithPlaneOfPolygon:ray];

if(intersectionData == nil) {

return nil;
}

Point3D *intersectionPoint = [intersectionData objectForKey:@"point"];
NSUInteger numberOfVertex = self.vertexList.count;

for (int i = 0; i < numberOfVertex; i++) {

Point3D *nextVertex = [self.vertexList objectAtIndex:((i + 1) % numberOfVertex)];
Point3D *currentVertex = [self.vertexList objectAtIndex:i];
Vector3D *edge = [nextVertex diff:currentVertex];

Vector3D *edge = [self.edgeList objectAtIndex:i];
Vector3D *vectorWithIntersection = [intersectionPoint diff:currentVertex];

Vector3D *crossProduct = [edge cross:vectorWithIntersection];

float dotProduct = [crossProduct dot:self.normal];

if(dotProduct < 0) {

//Point is outside polygon.
return nil;
}
}

return intersectionData;
}

• How did you solved the texture sampling in the end, or did you not need to have the rectangle textured? – wondra Aug 9 '15 at 20:46