Results 1 to 5 of 5

Thread: Problem rendering using OpenGL in VM

  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem rendering using OpenGL in VM

    Hi all,

    I have been making a 2D game using OpenGL and some other game libraries like SDL. I had made a custom GUI and used some other libraries for other things. But i have recently started rewriting most of it using Qt. I have done some of the UI and am now looking at implementing the OpenGL graphics i will use for the actual game.

    The problem i am seeing is not with my code, i haven't even started using OpenGL with Qt, but it seems that it's more to do with the system's graphics driver or something.
    Below are two screenshots of the "\examples\opengl\2dpainting" Qt example program. The first one is running on a Windows XP machine which is running in VMware Workstation 8. The second is running on my physical Windows 7 machine.
    qt_opengl_xp_vm.png qt_opengl_win7.png

    As you can see, the one running in the VM does not correctly render the images.
    The thing is, when i run the old game code (before i moved to Qt), the OpenGL images render fine. I was using an image library called SOIL (Simple OpenGL Image Library), so apparently that can work with the graphics driver of that VM but Qt can't.

    Does anyone know what the problem is, or how i can fix it? If it is something to do with the image format, how could i know if it will render correctly in another user's machine? I can post code if you need it.

    thanks


    EDIT: I just realised that the example i mentioned is not using images, it is drawing shapes. But i have seen the same thing happen with images. In fact, if i use the "-graphicssystem opengl" command line option on my application, most of the widgets are just white, and sometimes flicker. And, fyi, i have applied extensive styling on those widgets using css.
    Last edited by xtal256; 17th October 2011 at 23:41.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  2. #2
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem rendering using OpenGL in VM

    I just tried setting up a subclass of QGLWidget in my application.
    When i use the QPainter (in paintEvent), i can draw a line ok but drawing a pixmap just results in a white rectangle.
    But then i copied over the OpenGL code from my old application, which uses SOIL to load a texture then render it on a quad, that works.

    So, what is the difference? I'm guessing it has something to do with the parameters i am setting on initialization, or the way i am using the images as textures (how does QPainter::drawPixmap render the image in OpenGL? I'll have a look at the source code).
    I would like to use QPainter since it's easier, but since i have already written most of the graphics code using plain OpenGL functions, i do have a way of solving my problem.


    The code...
    Qt Code:
    1. GLWidget::GLWidget(QWidget* parent) :
    2. QGLWidget(QGLFormat(QGL::SampleBuffers), parent),
    3. testImage("C:\\blah\\Test.png"),
    4. testTexId(0) {
    5. setAutoFillBackground(false);
    6. }
    7.  
    8. //--- This does NOT work. ------------------------
    9. void GLWidget::paintEvent(QPaintEvent*) {
    10. QPainter painter;
    11. painter.begin(this);
    12. painter.setRenderHint(QPainter::Antialiasing);
    13. painter.setBackground(Qt::black);
    14. painter.setPen(QPen(Qt::red, 2));
    15. painter.drawLine(0, 0, 300, 300);
    16. painter.drawPixmap(50, 50, testImage);
    17. painter.end();
    18. }
    19. //--------------------------------------------------
    20.  
    21.  
    22. //--- But this DOES. -----------------------------
    23. void GLWidget::initializeGL() {
    24. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    25. glClearDepth(1.0f);
    26. glShadeModel(GL_FLAT);
    27. glEnable(GL_POINT_SMOOTH);
    28. glEnable(GL_TEXTURE_2D);
    29. glDisable(GL_DEPTH_TEST);
    30. glEnable(GL_BLEND);
    31. glEnable(GL_COLOR_MATERIAL);
    32. glEnable(GL_COLOR_LOGIC_OP);
    33. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    34. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    36. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    37.  
    38. // Set up orthographic view
    39. glMatrixMode(GL_PROJECTION);
    40. glPushMatrix();
    41. glLoadIdentity();
    42. glOrtho(0, 800, 600, 0, -1, 1);
    43. glMatrixMode(GL_MODELVIEW);
    44. glPushMatrix();
    45. glLoadIdentity();
    46.  
    47. // The SOIL image loading code
    48. unsigned char* img = NULL;
    49. int ch, width, height;
    50.  
    51. img = SOIL_load_image("C:\\blah\\Test.png", &width, &height, &ch, SOIL_LOAD_AUTO);
    52. testTexId = SOIL_create_OGL_texture(img, width, height, ch, SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS);
    53. SOIL_free_image_data(img);
    54. }
    55.  
    56. void GLWidget::paintGL() {
    57. glDisable(GL_TEXTURE_2D);
    58. glBegin(GL_LINES);
    59. glVertex2i(0, 0); glVertex2i(300, 300);
    60. glEnd();
    61. glEnable(GL_TEXTURE_2D);
    62.  
    63. glPushMatrix();
    64. glTranslated(10, 10, 0);
    65. glRotated(12.0f, 0.0f, 0.0f, 1.0f);
    66.  
    67. // Draw quad with texture
    68. glBindTexture(GL_TEXTURE_2D, testTexId);
    69. glBegin(GL_QUADS);
    70. glTexCoord2f( 0, 0); glVertex2i(0, 0);
    71. glTexCoord2f( 0, 1); glVertex2i(0, 200);
    72. glTexCoord2f(1, 1); glVertex2i(200, 200);
    73. glTexCoord2f(1, 0); glVertex2i(200, 0);
    74. glEnd();
    75.  
    76. glPopMatrix();
    77. }
    78. //--------------------------------------------------
    To copy to clipboard, switch view to plain text mode 
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  3. #3
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem rendering using OpenGL in VM

    anyone?

    Perhaps this is just something i will have to live with. If a user of my program is unlucky enough to have this problem, they will just have to update their graphics drivers or something.

  4. #4
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem rendering using OpenGL in VM

    Ok, i have been playing around this, seeing how things render using the Qt paintEvent() function as opposed to the paintGL() one. I am seeing even more inconsistencies than before.
    Since i also want to render text on the OpenGL surface, i tried the following code:
    Qt Code:
    1. void MapView::paintEvent(QPaintEvent*) {
    2. QPainter painter(this);
    3.  
    4. painter.setBackground(Qt::black);
    5. painter.setPen(Qt::red);
    6. painter.drawLine(0, 0, 300, 300);
    7. painter.drawPixmap(10, 10, testImage);
    8.  
    9. QFont font("Arial", 40);
    10.  
    11. font.setStyleStrategy(QFont::ForceOutline);
    12. painter.setRenderHint(QPainter::Antialiasing);
    13. QPainterPath textPath;
    14.  
    15. textPath.addText(20, QFontMetrics(font).height()+150, font, "Hello, World");
    16. painter.setPen(QPen(Qt::cyan, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    17. QLinearGradient gradient(0, 0, 0, 100);
    18. gradient.setColorAt(0.0, QColor(255, 0, 0));
    19. gradient.setColorAt(1.0, QColor(0, 0, 255));
    20. painter.setBrush(gradient);
    21. painter.drawPath(textPath);
    22.  
    23. painter.setPen(Qt::yellow);
    24. painter.drawText(150, 100, "Hello again");
    25. }
    To copy to clipboard, switch view to plain text mode 

    On the VM, the gradient is not rendered (it's just a white rectangle), but the blue outline is. But, on my physical machine, it still does not render the gradient correctly. And the plain yellow text is not yellow and looks a bit squashed up.
    I also tried turning on 3D acceleration in the VM and got different results still.

    Here are some screenshots to explain better:
    test.png

    This is quite disappointing as i had hoped to use Qt's code for rendering text and images. It looks like i will just have to use 3rd party libraries or my own code, as i have been doing in the past. Sure would have been easier doing it all using Qt.

  5. #5
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem rendering using OpenGL in VM

    I've ran into more complications.

    I have a map editor for my game, and it has two OpenGL windows - one to display the map, and the other to display the tile set image (the map is composed of 2D "tiles").
    The complication is that the OpenGL textures need to be loaded for each GL context. So i cannot just load all image resources up front, i need to do it in the initializeGL function of each window.
    My old code did something similar, it would switch to the window's GL context before loading the images. But i was storing those images in the Map object, which is a bit misleading since they are so closely tied to the GL context in which they are being drawn, and not the object which "owns" them.

    This just makes me wish i could use QPixmaps and QPainter even more. It would make life so much easier.

    Does anyone know how i can get QPainter working with OpenGL?

Similar Threads

  1. QGraphicsScene OpenGL rendering
    By nelisdnurste in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2011, 10:02
  2. Replies: 4
    Last Post: 20th October 2010, 08:13
  3. opengl rendering mesh elements contour problem
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 2nd February 2010, 07:07
  4. problem in rendering opengl on QGraphicsView
    By Sandip in forum Qt Programming
    Replies: 17
    Last Post: 15th April 2008, 08:27
  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.