Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 )
 { 
  assert(img);
  m_image = img;
  CHECKGL(glGenTextures(1,&m_textureID));

  CHECKGL(glBindTexture(GL_TEXTURE_2D,m_textureID));
  CHECKGL(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR));
  CHECKGL(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR));
  //CHECKGL(glTexImage2D(GL_TEXTURE_2D,0,img->getBpp(),img->getWitdh(),img->getHeight(),0,img->getFormat(),GL_UNSIGNED_BYTE,img->getImgData()));
  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()
 {
  CHECKGL(glBindTexture(GL_TEXTURE_2D,m_textureID));
 }

Also, I draw sprites with this method

void CSprite2D::render()
 {
  CHECKGL(glLoadIdentity());
  CHECKGL(glEnable(GL_TEXTURE_2D));
  CHECKGL(glEnable(GL_BLEND));

  CHECKGL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
  m_texture->bind();

  CHECKGL(glPushMatrix());

  CHECKGL(glBegin(GL_QUADS));
  CHECKGL(glTexCoord2f(m_textureAreaStart.s,m_textureAreaStart.t)); // 0,0 by default
  CHECKGL(glVertex3i(m_position.x,m_position.y,0));

  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));

  CHECKGL(glPopMatrix());

  CHECKGL(glDisable(GL_BLEND));
 }

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"; 
        log->addMessage(ss.str(),ZEL_APPENDER_GL,utils::LOGLEVEL_ERROR);
    }
    return 0;
}
share|improve this question
    
Please format the post better, being sure to put code into blocks (shift code in by four spaces). –  James Jan 14 '11 at 21:55

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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... –  Killrazor Jan 14 '11 at 22:21
    
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 –  jheriko Jul 16 '11 at 3:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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