This document is a chapter about math and metaphors related to 3D graphics programming for the iPhone. It discusses topics like the graphics rendering pipeline, matrix transformations including scaling, translation and rotation, homogeneous coordinates, and how vertices are processed from 4D to 2D. It also covers setting the model, view and projection matrices, matrix stacks, interpolation, quaternions, and examples of drawing simple 3D shapes like a cone using both fixed-function and shader approaches.
10. Homogeneous coordinates
Internally, the OpenGL implementation always converts it
into a 4D floating-point number.
It’s an artificial construction that allows all transformations to
be represented with matrix multiplication.
These 4D coordinates are known as homogeneous
coordinates.
So, shortly after entering the assembly line, all vertex
positions become 4D; don’t they need to become 2D at some
point? The answer is yes.
Sunday, December 9, 12
11. The Life of a Vertex
Perspective transform ( removal of w )
Sunday, December 9, 12
12. Vertex goes from 4D to 2D
Early life of a vertex. Top row is conceptual;
bottom row is OpenGL’s view
Sunday, December 9, 12
14. The photography metaphor
1. Arrange the various dishes on the table.
2. Arrange one or more light sources.
3. Position the camera.
4. Aim the camera toward the food.
5. Adjust the zoom lens.
6. Snap the picture.
1. Adjust the camera’s field-of-view angle. (projection matrix)
2. Position the camera and aim it in the appropriate direction.
(view matrix)
3. For each object:
a. Scale, rotate, and translate. (model matrix)
b. Render the object.
Sunday, December 9, 12
22. Setting the Model Matrix (Rotation ES 1.1)
glRotatef(m_currentAngle, 0, 0, 1);
counter-clockwise rotation about the Z-axis
angle in degrees
the latter three arguments define the axis of rotation
Sunday, December 9, 12
24. Setting the Model Matrix (Rotation)
arbitrary axis rotation
* ES 1.1
glRotatef generates the matrix for you
glRotatef only rotates about the origin
Sunday, December 9, 12
25. Setting the Model Matrix (Rotation)
arbitrary axis rotation
* ES 2.0
Sunday, December 9, 12
26. What we have been through
Sunday, December 9, 12
36. Example of
easing equations
Sunday, December 9, 12
37. Quaternions
For position keyframes and color keyframes
It's easy
What if interpolating between two
orientations?
Sunday, December 9, 12
38. Quaternions
Storing an angle for each joint would be
insufficient
you'd also need the axis of rotation
This is known as Axis-Angle notation
requires a total of four floating-point values for
each joint
Sunday, December 9, 12
39. Quaternions
Storing an angle for each joint would be
insufficient
you'd also need the axis of rotation
Quaternions
This is known as Axis-Angle notation
requires a total of four floating-point values for
each joint
Sunday, December 9, 12
40. Quaternions
Storing an angle for each joint would be
insufficient
Study of rotation
you'd also need the axis
and
This is understand it!!
known as Axis-Angle notation
requires a total of four floating-point values for
each joint
Sunday, December 9, 12
41. Vector Beautification
template <typename T> Vector3 Cross(const Vector3& v) const
struct Vector3 { {
return Vector3(y * v.z - z * v.y,
Vector3() {} z * v.x - x * v.z,
Vector3(T x, T y, T z) : x(x), y(y), z(z) {} x * v.y - y * v.x);
void Normalize() }
{ T Dot(const Vector3& v) const
float length = std::sqrt(x * x + y * y + z * z); {
x /= length; return x * v.x + y * v.y + z * v.z;
y /= length; }
z /= length; Vector3 operator-() const
} {
return Vector3(-x, -y, -z);
Vector3 Normalized() const }
{
Vector3 v = *this; bool operator==(const Vector3& v) const
v.Normalize(); {
return v; return x == v.x && y == v.y && z == v.z;
} }
T x;
T y;
T z;
};
Sunday, December 9, 12