PDA

View Full Version : problem in rendering opengl on QGraphicsView



Sandip
14th April 2008, 11:08
Hi,

I am trying to draw a 3D object on QGraphicsView. I set new glwidget as viewport of graphics view. I have written my own custom widget which is not inherited from glwidget. I am using some opengl functions in paint event of my custom widget. It is not showing me anything but the widget background is erased with white color.
while debugging I found that on first call to any opengl function I am getting error GL_INVALID_ENUM from glGetError.
If anybody knows what is wrong in this approach or any other approach that may work, then please tell me.
Waiting for reply because I can't proceed without solving this issue.

Thanks in advance.

Sandip

Caius Aérobus
14th April 2008, 12:10
Not a direct answer to your specific problem but an alternative way of dealing with OpenGL in Qt is the libQGLViewer (http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer/) tool, which provides a complete, customizable, and easy-to-use OGL display functionnality.

Sandip
14th April 2008, 12:34
Thanks Caius Aérobus!

But I don't want to use seperate classes to solve my problem. Is there any other way to solve it.
Or any idea which can be implemented using qt only (without third party library).

Regards,
Sandip

wysota
14th April 2008, 12:40
QGraphicsView is meant to display 2D, not 3D objects. If you are trying to force 3D, it'll be better if you do that without QGraphicsView.

Sandip
14th April 2008, 13:11
Yes Sir!

But I have one question about it Qt says "To render using OpenGL, simply call setViewport(new QGLWidget)." Does it mean that we can draw 3d objects on graphics view or not?

If it's mandetory to draw 3d object on graphics view then what to do.

Thanks and Regards,
Sandip

wysota
14th April 2008, 14:24
What is the point of drawing 3d objects in an environment that is meant to handle 2d coordinates? What does your item class look like?

Sandip
14th April 2008, 14:48
I am agree with you but I don't have other choice (as per my view). OR there should be any other graphics framework which will fulfill my requirements.

Actually I had many items in my graphics view. Now I am adding new items. These new items are 3-dimensional. I had selected graphics view because I can easily manage multiple items using it. So I am trying to add my new 3d item into it.

My item class will just display a cube and it will provide basic functionality like move, resize and rotate.
If you want I can drop some code here.

Thanks for spending your valuable time for me.

Regards,
Sandip

wysota
14th April 2008, 16:20
Can we see the paint() code of your 3D item?

Sandip
14th April 2008, 16:27
Hi,

Following is code snippet from my tool.

void MyWindow::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);

m_glObject = makeObject();

glEnable(GL_CULL_FACE);
printError(glGetError());

glClearColor(2.0, 2.0, 2.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0.0, 0.0,-10.0);
glRotated(m_nXRot / 10.0, 2.0, 0.0, 0.0);
glRotated(m_nYRot / 10.0, 0.0, 2.0, 0.0);
glRotated(m_nZRot / 10.0, 0.0, 0.0, 2);


glCallList(m_glObject);

printError(glGetError());

painter.end();
event->accept();
}


GLuint MyWindow::makeObject()
{
printError(glGetError()); // it is printing GL_INVALID_ENUM
GLuint list = glGenLists(1);

glNewList(list, GL_COMPILE);

glBegin(GL_TRIANGLES);
//qglColor(Qt::green);
glColor3d(255,255,255);
glVertex3d(0.0,2.0,0.0);
glVertex3d(2.0,-2.0,-2.0);
glVertex3d(-2.0,-2.0,-2.0);
glEnd();

glEndList();

printError(glGetError());

return list;
}

Waiting for your feedback.

Regards,
Sandip

Sandip
14th April 2008, 16:33
Hi,
the following code may also help you to find out the problem.

void MyWindow::resize(int width, int height)
{

glViewport(0,0,width,height);

printError(glGetError());

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.5, +3.5, +3.5, -3.5, 2.0, 15.0);
glMatrixMode(GL_MODELVIEW);

printError(glGetError());
}

Regards,
Sandip

wysota
14th April 2008, 16:52
So where is the graphics view in that? Is MyWindow the viewport or what? I can't seem to grasp what you are doing here...

Sandip
14th April 2008, 16:58
Sorry!

Here is the main function.

#include "mywindow.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;

QGraphicsView view(&scene);

MyWindow *widget = new MyWindow(0);
QGraphicsProxyWidget *w = scene.addWidget(widget);

view.showMaximized();

view.setViewport( new QGLWidget(new QGLContext(QGLFormat::defaultFormat())));
view.show();
w->show();

return app.exec();
}

MyWindow is showing 3D object. In above code I tried to draw a pyramid. MyWindow is inherited from QWidget and added to scene.

Sorry for providing incomplete information. Please let me know if you need any other information.

I am thinking of drawing 3d object directly using opengl instead of setViewport or QGLWidget. Is it possible?

Regards,
Sandip

wysota
14th April 2008, 17:41
Why don't you use QGLWidget instead of all that stuff? There is no need for a proxy or anything. I'm not sure graphics view can proxy gl widgets at all...

Sandip
15th April 2008, 07:17
Why don't you use QGLWidget instead of all that stuff?

I tried QGLWidget. It's working up to some mark on Windows but nothing happens on Mac or Linux.


There is no need for a proxy or anything. I'm not sure graphics view can proxy gl widgets at all...

If I will not use proxy then how can I add my object to scene. OR do you have any other idea?

If I will not add it to scene and just show it after making QGraphicsView it's parent then while moving, it will overlap on scrollbars of scene.

I observed a strange behavior on Mac OS. If you will add QGLWidget in scene, initializeGL method is called before each call to paintGL. It is supposed to happen only once. I don't know the exact reason may be context is getting changed on Mac. Another thing is that few opengl examples in qt are crashing on my Mac machine for example, overpainting, framebufferobject. If anyone will guide me in this direction, then it will be a great help for me.

Thanks and Regards,
Sandip

wysota
15th April 2008, 08:08
I tried QGLWidget. It's working up to some mark on Windows but nothing happens on Mac or Linux.
If you are using a GL viewport for QGraphicsView, you are working with QGLWidget anyway, so you'll have to solve that problem regardless if you use QGV or not.


If I will not use proxy then how can I add my object to scene. OR do you have any other idea?
Yes, don't use a scene, use a regular QGLWidget :)


Another thing is that few opengl examples in qt are crashing on my Mac machine for example, overpainting, framebufferobject. If anyone will guide me in this direction, then it will be a great help for me.
Maybe the whole problem is that your system doesn't support some advanced OpenGL capabilities and that causes applications to crash. Does the "Hello GL" example work correctly on all platforms for you?

Sandip
15th April 2008, 08:24
Thanks a lot!


Yes, don't use a scene, use a regular QGLWidget


I tried with regular QGLWidget and as I told you already it is overlapping on scrollbars because anyway I have to use scene for my other objects. It also resolves the problem on Mac.


Maybe the whole problem is that your system doesn't support some advanced OpenGL capabilities and that causes applications to crash. Does the "Hello GL" example work correctly on all platforms for you?

Yes. "Hello GL" example is working fine on all platforms.
Can you tell me something about issue of glcontext on Mac?

Regards,
Sandip

wysota
15th April 2008, 09:01
I tried with regular QGLWidget and as I told you already it is overlapping on scrollbars because anyway I have to use scene for my other objects. It also resolves the problem on Mac.
GraphicsView is for 2D objects... I can't stress that more :) If you want a 3D object, implement an item class that will handle your 3D object in a 2D environment, but for God's sake, do that by subclassing QGraphicsItem and not by proxying widgets.


Yes. "Hello GL" example is working fine on all platforms.
So build on it.

Sandip
15th April 2008, 09:27
Thanks a lot!

I will try your suggestion.
Thanks once again!