GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget....

34
GLWidget Description Jason Goffeney 3/8/2006

Transcript of GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget....

Page 1: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget Description

Jason Goffeney

3/8/2006

Page 2: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget

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

Page 3: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget

• When a QGLWidget (or an object that extends it) is created certain methods are immediately called in the following order:

1. Everything in the constructor is completed

2. The method initializeGL is called to set any variables or properties used by OpenGL

3. The method resizeGL is called to set up the GL window

4. The method paintGL is called to begin the drawing

Page 4: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget 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

Page 5: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget 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->start(20);

Page 6: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget 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->start(20);

Page 7: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 8: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget: 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.

Page 9: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget: 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).

Page 10: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget: 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).

Page 11: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 12: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget: 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

Page 13: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

GLWidget: 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.

Page 14: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Projection 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).

Page 15: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Projection Transformations

• Orthographic Projection

Two 3D Shapes Appears flat and only depth cueis overlapping

Page 16: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 17: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

left

right

top

bottom

near

far

Note: Even through the Z value into the screen is increasingly negative in value the values ofnear and far are set as positivevalues. But they should neverbe equal to one another.

Page 18: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 19: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 20: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Projection Transformations

– glFrustum is setup almost identically to glOrtho .

void glFrustum(GLdouble left, GLdouble right,

GLdouble bottom, GLdouble top,

GLdouble near, GLdouble far);

left right

top

bottom

near

By adjusting the size of the frustum,through any of the parameters you can change the aspect ratio of thescene to stretch it or squash it.

far

Page 21: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Projection Transformations

– gluPerspective does the same thing a little more in straightforward way.

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far);

fovyh

w

near

• 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

far

Page 22: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 23: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 24: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Viewport

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

Page 25: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

glWidget: 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.

Page 26: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

glWidget: 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.

Page 27: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Interacting 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, …

Page 28: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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

Page 29: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Mouse Events

• Qt has several constants defined to represent each mouse button.– None: Qt::NoButton– Left: Qt::LeftButton– Middle: Qt::MidButton– Right: Qt::RightButton

• It also has representations for modifier keys.– Shift: Qt::ShiftModifier– Control: Qt::ControlModifier– Alt: Qt::AltModifer

Page 30: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Mouse Events

• Getting the x and y coordinates within the widget.varX = qme->x(); varY = qme->y();

• Checking if a button is pressed:if( qme->button() == Qt::LeftButton )

• Checking if one of several buttons is pressedif( qme->buttons() & Qt::MidButton)

Page 31: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Mouse Events

• Checking if a modifer key is pressed:if( qme->modifers() == Qt::ControlModifier )

Page 32: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Keyboard 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: Qt::Key_Escape– Left Arrow: Qt::Key_Left– 5: Qt:Key_5– T: Qt::Key_T

Page 33: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

Keyboard Event

• Check if a key is pressed.if( kbe->key() & Qt::Key_H )

• Check if one of the modifiers is pressed. if( kbe->key() & Qt::ShiftModifer )

Page 34: GLWidget Description Jason Goffeney 3/8/2006. GLWidget The GLWidget class extends the Qt QGLWidget. The QGLWidget is a Qt Widget that happens to have.

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