Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Texture doesn't show

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());
like image 936
stanthecomputerman Avatar asked May 21 '14 21:05

stanthecomputerman


1 Answers

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.

like image 99
JudgingBird Avatar answered Oct 11 '22 17:10

JudgingBird