Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture image replaced by a "number" ?

Started by PaulCaben Jul 9, 2012 at 5:35 PM 1 replies 1.5k views
Original Post
PaulCaben
PaulCaben
Hi,

I am confused about what i get from my texture :

[attachment=9940:Capture d’écran 2012-07-09 à 19.30.45.png]


I followed this great tutorial about texturing, and i have tried to re-use the code with a png image (512x512). Here is the code i am using :

[color=#000000][font=Arial,]I created a texture from a call in the init method, then i called showTexture in the "draw" method to draw the texture :[/font]

-(void)showTexture{

static const GLfloat texCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);

//glDisableClientState(GL_VERTEX_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

- (void) draw {
//...
glEnable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, _polyVertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nPolyVertices);

[self showTexture];

}

-(void)initTexture{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_COLOR);

GLuint texture[1];
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

NSString *path = [[NSBundle mainBundle] pathForResource:@"brown" ofType:@"png"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
NSLog(@"Do real error checking here");

GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, height - height );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

CGContextRelease(context);

free(imageData);
[image release];
[texData release];
}


- (id)init {
if ((self = [super init])) {
//...
[self initTexture];
}
return self;
}



Thanks for your help
JTippetts
JTippetts
Upload the .PNG you are using. You can use a service such as imgur.com to upload, then post a link here.
2nd Edit: NM
I haven't much experience with those library calls. But I suspect that what is happening is that when you create a chunk of memory with malloc, then use it in the call to CGBitmapContextCreate, it creates the context then copies whatever pixels were already in the malloc block over to the context buffer. In this case, since you don't set any pixels, what yo uget is junk.
PaulCaben
PaulCaben
Thanks for the answer, Edit: so i am not sure why this did not work, but i found an example from the project "Explosions" made by 71squared, that has a file that deals with the texture import. It works fine, if anyone is looking for the same thing.

Thanks for the answer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.