- •Introduction
- •Credits
- •Prerequisites
- •Window and OpenGL context
- •Setup
- •Libraries
- •SFML
- •Building
- •Code
- •Building
- •Code
- •GLFW
- •Building
- •Code
- •One more thing
- •Drawing
- •The graphics pipeline
- •Vertex input
- •Shaders
- •Vertex shader
- •Fragment shader
- •Compiling shaders
- •Combining shaders into a program
- •Making the link between vertex data and attributes
- •Vertex Array Objects
- •Drawing
- •Uniforms
- •Adding some more colors
- •Exercises
- •Textures
- •Texture objects and parameters
- •Wrapping
- •Filtering
- •Loading texture images
- •SOIL
- •Alternative options
- •Using a texture
- •Texture units
- •Exercises
- •Transformations
- •Matrices
- •Basic operations
- •Addition and subtraction
- •Scalar product
- •Matrix-Vector product
- •Translation
- •Scaling
- •Rotation
- •Matrix-Matrix product
- •Combining transformations
- •Transformations in OpenGL
- •Model matrix
- •View matrix
- •Projection matrix
- •Putting it all together
- •Using transformations for 3D
- •A simple transformation
- •Going 3D
- •Exercises
- •Depth and stencils
- •Preparations
- •Setting values
- •Using values in drawing operations
- •Exercises
- •Attachments
- •Texture images
- •Post-processing
- •Changing the code
- •Color manipulation
- •Blur
- •Sobel
- •Conclusion
- •Exercises
- •Geometry shaders
- •Setup
- •Basic geometry shader
- •Input types
- •Output types
- •Vertex input
- •Vertex output
- •Creating a geometry shader
- •Geometry shaders and vertex attributes
- •Dynamically generating geometry
- •Conclusion
- •Exercises
- •Transform feedback
- •Basic feedback
- •Feedback transform and geometry shaders
- •Variable feedback
- •Conclusion
- •Exercises
Exercises
•Animate the blending between the textures by adding a time uniform. (Solution)
•Draw a reflection of the kitten in the lower half of the rectangle. (Solution)
•Now try adding distortion with sin and the time variable to simulate water. (Expected result, Solution)
Transformations
Matrices
Since this is a guide on graphics programming, this chapter will not cover a lot of the extensive theory behind matrices. Only the theory that applies to their use in computer graphics will be considered here and they will be explained from a programmer’s perspective. If you want to learn more about the topic, these Khan Academy videos are a really good general introduction to the subject.
A matrix is a rectangular array of mathematical expressions, much like a twodimensional array. Below is an example of a matrix displayed in the common square brackets form.
Figure 13:
Matrices values are indexed by (i,j) where i is the row and j is the column. That is why the matrix displayed above is called a 3-by-2 matrix. To refer to a specific value in the matrix, for example 5, the (a_{31}) notation is used.
Basic operations
To get a bit more familiar with the concept of an array of numbers, let’s first look at a few basic operations.
Addition and subtraction
Just like regular numbers, the addition and subtraction operators are also defined for matrices. The only requirement is that the two operands have exactly the
46