[SOLVED] OpenGL Sprite sheet clipping problem
Posted: Fri Aug 09, 2013 8:54 am
Hi everyone, its been a while since I have posted here, mostly because of school.
But I have ran into another problem in my game engine development.
This is the problem:
When I load a *small* sprite sheet in OpenGL, for example 64x64 with 4 tiles each 32x32, the sprite clipping works fine,
but when I try to load a large sprite sheet, for example 512x512, it doesn't clip the sprite properly.
This is my code:
If someone could at least tell me what is wrong I would be greatful, because I still can't figure it out.
[EDIT] Hi guys I've figured it out:
Thanks for trying to help me dandymcgee
But I have ran into another problem in my game engine development.
This is the problem:
When I load a *small* sprite sheet in OpenGL, for example 64x64 with 4 tiles each 32x32, the sprite clipping works fine,
but when I try to load a large sprite sheet, for example 512x512, it doesn't clip the sprite properly.
This is my code:
Code: Select all
void spritesheet::render() {
const float tw = float(size.getX()) / float(getTexture().w());
const float th = float(size.getY()) / float(getTexture().h());
numPerRow = getTexture().w() / size.getX();
const float tx = (m_frameIndex % numPerRow) * tw;
const float ty = (m_frameIndex / numPerRow + 1) * th;
texture.setCurrent();
glBegin(GL_QUADS);
glTexCoord2f(tx, ty + th); glVertex3f((float)pos.getX(), (float)pos.getY(), 0);
glTexCoord2f(tx + tw, ty + th); glVertex3f((float)pos.getX() + (float)size.getX(), (float)pos.getY(), 0);
glTexCoord2f(tx + tw, ty); glVertex3f((float)pos.getX() + (float)size.getX(), (float)pos.getY() + (float)size.getY(), 0);
glTexCoord2f(tx, ty); glVertex3f((float)pos.getX(), (float)pos.getY() + (float)size.getY(), 0);
glEnd();
}
[EDIT] Hi guys I've figured it out:
Code: Select all
void spritesheet::render() {
tile t;
t.numX = 4;
t.numY = 0;
t.w = 32.f / 512.f;
t.h = 32.f / 512.f;
GLfloat y = (16 - t.numY) * t.h;
GLfloat x = (t.w * t.numX);
texture.setCurrent();
glBegin(GL_QUADS);
glTexCoord2f(x, y); glVertex3f((float)pos.getX(), (float)pos.getY(), 0);
glTexCoord2f(x + t.w, y); glVertex3f((float)pos.getX() + (float)size.getX(), (float)pos.getY(), 0);
glTexCoord2f(x + t.w, y - t.h); glVertex3f((float)pos.getX() + (float)size.getX(), (float)pos.getY() + (float)size.getY(), 0);
glTexCoord2f(x, y - t.h); glVertex3f((float)pos.getX(), (float)pos.getY() + (float)size.getY(), 0);
glEnd();
}
Code: Select all
struct tile {
GLfloat numX;
GLfloat numY;
GLfloat w;
GLfloat h;
};