0% found this document useful (0 votes)
5 views3 pages

Tutorial1 P

This tutorial introduces the basics of OpenGL programming using Qt, guiding users through setting up their environment and creating a simple cube rendering application. It covers essential OpenGL functions for initialization, rendering, and camera setup, while encouraging experimentation with depth testing and polygon rendering order. Users are instructed to compile and run the program, observe results, and explore camera positioning effects on rendering output.

Uploaded by

boommaniac123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Tutorial1 P

This tutorial introduces the basics of OpenGL programming using Qt, guiding users through setting up their environment and creating a simple cube rendering application. It covers essential OpenGL functions for initialization, rendering, and camera setup, while encouraging experimentation with depth testing and polygon rendering order. Users are instructed to compile and run the program, observe results, and explore camera positioning effects on rendering output.

Uploaded by

boommaniac123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP3811: Computer Graphics

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.

Access to School Machine OpenGL Setup


• Open the Chrome browser.

• Paste the following url: https://feng-linux.leeds.ac.uk/gpu/ into your browser.


• A login screen should appear: give name, then password.
• In your browser window a Linux Desktop should appear. It will look as if you had logged on
to one of the School Desktops.

• 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.

• Type: qmake HelloCube.pro.


• Type: make
• There should be an executable textttHelloCube. Run it: ./HelloCube.

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

For the purpose of rendering, the following methods are important:


// c a l l e d when OpenGL c o n t e x t i s s e t up

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);

/ / T h i s c r e a t e a view volume around t h e o r i g i n t h a t e x t e n d s t o c o o r d i n a t e 4 i n e v e r y d i r e c t i o n .


g l M a t r i x M o d e ( GL PROJECTION ) ;
glLoadIdentity ( ) ;
glOrtho ( −4.0 , 4 . 0 , −4.0 , 4 . 0 , −4.0 , 4 . 0 ) ;

/ / You m u s t s e t t h e m a t r i x mode t o model v i e w d i r e c t l y b e f o r e e n a b l i n g t h e d e p t h t e s t


g l M a t r i x M o d e (GL MODELVIEW ) ;
// g l E n a b l e ( GL DEPTH TEST ) ; / / comment o u t d e p t h t e s t t o o b s e r v e t h e r e s u l t

} // 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

void SolidCubeWidget : : paintGL ( )


{ / / paintGL ( )
/ / clear the widget
g l C l e a r ( GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ;

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 ( ) {

glColor3f (1.0 ,0.0 ,0.0);


g l B e g i n (GL POLYGON ) ;
g l V e r t e x 3 f ( −1.0 , −1.0 , −1.0);
g l V e r t e x 3 f ( 1 . 0 , −1.0 , −1.0);
glVertex3f ( 1.0 , 1.0 , −1.0);
glVertex3f ( −1.0 , 1.0 , −1.0);
glEnd ( ) ;

glColor3f (0.0 ,0.0 ,1.0);


g l B e g i n (GL POLYGON ) ;
g l V e r t e x 3 f ( 1 . 0 , −1.0 , 1 . 0 ) ;
g l V e r t e x 3 f ( 1 . 0 , −1.0 , −1.0);

2
glVertex3f ( 1.0 , 1.0 , −1.0);
glVertex3f ( 1.0 , 1.0 , 1.0);
glEnd ( ) ;

glColor3f (0.0 ,1.0 ,0.0);


g l B e g i n (GL POLYGON ) ;
g l V e r t e x 3 f ( −1.0 , −1.0 , 1.0);
g l V e r t e x 3 f ( 1 . 0 , −1.0 , 1.0);
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.

• Compile and run the program. Observe the output


• Exchange the order in which the different polygons are created. Compile, run. Does the result
matter?
• Now uncomment glEnable(GL DEPTH TEST); and repeat the experiment. If necessary dis-
cuss the result with the demonstrator.

• Find the online documentation of gluLookAt


• Move the camera to position (1.,1.,1.). Again experiment with glEnable(GL DEPTH TEST).
• The use of the word ’camera’ is slightly misleading. Enable the depth test put the camera at
(0, 0, 0) and let the view direction be (0, 0, 1). Do you see the red or the green plane? Explain
the result.
• Complete the cube.

Figure 1: A partially built cube.

You might also like