Tutorial1 P
Tutorial1 P
Tutorial 1: Practical
Block 1: First Experiments with OpenGL
Marc de Kamps
September 25, 2020
Objective
In this tutorial, we’ll do a Hello World style version of a program. The main objective is to check
whether your set up works, whether this is on feng-linux, or on your own setup.
• You can run a browser in this Desktop. Go on Minerva and download the source code for
tutorial 1: tutorial1.tar.gz.
• Open a terminal. Any commands below need to be entered in the terminal window.
• Type: module add qt. Without this you will not be able to use Qt. As this gets annoying
after a while, add this command to your .bashrc file, or in its equivalent for whatever shell
you’re using.
• Untar tutorial1.tar.gz in a directory of your choice.
• Enter the cube construct directory.
1
Setting up a Shape
You are hopefully familiar with the basic structure of a Qt program: a main window containing
one or more widgets. In this case a SolidCubeWidget, which is in the inheritance chain of both
QWidget and QGLWidget. The following construction makes the OpenGL API available in the source
files that include the QGLWidget header:
# i n c l u d e <QGLWidget>
c l a s s S o l i d C u b e W i d g e t : p u b l i c QGLWidget
void SolidCubeWidget : : i n i t i a l i z e G L ( )
{ / / initializeGL ()
/ / s e t t he widget background colour
glClearColor (0.3 , 0.3 , 0.3 , 0.0);
} // initializeGL ()
OpenGL needs some initialization code, which is best placed in the initializeGL() method. A
camera volume is set up as an orthographic projection. There is also a statement to enable a Z buffer
test, or depth buffer test, which for now has been commented out.
// c a l l e d every time the widget needs p a i n t i n g
t h i s −>c u b e ( ) ;
glLoadIdentity ( ) ;
gluLookAt ( 0 . , 0 . , 2 . , 0 . 0 , 0 . 0 , 0 . 0 , 0 . 0 , 1 . 0 , 0 . 0 ) ;
/ / flush to screen
glFlush ( ) ;
} / / paintGL ( )
OpenGL is (mainly) a state machine, meaning that at the moment of rendering all graphical
primitives that need to rendered need to be defined. Rendering requires the use of buffers, that will
be discussed in greater detail in the lectures. When OpenGL renders, colour information is written
into a colour buffer. Depth buffering will change what you see on the screen. Try to guess what this
is doing.
All buffers in use need to be cleared before rendering which is achieved glClear. The rendering
of the cube will be discussed below. A camera is positioned at (0, 0, 2), directed to the point (0, 0, 0),
with an up direction in the positive y-direction. The reason that this statement is in paintGL rather
than in the initialization is that camera positions can be animated, i.e. shift from frame to frame.
void SolidCubeWidget : : cube ( ) {
2
glVertex3f ( 1.0 , 1.0 , −1.0);
glVertex3f ( 1.0 , 1.0 , 1.0);
glEnd ( ) ;
• Verify that these definitions create a world model like that of Figure 1.