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