Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Texture image replaced by a "number" ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 PaulCaben   Members   -  Reputation: 110

Like
0Likes
Like

Posted 09 July 2012 - 11:35 AM

Hi,

I am confused about what i get from my texture :

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 :

I created a texture from a call in the init method, then i called showTexture in the "draw" method to draw the texture :

-(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

Edited by PaulCaben, 09 July 2012 - 11:37 AM.


Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8664

Like
0Likes
Like

Posted 09 July 2012 - 12:53 PM

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, 09 July 2012 - 01:01 PM.


#3 PaulCaben   Members   -  Reputation: 110

Like
0Likes
Like

Posted 09 July 2012 - 11:20 PM

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, 10 July 2012 - 01:22 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS