★ TouchArcade needs your help. Click here to support us on Patreon.

que about loading textures in opengl es

09-08-2010, 05:31 AM
Joined: May 2009
Posts: 41
que about loading textures in opengl es

hi mates. im having to load a texture each time i use it in a frame, i cant just load them all in an array then use whichever member of the array. im thinking perhaps i need to unbind the texture after i use it, or is something wrong with my texture loading function? as it is now, it only uses the last texture i load in.

texture loading
- (void)loadTexture:(NSString *)name intoLocation:(GLuint)location
    CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
    if (textureImage == nil) 
        NSLog(@"Failed to load texture image");
    NSInteger texWidth = CGImageGetWidth(textureImage);
    NSInteger texHeight = CGImageGetHeight(textureImage);
	GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
    CGContextRef textureContext = CGBitmapContextCreate(textureData,
														texWidth, texHeight,
														8, texWidth * 4,
	//rotate image coz y is opposites
	CGContextTranslateCTM(textureContext, 0, texHeight);
	CGContextScaleCTM(textureContext, 1.0, -1.0);
	CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), textureImage);
	glBindTexture(GL_TEXTURE_2D, location);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
	glEnable( GL_BLEND );
	glBlendFunc( GL_ONE, GL_SRC_COLOR);
texture drawing
void player::doStuff(GLuint theTexture)
	glBindTexture(GL_TEXTURE_2D, theTexture);
	glTranslatef(playerx, playery, 0.0);
	glVertexPointer(3, GL_FLOAT, 0, squareVertices);
	glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
and loading both textures im trying to use is just

	[self loadTexture:@"crowdandpitchwithannotations.png" intoLocation:textures[0]];
	[self loadTexture:@"player.png" intoLocation:textures[1]];
thats done in setup view, f i move it to draw view and load each texture before i use it, it works fine
09-08-2010, 06:20 AM
Are you initializing your array with a different value for each texture?
09-08-2010, 06:41 AM
Joined: May 2009
Posts: 41
loadtexture is meant to be storing a seperate texture name in each array member. id imagine its not doing this for some reason.
09-08-2010, 07:03 AM
Joined: May 2009
Posts: 41
i re read what you wrote and realised i wasnt intialising, wondered how i should be, then realised the arrays should have been holding texture names before i sent them, i had been forgetting to use glGenTextures(2, &textures[0]);

all working now, cheers
09-08-2010, 07:03 AM
That's my point: your loadtexure isn't storing anything. If takes a read only parameter as input, and returns nothing.

I'd expect your problem to go away if before calling loadtexture you initialize:
intoLocation:textures[0] = 100;
intoLocation:textures[1] = 101;

Originally Posted by cool mr croc View Post
loadtexture is meant to be storing a seperate texture name in each array member. id imagine its not doing this for some reason.