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.

Slides:



Advertisements
Similar presentations
Coordinate System.
Advertisements

Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
1 Computer Graphics Chapter 8 3D Transformations.
OpenGL Basics Donghui Han. Assignment Grading Visual Studio Glut Files of four types needed: – Source code:.cpp,.h – Executable file:.exe (build in release.
Viewing and Transformation
1 3D modelling with OpenGL Brian Farrimond Robina Hetherington.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
OpenGL (II). How to Draw a 3-D object on Screen?
CS 4731: Computer Graphics Lecture 11: 3D Viewing Emmanuel Agu.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
CS 480/680 Computer Graphics Programming with Open GL Part 8: Working with Callbacks Dr. Frederick C Harris, Jr. Fall 2011.
Pictorials with AutoCAD Class 8.2 : Using AutoCAD to Create Pictorial Drawings Thursday, October 21 st 2004.
Introduction to 3D viewing 3D is just like taking a photograph!
19/4/ :32 Graphics II Syllabus Selection and Picking Session 1.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
1 Working with Callbacks Yuanfeng Zhou Shandong University.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
The Viewing Pipeline (Chapter 4) 5/26/ Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel.
Microsoft Word 2000 Presentation 5. Major Word Topics Columns Tables Lists.
Week 2 - Wednesday CS361.
Geometric transformations The Pipeline
2 COEN Computer Graphics I Evening’s Goals n Discuss the mathematical transformations that are utilized for computer graphics projection viewing.
Korea University Korea University Computer Graphics Laboratory Computer Graphics Laboratory Jung Lee, Chapter 13.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Adding a New Option to the Framework. Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of.
Lecture 6: 3D graphics Concepts 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 
Lecture 11: Exam Revision 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271  Coursework.
Laboratory Exercise # 9 – Inserting Graphics to Documents Office Productivity Tools 1 Laboratory Exercise # 9 Inserting Graphics to Documents Objectives:
Computer Graphics Bing-Yu Chen National Taiwan University.
Computer Science Term 1, 2006 Tutorial 2 Assignment 3 – The Virtual World.
Computer Graphics Zhen Jiang West Chester University.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
OpenGL Viewing and Modeling Transformation Geb Thomas Adapted from the OpenGL Programming Guidethe OpenGL Programming Guide.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Basic Perspective Projection Watt Section 5.2, some typos Define a focal distance, d, and shift the origin to be at that distance (note d is negative)
Computer Graphics I, Fall 2010 Working with Callbacks.
Advanced Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003.
1 Graphics CSCI 343, Fall 2015 Lecture 6 Viewing, Animation, User Interface.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
The Camera Analogy ► Set up your tripod and point the camera at the scene (viewing transformation) ► Arrange the scene to be photographed into the desired.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
Introduction to Drafting and Design In order to begin our drawing we have to set the drawing limits or the paper size.
Taxonomy of Projections FVFHP Figure Taxonomy of Projections.
Chap 3 Viewing and Transformation
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Viewing and Projection. The topics Interior parameters Projection type Field of view Clipping Frustum… Exterior parameters Camera position Camera orientation.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
Viewing. Classical Viewing Viewing requires three basic elements - One or more objects - A viewer with a projection surface - Projectors that go from.
Working with Callbacks
3D Computer Graphics (3080/GV10) Week 5-6 Tutorial 3
CSCE 441 Computer Graphics 3-D Viewing
Programming with OpenGL Part 2: Complete Programs
Introduction to Events
Computer Graphics Imaging
Working with Callbacks
Projection in 3-D Glenn G. Chappell
Introduction to OpenGL
2D Graphics Lecture 4 Fri, Aug 31, 2007.
The View Frustum Lecture 10 Fri, Sep 14, 2007.
Computer Graphics 3Practical Lesson
Chapter 3 Viewing.
Presentation transcript:

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

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

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

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

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

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

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.

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

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

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

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

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.

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

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

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.

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.

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)

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

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

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

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)

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.

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.

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.

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.

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

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.

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

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)

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

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

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 )

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.