Lecture 4 - Finishing Rasterizer, Moving to OpenGL
Review
For the rasterizer due tonight:
- Look at Piazza for
epilson
. We do alpha, beta, gamma between 0 and 1. But due to floating point math on computers we may be off by just enough so that we don't miss the edges of the trianglesepsilon = 1e-5
or something like that. Do 0 - EPSILON <= alpha <= 1 + EPSILON and stuff like that.
- There may be some depth test precision issues, so again you can use epsilon like we did above.
One fun thing is that the bugs you get are really cool looking!!!
Depth Buffer
In general, ultimately we just want to render the triangles on the front of the object. For the file, when you read in the triangles and rendering them, they may be in a crazy order (which shouldn't matter). The Zbuf
should tell you which ones are closer to the viewer, so in our case anything that has the higher z-value should be the value we write. We allocate:
float Zbuf[width][height];
// init default values of -infty
z_coord = alpha*v1z + beta*v2z + gamma*v3z
if(z_coord > Zbuff[x][y])
{
// write the pixel
Zbuf[x][y] = z_coord;
}
OpenGL
Our goal today is to get OpenGL to work! OpenGL is good for using the GPU to utilize graphics processes, such as rasterization. Lab 3 will pretty much be setting up OpenGL working on your machine, and beginning to get the mental model of the hardware and what's going on under the hood of each library call we need.
At this point, we have a mental model of what we need a graphic pipeline to do:
- Get a list of vertices from some file stream
- Get a list of faces for said vertices
- Draw triangles for each face, using a zbuffer
We now need to consider the different between CPU work (in C++
) vs. GPU work (work running in parallel with each other, using OpenGL
library calls):
The power of this is that you can set per-pixel colors to do per pixel coloring.
GPU
A GPU is just multiple SIMD (Single Instruction, Multiple Data) units, all of them can access some global memory.
Each compute will have some shared memory to, between similar cores.
But the main job is to use the programming view prior, but keep in mind how the hardware works so we have a reason to use it.
Why OpenGL?
- Window system independent
- OS independent
- Scales from PC to high-end machines
Graphics Library as a Renderer
We consider how the library uses itself to store/use:
- Geometric primitive
- Image primitives
There's also lots of states you can set as well for colors/lighting/styles/etc. Consider the 'state' that you set prior to rendering Assignment 1.
We'll use glfw
will handle the inputs between the windowing system of your OS and our inputs to it. However, all the gl
starting calls are for OpenGL.
Render and Handle Data
All drawing data is stored in arrays, passed to the GPU and create a buffer object. You must indicate how the data is packed into the array.
GLSL
This is the OpenGL shading language. It is, in runtime, assembled and linked on the GPU.
Modern Graphics Challenges (Summary)
We must do more work as the programmer:
- Manage passing data to the GPU
- Writing and loading shaders
- Compile and link them
- ...
In general:
glfw
is for windowing controlOpenGL
for managing data and state for the graphicsGLSL
is for the shaders and algorithms related to it.
The Code
This is an overview of files seen in Lab 3. Some notes on important points are as follows.
Folders
ext
is just a bunch of library referencessrc
contains our source files, like beforeresources
contains ourobj
files. The difference now is that there's now files likesimple_vert.glsl
, which at runtime is compiled and linked and ran.vertPos
has a location value that dictates that the location data is at index 0. Color information may be at say 1, and so on. Don't worry, we'll look at these later.
You'll start by editing main.cpp
. In general main
will:
- Set up the window manager for keyboard and mouse callback functions.
- We loop our window to keep existing to poll for events (keyboard and mouse) while also rendering our data in
render
. Therender
function will be the bulk of our work this quarter.