que about loading textures in opengl es

Discussion in 'Public Game Developers Forum' started by cool mr croc, Sep 8, 2010.

  1. cool mr croc

    cool mr croc Active Member

    May 27, 2009
    41
    0
    0
    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 :confused:
     
  2. Stroffolino

    Stroffolino Well-Known Member

    Apr 28, 2009
    1,093
    5
    38
    Software Engineer
    Pennsylvania
    Are you initializing your array with a different value for each texture?
     
  3. cool mr croc

    cool mr croc Active Member

    May 27, 2009
    41
    0
    0
    loadtexture is meant to be storing a seperate texture name in each array member. id imagine its not doing this for some reason.
     
  4. cool mr croc

    cool mr croc Active Member

    May 27, 2009
    41
    0
    0
    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
     
  5. Stroffolino

    Stroffolino Well-Known Member

    Apr 28, 2009
    1,093
    5
    38
    Software Engineer
    Pennsylvania
    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;


     

Share This Page