OpenGL ES and Texture2D

Member
Posts: 215
Joined: 2008.06
Post: #16
AnotherJake Wrote:Okay, here we go.

- start a new iPhone project using the "OpenGL ES Application" template
- add Texture2D.h and Texture2D.m to the project (for anyone else reading this: hopefully you already have those because it appears Apple removed CrashLanding from their sample code offerings for some unknown reason)
- add CoreGraphics.framework to the project
- in EAGLView.m, add #import "Texture2D.h"
- optionally add a background texture named Background.png to the Resources group
- in EAGLView.m, replace the drawView method with this:

Code:
Texture2D    *testTex = nil;
Texture2D    *backgroundTex = nil;

- (void)drawView {
    
    CGRect    bounds = [self bounds];

    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

    // setup viewport and projection
    glViewport(0, 0, backingWidth, backingHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0, bounds.size.width, 0, bounds.size.height, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    // texturing will need these
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);

    // clear background to gray (don't need these if you draw a background image, since it will draw over whatever's there)
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    /* uncomment to draw a background image instead of gray background
    if (backgroundTex == nil)
        backgroundTex = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];

    // background doesn't need blending
    glDisable(GL_BLEND);

    [backgroundTex drawInRect:bounds];
    */

    if (testTex == nil)
        testTex = [[Texture2D alloc] initWithString:@"TEST" dimensions:CGSizeMake(64, 64) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:14];

    // text will need blending
    glEnable(GL_BLEND);

    // text from Texture2D uses A8 tex format, so needs GL_SRC_ALPHA
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    [testTex drawAtPoint:CGPointMake(bounds.size.width * 0.5f, bounds.size.height * 0.5f)];

    // switch it back to GL_ONE for other types of images, rather than text because Texture2D uses CG to load, which premultiplies alpha
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    // ... do more drawing here

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Alright! Well, following all of that works. I get a white "TEST" text in the center of the screen. Gonna poke a few things in my engine and see what happens, I will let you know.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #17
Talyn Wrote:Alright! Well, following all of that works. I get a white "TEST" text in the center of the screen. Gonna poke a few things in my engine and see what happens, I will let you know.

Alright, so with some modified code, I now see the test text texture (Just call me Dr. Suess Grin), but the scene does not render the objects in the engine. It does, however, change the text color to yellow (the color of the last object added to the engine). I'm forced to break down and inundate you with code, here's what I have:

Code:
Texture2D    *testTex = nil;
Texture2D    *backgroundTex = nil;

- (void) startFrame
{
    [EAGLContext setCurrentContext:context];
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // texturing will need these
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    
    if (usePerspective)
    {
        [self setPerspectiveForFieldOfVision:BBS_FIELD_OF_VISION withAspectRatio:BBS_STANDARD_ASPECT nearClippingPlane:BBS_ZNEAR_CLIPPING_PLANE andFarClippingPlane:BBS_ZFAR_CLIPPING_PLANE];
        glEnable(GL_DEPTH_TEST);
        
        glMatrixMode(GL_MODELVIEW);
        
        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glLoadIdentity();
        
        glTranslatef(0, 0, BBS_STANDARD_RENDER_DEPTH);
    }
    else
    {
        glOrthof(0, self.bounds.size.width, 0, self.bounds.size.height, -1, 1);
        
        glMatrixMode(GL_MODELVIEW);
        
        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glLoadIdentity();
    }
    
    if (testTex == nil)
        testTex = [[Texture2D alloc] initWithString:@"TEST" dimensions:CGSizeMake(64, 64) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:14];
    
    // text will need blending
    glEnable(GL_BLEND);
    
    // text from Texture2D uses A8 tex format, so needs GL_SRC_ALPHA
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    [testTex drawAtPoint:CGPointMake(self.bounds.size.width * 0.5f, self.bounds.size.height * 0.5f)];
    
    // switch it back to GL_ONE for other types of images, rather than text because Texture2D uses CG to load, which premultiplies alpha
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}


- (void) drawObjectsInArray:(NSArray*)objectArray
{    
    for (id <BBSDrawableObject> object in objectArray)
    {
        glPushMatrix();
        if ([object respondsToSelector:@selector(z)])
        {
            glTranslatef([object x], [object y], [object z]);
        }
        else
        {
            glTranslatef([object x], [object y], 0.0f);
        }
        
        [object drawObjectVBO];
        
        glPopMatrix();
    }
}

- (void) endFrame
{
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Let me know if you seeing anything. Thanks.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #18
Call glColor4f(1.0f, 1.0f, 1.0f, 1.0f) right before you draw the text to get it back to white. Remember OpenGL is a state machine, so it stays at whatever the color was last set to until you change it otherwise.

For the other objects not drawing, my *guess* is that maybe you're not texturing your objects? If you're not texturing them then you need to disable textures: after the test text is drawn, do glDisableClientState(GL_TEXTURE_COORD_ARRAY) and glDisable(GL_TEXTURE_2D)
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #19
Talyn, have you read the relevant OpenGL documentation, so you know what you're doing?
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #20
AnotherJake Wrote:Call glColor4f(1.0f, 1.0f, 1.0f, 1.0f) right before you draw the text to get it back to white. Remember OpenGL is a state machine, so it stays at whatever the color was last set to until you change it otherwise.

For the other objects not drawing, my *guess* is that maybe you're not texturing your objects? If you're not texturing them then you need to disable textures: after the test text is drawn, do glDisableClientState(GL_TEXTURE_COORD_ARRAY) and glDisable(GL_TEXTURE_2D)

Checked into all of that, and came up with the following code. This code displays both Texture2D text textures, but no other objects no matter what. This is hair-tearing frustration.

Code:
Texture2D    *testTex = nil;
Texture2D    *backgroundTex = nil;

- (void) startFrame
{
    [EAGLContext setCurrentContext:context];
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // texturing will need these
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    
    glOrthof(0, self.bounds.size.width, 0, self.bounds.size.height, -1, 1);
    
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glLoadIdentity();
    
    if (testTex == nil)
        testTex = [[Texture2D alloc] initWithString:@"TEST" dimensions:CGSizeMake(64, 64) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:14];
    
    if (backgroundTex == nil)
    {
        backgroundTex = [[Texture2D alloc] initWithString:@"Other test" dimensions:CGSizeMake(64, 64) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:16];
    }
    
    // text will need blending
    glEnable(GL_BLEND);
    
    // text from Texture2D uses A8 tex format, so needs GL_SRC_ALPHA
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
    [testTex drawAtPoint:CGPointMake(self.bounds.size.width * 0.5f, self.bounds.size.height * 0.5f)];
    [backgroundTex drawAtPoint:CGPointMake(self.bounds.size.width * 0.25f, self.bounds.size.height * 0.5f)];
    
    // switch it back to GL_ONE for other types of images, rather than text because Texture2D uses CG to load, which premultiplies alpha
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
    
    if (usePerspective)
    {
        [self setPerspectiveForFieldOfVision:BBS_FIELD_OF_VISION withAspectRatio:BBS_STANDARD_ASPECT nearClippingPlane:BBS_ZNEAR_CLIPPING_PLANE andFarClippingPlane:BBS_ZFAR_CLIPPING_PLANE];
        glEnable(GL_DEPTH_TEST);
        
        glMatrixMode(GL_MODELVIEW);
        
        //glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        //glClear(GL_DEPTH_BUFFER_BIT);
        
        glLoadIdentity();
        
        glTranslatef(0, 0, BBS_STANDARD_RENDER_DEPTH);
    }
    else
    {
        //glOrthof(0, self.bounds.size.width, 0, self.bounds.size.height, -1, 1);
        
        glMatrixMode(GL_MODELVIEW);
        
        //glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        //glClear(GL_DEPTH_BUFFER_BIT);
        
        glLoadIdentity();
    }
}

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #21
If you are absolutely certain that you have texturing and lighting and the color set as you expect, then check your transforms. Start at the beginning, with your viewport and projection. Viewport is pretty easy to get right, but is your projection now what it was before you got the test textures working?

The template was:
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);

Now you're using:
glOrthof(0, self.bounds.size.width, 0, self.bounds.size.height, -1, 1);

[adding] Don't drive yourself mad! This stuff really is hard to learn, but it's definitely not impossible. I've found that a night's sleep will often let the brain reset its patience meter and sometimes fix things like magic in fifteen minutes the next morning, after many hours of frustration the day before. Don't know how that works, but it is truly like magic.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #22
How about the color array. I hardly ever use it, but do you need it enabled for the way you're doing your object drawing?

glEnableClientState(GL_COLOR_ARRAY);

Totally taking stabs in the dark here...
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #23
It's me again. Frustrated as ever. So, I decided to give Texture2D one last shot and here is the code, CPed from Xcode. This code generates a black square that fills the upper-right quarter of the screen. Other than that, it does nothing. This code was originally pulled straight from the OpenGL ES application template.

Code:
//
//  EAGLView.m
//  testagain
//
//  Created by Kyle Roucis on 1/1/09.
//  Copyright Home 2009. All rights reserved.
//



#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>

#import "EAGLView.h"

#define USE_DEPTH_BUFFER 0

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

@property (nonatomic, retain) EAGLContext *context;
@property (nonatomic, assign) NSTimer *animationTimer;

- (BOOL) createFramebuffer;
- (void) destroyFramebuffer;

@end


@implementation EAGLView

@synthesize context;
@synthesize animationTimer;
@synthesize animationInterval;


// You must implement this method
+ (Class)layerClass {
    return [CAEAGLLayer class];
}


//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {
    
    if ((self = [super initWithCoder:coder])) {
        // Get the layer
        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
        
        eaglLayer.opaque = YES;
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
        
        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
        
        if (!context || ![EAGLContext setCurrentContext:context]) {
            [self release];
            return nil;
        }
        
        animationInterval = 1.0 / 60.0;
    }
    return self;
}


- (void)drawView
{
    
    if (!testTex)
    {
        testTex = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"Flick.png"]];
    }
    
    [EAGLContext setCurrentContext:context];
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_BLEND);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    [testTex drawInRect:[self bounds]];
    //[testTex drawAtPoint:CGPointZero];
    //[testTex drawAtPoint:CGPointMake(1.0, 0.5)];
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}


- (void)layoutSubviews {
    [EAGLContext setCurrentContext:context];
    [self destroyFramebuffer];
    [self createFramebuffer];
    [self drawView];
}


- (BOOL)createFramebuffer {
    
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
    
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    
    if (USE_DEPTH_BUFFER) {
        glGenRenderbuffersOES(1, &depthRenderbuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
        glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
        glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
    }
    
    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
        NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }
    
    return YES;
}


- (void)destroyFramebuffer {
    
    glDeleteFramebuffersOES(1, &viewFramebuffer);
    viewFramebuffer = 0;
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
    viewRenderbuffer = 0;
    
    if(depthRenderbuffer) {
        glDeleteRenderbuffersOES(1, &depthRenderbuffer);
        depthRenderbuffer = 0;
    }
}


- (void)startAnimation {
    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
}


- (void)stopAnimation {
    self.animationTimer = nil;
}


- (void)setAnimationTimer:(NSTimer *)newTimer {
    [animationTimer invalidate];
    animationTimer = newTimer;
}


- (void)setAnimationInterval:(NSTimeInterval)interval {
    
    animationInterval = interval;
    if (animationTimer) {
        [self stopAnimation];
        [self startAnimation];
    }
}


- (void)dealloc {
    
    [self stopAnimation];
    
    if ([EAGLContext currentContext] == context) {
        [EAGLContext setCurrentContext:nil];
    }
    
    [context release];  
    [super dealloc];
}

@end

Code:
//
//  testagainAppDelegate.m
//  testagain
//
//  Created by Kyle Roucis on 1/1/09.
//  Copyright Home 2009. All rights reserved.
//

#import "testagainAppDelegate.h"
#import "EAGLView.h"

@implementation testagainAppDelegate

@synthesize window;
@synthesize glView;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    
    //Set up OpenGL projection matrix
    glMatrixMode(GL_PROJECTION);
    glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    
    //Initialize OpenGL states
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_TEXTURE_2D);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
    glView.animationInterval = 1.0 / 60.0;
    [glView startAnimation];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    glView.animationInterval = 1.0 / 5.0;
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    glView.animationInterval = 1.0 / 60.0;
}


- (void)dealloc {
    [window release];
    [glView release];
    [super dealloc];
}

@end

I would be more than willing to zip up my project and email it to anyone who is interested. If I can't get Texture2D working here soon, I'm going to have to give up and punish myself by writing my own png importer/storage class (don't want!). Thanks ahead of time, this is a LOT of code to mull over and I've been whining about this for quite a while now, so I'm sure you're all sick of it. I know I am. Cheers.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #24
You set up your projection matrix in applicationDidFinishLaunching but then it gets set again with something else every frame in drawView. I'm guessing that's probably not what you want.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #25
I'm at my wit's end about this. Does anyone (AnotherJake...? Smile ) have code they KNOW works with Texture2D and are willing to divulge it so I can pick it apart and see what I need to do/what I am doing wrong? It can be for OpenGL or iPhone GLES. If so, thank you, and here is my email: bigbytestudios@gmail.com Thanks again!

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #26
Heh, I already "divulged" known working Texture2D code a few posts up, and there really isn't anything more to it than that! Rasp

You can email your project to me at anotherjake A.T. mac.com and I'll see what I can do to smooth it over in the morning... I see a couch with my name on it right now ZZZZZZ
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.09
Post: #27
Hi. Good stuff here. I am also trying to get text from Texture2D. I think my problem lies in that I am using a frustum and not an Ortho view but I am not sure. Here is my drawView. Most of the setup if for other drawing that I do.
Code:
-(void)drawView:(GLView *)view
{

  if(NotSetup)
  {
       NotSetup=NO;
        const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 20.0;
    GLfloat size;
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glLineWidth(1.0f);
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_ALPHA);
    glEnable(GL_ALPHA_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glAlphaFunc(GL_GREATER, 0.1f);
    //glEnable(GL_CULL_FACE);
    glMatrixMode(GL_PROJECTION);
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
    CGRect rect = view.bounds;
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
               (rect.size.width / rect.size.height), zNear, zFar);
    glViewport(0, 0, rect.size.width, rect.size.height);  
    glMatrixMode(GL_MODELVIEW);

    // texturing will need these
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    
    gameTitle = [[Texture2D alloc] initWithString:[NSString stringWithFormat:@"WELLTRIS"] dimensions:CGSizeMake(256, 128) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:15];
        /*Here I setup some other variable not related to directly to OpenGL state setup or texture setup*/
   }

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glEnableClientState(GL_VERTEX_ARRAY);
  /* here I draw a bunch of lines and traingle using vertex arrays and glDrawElements*/

  //Now I want to use Texture2D to draw text to the screen
    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        //I thought that maybe I was drawing offscreen so I have tried to translate and scale it into the view
    //glTranslatef(0.0, 0.0, -6.0);  
    //glScalef(0.01, 0.01, 0.01);
    [gameTitle drawInRect:CGRectMake(0,0,1.0,1.0)];
    glRotatef(90, 0.0, 1.0, 0.0);  //just in case I was drawing it on edeg
    [gameTitle drawInRect:CGRectMake(0,0,1.0,1.0)];
     [gameTitle drawAtPoint:CGPointMake(0,0)];  //tried drawing at a point too, instead of in a rect
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_TEXTURE_2D);

        glDisableClientState(GL_VERTEX_ARRAY);
}
I will keep looking at it but anybody see where I am going astray? Thank you.
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.09
Post: #28
OK, this is weird. I have been monkeying around with the text to try to get it to work. I was fooling with the Projection. I was about to make it Orthogonal. I had yet to add glOrthof() so my code looks like this.

Code:
//Same code as before
    ...
    //Now I want to use Texture2D to draw text to the screen
        glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    
    glLoadIdentity();
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
    [gameTitle drawInRect:CGRectMake(0.0,0.0,1.0,0.5)];

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
And now it works! At least drawing the text works. What is an identity projection? My knowledge of the matrix representation of 3d space is a little rusty. Well, all the other drawing code is in a perspective projection so if I draw text in an identity project I would have to flip back and forth. Is this costly? Even if it is efficient I would rather not have to do it. Would like to figure out what I am screwing up with my perspective projection. Hmmm
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #29
You should read up more on matrices and projections. I would highly recommend reviewing that section in the Red Book.

LoadIdentity() loads the identity matrix onto the stack, which is the matrix equivalent of "one", so to speak. Once you've "cleared" a matrix to the identity matrix you can begin adding (multiplying, actually) transforms to it; in the case of the projection matrix you are adding projection transforms to it.

There's nothing wrong or inherently inefficient with modifying the projection matrix multiple times during a single frame if you wish.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.06
Post: #30
I too was getting solid rectangles when drawing Texture2D in my frame loop.

My mistake was to write
Code:
glEnableClientState(GL_TEXTURE_2D);
when the correct statement is:
Code:
glEnable(GL_TEXTURE_2D);

I didn't see this until I put AnotherJake's code in a new project side by side with my own.

Time to check error codes! This mistake would raise a GL_INVALID_ENUM error code.

I used this method, called from numerous places in my frame loop, to track down where errors were coming from:

Code:
/** checkGLError: Warn if new error (not matching last known error.)
Call this after any batch of commands. This provides a warning that a coding error was made.
The NSLog call will be made only once if the same error is repeating.
**/

-(void)checkGLErrorAtLabel:(NSString*)label {
    GLenum err = glGetError();
    if (err != GL_NO_ERROR && err != lastError) {
        lastError = err;
        switch(err) {
            case GL_INVALID_ENUM:
                NSLog(@"%@: GL_INVALID_ENUM", label);
                break;
            case GL_INVALID_VALUE:
                NSLog(@"%@: GL_INVALID_VALUE", label);
                break;
            case GL_INVALID_OPERATION :            
                NSLog(@"%@: GL_INVALID_OPERATION", label);
                break;
            case GL_STACK_OVERFLOW:  
                NSLog(@"%@: GL_STACK_OVERFLOW", label);
                break;
            case GL_STACK_UNDERFLOW:  
                NSLog(@"%@: GL_STACK_UNDERFLOW", label);
                break;
            case GL_OUT_OF_MEMORY :
                NSLog(@"%@: GL_OUT_OF_MEMORY", label);
                break;
            default:
                NSLog(@"%@: Unrecognized glGetError code %d", label, err);
                break;
        }
    }
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Texture2d crash lander ajrs84 0 2,635 Apr 1, 2012 08:54 AM
Last Post: ajrs84
  Problem using Texture2D sub-section bendell 0 2,341 Mar 20, 2010 02:06 AM
Last Post: bendell
  Create a texture2d with contents of other textures godexsoft 6 4,320 Nov 12, 2009 10:24 PM
Last Post: godexsoft
  Texture2d with strings kendric 10 5,131 Jul 14, 2009 09:28 PM
Last Post: warmi
  Texture2D and other transparent objects kappolo 9 6,193 Apr 17, 2009 12:07 PM
Last Post: AnotherJake