Results 1 to 10 of 10

Thread: OpenGL Texture Loading Problem

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default OpenGL Texture Loading Problem

    Hi guys, I started playing around with OpenGL and initially, I was getting the message box popping up in the LoadGLWidget function call but I changed the type to void over int and that stopped happening... so by the looks of it, the image started to load without a problem. The problem however is that despite no error message box popping up, I see the coloured box, rather than having that box filled with my test image (both can be seen in the attachment. Any ideas as to what I'm doing wrong?

    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. GLWidget::GLWidget(QWidget *parent) :
    4. QGLWidget(parent)
    5. {
    6. }
    7.  
    8. void GLWidget::initializeGL()
    9. {
    10. glEnable(GL_TEXTURE_2D);
    11. glClearColor(1,1,0.5,1);
    12.  
    13. glClearDepth(1.0f);
    14. glEnable(GL_DEPTH_TEST);
    15. glDepthFunc(GL_LEQUAL);
    16. }
    17.  
    18. //Setup viewport, projection etc...
    19. void GLWidget::resizeGL(int w, int h)
    20. {
    21.  
    22. if (h == 0)
    23. {
    24. h = 1;
    25. }
    26. }
    27.  
    28. //Draw the Scene
    29. void GLWidget::paintGL() //corresponds to DrawGLScene
    30. {
    31.  
    32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    33. glLoadIdentity();
    34.  
    35. //glTranslatef(x, y, z)
    36.  
    37. //glTranslatef(0.0f,0.0f,-10.0f);
    38.  
    39. glBindTexture(GL_TEXTURE_2D, texture[0]);
    40.  
    41. glBegin(GL_QUADS);
    42. //Bottom Left
    43. glColor3f(0.5,0.5,0.5);
    44. glTexCoord2f(0.0f, 0.0f);
    45. glVertex2f(-1.0f, -1.0f);
    46.  
    47. //Top Left
    48. glColor3f(0.2,0.7,0);
    49. glTexCoord2f(0.0f, 1.0f);
    50. glVertex2f(-1.0f,1.0f);
    51.  
    52. //Top Right
    53. glColor3f(0.9,0.1,0.5);
    54. glTexCoord2f(1.0f, 1.0f);
    55. glVertex2f(1.0f,1.0f);
    56.  
    57. //Bottom Right
    58. glColor3f(0.6,0.6,0.6);
    59. glTexCoord2f(1.0f, 0.0f);
    60. glVertex2f(1.0f,-1.0f);
    61. glEnd();
    62.  
    63. /*
    64.   glBegin(GL_TRIANGLES);
    65.   glColor3f(0,1,0.7);
    66.   glVertex3f(-1,0,0);
    67.   glColor3f(0,0.1,0.5);
    68.   glVertex3f(1,0,0);
    69.   glColor3f(0.9,0.1,0);
    70.   glVertex3f(0,1,0);
    71.   glEnd();
    72. */
    73.  
    74. }
    75.  
    76. void GLWidget::LoadGLTexture()
    77. {
    78. QImage GLFormatImage;
    79. QImage QFormatImage;
    80.  
    81. if (!QFormatImage.load("actual path to the file with .bmp format"))
    82. {
    83. QMessageBox::information(this, "Error", "Could not load image");
    84. }
    85.  
    86. GLFormatImage = QGLWidget::convertToGLFormat(QFormatImage);
    87. glGenTextures(1, &texture[0]);
    88. glBindTexture(GL_TEXTURE_2D, texture[0]);
    89. glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    92. }
    To copy to clipboard, switch view to plain text mode 
    What I want.png
    What I get.jpg
    Last edited by Atomic_Sheep; 17th May 2012 at 17:50.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: OpenGL Texture Loading Problem

    Your texture definition is wrong. In glTexImage2D is a error. You pass 3bytes, 24bits per pixel (3), but Your data has 4bytes, 32 bits per pixel (GL_RGBA):

    Qt Code:
    1. glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    To copy to clipboard, switch view to plain text mode 
    it should be either:
    Qt Code:
    1. glTexImage2D(GL_TEXTURE_2D, 0, 4, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    2. // or
    3. glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    To copy to clipboard, switch view to plain text mode 
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: OpenGL Texture Loading Problem

    I tried playing around with those settings, still not working but I'll have to read a little bit about image formats it seems. Don't fully understand all the various formats that are possible.

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: OpenGL Texture Loading Problem

    Is that Your entire code, that you posted in first post, for GLWidget? or did You truncated it?
    If yes, then You are missing a lot of important stuff, i.e. model/view matrix definition etc...
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: OpenGL Texture Loading Problem

    Yep, that's pretty much all the code I've got so far... I'm just following along with NeHe's tutorial and a Qt version of it that I found elsewhere. Hmm... I'll have to look over what I might have missed in that case. What do you mean by model and view matrix definition? I've provided the 2D quad that I want to map the texture to, isn't that the model?

    P.S. After reviewing NeHe's tutorial again, I'm still failing to see what I might have missed.

    I also made sure the image was a power of 2 dimension... not necessary I know but just in case.

    Finally I read about the image formats... and according to what I read:

    Qt Code:
    1. glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    To copy to clipboard, switch view to plain text mode 

    should be right... it's 24 bit which I assume to be per pixel and that also suggests to me at least that it's RGB and not RGBA... although in the attributes it does have A, but I couldn't find out what that meant.
    Last edited by Atomic_Sheep; 22nd May 2012 at 13:09.

  6. #6
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: OpenGL Texture Loading Problem

    GL_RGB = GL Red Green Blue
    GL_RGBA GL Red Green Blue Alpha

    BMP don't support Alpha, so only mistake that You coukd make is with BPP (bits per pixel), i.e. is BMP 24bit.

    Matrix'es are to describe projection (how to draw 3D scene into 2D image) and model (scene scales etc..)

    I just looked at first Nehe tutorial and they create matrixes, so I don't know with tutorial You fallowed.
    On nehe www they have also Qt project for (probably) each tutorial so I suggest to take a look at them.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  7. The following user says thank you to Talei for this useful post:

    Atomic_Sheep (24th May 2012)

  8. #7
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: OpenGL Texture Loading Problem

    Thanks for the advice, reviewed a lot of NeHe's tutorials... I finally got the texture to load, but it's not as I expected it to be, but I'll try to figure it out, don't have any ideas as to what is going wrong at the moment but hopefully after reviewing the graphics pipeline I'll figure it out. Well I have an idea of where in the graphics pipeline the problem lies, that's about the extent of my understanding of the problem.
    Last edited by Atomic_Sheep; 23rd May 2012 at 07:19.

  9. #8
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL Texture Loading Problem

    how do you solved this problem.
    please post out your answer here, Thanks


    Added after 35 minutes:


    oh,i know the answer
    you must call your LoadGLTexture function in initializeGL or where you really need a texture
    i.m a qt beginner.
    Last edited by catchxiaoshuang; 10th January 2013 at 14:26.

  10. #9
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL Texture Loading Problem

    Hi,

    I would be really pleased if you could post your answer. I have the same probleme and both equivalents of :
    glTexImage2D(GL_TEXTURE_2D, 0, 4, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    // or
    glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits());
    doesnt work.

    Call of "QGLWidget::convertToGLFormat" crash whenever !

  11. #10
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: OpenGL Texture Loading Problem

    Quote Originally Posted by 0_Azerty_0 View Post
    Hi,

    I would be really pleased if you could post your answer. I have the same probleme and both equivalents of : doesnt work.

    Call of "QGLWidget::convertToGLFormat" crash whenever !
    are you using BMP or bits to load the image? Could you post your code here. The whole code.
    if it's not necessarily BMP, you may use
    Qt Code:
    1. texture = bindTexture(QPixmap(":/texture.png"));
    To copy to clipboard, switch view to plain text mode 
    to load the texture from a file.

    On the other hand, if you want to load a dynamic texture, then use FBO,
    Qt Code:
    1. fbo->bind();
    2.  
    3. //Render something here and load it later as the texture
    4.  
    5. fbo->release();
    6.  
    7. dynamicTexture = fbo->texture();
    To copy to clipboard, switch view to plain text mode 

    for fbo, you may read the documentation for QGLFramebufferObject.

    If you still have problem debugging paste the whole code.

Similar Threads

  1. openGL bindtexture from within thread not working
    By thinkabit in forum Qt Programming
    Replies: 0
    Last Post: 23rd April 2011, 12:13
  2. Replies: 1
    Last Post: 4th February 2011, 03:34
  3. Replies: 0
    Last Post: 6th December 2009, 00:41
  4. Qt with OpenGL ES for ARM9 - All OpenGL ES tests have failed!
    By vinpa in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 3rd December 2009, 10:10
  5. aux functions not working on opengl
    By john_god in forum Qt Programming
    Replies: 7
    Last Post: 11th April 2009, 12:23

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.