SlideShare a Scribd company logo
The Day You 
Finally Use Algebra! 
Janie Clayton-Hasz
About Me
But math is hard! 
(Let’s go shopping!)
Math is hard. 
But math is fun too.
Demo
Normalized 
Coordinate 
Systems
Cartesian 
Coordinates
320 
480
320 
480 
or 
568
1 
1
(0,0) (1,0) 
(0,1) (1,1)
(0,0) (1,0) 
(0,1) (1,1)
(0,0) (1,0) 
(0,1) (1,1)
self.size.width 
self. 
size. 
height
- Colors, like the 
screen dimensions, 
are based on 
percentages rather 
than absolute 
values. 
- If you come from 
a graphic design 
background, you 
need to convert 
your 255 scale to 
percentages.
Algorithm Rosetta 
Stone
Rosetta Stone 
- Had the same text in 
Greek, demotic, and 
hieroglyphics. Was 
used to translate 
hieroglyphics 
- Going to do similar 
thing, but with math 
algorithms, plain 
English, and code
√-1 2ˆ3 Σ π
Algoritm 
Σ5 
i = 1 
4i 
I have a starting value of one. 
I have an end value of five. 
I want to multiply each value 
by four and add them together.
Plain English 
I have a starting value of one. 
I have an end value of five. 
I want to multiply each value 
by four and add them together.
var x = 0 
! 
for index in 1…5 { 
! 
x += (4 * index) 
! 
} 
Code
It’s All Greek to 
Me 
π 
Δ 
i 
θ 
Constant: 3.14159… 
Change between two values 
Square root of negative one 
Variable representing an angle 
Consolidate slide, get rid of alpha and beta
√-1 2ˆ3 Σ π 
…and it was delicious!
i 8 sum pi 
…and it was delicious!
Trigonometry
Triangles 
A shape with three sides where the 
angles add up to 180 degrees 
Everything in our world comes back 
to triangles 
The most stable shape 
Foundation of 3D graphics
Right Triangles
Pythagorean 
Theorem 
aˆ2 + bˆ2 = cˆ2 
Distance 
- Need to figure out better examples/stories
Triangle Formulas 
Tangent 
Sin 
Cosine 
Arctangent 
Arcsin 
Arccosine
Triangle Formulas
Triangle Formulas
Circle Formulas 
Circumference: 2πr 
Area: πrˆ2
So What Can We Do 
Knowing This? 
Change the direction a character is 
moving in 
Check to see if the user is hitting a 
target area on the screen 
Draw shapes and filters in specific 
configurations
Demo
Linear Algebra 
Google “Better Explained Rotation” 
Matrices 
Complex numbers 
Show how to use linear algebra to do more efficient affine transforms 
rotation through matrix multiplications
What is Linear 
Algebra? 
Linear Algebra allows you to 
perform an action on many values 
at the same time. 
This action must be consistent 
across all values, such as multiplying 
every value by two.
What is Linear 
Algebra? 
Values are placed in an object 
called a matrix and the actions 
performed on the values are called 
transforms 
Linear algebra is optimized for 
parallel mathematical operations.
Data Types 
vec2, vec3, vec4: 2D, 3D, and 4D 
floating point vector objects. 
vec2: (x, y) 
vec3: (x, y, z) 
vec4: (r, g, b, a)
Data Types 
mat2, mat3, mat4: 2, 3, and 4 
element matrices. 
mat2: Holds a 2 X 2 number matrix 
mat3: Holds a 3 X 3 number matrix, 
used for 2D linear algebra 
mat4: Holds a 4 X 4 number matrix, 
used for 3D linear algebra
Enter the Matrix
Column 
1.0 1.0 1.0 0 
1.0 1.0 1.0 0 
1.0 1.0 1.0 0 
1.0 1.0 1.0 0 
Row
mat4 genericMatrix = mat4( 
! 
1.0, 1.0, 1.0, 1.0, 
1.0, 1.0, 1.0, 1.0, 
1.0, 1.0, 1.0, 1.0, 
0, 0, 0, 0 
Column 
); 
Row 
Column major vs Row major??
vec4 firstColumn = vec4(1.0, 1.0, 1.0, 
1.0); 
vec4 secondColumn = vec4(1.0, 1.0, 1.0, 
1.0); 
vec4 thirdColumn = vec4(1.0, 1.0, 1.0, 
1.0); 
vec4 fouthColumn = vec4(0, 0, 0, 0); 
mat4 myMatrix = mat4( 
firstColumn, SecondColumn, 
thirdColumn, FourthColumn 
);
CGAffineTransform
Affine, Wha?? :( 
A transform is any function that 
alters the size, position, or rotation 
of an object on your screen. 
Four types: Identity, Translate, 
Rotation, and Scale. 
For a transform to be affine, the 
lines in your shape must be parallel.
CGAffine 
Transform Methods 
CGAffineTransformMakeRotation 
(GLFloat angle); 
CGAffineTransformMakeScale 
(CGFLoat sx, CGFloat sy); 
CGAffineTransformMakeTranslation 
(CGFloat tx, CGFloat ty); 
Affine Transform Rotate 
Talk about chaining transforms 
CAAffineTransform
struct CAAffineTransform { 
CGFloat a; 
GLFloat b; 
CGFloat c; 
CGFloat d; 
CGFloat tx; 
CGFloat ty 
}
new point x = a * x 
+ c * y + tx; 
new point y = b * x 
+ d * y + ty; 
Show the matrix of the vector and the actual math around it
How Does This 
Work? 
For each point in your shape, the 
computer uses this calculation to 
figure out where the point should 
be. 
If you have a rectangle, this gets 
run four times: One for each point 
in your shape.
Refraction 
Fragment 
Shader 
Example
void main() 
{ 
highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * 
aspectRatio + 0.5 - 0.5 * aspectRatio)); 
highp float distanceFromCenter = distance(center, textureCoordinateToUse); 
lowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius); 
distanceFromCenter = distanceFromCenter / radius; 
highp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * 
distanceFromCenter); 
highp vec3 sphereNormal = normalize(vec3(textureCoordinateToUse - center, 
normalizedDepth)); 
highp vec3 refractedVector = 2.0 * refract(vec3(0.0, 0.0, -1.0), sphereNormal, 
refractiveIndex); 
refractedVector.xy = -refractedVector.xy; 
highp vec3 finalSphereColor = texture2D(inputImageTexture, (refractedVector.xy + 1.0) * 
0.5).rgb; 
// Grazing angle lighting 
highp float lightingIntensity = 2.5 * (1.0 - pow(clamp(dot(ambientLightPosition, 
sphereNormal), 0.0, 1.0), 0.25)); 
finalSphereColor += lightingIntensity; 
// Specular lighting 
lightingIntensity = clamp(dot(normalize(lightPosition), sphereNormal), 0.0, 1.0); 
lightingIntensity = pow(lightingIntensity, 15.0); 
finalSphereColor += vec3(0.8, 0.8, 0.8) * lightingIntensity; 
gl_FragColor = vec4(finalSphereColor, 1.0) * checkForPresenceWithinSphere; 
} 
So what this calculation does is it adjusts for a non-square aspect ratio of the image. The image aspect ratio is passed in as a uniform, and this adjusts the 
normally 0.0-1.0 texture coordinate for the Y axis to instead be from 0.0-1.0*(imageHeight/imageWidth). It has to expand the Y axis coordinate about its 
center point (0.5), thus the weird addition and subtraction in there. If you don’t do this, your sphere turns into an egg in non-square images.
highp vec2 textureCoordinateToUse = 
vec2(textureCoordinate.x, 
(textureCoordinate.y * aspectRatio + 
0.5 - 0.5 * aspectRatio)); 
So what this calculation does is it adjusts for a non-square aspect ratio of the image. The image aspect ratio is passed in as a uniform, and this adjusts the 
normally 0.0-1.0 texture coordinate for the Y axis to instead be from 0.0-1.0*(imageHeight/imageWidth). It has to expand the Y axis coordinate about its 
center point (0.5), thus the weird addition and subtraction in there. If you don’t do this, your sphere turns into an egg in non-square images.
highp float 
distanceFromCenter = 
distance(center, 
textureCoordinateToUse); 
This is a Pythagorean distance calculation to determine how far the current pixel (texture coordinate) is from the center that we’ve provided as a uniform. 
It’s a sqrt(xdiff^2 + ydiff^2) calculation.
lowp float 
checkForPresenceWithinSphere 
= step(distanceFromCenter, 
radius); 
The step() function returns 1 if the second value is greater than the first, 0 if not. I use these to avoid if() statements, which are expensive in fragment 
shaders (branching does not work well in massively parallel operations).
distanceFromCenter = 
distanceFromCenter / 
radius; 
This normalizes the distance from the center to be 0.0 for a value at the center of the sphere, and 1.0 for a value at the edge of the sphere. Values outside 
that range will get filtered out by the product of the above step() calculation later on.
highp float normalizedDepth = 
radius * sqrt(1.0 - 
distanceFromCenter * 
distanceFromCenter); 
This is where we calculate the Z height that a sphere, cut in half, would extend above the plane of the image. This is another geometrical calculation based 
on knowing the distance of our point from the center of the sphere.
highp vec3 sphereNormal = 
normalize(vec3! 
(textureCoordinateToUse - 
center, normalizedDepth)); 
Once we know the height of the spherical cap at that point, we can calculate the normal for that point on the sphere’s surface. Think of it as a ray that 
extends from the center of the sphere to the surface at this X, Y coordinate. The normal is based on the center of the sphere, so we subtract our aspect-ratio- 
adjusted-coordinate from the center to get the relative X, Y coordinate from the center of the sphere. The Z component is the height of the sphere 
we just calculated.
highp vec3 refractedVector = 
refract(vec3(0.0, 0.0, -1.0), ! 
sphereNormal, 
refractiveIndex); 
With the normal, we can calculate the refraction of light from that point on the sphere’s surface. The refraction calculation uses the surface normal, the 
refractiveIndex (a material property that you pass in, I think I use glass’s here), and a ray direction. I believe the ray direction here is from the eye going 
into the screen, although I can never remember positive/negative Z directions in OpenGL. This then generates a refracted vector, pointing in the direction 
light would as it refracts through a sphere.
gl_FragColor = 
texture2D(inputImageTexture, 
(refractedVector.xy + 1.0) * 0.5) 
* checkForPresenceWithinSphere; 
We then take this refracted vector, which is in the -1.0-1.0 coordinate space, and adjust it to the 0.0-1.0 coordinate space for texture sampling. We read 
the texture color at the location pointed to by that vector. The earlier step() function to determine if a point was within the sphere comes into play here, 
where we only display a color if the point was within the sphere. If it was not, we output 0.0, 0.0, 0.0, 0.0 as an RGBA color because that’s the result of 
multiplying with 0.
The End

More Related Content

What's hot

Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Projection Matrices
Projection MatricesProjection Matrices
Projection Matrices
Syed Zaid Irshad
 
numerical methods
numerical methodsnumerical methods
numerical methods
HaiderParekh1
 
6161103 10.4 moments of inertia for an area by integration
6161103 10.4 moments of inertia for an area by integration6161103 10.4 moments of inertia for an area by integration
6161103 10.4 moments of inertia for an area by integrationetcenterrbru
 
6161103 10.5 moments of inertia for composite areas
6161103 10.5 moments of inertia for composite areas6161103 10.5 moments of inertia for composite areas
6161103 10.5 moments of inertia for composite areasetcenterrbru
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
Kamal Acharya
 
3 d transformations
3 d transformations3 d transformations
3 d transformations
Ankit Garg
 
Intro to scan conversion
Intro to scan conversionIntro to scan conversion
Intro to scan conversionMohd Arif
 
A mid point ellipse drawing algorithm on a hexagonal grid
A mid  point ellipse drawing algorithm on a hexagonal gridA mid  point ellipse drawing algorithm on a hexagonal grid
A mid point ellipse drawing algorithm on a hexagonal grid
S M K
 
Interpolation
InterpolationInterpolation
Interpolation
Bhavik A Shah
 
2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear Regression
AkmelSyed
 
Unit 3
Unit 3Unit 3
probability assignment help
probability assignment helpprobability assignment help
probability assignment help
Statistics Homework Helper
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterization
Maaz Rizwan
 

What's hot (20)

Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Projection Matrices
Projection MatricesProjection Matrices
Projection Matrices
 
numerical methods
numerical methodsnumerical methods
numerical methods
 
6161103 10.4 moments of inertia for an area by integration
6161103 10.4 moments of inertia for an area by integration6161103 10.4 moments of inertia for an area by integration
6161103 10.4 moments of inertia for an area by integration
 
6161103 10.5 moments of inertia for composite areas
6161103 10.5 moments of inertia for composite areas6161103 10.5 moments of inertia for composite areas
6161103 10.5 moments of inertia for composite areas
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
3 d transformations
3 d transformations3 d transformations
3 d transformations
 
Intro to scan conversion
Intro to scan conversionIntro to scan conversion
Intro to scan conversion
 
The integral
The integralThe integral
The integral
 
A mid point ellipse drawing algorithm on a hexagonal grid
A mid  point ellipse drawing algorithm on a hexagonal gridA mid  point ellipse drawing algorithm on a hexagonal grid
A mid point ellipse drawing algorithm on a hexagonal grid
 
Es272 ch5b
Es272 ch5bEs272 ch5b
Es272 ch5b
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Interpolation
InterpolationInterpolation
Interpolation
 
2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)
 
Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear Regression
 
Unit 3
Unit 3Unit 3
Unit 3
 
probability assignment help
probability assignment helpprobability assignment help
probability assignment help
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterization
 
Es272 ch5a
Es272 ch5aEs272 ch5a
Es272 ch5a
 

Similar to The Day You Finally Use Algebra: A 3D Math Primer

3D Math Primer: CocoaConf Chicago
3D Math Primer: CocoaConf Chicago3D Math Primer: CocoaConf Chicago
3D Math Primer: CocoaConf Chicago
Janie Clayton
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Computer graphic
Computer graphicComputer graphic
Computer graphic
nusratema1
 
Primitives
PrimitivesPrimitives
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
Philip Schwarz
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
Mattupallipardhu
 
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
aravindangc
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Vishal Butkar
 
99995320.ppt
99995320.ppt99995320.ppt
99995320.ppt
MohdSaqib79
 
3 d scaling and translation in homogeneous coordinates
3 d scaling and translation in homogeneous coordinates3 d scaling and translation in homogeneous coordinates
3 d scaling and translation in homogeneous coordinates
KRIPA SHNAKAR TIWARI
 
Week 3-4 solutions
Week 3-4 solutionsWeek 3-4 solutions
Week 3-4 solutionsBrian Larson
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
ssuser255bf1
 
Trytten computergraphics(1)
Trytten computergraphics(1)Trytten computergraphics(1)
Trytten computergraphics(1)
nriaz469
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicing
Elsayed Hemayed
 
Computer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric TransformationsComputer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric Transformations
Chandrakant Divate
 
2 d transformation
2 d transformation2 d transformation
2 d transformation
Ankit Garg
 
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation PipelineComputer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
💻 Anton Gerdelan
 
Computer Graphics - transformations in 2d
Computer Graphics - transformations in 2dComputer Graphics - transformations in 2d
Computer Graphics - transformations in 2d
Hisham Al Kurdi, EAVA, DMC-D-4K, HCCA-P, HCAA-D
 
Computer Graphic - Transformations in 2D
Computer Graphic - Transformations in 2DComputer Graphic - Transformations in 2D
Computer Graphic - Transformations in 2D
2013901097
 

Similar to The Day You Finally Use Algebra: A 3D Math Primer (20)

3D Math Primer: CocoaConf Chicago
3D Math Primer: CocoaConf Chicago3D Math Primer: CocoaConf Chicago
3D Math Primer: CocoaConf Chicago
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Computer graphic
Computer graphicComputer graphic
Computer graphic
 
Primitives
PrimitivesPrimitives
Primitives
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
 
Windows and viewport
Windows and viewportWindows and viewport
Windows and viewport
 
99995320.ppt
99995320.ppt99995320.ppt
99995320.ppt
 
3 d scaling and translation in homogeneous coordinates
3 d scaling and translation in homogeneous coordinates3 d scaling and translation in homogeneous coordinates
3 d scaling and translation in homogeneous coordinates
 
Week 3-4 solutions
Week 3-4 solutionsWeek 3-4 solutions
Week 3-4 solutions
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
 
Trytten computergraphics(1)
Trytten computergraphics(1)Trytten computergraphics(1)
Trytten computergraphics(1)
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicing
 
Computer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric TransformationsComputer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric Transformations
 
2 d transformation
2 d transformation2 d transformation
2 d transformation
 
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation PipelineComputer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
 
Computer Graphics - transformations in 2d
Computer Graphics - transformations in 2dComputer Graphics - transformations in 2d
Computer Graphics - transformations in 2d
 
Computer Graphic - Transformations in 2D
Computer Graphic - Transformations in 2DComputer Graphic - Transformations in 2D
Computer Graphic - Transformations in 2D
 

More from Janie Clayton

Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
Janie Clayton
 
Beyond White: Embracing the iOS Design Aesthetic
Beyond White: Embracing the iOS Design AestheticBeyond White: Embracing the iOS Design Aesthetic
Beyond White: Embracing the iOS Design Aesthetic
Janie Clayton
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
Janie Clayton
 
GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015
Janie Clayton
 
GPU Programming: CocoaConf Atlanta
GPU Programming: CocoaConf AtlantaGPU Programming: CocoaConf Atlanta
GPU Programming: CocoaConf Atlanta
Janie Clayton
 
GPU Programming 360iDev
GPU Programming 360iDevGPU Programming 360iDev
GPU Programming 360iDev
Janie Clayton
 
Bug Hunting Safari
Bug Hunting SafariBug Hunting Safari
Bug Hunting Safari
Janie Clayton
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and Metal
Janie Clayton
 

More from Janie Clayton (8)

Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Beyond White: Embracing the iOS Design Aesthetic
Beyond White: Embracing the iOS Design AestheticBeyond White: Embracing the iOS Design Aesthetic
Beyond White: Embracing the iOS Design Aesthetic
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015GPU Programming: Chicago CocoaConf 2015
GPU Programming: Chicago CocoaConf 2015
 
GPU Programming: CocoaConf Atlanta
GPU Programming: CocoaConf AtlantaGPU Programming: CocoaConf Atlanta
GPU Programming: CocoaConf Atlanta
 
GPU Programming 360iDev
GPU Programming 360iDevGPU Programming 360iDev
GPU Programming 360iDev
 
Bug Hunting Safari
Bug Hunting SafariBug Hunting Safari
Bug Hunting Safari
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and Metal
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

The Day You Finally Use Algebra: A 3D Math Primer

  • 1. The Day You Finally Use Algebra! Janie Clayton-Hasz
  • 3. But math is hard! (Let’s go shopping!)
  • 4. Math is hard. But math is fun too.
  • 10.
  • 11. 1 1
  • 16. - Colors, like the screen dimensions, are based on percentages rather than absolute values. - If you come from a graphic design background, you need to convert your 255 scale to percentages.
  • 18. Rosetta Stone - Had the same text in Greek, demotic, and hieroglyphics. Was used to translate hieroglyphics - Going to do similar thing, but with math algorithms, plain English, and code
  • 20. Algoritm Σ5 i = 1 4i I have a starting value of one. I have an end value of five. I want to multiply each value by four and add them together.
  • 21. Plain English I have a starting value of one. I have an end value of five. I want to multiply each value by four and add them together.
  • 22. var x = 0 ! for index in 1…5 { ! x += (4 * index) ! } Code
  • 23. It’s All Greek to Me π Δ i θ Constant: 3.14159… Change between two values Square root of negative one Variable representing an angle Consolidate slide, get rid of alpha and beta
  • 24. √-1 2ˆ3 Σ π …and it was delicious!
  • 25. i 8 sum pi …and it was delicious!
  • 27. Triangles A shape with three sides where the angles add up to 180 degrees Everything in our world comes back to triangles The most stable shape Foundation of 3D graphics
  • 29. Pythagorean Theorem aˆ2 + bˆ2 = cˆ2 Distance - Need to figure out better examples/stories
  • 30. Triangle Formulas Tangent Sin Cosine Arctangent Arcsin Arccosine
  • 33. Circle Formulas Circumference: 2πr Area: πrˆ2
  • 34. So What Can We Do Knowing This? Change the direction a character is moving in Check to see if the user is hitting a target area on the screen Draw shapes and filters in specific configurations
  • 35. Demo
  • 36. Linear Algebra Google “Better Explained Rotation” Matrices Complex numbers Show how to use linear algebra to do more efficient affine transforms rotation through matrix multiplications
  • 37. What is Linear Algebra? Linear Algebra allows you to perform an action on many values at the same time. This action must be consistent across all values, such as multiplying every value by two.
  • 38. What is Linear Algebra? Values are placed in an object called a matrix and the actions performed on the values are called transforms Linear algebra is optimized for parallel mathematical operations.
  • 39. Data Types vec2, vec3, vec4: 2D, 3D, and 4D floating point vector objects. vec2: (x, y) vec3: (x, y, z) vec4: (r, g, b, a)
  • 40. Data Types mat2, mat3, mat4: 2, 3, and 4 element matrices. mat2: Holds a 2 X 2 number matrix mat3: Holds a 3 X 3 number matrix, used for 2D linear algebra mat4: Holds a 4 X 4 number matrix, used for 3D linear algebra
  • 42. Column 1.0 1.0 1.0 0 1.0 1.0 1.0 0 1.0 1.0 1.0 0 1.0 1.0 1.0 0 Row
  • 43. mat4 genericMatrix = mat4( ! 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0, 0, 0, 0 Column ); Row Column major vs Row major??
  • 44. vec4 firstColumn = vec4(1.0, 1.0, 1.0, 1.0); vec4 secondColumn = vec4(1.0, 1.0, 1.0, 1.0); vec4 thirdColumn = vec4(1.0, 1.0, 1.0, 1.0); vec4 fouthColumn = vec4(0, 0, 0, 0); mat4 myMatrix = mat4( firstColumn, SecondColumn, thirdColumn, FourthColumn );
  • 46. Affine, Wha?? :( A transform is any function that alters the size, position, or rotation of an object on your screen. Four types: Identity, Translate, Rotation, and Scale. For a transform to be affine, the lines in your shape must be parallel.
  • 47. CGAffine Transform Methods CGAffineTransformMakeRotation (GLFloat angle); CGAffineTransformMakeScale (CGFLoat sx, CGFloat sy); CGAffineTransformMakeTranslation (CGFloat tx, CGFloat ty); Affine Transform Rotate Talk about chaining transforms CAAffineTransform
  • 48. struct CAAffineTransform { CGFloat a; GLFloat b; CGFloat c; CGFloat d; CGFloat tx; CGFloat ty }
  • 49. new point x = a * x + c * y + tx; new point y = b * x + d * y + ty; Show the matrix of the vector and the actual math around it
  • 50. How Does This Work? For each point in your shape, the computer uses this calculation to figure out where the point should be. If you have a rectangle, this gets run four times: One for each point in your shape.
  • 52. void main() { highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio)); highp float distanceFromCenter = distance(center, textureCoordinateToUse); lowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius); distanceFromCenter = distanceFromCenter / radius; highp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * distanceFromCenter); highp vec3 sphereNormal = normalize(vec3(textureCoordinateToUse - center, normalizedDepth)); highp vec3 refractedVector = 2.0 * refract(vec3(0.0, 0.0, -1.0), sphereNormal, refractiveIndex); refractedVector.xy = -refractedVector.xy; highp vec3 finalSphereColor = texture2D(inputImageTexture, (refractedVector.xy + 1.0) * 0.5).rgb; // Grazing angle lighting highp float lightingIntensity = 2.5 * (1.0 - pow(clamp(dot(ambientLightPosition, sphereNormal), 0.0, 1.0), 0.25)); finalSphereColor += lightingIntensity; // Specular lighting lightingIntensity = clamp(dot(normalize(lightPosition), sphereNormal), 0.0, 1.0); lightingIntensity = pow(lightingIntensity, 15.0); finalSphereColor += vec3(0.8, 0.8, 0.8) * lightingIntensity; gl_FragColor = vec4(finalSphereColor, 1.0) * checkForPresenceWithinSphere; } So what this calculation does is it adjusts for a non-square aspect ratio of the image. The image aspect ratio is passed in as a uniform, and this adjusts the normally 0.0-1.0 texture coordinate for the Y axis to instead be from 0.0-1.0*(imageHeight/imageWidth). It has to expand the Y axis coordinate about its center point (0.5), thus the weird addition and subtraction in there. If you don’t do this, your sphere turns into an egg in non-square images.
  • 53. highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio)); So what this calculation does is it adjusts for a non-square aspect ratio of the image. The image aspect ratio is passed in as a uniform, and this adjusts the normally 0.0-1.0 texture coordinate for the Y axis to instead be from 0.0-1.0*(imageHeight/imageWidth). It has to expand the Y axis coordinate about its center point (0.5), thus the weird addition and subtraction in there. If you don’t do this, your sphere turns into an egg in non-square images.
  • 54. highp float distanceFromCenter = distance(center, textureCoordinateToUse); This is a Pythagorean distance calculation to determine how far the current pixel (texture coordinate) is from the center that we’ve provided as a uniform. It’s a sqrt(xdiff^2 + ydiff^2) calculation.
  • 55. lowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius); The step() function returns 1 if the second value is greater than the first, 0 if not. I use these to avoid if() statements, which are expensive in fragment shaders (branching does not work well in massively parallel operations).
  • 56. distanceFromCenter = distanceFromCenter / radius; This normalizes the distance from the center to be 0.0 for a value at the center of the sphere, and 1.0 for a value at the edge of the sphere. Values outside that range will get filtered out by the product of the above step() calculation later on.
  • 57. highp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * distanceFromCenter); This is where we calculate the Z height that a sphere, cut in half, would extend above the plane of the image. This is another geometrical calculation based on knowing the distance of our point from the center of the sphere.
  • 58. highp vec3 sphereNormal = normalize(vec3! (textureCoordinateToUse - center, normalizedDepth)); Once we know the height of the spherical cap at that point, we can calculate the normal for that point on the sphere’s surface. Think of it as a ray that extends from the center of the sphere to the surface at this X, Y coordinate. The normal is based on the center of the sphere, so we subtract our aspect-ratio- adjusted-coordinate from the center to get the relative X, Y coordinate from the center of the sphere. The Z component is the height of the sphere we just calculated.
  • 59. highp vec3 refractedVector = refract(vec3(0.0, 0.0, -1.0), ! sphereNormal, refractiveIndex); With the normal, we can calculate the refraction of light from that point on the sphere’s surface. The refraction calculation uses the surface normal, the refractiveIndex (a material property that you pass in, I think I use glass’s here), and a ray direction. I believe the ray direction here is from the eye going into the screen, although I can never remember positive/negative Z directions in OpenGL. This then generates a refracted vector, pointing in the direction light would as it refracts through a sphere.
  • 60. gl_FragColor = texture2D(inputImageTexture, (refractedVector.xy + 1.0) * 0.5) * checkForPresenceWithinSphere; We then take this refracted vector, which is in the -1.0-1.0 coordinate space, and adjust it to the 0.0-1.0 coordinate space for texture sampling. We read the texture color at the location pointed to by that vector. The earlier step() function to determine if a point was within the sphere comes into play here, where we only display a color if the point was within the sphere. If it was not, we output 0.0, 0.0, 0.0, 0.0 as an RGBA color because that’s the result of multiplying with 0.
  • 61.
  • 62.
  • 63.