2D OpenGL with QMouseEvents
Look at the updated Qt OpenGL example program (introduced in the
previous lab).
You can find it at:
/home/jungle/pj/www/453/box/2DwithMouseEvents
- The changes listed allow you to use the mouse to rotate the
object, as well as the slider.
You use the left mouse button to rotate.
- You can also use the mouse to move the object on the screen.
You use the right mouse button to move
- You can use the middle mouse button to output the mouse position
to the terminal.
mouse position: 221, 200
Remember that Qt screen coordinates and OpenGL coordinates are
not the same. You will need to perform a conversion to get the
correct values for OpenGL. This, you can do yourselves!
- The
glbox.cpp
and glbox.h
files are the only ones to have changed.
The changes are as follows:
- glbox.h
- glbox.cpp
- mousePressEvent( QMouseEvent* event )
//get the information about the position of the mouse.
//using Qt's QMouseEvent
lastPos = event->pos();
//if the middle mouse button is pressed ...
if(event->button()==MidButton)
{
float dx = event->pos().x();
float dy = event->pos().y();
//get the x and y (screen) coordinates of the mouse
//and output the value.
cout<<"mouse position: "<< dx<<", "<< dy<< endl;
}
- mouseReleaseEvent( QMouseEvent* event
)
//update the OpenGL, this is an OpenGL command.
updateGL();
- mouseMoveEvent( QMouseEvent* event
)
//these values use the distance the mouse has moved
//using the current position and the last position.
float dx = (event->pos().x()-lastPos.x());
float dy = (event->pos().y()-lastPos.y());
// ...while the left button is presssed
if(event->state()&LeftButton)
{
zRot -= dy;
zRot += dx;
//set the rotations values appropriately (rotate the scene)
// using the values from the dx dy calculated from mouse positions.
}
// ...while the middle button is pressed
else if(event->state()&MidButton)
{
//nothing at the moment
}
// ...while the right button is pressed
else if(event->state()&RightButton)
{
xTrans -= dx/100.0;
yTrans -= dy/100.0;
//translate the scene
}
//update the last position so we can use it to find the distance the mouse
// has travelled.
lastPos = event->pos();
//If the mouse is moving we want to keep a note of this, will be
//useful for other interactions and events in future.
moving = true;
updateGL();
moving = false;
-
Exercise: write the conversion function, as per the lecture notes,
to convert Qt coordintes to OpenGL ones.
- Hints for a conversion function:
- The Resize function has a function call:
- glViewport( 0, 0, (GLint)w, (GLint)h );
- The parameter values are (x, y, width, height).
- x, y specifies (in pixels) the lower left corner of
the viewport.
- width, height specifies the width and height of
the viewport.
- You can store these values in a class variable to use in
your conversion function.
For example:
- viewwidth = w;
- viewheight = h;
- The Resize function has a function call:
- gluOrtho2D( -1.5, 1.5, -1.5, 1.5);
- This means that the view you have will have the origin
centered in the middle of the OpenGL viewport and an object
on the screen will be shown if it has coordinates that lie
within this range. For example look at the square's
coordinates.
- You can change these to any values you wish. I chose
them just to be able to view the square at a sensible
size.
- NB remember this means that the viewport is 3 OpenGL
units wide and 3 OpenGL units high.
- You can calculate the x ratio using:
- viewwidth (approx 300 in the box example; the same
width as in the glViewport() function)
- OpenGLwidth (3 in the above example, as in the
gluOrtho2D() function).
- Calculate the y-ratio in exactly the same way.
- Apply these ratios to the mouse position x and y values to get
your OpenGL coordinate values.
- This is all the information you need to write your
conversion function.
- Try not to make it more complicated for yourself than need be.
Remember to check the documentation.
More OpenGL ~ 3D objects
Useful References
OpenGL Programming Guide
(The Red Book).
OpenGL Reference Manual (Blue Book)
The OpenGL Website
The OpenGL Website - tutorials
Contact me
email: pj@cpsc.ucalgary.ca
Tel: (403) 220 7041.