OpenGL Texture doesn't show
Asked Answered
F

2

6

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());
Farseeing answered 21/5, 2014 at 21:24 Comment(5)
Where's the corresponding glPopMatrix() for the glPushMatrix() at the top of paintGL()?Surbased
In your glTexImage2D() call does img.depth() actually return 1, 2, 3, 4, or any of the valid OpenGL enum values for internalFormat?Surbased
I'll add glPopMatrix to the code, and img.depth() returns 32.Farseeing
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 xDFarseeing
Why are you not using QOpenGLTexture?Scowl
S
2
                             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.

Surbased answered 21/5, 2014 at 22:12 Comment(1)
You're right, after only changing the internalFormat, the texture looked a bit odd. By also changing the format form GL_RGB to GL_RGBA, the texture showed perfectlyFarseeing
S
4

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.

Semblance answered 21/5, 2014 at 21:34 Comment(3)
thanks for the response! I tried it but I still can't see the texture. It's still giving me the red color defined by glColor3f(1.0, 0.0, 0.0); instead of the texture.Farseeing
@user3662600: Make sure to update the question with the current state of the code.Surbased
After revising the code, I saw this also was one of the reasons wich was causing my problem, so thanks!Farseeing
S
2
                             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.

Surbased answered 21/5, 2014 at 22:12 Comment(1)
You're right, after only changing the internalFormat, the texture looked a bit odd. By also changing the format form GL_RGB to GL_RGBA, the texture showed perfectlyFarseeing

© 2022 - 2024 — McMap. All rights reserved.