EDIT: Issue is solved.
Being new to OpenGL, I'm figuring the problem is due to binding an image, but I'm not exactly sure where.
I loaded in my first sprite, and it animates fine.
I loaded in the second sprite, and now the second sprite only shows, and the first loaded sprite will not show.
Higher level code:
// Load in the background image
if (!img.init("res/art/grass.jpg"))
return false;
img.setPos(0, 0);
img.stretch(SCREEN_WIDTH, SCREEN_HEIGHT);
// Load alien sprite
if (!alien.init("res/art/sprite.png")) {
return false;
} else {
alien.setCounterLimit(2);
Sequence s(WALK_RIGHT);
s.addCell(10, 169, 23, 203);
s.addCell(26, 169, 42, 203);
s.addCell(45, 169, 59, 203);
s.addCell(60, 169, 72, 203);
s.addCell(75, 169, 88, 203);
s.addCell(90, 169, 106, 203);
s.addCell(108, 169, 122, 203);
s.addCell(124, 169, 139, 203);
alien.addAnimSequence(s);
Sequence s2;
s2.setID(FIRE);
s2.addCell(216, 6, 246, 40);
alien.addAnimSequence(s2);
alien.setAnimSequenceID(WALK_RIGHT);
alien.setPos(0.0f, 20.0f);
alien.setVel(10.0f);
alien.scale(2.0f);
}
// Load druid sprite
if (!spr.init("res/art/druid.png")) {
return false;
} else {
spr.setCounterLimit(2);
Sequence s3;
s3.setID(STILL);
s3.addCell(0, 0, 160, 120);
spr.addAnimSequence(s3);
spr.setAnimSequenceID(STILL);
spr.setPos(400.0f, 20.0f);
spr.scale(1.0f);
}
// ... later in the rendering method
gl.draw(img);
alien.draw();
spr.draw();
Likewise, if I reverse the order of loading, the last sprite loaded will only show. So, I've been able to pinpoint that this could be a binding issue either in the init() or draw() function members. Can you spot it?
My class hiarchy is...
class Sprite : public Image
- Sprite does the drawing via draw(), initialization with init(), which calls class Image's init() to load in the texture.
Here's the initialization function in Image. Is it binding each new texture correctly?
bool Image::init(const string &filename)
{
// Check if this is the first time initializing an image
if (refCount == 0)
{
// Init DevIL
ilInit();
iluInit();
ilutRenderer(ILUT_OPENGL);
// Will be using GL_TEXTURE_RECTANGLE_ARB for image coordinates.
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// Set origin for all images
ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
}
// Generate the image
ilGenImages(1, &m_handle);
// Bind the current image
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_handle);
// load the image
if (ilutGLLoadImage((char *)filename.c_str()) == NULL)
return false;
// Check if image is backwards
if (ilGetInteger(IL_IMAGE_ORIGIN) == IL_ORIGIN_LOWER_LEFT) {
m_flipped = true;
iluFlipImage();
}
// Convert to IL_RGBA
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
// get the image's width and height
m_width = ilGetInteger(IL_IMAGE_WIDTH);
m_height = ilGetInteger(IL_IMAGE_HEIGHT);
m_size = ilGetInteger(IL_IMAGE_SIZE_OF_DATA);
m_imageType = ilGetInteger(IL_IMAGE_TYPE);
m_imageFormat = ilGetInteger(IL_IMAGE_FORMAT);
m_channels = ilGetInteger(IL_IMAGE_CHANNELS);
// Generate texture for mapping
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, m_imageFormat, m_width,
m_height, 0, m_imageFormat, m_imageType, ilGetData());
// Set texture parameters
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Set initial color components
m_red = 1.0f;
m_green = 1.0f;
m_blue = 1.0f;
m_alpha = 1.0f;
// Increase the reference count
refCount++;
m_show = true; // Default show state
m_angle = 0.0f; // Rotation angle
m_scale_x = 1.0f; // Default X scale value
m_scale_y = 1.0f; // Default Y scale value
m_stretch_width = 0.0f; // Default Stretch Width
m_stretch_height = 0.0f; // Default Stretch Height
return true;
}
If that looks fine, perhaps it's a drawing issue on binding. I put that code below for viewing. Oddly enough, if I create just an Image object and display it with one Sprite object, it shows the two textures just fine. If I create two Sprite objects, I only get one sprite displaying. See attached image.
[attachment=25342:image texture with one sprite.png]
void Sprite::draw()
{
// Check if the sprite is visible
if (!m_show)
return;
// Check if any cells are available for rendering
if (m_cells.empty())
return;
glPushMatrix();
glLoadIdentity();
// bind texture
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&m_boundTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_handle);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_boundTexture);
// Set color tint
glColor4f(m_red, m_green, m_blue, m_alpha);
// Draw the texture
glBegin(GL_QUADS);
// bottom left
glTexCoord2f(m_cells[m_currentFrame].x + 1, m_cells[m_currentFrame].height);
glVertex2f(m_x, m_y + (m_cells[m_currentFrame].height - m_cells[m_currentFrame].y) * m_scale_y);
// bottom right
glTexCoord2f(m_cells[m_currentFrame].width, m_cells[m_currentFrame].height);
glVertex2f(m_x + (m_cells[m_currentFrame].width - m_cells[m_currentFrame].x)*m_scale_x,
m_y + (m_cells[m_currentFrame].height - m_cells[m_currentFrame].y) * m_scale_y);
// upper right
glTexCoord2f(m_cells[m_currentFrame].width, m_cells[m_currentFrame].y);
glVertex2f(m_x + (m_cells[m_currentFrame].width - m_cells[m_currentFrame].x)*m_scale_x, m_y);
// upper left
glTexCoord2f(m_cells[m_currentFrame].x + 1, m_cells[m_currentFrame].y);
glVertex2f(m_x, m_y);
glEnd();
glPopMatrix();
m_boundTexture = -1;
}