PDA

View Full Version : OpenGL Texture problems




seventoes
Aug 7, 2008, 09:50 PM
This is probably something simple, but I cant seem to spot whats wrong with it.

http://img386.imageshack.us/my.php?image=picture4gk6.png

:confused:

I load the image just like the GLSprite example,

tempImage = [UIImage imageNamed:@"BackgroundGrid.png"].CGImage;
width = next_power_of_2(CGImageGetWidth(tempImage));
height = next_power_of_2(CGImageGetHeight(tempImage));

// Allocate memory for bitmap context
tempData = (GLubyte *) malloc(width * height * 4);
tempContext = CGBitmapContextCreate(tempData, width, height, 8, width*4, CGImageGetColorSpace(tempImage), kCGImageAlphaPremultipliedLast);
// Draw sprite on the context
CGContextDrawImage(tempContext, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), tempImage);

// etc...

My power of 2 function is working fine, but it looks like some size is being messed up somewhere. I'm thinking it has something to do with the arrays im using for glTexturePointer and glTexCoordPointer, since i still dont fully get what those 2 are for. I tried to go through the Texture2D code to figure out what values to use, and i came up with this:

// Sets up an array of values to use as the sprite vertices.
const GLfloat spriteVertices[] = {
0, 0, 1,
320, 0, 1,
0, 480, 1,
320, 480, 1,
};

// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 1,
1, 1,
0, 0,
1, 0,
};
Drawing with:glBindTexture(GL_TEXTURE_2D, sprites[eTexBackground].tex);
glVertexPointer(3, GL_FLOAT, 0, spriteVertices);
glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Also the texture randomly flashes on and off the screen :-/

Anyone tell me what noob mistake I'm making?



DipDog3
Aug 7, 2008, 10:11 PM
Try:

// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};

seventoes
Aug 7, 2008, 10:24 PM
Try:

No luck, same result :(

DipDog3
Aug 8, 2008, 09:01 AM
Try

tempContext = CGBitmapContextCreate(tempData, width, height, 8, width, CGImageGetColorSpace(tempImage), kCGImageAlphaPremultipliedLast);

Changed the width*4 to width for bytesPerRow.

seventoes
Aug 8, 2008, 01:57 PM
Try



Changed the width*4 to width for bytesPerRow.

Nope,
CGBitmapContextCreate: invalid data bytes/row: should be at least 2048 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedLast.

DipDog3
Aug 8, 2008, 02:30 PM
Nope,

That sucks.

Try resizing your image to 256x256 and try applying it.

seventoes
Aug 8, 2008, 02:40 PM
That sucks.

Try resizing your image to 256x256 and try applying it.

K, resized it and changed:const GLfloat spriteVertices[] = {
0, 0, 1,
256, 0, 1,
0, 256, 1,
256, 256, 1,
};
Now i get this:
http://img371.imageshack.us/my.php?image=picture4aa4.png

ThaBunny
Aug 8, 2008, 07:13 PM
You're passing your texcoords as FLOAT, but they're SHORTs

Change the texcoord array to

const float spriteTexcoords[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};

seventoes
Aug 8, 2008, 07:18 PM
You're passing your texcoords as FLOAT, but they're SHORTs

Change the texcoord array to

const float spriteTexcoords[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
That was it!! I knew it was something stupid. :p Thanks so much!

Now the texture is showing up just fine, but its randomly flashing :(

EDIT: Nevermind on that one, my erase function was swapping the buffers :|

Thanks for the help!