PDA

View Full Version : Qt and OpenGL



ToddAtWSU
12th June 2006, 21:32
I am working on an application that imbeds some OpenGL into Qt. I have a red 'L' drawing onto the screen using a GL_LINE_LOOP from (0,1) to (0,0) to (1,0). The 'L' always draws, but it always goes from the top-center of the screen to the middle of the screen to the right-middle of the screen. This never matters on what my glOrtho( ) is. It can be something like (-100, 100, -100, 100, 0, 0) or (-5, 5, -5, 5, 0, 0) or even (-10, -5, -10, -5, 0, 0). This last one should theoretically result in an empty black screen but it doesn't. :confused: I have no idea why this is doing this. Does Qt have its own glOrtho type function that I have not found for a QGLWidget? I have been using OpenGL for over a year now so I know how to handle simple things like this. But this is confusing me like crazy! Does anyone have any idea why this behaves like this? Thanks!

wysota
12th June 2006, 22:00
Qt doeasn't introduce any own interpretation to OpenGL command -- it just calls them on an internal GL context which renders the display to the widget (or not :P). So the thing has to do something with plain OpenGL. Maybe that's because you use 2D coordinates? Or you change one of matrices and simply didn't notice that?

Maybe you should try the same piece of code with glut and see if the effect persists?

jwintz
12th June 2006, 22:40
I've been using QGLWidget in the past and it behaves essentially like any other toolkit such as the glut framework. Maybe we could see some code ?

If this is not really an answer I may point you out to libqglviewer (http://artis.imag.fr/~Gilles.Debunne/QGLViewer/) which is a wonderfull opengl library for QT.

It provides a lot of functionnalities and gets rid of a lot of problems for you. I've been using it for a while in long term development and it is very useful.

nupul
13th June 2006, 06:32
I am working on an application that imbeds some OpenGL into Qt. I have a red 'L' drawing onto the screen using a GL_LINE_LOOP from (0,1) to (0,0) to (1,0). The 'L' always draws, but it always goes from the top-center of the screen to the middle of the screen to the right-middle of the screen. This never matters on what my glOrtho( ) is. It can be something like (-100, 100, -100, 100, 0, 0) or (-5, 5, -5, 5, 0, 0) or even (-10, -5, -10, -5, 0, 0). This last one should theoretically result in an empty black screen but it doesn't. :confused: I have no idea why this is doing this. Does Qt have its own glOrtho type function that I have not found for a QGLWidget? I have been using OpenGL for over a year now so I know how to handle simple things like this. But this is confusing me like crazy! Does anyone have any idea why this behaves like this? Thanks!


A few points to ponder:

1. You are using a Line_loop to draw an 'L'? This should give you a right angle triangle and not a plain 'L'

2. Try using gluOrtho2d(-x,x,-y,y) and not just plain gluOrtho(). Set the coordinates to (0,100,0,100). This will draw your 'L' on the bottom left corner.

3. Is this what you wanted? ;)

hope it helps

Nupul

ToddAtWSU
13th June 2006, 13:28
Yes I meant GL_LINE_STRIP and not LINE_LOOP. Been working on my previous OpenGL project way too long...;) I didn't think this would be a Qt problem but didn't know since stuff was behaving very funny. Someone on the OpenGL forums said my problem was I made the znear and zfar values the same and that they need to be different. So I will try this out. Thanks though for your help and I will check out that library you mentioned, jwintz. Thanks!

ToddAtWSU
13th June 2006, 15:19
One other thing I am trying to do is draw a white box from the point a user clicks and drags until. I just trigger a bool to true when the user left clicks on the screen and set the bool to false when the user releases the left mouse button and while the button is held and the mouse is moved, I calculate the new position of the mouse. I want to draw a box from the initial click position to the current position. I know how to and successfully retrieve the screen coordinates. But how do I convert these to the grid coordinates? Especially if my grid goes from -x1 to +x2 and -y1 to +y2? I think this shouldn't be too bad if the grid lies entirely in the positive or negative planes, but when the grid overlaps, I have no idea how to approach this. Thanks for your help in trying to solve this problem!

jwintz
13th June 2006, 15:31
I guess this is for selection purposes. Once again this is not really a Qt issue, but in the libqglviewer library you have selection tools provided which draw selection regions. See http://artis.imag.fr/~Gilles.Debunne/QGLViewer/examples/multiSelect.html

If it is not for selection purpose, the same library provides the following functions :

- virtual void startScreenCoordinatesSystem (bool upward=false) const
- virtual void stopScreenCoordinatesSystem () const

See http://artis.imag.fr/~Gilles.Debunne/QGLViewer/refManual/classQGLViewer.html, section Display functions.

Of course the corresponding OpenGL code can be found here. I can provide you some code but it would be easier to use this ...

bits
15th June 2006, 19:08
Try using gluOrtho2d(-x,x,-y,y) and not just plain gluOrtho()
Following is the relation between gluOrtho2d() and glOrtho().


void GLAPIENTRY
gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glOrtho(left, right, bottom, top, -1, 1);
}

For gluOrtho2D() is equivalent to calling glOrtho() with near = −1 and far = 1 .


I know how to and successfully retrieve the screen coordinates. But how do I convert these to the grid coordinates? Especially if my grid goes from -x1 to +x2 and -y1 to +y2? I think this shouldn't be too bad if the grid lies entirely in the positive or negative planes, but when the grid overlaps, I have no idea how to approach this.

What do you mean by Grid coordinates?
In case you are looking for a conversion from window coordinates to object coordinates, you can use gluUnproject().
You can get to know about gluUnproject() at gluUnproject man page (http://www.talisman.org/opengl-1.1/Reference/gluUnProject.html) and Nehe gluUnProject Article (http://nehe.gamedev.net/data/articles/article.asp?article=13)

If you aren't using libQGLViewer, then you are looking at wrong place. You should rather refer to Nehe's GameDev Forum (http://www.gamedev.net/community/forums/forum.asp?forum_id=27&forum_title=NeHe+Productions) for a OpenGL related help.

-Shobhit

ToddAtWSU
15th June 2006, 21:37
I just figured out my problem and I just did some quick mathematic conversions from the screen coordinates so I could convert them to my ortho (grid) coordinates. It appears to me that either OpenGL or Qt puts a default ortho of (-1, 1, -1, 1, -1, 1) if you provide a min and max as the same values. So sorry if this ended up not being a real Qt question, but when the orthos were acting in a way I had never seen when I used the glut functions, I thought maybe the QGLWidget did something a little different. But thanks for all your help!