Lab 06: October 23, 2018

Overview

Today's lab is to calculate the collision of a ray with a sphere object from the rays cast manually from the camera's perspective.

Tasks

The code provided can be used to create rays that cast out through each pixel on the screen, today's task is to create a sphere in front of the camera and simply determine if any of those rays collide with the sphere.

The value RelX linearly interpolate between the left column of pixels and the right column of pixels representing the values inbetween -1 and 1 respectively. Similarly he value RelY linearly interpolate between the bottom row of pixels and the top row of pixels representing the values inbetween -1 and 1 respectively.

We can interpret these values as rays in the real world if we know (or create) the values: FOVX the field of view along the x axis
FOVY the field of view along the x axis
NEAR the distance to the near clip plane

And use the camera's transformation matrix to get the camera's CamPosition, CamRight, CamUp and CamAt vectors.
RayVector = CamRight * tan(FOVX) * NEAR * RelX + CamUp * tan(FOVY) * NEAR * RelY + CamAt * NEAR

RayVector represents the position in the world relative to the camera. We can construct a line segment that represents the rays cast from the camera into the scene by defining a start and end point of this ray.
LineStart = CamPosition + RayVector
LineEnd = LineStart + 1000.0f * RayVector


Using: LineSphere.pdf We can create a simple function to set a pixel red if a intersection occurs between the ray and the sphere, or black if no intersections occurs. Note: For this exercise, if any intersections occur (i.e 1 or 2) we would want to return the colour red and if 0 intersections occur, return black.

gsPixelBuffer[x,y] = DoesCollide(x, y, Sphere);


Note: You can start with NEAR=1, FOVX=1 and FOVY=1
Note: You can start with CamPosition=<0, 0, 1>, CamRight=<1, 0, 0>, CamUp=<0, 1, 0> and CamAt=<0, 0, -1>


Note: You must hit 'r' to tell the screen to refresh the pixels.

Code Files

Lab06.cpp
makefile
colour.h
mathvector.h

References

OpenGL: glDrawPixels