OpenGL ES and Texture2D

Nibbie
Posts: 1
Joined: 2009.07
Post: #31
The Txture2D file is really hard to find. I found it here

http://code.google.com/p/cocos2d-iphone/...exture2D.m
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #32
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.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.07
Post: #33
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? Blink
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #34
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? Blink

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];
... or something. I didn't test this, so I don't know if it fits efficiently on the texture or not, but fiddle with the sizes yourself some.

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.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.07
Post: #35
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. Smile
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.07
Post: #36
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?
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #37
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. Smile

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.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.07
Post: #38
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.
Quote this message in a reply
Post Reply 

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