iDevGames Forums
Create a texture2d with contents of other textures - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Create a texture2d with contents of other textures (/thread-607.html)



Create a texture2d with contents of other textures - godexsoft - Nov 11, 2009 02:17 AM

Hi guys!

I have a texture based font engine here. it works good but I have to create textures using the font glyphs from the texture map(s).
I started to develop a generic quad class which should provide ways to:
1) init with some rectangular size (to fit all glyphs for example). creates a texture2d
2) change the contents of the texture2d with contents (texels) from other textures (texture2d pixels from say location 46,128 with size 20x24)
3) draw the resulting texture onto the screen (just like Texture2D class does) - no problem here

I was looking into glCopyTexSubImage2D and the rest but they all seem to draw TO the texture from the ReadBuffer.
I even created a local framebuffer and tried to use it as the readbuffer but had no much luck yet.

Does anyone have an example for this? maybe some code from your own texture based font engine?

Thanks in advance!
Alex


Create a texture2d with contents of other textures - godexsoft - Nov 12, 2009 02:35 AM

Hi,

Code:
- (void) copyFrame:(int)frameId toPoint:(CGPoint)inPoint fromTexture:(TMFramedTexture*)texture {
    CGSize frameSize = CGSizeMake( texture.contentSize.width/[texture cols], texture.contentSize.height/[texture rows] );
        
    // This will draw the frame into the buffer
    glEnable(GL_BLEND);
    [texture drawFrame:frameId inRect:CGRectMake(0, 0, frameSize.width, frameSize.height)];
    glDisable(GL_BLEND);
    
        // And this will copy the rendered frame back to our quad
    glBindTexture(GL_TEXTURE_2D, m_unName);
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, inPoint.x, inPoint.y, 0, 0, frameSize.width, frameSize.height);        
}

Have this code. It works only if you render into the bounds of the quad.. so if inPoint.x or inPoint.y is less than 0 it will fail (not draw at all. just transparent).

Maybe I'm totally wrong and this is not the way it should be done?
Any ideas?

TIA,
Alex


Create a texture2d with contents of other textures - godexsoft - Nov 12, 2009 07:44 AM

Nevermind. Found the right way to do this.
I was surprised to see that this question was already answered before and I even can see myself commenting in that post!
Thanks goes to Flash, again Smile Here is the link if anyone is interested: http://www.idevgames.com/forum/showthread.php?t=16776

Regards!
Alex


Create a texture2d with contents of other textures - warmi - Nov 12, 2009 08:29 AM

godexsoft Wrote:Hi guys!

I have a texture based font engine here. it works good but I have to create textures using the font glyphs from the texture map(s).
I started to develop a generic quad class which should provide ways to:
1) init with some rectangular size (to fit all glyphs for example). creates a texture2d
2) change the contents of the texture2d with contents (texels) from other textures (texture2d pixels from say location 46,128 with size 20x24)
3) draw the resulting texture onto the screen (just like Texture2D class does) - no problem here

I was looking into glCopyTexSubImage2D and the rest but they all seem to draw TO the texture from the ReadBuffer.
I even created a local framebuffer and tried to use it as the readbuffer but had no much luck yet.

Does anyone have an example for this? maybe some code from your own texture based font engine?

Thanks in advance!
Alex

The question is why would you want to create a separate texture since you could just as easily draw your glyphs to the screen using the original texture.


Create a texture2d with contents of other textures - godexsoft - Nov 12, 2009 09:00 AM

warmi Wrote:The question is why would you want to create a separate texture since you could just as easily draw your glyphs to the screen using the original texture.

Hi,

Because constructing and calculating the textured string out of the texture maps every frame is painful. I could probably create another class which would hold a array of pointers to the right glyphs.. but creating a texture and later just drawing it is better in my eyes :-)

Regards,
Alex


Create a texture2d with contents of other textures - warmi - Nov 12, 2009 09:32 AM

It depends, if you are displaying a few simple labels which don't change often then yeah, but if you are displaying text which changes frequently (i.e score numbers or such) then this is going to be way slower.

If I were you I would simply create a font class which holds mappings of each character in your font to a set of uv coordinates and draw your quads directly off of that class.
Much simpler and faster - the problem with 2d drawing is not the number of quads you are drawing (vertices) but the fill rate which means drawing a quad for each letter is going to be generally faster then drawing these letters into a separate texture and drawing that texture again.


Create a texture2d with contents of other textures - godexsoft - Nov 12, 2009 10:24 PM

warmi Wrote:It depends, if you are displaying a few simple labels which don't change often then yeah, but if you are displaying text which changes frequently (i.e score numbers or such) then this is going to be way slower.

If I were you I would simply create a font class which holds mappings of each character in your font to a set of uv coordinates and draw your quads directly off of that class.
Much simpler and faster - the problem with 2d drawing is not the number of quads you are drawing (vertices) but the fill rate which means drawing a quad for each letter is going to be generally faster then drawing these letters into a separate texture and drawing that texture again.

That's interesting!

I never thought about creating textures for frequently changing texts (like score, combo etc.). It's all about labels and other static GUI elements.

See, finding the right uv coordinates is not the biggest problem. Finding the Glyphs from a string is much worse.. it's actually going through some maps (dictionaries) which may be very fast but still not as fast as rendering a single texture.. or I'm wrong here?
What could be fast and probably that's the way to go is create a FontString class which will hold a vector of glyphs corresponding to the given font and the string for which it was created. That way we don't need to create a new texture and don't need to search for the right glyphs (uv coordinates in the texture for this font->page) every time. How do you think?

Thanks,
Alex