Problem using Texture2D sub-section

Nibbie
Posts: 1
Joined: 2010.03
Post: #1
Hi. I've been coding iphone apps for a while but am new(ish) to opengl and its starting to make me go a little crazy (well, one particular problem anyway).

I basically have a 3d globe. On there I have placed random speech bubbles. The speech bubble is a png of dimensions 200px wide by 40px high. I have this rendered at runtime to sit inside a 256x256 image, which is then used to initialise the Texture2D object.

The image renders to the screen fine and in the correct place. The problem I am having is that it renders the entire 256x256 texture. I know i need to be drawing a sub-section of that (i.e. my bubble png which is at 0,0,200,4)... but i cant for the life of me put the pieces of the jigsaw together to achieve this.

I have a separate class for the bubble, so you might need to ask to see what code sits in the EAGLView class. But for now, here is the code in the bubble class:

Code:
#import "GlobeMarker.h"
#import "Texture2D.h"
#import <OpenGLES/ES1/glext.h>
#define kMaxTextureSize     1024

// A class extension to declare private methods
@interface GlobeMarker ()

- (void) createBubble;

@end

@implementation GlobeMarker

-(GlobeMarker*)initWithX:(float)xIn Y:(float)yIn Z:(float)zIn markerType:(GlobeMarkerRenderEffect)mType {
    self = [super init];
    
    if( self ){
        BoxRadius = 0.075;
        bounds = CGRectMake(-BoxRadius/2, -BoxRadius/2, BoxRadius, BoxRadius);
        transX = xIn;    transY = yIn;    transZ = zIn;
        drawPoint = NO;
        
        locationsVertices[0] = 0.0;
        locationsVertices[1] = 1.0;
        locationsVertices[2] = 0.0;
        
        locationsVertexCount = 1;
        locationsIndices[0] = 0;
        locationsIndices[1] = 1;
        locationsIndices[2] = 2;
        
        locationsIndexCount = 3;
        locationsNumVertices = 1;
        
        currentEffect = mType;
        
        [self createBubble];
    }
    return self;
}

-(void) createBubble {
    UIImageView *bubbleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    bubbleView.backgroundColor = [UIColor clearColor];
    bubbleView.image = [[UIImage imageNamed:@"bubble.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:18];
    
    // Save the view as an image
    UIGraphicsBeginImageContext(CGSizeMake(256, 256));
    [bubbleView drawRect:CGRectMake(0, 0, 200, 40)];
    UIImage *layeredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    marker = [[Texture2D alloc] initWithImage:layeredImage filter:GL_LINEAR];
}

-(void)renderRotX: (float)xIn rotY: (float)yIn rotZ: (float)zIn zoom: (float)zoom offset: (float) offset
{
    //Zoom range is [-10, -6.5] -> [far, close]
    //Bounds raide is .075 @ -10
    //It needs to look the same size at -6.5
    offset -= 0.012;
    BoxRadius = .055*zoom/2;
    bounds = CGRectMake(-BoxRadius/2, -BoxRadius/2, BoxRadius, BoxRadius);
    
    [self renderRotX: xIn rotY: yIn rotZ: zIn offset: offset];
}

-(void)renderRotX: (float)xIn rotY: (float)yIn rotZ: (float)zIn offset: (float)offset
{
    float scaleFactor = 2.0;
        
    glPushMatrix();
        
    glScalef(1.0, 1.0, 1.1); // How far from the globe to draw the marker
    glTranslatef(transX, transY+1, transZ);
    glRotatef(xIn+90, 0.0f, 0.0f, 1.0f);
    glRotatef(yIn, 0.0f, 1.0f, 0.0f);
    glRotatef(90, 0.0f, 0.0f, 1.0f);
    glScalef(scaleFactor, scaleFactor, 1); // The scale of the image
        
    [marker drawInRect:bounds];
        
    glPopMatrix();
}

-(void)setTransX: (float)transXIn transY: (float)transYIn transZ: (float)transZIn
{
    transX -= transXIn;    transY -= transYIn;    transZ -= transZIn;
}

-(void)setTransX: (float)transXIn transY: (float)transYIn transZ: (float)transZIn
            rotX: (float)rotXIn rotY: (float)rotYIn rotZ: (float)rotZIn
          scaleX: (float)scaleXIn scaleY: (float)scaleYIn{
    
    transX = transXIn;    transY = transYIn;    transZ = transZIn;
    rotX = rotXIn;        rotY = rotYIn;        rotZ = rotZIn;
    scaleX = scaleXIn;    scaleY = scaleYIn;
    bounds = CGRectMake(-(scaleX)/2, -(scaleY)/2, scaleX, scaleY);
}

-(BOOL)deltaTransX: (float)transXIn transY: (float)transYIn transZ: (float)transZIn
              rotX: (float)rotXIn rotY: (float)rotYIn rotZ: (float)rotZIn
            scaleX: (float)scaleXIn scaleY: (float) scaleYIn{
    
    transX -= transXIn;    transY -= transYIn;    transZ -= transZIn;
    rotX -= rotXIn;        rotY -= rotYIn;        rotZ -= rotZIn;
    if( scaleX >= 0.1 )
    {
        scaleX -= scaleXIn;    scaleY -= scaleYIn;
        bounds = CGRectMake(-(scaleX)/2, -(scaleY)/2, scaleX, scaleY);
        return NO;
    }else
    {
        return YES;
    }
}
@end
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Texture2d crash lander ajrs84 0 2,919 Apr 1, 2012 08:54 AM
Last Post: ajrs84
  Hope this is the right section to ask this question...about "what's hot" in app store sipahiro 2 2,235 Nov 6, 2010 02:53 PM
Last Post: funkboy
  Create a texture2d with contents of other textures godexsoft 6 4,866 Nov 12, 2009 10:24 PM
Last Post: godexsoft
  OpenGL ES and Texture2D Talyn 37 32,712 Jul 30, 2009 09:34 AM
Last Post: Splat21
  Texture2d with strings kendric 10 5,623 Jul 14, 2009 09:28 PM
Last Post: warmi