Squishy Maps for Soft Body Modelling Using Generalised Chain Mail
KIT308/408 (Advanced) Multicore Architecture and Programming
Crash Course – Raytracing
Dr. Ian Lewis
Discipline of ICT, School of TED
University of Tasmania, Australia
1
Need a complex and interesting problem for the assignments
Raytracing
Just like the Mandelbrot set, don’t really need to know how this works completely to be able to do the assignments
Lots of other similarities with Mandelbrot
Generating an image
Working with different coordinates for the image and where it’s calculated from
2
Raytracing
Raytracing Basics
3
Semester 2
2015
3
Trace a bunch of “rays” from the position of the camera (/eye) and see what they hit
The opposite of what happens in the real world where light bounces off things and hits our retinas (/camera light sensor)
When a ray intersects with an object
Work out whether it is lit by any lights in the scene
Calculate the effect each light generates
Shadows appear when lights aren’t direction visible from the intersection
4
Raytracing
View plane
Camera/Eye position
Rays
through
view plane
For each sample:
Construct ray from eye position through view plane
Find first surface intersected by ray through pixel
Compute colour sample based on lighting
5
Raytracing
Samples on
view plane
Eye position
Rays
through
view plane
Code loops over the width and height of the image to be generated
Calculates a ray from the fixed camera point proportional to current pixel
Image RayCast(Camera camera, Scene scene, int width, int height)
{
Image image = new Image(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Ray ray = ConstructRayThroughPixel(camera, i, j);
Intersection hit = FindIntersection(ray, scene);
image[i][j] = GetColor(hit);
}
}
return image;
}
6
Pseudocode: Rays
Intersections with geometric primitives
Spheres
Boxes
Others are possible, but tried to limit the size of the code
Image RayCast(Camera camera, Scene scene, int width, int height)
{
Image image = new Image(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Ray ray = ConstructRayThroughPixel(camera, i, j);
Intersection hit = FindIntersection(ray, scene);
image[i][j] = GetColor(hit);
}
}
return image;
}
7
Pseudocode: Intersections
Ray: P = P0 + tV
Sphere: |P - C|2 - r2 = 0
Substituting for P, we get:
|P0 + tV - C|2 - r2 = 0
Solve quadratic equation:
at2 + bt + c = 0
where:
a = |V|2 = 1
b = 2 V • (P0 - C)
c = |P0 - C|2 - r2
Ignoring the maths, there are either 0, 1, or 2 solutions
We want the closest one to the camera origin (P0)
8
Ray-Sphere Intersection
First, intersect ray with six planes
Ensure hits are within bounds of other planes
Find out which one is closest hit
9
Ray-(Axis Aligned Bounding)Box Intersection
First, intersect ray with plane
Easier maths
Then, check if point is inside triangle
Harder maths
10
Ray-Triangle Intersection
P
P0
V
Find closest intersection primitive
Intersection FindIntersection(Ray ray, Scene scene)
{
min_t = infinity
min_primitive = NULL
For each primitive in scene {
t = Intersect(ray, primitive);
if (t > 0 && t < min_t) then
min_primitive = primitive
min_t = t
}
}
return Intersection(min_t, min_primitive)
}
11
Pseudocode: Intersections
A
B
C
D
E
F
Assignment Code
12
main()
Read command line arguments
Render image
A number of times if requested (for timing)
Report times
Save .BMP file
render()
Loop through pixels
Loop through subsamples
Generate ray through pixel
Raytrace
Store (averaged) value in image
traceRay()
Check for object intersections
Apply lighting
Do reflection or refraction if needed
And loop again
Return calculated colour value for this sample
calculateRefraction()
Bend the ray through object
calculateReflection()
Bounce the ray off object
13
Raytracer.cpp
isSphereIntersected()
Check to see if ray hits specified sphere
Return true if so,
Stores result in t parameter
isBoxIntersected()
Check to see if ray hits specified box
Return true if so
Stores result in t parameter
objectIntersection()
Check to see if ray hits any sphere or box
Return true is so
Stores point of intersection in intersect parameter
calculateIntersectionResponse()
Calculate normal at point of intersection
Store material of object intersected with
Work out if ray is inside object (for refractioin)
14
Intersection.cpp
applyLighting()
Calculate all lighting for a given intersection point
Accumulates light from all light sources
Only adds lighting if not in shadow
Adds both diffuse and specular lighting
isInShadow()
Checks to see if any object between light and intersection point (i.e. is point in shadow)
applyDiffuse()
Applies texture and basic diffuse lighting
applySpecular()
Applies shininess
15
Lighting.cpp
applyCheckerboard()
Calculate colour based on basic checkboard texture
applyCircles() and applyWood()
Calculate colour based on mathematically defined texture
All textures are 3D textures
Rather than wrapping a 2D texture around the object, it’s like they are carved out of a solid block of material
Simpler approach than UV-mapping, but textures harder to create
16
Texturing.cpp
Helpers for
Loading the scene
Saving the .BMP image
Dealing with the nitty gritty of Scene IO
1. https://i.pinimg.com/originals/8f/11/19/8f1119326013d817b5e62df789bc2033.jpg
17
Scene.cpp, ImageIO.cpp, and Config.cpp
Defines the entire Colour struct
Functions for
Converting to/from pixel formats
Accumulating colour
Other colour arithmetic operators
1. https://coloursenseandstyle.com.au/wp-content/uploads/2017/04/aspergers.jpg
18
Colour.h
Constants for
Maximum image buffer size
Mathematical constants
Raytracing limits
// maximum size of image
const int MAX_WIDTH = 2048, MAX_HEIGHT = 2048;
// math constants
const float PI = 3.14159265358979323846f;
const float PIOVER180 = 0.017453292519943295769236907684886f;
// a small value (used to make sure we don't get stuck detecting collision of the same object over and over)
const float EPSILON = 0.01f;
// maximum ray distance
const float MAX_RAY_DISTANCE = 2000000.0f; //** maybe should be maxfloat?!
// the maximum number of rays to cast before giving up on finding final ray destination
const int MAX_RAYS_CAST = 10;
// default refractive index (of air effectively)
const float DEFAULT_REFRACTIVE_INDEX = 1.0f;
19
Constants.h
Definitions for structs
Point
Vector
Ray
And their arithmetic operators
1. https://i.pinimg.com/originals/2e/50/44/2e504410643b8a7a2079d3e8e89ddbc4.gif
20
Primitives.h
Definitions for structs
Material
Sphere
Box
Light
1. http://payload.cargocollective.com/1/2/86419/2064565/triangle_o.gif
21
SceneObjects.h
Timer class
Cross-platform CPU timing
Supports Cell (SPU and PPU) and Windows
So not really that many platforms
1. http://lifepalette.com/wp-content/uploads/2017/06/In-the-tech-industry-timing-is-everything_Link.jpg
22
Timer.h
“Ray Casting” Aaron Bloomfield, CS 445: Introduction to Graphics, Fall 2006
23
References