Title: GLWidget Description
1GLWidget Description
2GLWidget
- The GLWidget class extends the Qt QGLWidget.
- The QGLWidget is a Qt Widget that happens to have
an OpenGL drawing context inside of it which
responds to OpenGL commands. - By extending the QGLWidget it allows us to
override the original methods and insert our own
code for drawing, handling user input, etc.
3GLWidget
- When a QGLWidget (or an object that extends it)
is created certain methods are immediately called
in the following order - Everything in the constructor is completed
- The method initializeGL is called to set any
variables or properties used by OpenGL - The method resizeGL is called to set up the GL
window - The method paintGL is called to begin the drawing
4GLWidget Constructor
- Most of what happens in the constructor is just
initializing variables and creating the dialog
boxes for the application (inputs for the initial
shapes and the print dialog). - The interesting bits
- The Timer
- Mouse Tracking
5GLWidget Constructor Timer
- The timer is used to regulate the maximum number
of times per second the graphics will be drawn to
the screen. - QTimer timer new QTimer(this)
- connect(timer, SIGNAL(timeout()), this,
SLOT(updateGL())) - timer-gtstart(20)
6GLWidget Constructor Timer
- Create
- Takes the parent widget as its parameter
- QTimer timer new QTimer(this)
- Connect
- Every time the timer hits its limit it emits the
signal timeout which is connected to the
QGLWidget slot updateGL which itself calls the
paintGL method. - connect(timer, SIGNAL(timeout()), this,
SLOT(updateGL())) - Start
- Starts the timer counting and it takes the number
in milliseconds it will count to before emitting
timeout and starting its count over. - timer-gtstart(20)
7GLWidget Constructor Mouse Tracking
- Mouse tracking used to indicate whether we want
to passively track the mouse cursor in the widget
without any buttons needing to be pressed. - The would be standard for any game or simulation
where looking around the scene is performed via
moving the mouse. - setMouseTracking(true)
8GLWidget initializeGL
- This method is used to setup any variables or
properties necessary before any drawing is done. - There are easily dozens of different properties
that can be set up before drawing including - Light Sources
- Clearing Color
- How objects are shaded
- etc.
9GLWidget resizeGL
- This method is called after initializeGL and
anytime the widget is resized. - It basically is responsible for setting up the
view in terms of its dimensions, where the
viewpoint is located, and the method of
displaying depth information (2D orthographic, 3D
orthographic or 3D perspective).
10GLWidget resizeGL
- This method is called after initializeGL and
anytime the widget is resized. - It basically is responsible for setting up the
view in terms of its dimensions, where the
viewpoint is located, and the method of
displaying depth information (2D orthographic, 3D
orthographic or 3D perspective).
11GLWidget resizeGL
- The method takes height and width as its
parameters but is never explicitly called. - Anytime the widget is resized this method is
called and its new height and width are
automatically passed as the parameters.
w
h
w
h
resizeGL(int w, int h)
12GLWidget resizeGL
- The first command to issue in resizeGL should be
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- OpenGL operates on two different stacks of
matrices for doing various transformation. - GL_PROJECTION contains a matrix for projection
transformations which describe the viewing volume - GL_MODELVIEW contains a matrix for modeling
transforming that affect the scene
13GLWidget resizeGL
- The first command to issue in resizeGL should be
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- By setting the matrix mode to GL_PROJECTION it
specifies which matrix stack to operate on. - The call of glLoadIdentity then sets the current
projection matrix to the identity matrix.
14Projection Transformations
- OpenGL supports two types of projections
Orthographic and Perspective. - Orthographic Projection
- For this type of projection the viewing volume is
set up like a box. - There is no real sense of depth in terms of
visual cues (size or shape of object as it grows
more distant).
15Projection Transformations
Two 3D Shapes
Appears flat and only depth cue is overlapping
16Projection Transformations
- Orthographic Projection
- This kind of projection are very useful when a 3D
view complicates an area of interest. - CAD and Modeling programs often make extensive
use of orthographic projections.
17Projection Transformations
- Orthographic Projection
- To create the an orthographic projection matrix
the function glOrtho is available. - void glOrtho(GLdouble left, GLdouble right,
- GLdouble bottom, GLdouble
top, - GLdouble near, GLdouble far)
top
Note Even through the Z value into the screen
is increasingly negative in value the values
of near and far are set as positive values. But
they should never be equal to one another.
far
left
right
near
bottom
18Projection Transformations
- Perspective Projection
- This the projection model that represents how a
camera (or eyes) view the world. - As objects recede in the distance they grow
smaller and we would see their sides.
Two 3D Shapes
Objects have perspective (in a PowerPoint
approximate kind of way)
19Projection Transformations
- Perspective Projection
- OpenGL provides two functions for automatically
setting up a perspective projection. - void glFrustum(GLdouble left, GLdouble right,
- GLdouble bottom,
GLdouble top, - GLdouble near, GLdouble
far) - void gluPerspective(GLdouble fovy,
- GLdouble aspect,
- GLdouble near,
- GLdouble far)
20Projection Transformations
- glFrustum is setup almost identically to glOrtho
. - void glFrustum(GLdouble left, GLdouble right,
- GLdouble bottom,
GLdouble top, - GLdouble near, GLdouble
far)
By adjusting the size of the frustum, through any
of the parameters you can change the aspect
ratio of the scene to stretch it or squash it.
top
right
left
bottom
near
far
21Projection Transformations
- gluPerspective does the same thing a little more
in straightforward way. - void gluPerspective(GLdouble fovy,
- GLdouble aspect,
- GLdouble near,
- GLdouble far)
- fovy is the angle of the top and
- bottom planes of the frustum
- 0.0 to 180.0
- aspect is the aspect ratio which is
- the width divided by the height
w
h
fovy
near
far
22Projection Transformations
- gluPerspective actually calls glFrustum and here
are how they are related. - gluPerspective(fovy, aspect, near, far)
- height tan( fovY / 360 pi) near
- width height aspect
- glFrustum(-width, width, -height, height, near,
far)
23Projection Transformations
- It is important to note that all these functions
do is automatically construct a projection
matrix. The matrix could also be constructed
manually (and tediously) through calculations
instead and applied directly. - Also, the walls of the viewing volume also act as
clipping planes. Anything inside the volume is
drawn, anything outside is not, while anything on
the border is automatically trimmed by OpenGL.
24Viewport
- A final optional part of resizeGL is setting the
viewport which is the window which allows you to
see the scene. - By default the viewport is set to the size of the
widget. - However the viewport can be smaller and multiple
viewports can be defined for multiple views in
the same windows.
25glWidget paintGL
- This method is responsible for all your scene
drawing and is called every time the timer fires. - The general routine is to
- Clear the screen by overdrawing everything with
the clear color - Draw the scene
- Animation is achieved by changing the
transformations of the objects to be drawn
between screen clearings.
26glWidget paintGL
- Why erase everything every time? Why not just
update the part that changes? - It is easy to just redraw everything.
- Often it is also cheaper to redraw than to
determine exactly what and how it needs to change - Most dynamic applications (games) are going to
routinely change most of the screen. -
27Interacting with the Widget
- Most QWidgets have many methods which allow the
user to interact with them. Since the GLWidget
is a QGLWidget which is a QWidget then it
inherits all those methods. - Mouse, keyboard, touchpad with stylus,
28Mouse Events
- Every time the mouse moves it generates a
QMouseEvent and passes it to mouseMoveEvent. - Similarly when a mouse button is pressed it also
generates a QMouseEvent and passes it to
mousePressEvent. - Contained within this Object are the x and y
mouse positions, which buttons are currently
pressed and if any modifier keys on the keyboard
(Shift, Alt, Control) are pressed.
29Mouse Events
- Qt has several constants defined to represent
each mouse button. - None QtNoButton
- Left QtLeftButton
- Middle QtMidButton
- Right QtRightButton
- It also has representations for modifier keys.
- Shift QtShiftModifier
- Control QtControlModifier
- Alt QtAltModifer
30Mouse Events
- Getting the x and y coordinates within the
widget. - varX qme-gtx() varY qme-gty()
- Checking if a button is pressed
- if( qme-gtbutton() QtLeftButton )
- Checking if one of several buttons is pressed
- if( qme-gtbuttons() QtMidButton)
31Mouse Events
- Checking if a modifer key is pressed
- if( qme-gtmodifers() QtControlModifier )
32Keyboard Event
- Whenever a key is pressed on the keyboard it
generates a QKeyEvent which is passed to the
method keyPressEvent. - Like with the mouse Qt has several constants
defined to represent each key with a fairly
predictable format. - Escape QtKey_Escape
- Left Arrow QtKey_Left
- 5 QtKey_5
- T QtKey_T
33Keyboard Event
- Check if a key is pressed.
- if( kbe-gtkey() QtKey_H )
- Check if one of the modifiers is pressed.
- if( kbe-gtkey() QtShiftModifer )
-
34Keyboard Event
- Whenever a key is released on the keyboard it
also generates a QKeyEvent which is passed to the
method keyReleaseEvent which can be queried as in
the previous examples.