Saturday, April 9, 2011

CG2 Assignment 4

So we had to do procedural shading this time. Checkerboard texture was required so here it is:





Interesting bit about this is that I had a really hard time finding out how to calculate UV coordinates for a quad. If you use triangles, you can apparently do some fancy stuff with barycentric coordinates to figure out UVs, but I couldn't really work that out and my triangle intersection code generates parametric coordinates instead (I guess this is more efficient for intersection tests). So what I ended up doing instead was figuring out the math on my own using vector projections as described on this wikipedia page. Here's my method:

After I've got the intersection registered for the quad, I do the following calculations:

  1. Math::Point3D PoS = ray.origin + ray.dir*intersection.distanceFromEye;

  2. Math::Vec3D uvOffsets(PoS - myOrigin);

  3. intersection.u = Math::Dot(myEdge1, uvOffsets) / myEdge1.lengthSquared();

  4. intersection.v = Math::Dot(myEdge2, uvOffsets) / myEdge2.lengthSquared();

And that's it, intersection.u/v holds the UV coordinates. The cool thing is that the actual projection equation to get the components of the offset relative to the edges of the quad actually only requires the dot product to be divided by the edge's length, but then to normalize the coordinates to u/v space I needed to divide by the length again. Obviously that means that the two divides could be simplified by dividing by the length of the vector squared, avoiding a costly square root calculation on both parts.

I'll update this space as I finish the extras. I'm shooting for a mandelbrot as extra #1.


Update #1: Mandelbrot


Here it is:



Kinda hard to see, but it's there. I'm just drawing it on the floor quad which is why it's at a weird angle.

No comments:

Post a Comment