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

que about loading textures in opengl es

09-08-2010, 05:31 AM
#1
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
Code:
- (void)loadTexture:(NSString *)name intoLocation:(GLuint)location
{
    CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
    if (textureImage == nil) 
	{
        NSLog(@"Failed to load texture image");
		return;
    }
	
    NSInteger texWidth = CGImageGetWidth(textureImage);
    NSInteger texHeight = CGImageGetHeight(textureImage);
	
	GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
	
    CGContextRef textureContext = CGBitmapContextCreate(textureData,
														texWidth, texHeight,
														8, texWidth * 4,
														CGImageGetColorSpace(textureImage),
														kCGImageAlphaPremultipliedLast);
	//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);
	CGContextRelease(textureContext);
	
	glBindTexture(GL_TEXTURE_2D, location);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
	
	free(textureData);
	
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
	glBlendFunc( GL_ONE, GL_SRC_COLOR);
}
texture drawing
Code:
void player::doStuff(GLuint theTexture)
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glBindTexture(GL_TEXTURE_2D, theTexture);
	glTranslatef(playerx, playery, 0.0);
	glVertexPointer(3, GL_FLOAT, 0, squareVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	
	glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
and loading both textures im trying to use is just

Code:
	[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
#2
Joined: Apr 2009
Location: Pennsylvania
Posts: 1,005
Are you initializing your array with a different value for each texture?
09-08-2010, 06:41 AM
#3
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
#4
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
#5
Joined: Apr 2009
Location: Pennsylvania
Posts: 1,005
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;


Quote:
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.