Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Texture image replaced by a "number" ?

This topic is 2233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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]


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

glTexCoordPointer(2, GL_FLOAT, 0, texCoords);


- (void) draw {
glVertexPointer(2, GL_FLOAT, 0, _polyVertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nPolyVertices);

[self showTexture];


glBlendFunc(GL_ONE, GL_SRC_COLOR);

GLuint texture[1];
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);

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);


[image release];
[texData release];

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

Thanks for your help Edited by PaulCaben

Share this post

Link to post
Share on other sites
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. Edited by JTippetts

Share this post

Link to post
Share on other sites
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 Edited by PaulCaben

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!