Results 1 to 7 of 7

Thread: Simple question about CL_Texture and CL_TextureGroup

  1. #1

    Default Simple question about CL_Texture and CL_TextureGroup

    The question is very simple:

    how do you render a CL_Texture or a CL_TextureGroup to the screen? I know how to render a Sprite and a PixelBuffer, but i've spent the last 2 hours trying to figure out how to render a CL_TextureGroup and I simply cannot find it.

    Thanks!

    Flav

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    A TextureGroup is just a collection of many sub textures packed into one or more Textures. It is not meant to be drawed directly. You'll have to iterate through its get_textures() if you want to draw them. But this is normally not how you use TextureGroup..

    Regarding drawing CL_Texture, we have a convenience class called CL_Draw that helps you to do this.

    Code:
    CL_Draw
    
    static void texture(
    	CL_GraphicContext &gc,
    	const CL_Rectf &rect,
    	const CL_Colorf &color = CL_Colorf::white,
    	const CL_Rectf &texture_unit1_coords = CL_Rectf(0.0, 0.0, 1.0, 1.0));
    You use it like this:

    Code:
    CL_Rect rect(10,10,100,100);
    gc.set_texture(0, texture_image);
    CL_Draw::texture(gc, rect, color);
    gc.reset_texture(0);
    This will draw the texture using a standard shader we provide.
    If you want to draw it using a custom shader, look at the CL_Draw::texture() code to see how to use CL_PrimitivesArray and gc.set_program_object. The Postprocess example also shows some examples how do to texture drawing.

  3. #3

    Default

    Thanks sphair.

    In one of the tutorials it said the TextureGroup would help improve performance. I'm not so sure I understand why the TextureGroup is any better than a vector<CL_Texture> if all we do is iterate through it and use the same graphic context.

    In past projects (using a different engine) I drew large amounts of textured quads stored in a vertex buffer, which dramatically sped up the rendering compared with a simple iteration. Does rendering using the TextureGroup give me decent performance when rendering hundreds of textures in one screen plus a few dozen sprites? I'll test so myself but it will be a while till i properly get there and would like to avoid using methods that aren't worthwhile.

    Thanks for all the help!=)

    Flav

  4. #4
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    It depends

    (Note, this may not be technically accurate)

    If your polygon mesh does not change, the fastest way is to use CL_VertexArrayBuffer than drawing one at a time. If parts of your mesh does change (ie, your using bones), then maybe vertex buffer is still faster (depending on situation)

    Telling OpenGL to perform a graphic operation takes time, so it's faster to batch draw.

    If you have 2 CL_Texture's - You can use texture1...
    ou would have to perform: gc.set_texture(0, texture_1) ... CL_Draw::texture()... gc.set_texture(0, texture_2) ... CL_Draw::texture() ... gc.reset_texture(0)

    This works, but is the slowest method.

    Using CL_Image (and CL_Sprite), we internally batch multiple calls to CL_Image::draw() for optimum performance - even if the CL_Texture changes (up to 4 different textures) - by using a special GLSL shader.

    Using CL_TextureGroup, stores multiple images in a large CL_Texture(s) - So you don't need to call gc.set_texture() as often. The system font glyphs use this method.

    Note, when scaling textures, remember the "Texture Filtering Problem" http://www.rtsoft.com/forums/showthread.php?t=2695

  5. #5

    Default

    Which method do you then suggest would give the best performance for drawing terrain tiles on a map in 2D? A map would give each tile its own graphic (like a map on a SNES RPG) rather than one large texture for the whole map. As I understand, a vertex buffer is ideal in a case like this because it works some sort of magic for adjacent quads. Should I rewrite my tile class to use a vertex buffer rather than a texture group?

    Thanks for all the help!=)

    Flav

  6. #6
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Again, it depends.

    1) If the background is static (it does not scroll or change) - Use CL_VertexArrayBuffer.

    2) Else, if you are scrolling a large map, or you have animated blocks in the map) - Use an array of positions, to batch draw them.

    Have a look at the methane example:
    svn://esoteric.clanlib.org/ClanLib/Development/Contrib/Methane

    The draw code is found at: - sources/target.cpp - CGameTarget::Draw()
    The entire game graphics are contained in 5 pngs (resources/page_0x.png). Each png is a CL_Texture of 512x512 size

    If "SOFTWARE_RENDERER" is enabled - The game uses
    Code:
    	CL_Image image(gc, sub_texture);
    	image.draw(gc, dest);
    This takes advantange of ClanLib's internal batching.

    Else, when the GLSL shader is used, to draw each image, I use:
    CL_Vec2f positions[6] .... prim_array.set_attributes(0, positions); .... gc.draw_primitives(cl_triangles, 6, prim_array);

    Ideally, this could have been batched as well.

    Since you use CL_TextureGroup to store the images, and assuming you do not have any CL_ProgramObject's, I would do:

    Save CL_Subtexture from CL_TextureGroup:add. And draw it when required
    Code:
    	CL_Image image(gc, sub_texture);
    	image.draw(gc, dest);
    ClanLib will automatically batch these images.

    However, if your modelview matrix does not change, writing a custom batcher would be faster
    Last edited by rombust; 08-06-2009 at 07:21 AM. Reason: I keep forgetting to tick "Disable smilies in text"

  7. #7
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    And in most cases none of the hardcore optimizations are really neccessary unless you're drawing tens-of-thousands of images

    I suggest you first do a simple implementation and see if its fast enough

Similar Threads

  1. ClanLib 0.9 - CL_Texture vs CL_Image vs CL_Sprite
    By rombust in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 04-20-2009, 07:22 PM
  2. Simple Path Movement
    By Pleng in forum Novashell Game Creation System
    Replies: 11
    Last Post: 03-22-2008, 11:30 AM
  3. Simple Animation
    By ares in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 02-27-2008, 05:20 AM
  4. simple ClanLib example problem - segfault
    By phx_one in forum Official ClanLib SDK Forums
    Replies: 3
    Last Post: 02-17-2008, 12:54 AM
  5. Simple app won't compile when linked with clanlib
    By mmiikkee12 in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 07-17-2007, 02:41 PM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •