Results 1 to 18 of 18

Thread: problem in rendering opengl on QGraphicsView

  1. #1
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile problem in rendering opengl on QGraphicsView

    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

  2. #2
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem in rendering opengl on QGraphicsView

    Not a direct answer to your specific problem but an alternative way of dealing with OpenGL in Qt is the libQGLViewer tool, which provides a complete, customizable, and easy-to-use OGL display functionnality.

  3. #3
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

    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.

  5. #5
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

    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?

  7. #7
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

    Can we see the paint() code of your 3D item?

  9. #9
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    Hi,

    Following is code snippet from my tool.

    void MyWindow:aintEvent(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

  10. #10
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

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

  12. #12
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    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
    Last edited by Sandip; 14th April 2008 at 16:02. Reason: adding additional information

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

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

  14. #14
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    Quote Originally Posted by wysota View Post
    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.

    Quote Originally Posted by wysota View Post
    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

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

    Quote Originally Posted by Sandip View Post
    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?

  16. #16
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    Thanks a lot!

    Quote Originally Posted by wysota View Post
    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.

    Quote Originally Posted by wysota View Post
    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

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in rendering opengl on QGraphicsView

    Quote Originally Posted by Sandip View Post
    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.

  18. The following user says thank you to wysota for this useful post:

    Sandip (15th April 2008)

  19. #18
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in rendering opengl on QGraphicsView

    Thanks a lot!

    I will try your suggestion.
    Thanks once again!

Similar Threads

  1. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 21:48
  2. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14:54
  3. Replies: 3
    Last Post: 12th February 2008, 21:17
  4. QGraphicsView scrolling problem with 4.3.0
    By hb in forum Qt Programming
    Replies: 8
    Last Post: 30th August 2007, 22:18
  5. OpenGL rendering problem
    By spud in forum Qt Programming
    Replies: 5
    Last Post: 27th February 2007, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.