5
votes

I'm trying to show a texture in Qt with opengl but it just doesn't show the texture when i run.

I did some research and found out I needed to make the height and width of the texture as a power of 2. my texture is now (1024x1024).

I also added a lot of glTexParameterf's that could resolve my problem, but still no luck.

void WorldView::paintGL ()
{
this->dayOfYear = (this->dayOfYear+1);
this->hourOfDay = (this->hourOfDay+1) % 24;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

// store current matrix
glMatrixMode( GL_MODELVIEW );
glPushMatrix( );

gluLookAt(camPosx ,camPosy ,camPosz,
    camViewx,camViewy,camViewz,
    camUpx, camUpy, camUpz );

//Draw Axes
glDisable( GL_LIGHTING );
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(10.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 10.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 10.0);
glEnd();

//load texture
QImage img;
if(!img.load(":/files/FloorsCheckerboardSmall.bmp"))
    printf("could not open image");

//try showing texture
glEnable( GL_LIGHTING );
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_TEXTURE_2D);

unsigned int m_textureID;
glGenTextures(1, &m_textureID);
glBindTexture(GL_TEXTURE_2D,m_textureID);
glTexImage2D(GL_TEXTURE_2D,0,(GLint)img.depth(),(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGB,GL_UNSIGNED_BYTE,img.bits());
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
    glTexCoord2d(0.0,5.0);
    glVertex2d(0.0,3.0);

    glTexCoord2d(0.0,0.0);
    glVertex2d(0.0,0.0);

    glTexCoord2d(5.0,0.0);
    glVertex2d(3.0,0.0);

    glTexCoord2d(5.0,5.0);
    glVertex2d(3.0,3.0);
    glEnd();
}

EDIT1: Could it be my texture is too big?

EDIT2: glBindTexture(GL_TEXTURE_2D,m_textureID); placed before glBindTexture instead of before glColor3d

SOLVED: My img.depth() returned an invalid internalFormat value. I replaced this GLint with the valid interalFormat value GL_RGBA. I also changed the format from GL_RGB to GL_RGBA (see genpfaults answer)

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,img.bits());
2
Where's the corresponding glPopMatrix() for the glPushMatrix() at the top of paintGL()? - genpfault
In your glTexImage2D() call does img.depth() actually return 1, 2, 3, 4, or any of the valid OpenGL enum values for internalFormat? - genpfault
I'll add glPopMatrix to the code, and img.depth() returns 32. - stanthecomputerman
Alright, depth was indeed the problem. I tried the a valid internalFormat value (1) and it worked! Thank you! I would like to give that comment a thumbs up, but i don't think my reputation is high enough xD - stanthecomputerman
Why are you not using QOpenGLTexture? - peppe

2 Answers

1
votes
                             vvvvvvvvvvvvvvvvvv
glTexImage2D(GL_TEXTURE_2D,0,(GLint)img.depth(),(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGB,GL_UNSIGNED_BYTE,img.bits());

img.depth() returns 32

Then you need to pass in a valid internalFormat value, not img.depth(). Try GL_RGBA.

You should also set format to GL_RGBA since img.bits() is actually four-component.

4
votes

You have to call

glBindTexture(GL_TEXTURE_2D,m_textureID); 

before calling glTexImage2D(...). Or OpenGL won't know where the data you send it belongs.