OpenGL ES and Texture2D
The Txture2D file is really hard to find. I found it here
http://code.google.com/p/cocos2d-iphone/...exture2D.m
http://code.google.com/p/cocos2d-iphone/...exture2D.m
It's really better if you write your own texture loader, and understand all of the issues involved, than to copy this code.
We've seen many problems arise from developers blindly copying Texture2D without understanding what it is doing.
We've seen many problems arise from developers blindly copying Texture2D without understanding what it is doing.
The code provided by Anotherjake works well in Ortho mode. But in full 3D the text looks bad when put close ( eg. glTranslatef(0, 0, -50); ). The text looks good from a distance about -150, but then it's covered by other objects on the screen. What's the solution for this?

twig0 Wrote:The code provided by Anotherjake works well in Ortho mode. But in full 3D the text looks bad when put close ( eg. glTranslatef(0, 0, -50); ). The text looks good from a distance about -150, but then it's covered by other objects on the screen. What's the solution for this?
Probably because it's a pretty small texture to begin with. You can try making a bigger texture and render a larger font for more resolution. Maybe try something like:
Code:
testTex = [[Texture2D alloc] initWithString:@"TEST" dimensions:CGSizeMake(256, 128) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:28];
I would highly recommend learning what's going on in Texture2D, and also add your own addition to it to draw the outline of the texture so you can see how the text fits on it.
I have successfully used AnotherJake's code from the post dated 12/27/2008. My end goal is to have individual 2D images rotate, translate and scale. To simplify, I will show code below that only tries to rotate the text. I have only added four lines to the original code: glPushMatrix, glLoadIdentity and glRotatef (20 degrees) before drawing the text, and a glPopMatrix after drawing the text. Here is the 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);
glPushMatrix(); // Added line
glLoadIdentity(); // Added line
glRotatef(20.0f, 0.0f, 0.0f, 1.0f); // Added line
[testTex drawAtPoint:CGPointMake(bounds.size.width * 0.5f, bounds.size.height * 0.5f)];
glPopMatrix(); // Added line
// 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];
}
====
When running this code, the "TEST" text appears to be rotating around the bottom left corner. For example, at 20 degrees it is moved above the vertical center and to the left of center. How can I change the code so that the "TEST" text only rotates in place and at the center position?
Any assistance would certainly be appreciated.
====
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);
glPushMatrix(); // Added line
glLoadIdentity(); // Added line
glRotatef(20.0f, 0.0f, 0.0f, 1.0f); // Added line
[testTex drawAtPoint:CGPointMake(bounds.size.width * 0.5f, bounds.size.height * 0.5f)];
glPopMatrix(); // Added line
// 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];
}
====
When running this code, the "TEST" text appears to be rotating around the bottom left corner. For example, at 20 degrees it is moved above the vertical center and to the left of center. How can I change the code so that the "TEST" text only rotates in place and at the center position?
Any assistance would certainly be appreciated.

AnotherJake Wrote:Probably because it's a pretty small texture to begin with..
Thanks for your prompt reply. Changing those dimensions didn't help much, but using drawInRect instead of drawAtPoint solves the problem - the text looks nice then.
I'm having problem drawing a textured text - maybe it works only in Ortho mode? Or maybe I can't see the texture because I'm working on Powerbook G4, where all the graphics don't look like they should. Looks like it's high time to buy an Intel MAC.
Anyway textured font is not what I need, I'd like to have an outlined text. Looks like I'm gonna have to take a deep look into the Texture2D.h. Or maybe there's already a nice method to draw an outlined text?
Splat21 Wrote:When running this code, the "TEST" text appears to be rotating around the bottom left corner. For example, at 20 degrees it is moved above the vertical center and to the left of center. How can I change the code so that the "TEST" text only rotates in place and at the center position?
Any assistance would certainly be appreciated.
It's probably because Texture2D is calculating coordinates based upon the location you give it, so it's not transforming as you expect. Try doing it like this instead:
Code:
glPushMatrix();
glLoadIdentity();
glTranslatef(bounds.size.width * 0.5f, bounds.size.height * 0.5f, 0.0f);
glRotatef(20.0f, 0.0f, 0.0f, 1.0f);
[testTex drawAtPoint:CGPointMake(0.0f, 0.0f)];
glPopMatrix();
twig0 Wrote:I'm having problem drawing a textured text - maybe it works only in Ortho mode? Or maybe I can't see the texture because I'm working on Powerbook G4, where all the graphics don't look like they should. Looks like it's high time to buy an Intel MAC.Textured text should work just fine on your PB G4 too, although it's not a bad idea to upgrade to an Intel machine now. Ortho or perspective works fine, but ortho probably yields more predictable results. This snippet of code was for iPhone, although it can be used elsewhere. Texture2D might have some stuff in it that only works with iPhone, I don't know.
twig0 Wrote:Anyway textured font is not what I need, I'd like to have an outlined text. Looks like I'm gonna have to take a deep look into the Texture2D.h. Or maybe there's already a nice method to draw an outlined text?Ingemar was working on this a while back. You might be able to pester him for a little help.
AnotherJake, many thanks! That solved the problem. I have been banging my head on that one for awhile. I very much appreciate your help! Cheers.
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Texture2d crash lander | ajrs84 | 0 | 3,983 |
Apr 1, 2012 08:54 AM Last Post: ajrs84 |
|
Problem using Texture2D sub-section | bendell | 0 | 3,557 |
Mar 20, 2010 02:06 AM Last Post: bendell |
|
Create a texture2d with contents of other textures | godexsoft | 6 | 7,776 |
Nov 12, 2009 10:24 PM Last Post: godexsoft |
|
Texture2d with strings | kendric | 10 | 8,790 |
Jul 14, 2009 09:28 PM Last Post: warmi |
|
Texture2D and other transparent objects | kappolo | 9 | 9,828 |
Apr 17, 2009 12:07 PM Last Post: AnotherJake |