I'm having problems making a simple sprite rendering. I load 2 different textures. Then, I bind these textures and draw 2 squares, one with each texture. But only the texture of the first rendered object is drawn in both squares. Its like if I'd only use a texture or as if glBindTexture don't work properly. I know that GL is a state machine, but I think that you only need to change active texture with glBindTexture.

I load texture with this method:

 bool CTexture::generate( utils::CImageBuff* img )
  m_image = img;

  CHECKGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->getWitdh(), img->getHeight(),
   0, GL_RGBA, GL_UNSIGNED_BYTE, img->getImgData()));

  return true;

And I bind textures with this function:

void CTexture::bind()

Also, I draw sprites with this method

void CSprite2D::render()



  CHECKGL(glTexCoord2f(m_textureAreaStart.s,m_textureAreaStart.t)); // 0,0 by default

  CHECKGL(glTexCoord2f(m_textureAreaEnd.s,m_textureAreaStart.t)); // 1,0 by default
  CHECKGL(glVertex3i( m_position.x + m_dimensions.x, m_position.y, 0)); 

  CHECKGL(glTexCoord2f(m_textureAreaEnd.s, m_textureAreaEnd.t)); // 1,1 by default
  CHECKGL(glVertex3i( m_position.x + m_dimensions.x, m_position.y + m_dimensions.y, 0));  

  CHECKGL(glTexCoord2f(m_textureAreaStart.s, m_textureAreaEnd.t));  // 0,1 by default
  CHECKGL(glVertex3i( m_position.x, m_position.y + m_dimensions.y,0));



Edit: I bring also the check error code:

int CheckGLError(const char *GLcall, const char *file, int line)
    GLenum errCode;

    //avoids infinite loop
    int errorCount = 0;
    while ( (errCode=glGetError()) != GL_NO_ERROR && ++errorCount < 3000)
        utils::globalLogPtr log = utils::CGLogFactory::getLogInstance();
        const GLubyte *errString;
        errString = gluErrorString(errCode);
        std::stringstream ss;
        ss << "In "<< __FILE__<<"("<< __LINE__<<") "<<"GL error with code: " << errCode<<" at file " << file << ", line " << line << " with message: " << errString << "\n"; 
    return 0;
  • \$\begingroup\$ Please format the post better, being sure to put code into blocks (shift code in by four spaces). \$\endgroup\$ – James Jan 14 '11 at 21:55

Once you are done sending vertex data to OpenGL, you need to call glEnd, which I don't see called anywhere in your code. It should probably be before your PopMatrix call.

For example, glBindTexture's documentation says:

GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd.

I assume your CHECKGL macro is supposed to catch reports via glGetError, in which case you might want to go back and make sure it works.

  • \$\begingroup\$ You are right! What stupid error!!! But CHECKGL didn't said nothing. It is very strange, because the code works fine, but no error is found... \$\endgroup\$ – Killrazor Jan 14 '11 at 22:21
  • \$\begingroup\$ I wouldn't expect glGetError to work between begin and end (or if you miss an end) you can easily wrap glBegin and glEnd yourself to catch this on a frame by frame basis. zero some value at start of frame, increment on a begin, decrement on an end and assert/trace/crash if its not zero when done rendering that frame to catch the error \$\endgroup\$ – jheriko Jul 16 '11 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.