Wednesday, March 29, 2017

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


enter image description here


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.



Answer




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;
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...