Help with spritesheet texture coords
Okay I have taken the GLSprite example with iPhone Apple opengl es
When it's 1 texture (32x32) I can draw it fine. But I created a sprite sheet ( 96x32 ) It's a horizontal sheet with 3 (32x32) images with 1 row.
It always appears as white with the sheet, but correctly when it's a single image.
Here are my defines ( I tried and prefer 1:1 pixel coords but I doubt it's possible)
Sprite (32x32)
// Sets up an array of values to use as the sprite vertices.
const GLfloat spriteVertices[] = {
-16.0f, -16.0f,
16.0f, -16.0f,
-16.0f, 16.0f,
16.0f, 16.0f
};
Texture Coords
// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0, // bottom left
96, 0, // bottom right
0, 32, // top left
96, 32, // top right
};
// Sets up an array of values for the texture coordinates.
const GLshort spriteWallcoords[] = {
0, 0, // bottom left
32, 0, // bottom right
0, 32, // top left
32, 32, // top right
};
/*
// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0, // bottom left
1, 0, // bottom right
0, 1, // top left
1, 1, // top right
};
// Sets up an array of values for the texture coordinates.
const GLshort spriteWallcoords[] = {
0, 0, // bottom left
1/3, 0, // bottom right
0, 1, // top left
1/3, 1, // top right
};
*/
Load Texture
if(spriteImage) {
// Allocated memory needed for the bitmap context
spriteData = (GLubyte *) malloc(width * height * 4);
// Uses the bitmatp creation function provided by the Core Graphics framework.
colorSpace = CGColorSpaceCreateDeviceRGB();
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4,colorSpace, kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(spriteContext);
// Use OpenGL ES to generate a name for the texture.
glGenTextures(1, &spriteTexture);
// Bind the texture name.
glBindTexture(GL_TEXTURE_2D, spriteTexture);
// Speidfy a 2D texture image, provideing the a pointer to the image data in memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
// Release the image data
free(spriteData);
Display Texture(drawView)
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, backingWidth, backingHeight, 0, -20, 20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Make sure texturing is enabled
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// Clear background to beautiful green :o)
glClearColor(1.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// setup viewport and projection
glTranslatef(100.0f,100.0f, 0.0f);
glTexCoordPointer(2, GL_SHORT, 0, spriteWallcoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Any advice is greatly appreciated.
Thanks,
Nick
Hi nick5454, the problem looks to be that you're attempting to pass your texture coordinates as a short. So when you try to do 1 / 3, you wind up with 0, rather than .333333 as you are expecting. This is because it's using integer division as opposed to floating point. Easiest fix would to just swap over to using floats for your tex coordinates.
I tried that also....It comes over as a white square.
I changed the struct and the calls to "GLfloat" and "GL_FLOAT" respectively.
Any advice is appreciated.
I changed the struct and the calls to "GLfloat" and "GL_FLOAT" respectively.
Any advice is appreciated.
It appears to be something with my png file that I created with paint.net.
I widened it and it's still white.
I widened it and it's still white.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement