Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 GLWidget Description Jason Goffeney 3/8/2006

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

3 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

4 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

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

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

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

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

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

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

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

12 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

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

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

15 Projection Transformations Orthographic Projection Two 3D Shapes Appears flat and only depth cue is overlapping

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

17 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 of near and far are set as positive values. But they should never be equal to one another.

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

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

20 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 the scene to stretch it or squash it. far

21 Projection Transformations –gluPerspective does the same thing a little more in straightforward way. void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); fovy h 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

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

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

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

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

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

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

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

29 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

30 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 pressed if( qme->buttons() & Qt::MidButton)

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

32 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

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

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


Download ppt "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."

Similar presentations


Ads by Google