A couple of minor edits to the
qtogl files from last week.
[~]$ cd ~/cs40/examples/w01-intro/qtogl
[qtogl]$ cp ~adanner/public/cs40/examples/w01-intro/qtogl/mypanel* ./
[qtogl]$ cp ~adanner/public/cs40/examples/w01-intro/qtogl/fshader.glsl ./
[qtogl]$ git commit -am "Use QOpenGL classes"
[qtogl]$ git push
[qtogl]$ cd ~/cs40/examples/build/w01-intro/qtogl
[qtogl]$ make -j8
[qtogl]$ ./qtogl
Remember, you could open this in qtcreator too and build the project there. Remember to select
qtogl as the target after opening the
~/cs40/examples/CMakeLists.txt project.
QT and OpenGL
The class
QGLWidget creates an OpenGL context inside QT.
OpenGL functions are called within this context. Additionally, QT has some
OpenGL wrapper classes beginning with the prefix
QGL.
Creating a QT OpenGL application
- Create a new class, e.g., MyPanelOpenGL which inherits from QGLWidget
- Add a widget in the UI Designer and a promote it to MyPanelOpenGL
- Implement the methods initializeGL(), paintGL() and resizeGL ( int width, int height ) in your MyPanelOpenGL class.
- Add additional methods, slots, signals, widgets as needed.
- ~/cs40/examples/w01-intro/qtogl
- MyPanelOpenGL inherits from QGLWidget; provides OpenGL context in QT
- clip coordinates 2x2x2 box
- geomety -> vertex shader -> clip -> rasterize -> fragment shader -> framebuffer
- framebuffer squished into viewport
- Copying CPU data to GPU memory using Vertex Buffer Objects (VBOs)
We store the vertices of a triangle in a VBO in GPU memory. The next step is to define, create, load, and compile
shaders. The
vertex shader runs first and takes vertex data from the VBO and outputs geometry in clip coordinates. This geometry is then clipped, rasterized, and fed to the
fragment shader which runs on each fragmenet, or potential output pixel. The output of the fragment shader is written to a framebuffer and displayed in the viewport.
Once each shader is compiled, we define, create, and link a
shader program, which is the combination of a vertex shader and a fragment shader.
Finally, we are ready to draw. The main steps are:
- clear the display
- bind buffers, programs
- connect shader parameters to data
- glDraw...
- glFlush
- repeat as needed using updateGL()
Once the geometric data are copied to GPU memory, almost everything else happens on the GPU.
paintGL is just issuing commands to the GPU. The GPU itself will process those commands.
Shaders, GLSL
- keywords: in, out, uniform
- types: vec3, mat4
- variables: gl_Position;
Modifying Shaders
- Making fragColor a uniform value
- modifying geometry
- uniform time variable
- QTimer
- updateGL -> Animation!
Geometry intro
Before we dive into drawing, manipulating, and lighting shapes, we need to know a little bit more about how to represent geometric objects in OpenGL.
- Point, Vector, Line, Ray, Halfplane, Half-Orc
- basis and frame
- dot product
- cross product
- Matrices
- Matrix/Vector math
- w03-geomtry/vector3d.h