Results 1 to 8 of 8

Thread: Low-level rendering

  1. #1
    Lesser Knight
    Join Date
    Jul 2011
    Posts
    57

    Default Low-level rendering

    Good time of a day.
    Is there some tutorial of creating and using custom low-level renderers in ClanLib? I couldn't find anything comprehensible by my inexperienced in this area brains. Examples of using shaders are not very obvious for me, and, as I understand, you can apply shaders only for finished picture but I need to render object in the middle of rendering process. I couldn't figure out how to use direct OpenGL calls too, yet.

    What I want to do is to create a drawing primitive that would allow me to render a scalable texture-filled ring with a standard square texture running along circle of required diameter. I can't do it with present ClanLib functions so I have to write my own renderer, I assume. But I'm not quite know even where to start from.

    I'm sorry for quite mussy explanation.

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    ClanLib roughly gives you two ways to do graphics. There's the high level 2D classes (CL_Font, CL_Sprite, CL_Image, etc) and then there's the low level classes (CL_GraphicContext, CL_ProgramObject, CL_PrimitivesArray, etc). You already seem familiar with the high level ones so I'll try explain how they interact with the lower level.

    All the high level classes are implemented using the low level classes. In particular, the classes use CL_RenderBatcher internally to queue up commands. There is a HSVSprite example in ClanLib that shows to build a simple sprite class of your own that use the batcher. The principle is quite simple, CL_GraphicContext::set_batcher control which batcher is currently active and CL_GraphicContext::flush_batcher forces all currently queued commands to be rendered. The main speed gain in the high level classes is simply to send 1000 draw sprite commands in a single call to CL_GraphicContext::draw_primitives, instead of sending them one at a time.

    Our low level classes are effectively OpenGL 3+, so the best way to understand them is simply to learn a bit OpenGL. CL_Texture is simply a wrapper around an OpenGL texture handle, CL_ProgramObject wraps the OpenGL program object and so on. It is a slightly higher level C++ friendly variant of OpenGL where something in OpenGL means exactly the same in clanDisplay.

    The drawing process (in both OpenGL and ClanLib) consists of four steps:

    1) Compile, bind attributes to index locations and link a shader program.
    2) Prepare/upload some vertex attribute data.
    3) Set some uniform shader variables.
    4) Tell OpenGL to render the vertices as points, lines or triangles by calling CL_GraphicContext::draw_primitives

    Uniforms are global variables in your shader programs. Vertex attributes are variables on your vertices, also sent to your shader programs.

    You can safely mix CL_Sprite with your own objects. The only real requirement is that you call flush_batcher() first to ensure ClanLib finished its draw_primitives calls before you begin to make your own.

    You can also do direct OpenGL calls instead of using the low level ClanLib classes if you prefer that. To do this you need to call CL_OpenGL::set_active(gc) first to ensure the right OpenGL context is active. It is also your responsibility to restore states you change if you mix ClanLib and OpenGL.

    Hope that helps to get you started.

  3. #3
    Lesser Knight
    Join Date
    Jul 2011
    Posts
    57

    Default

    Thank you, I think that's enough for me to start digging now

  4. #4
    Lesser Knight
    Join Date
    Jul 2011
    Posts
    57

    Default

    Time for new questions!

    I did some readings about OpenGL and some of ClanLib examples and sources. Now, I think, I know what to do and how to make my own object renderer but some questions still remains:

    1. Can I use a standard program cl_program_sprite for rendering or should I write my own anyway? All I need to draw is rotating and scaling triangle primitives as far as I understand and this should be what this program do anyway if I understand correctly.
    2. How do I set texture coordinates exactly? It is somehow confusing in examples. HSVExample sets texture coords in range (0, 1.0) and HSVSprite example sets it as (texture size)/256.0 . Shouldn't you set texture coordinates as actual pixel coordinates of the texture?

  5. #5
    Lesser Knight
    Join Date
    Jul 2011
    Posts
    57

    Default

    Well, I did it, working just fine with cl_program_single_texture \o/
    Now only to inject it into the code so it whouldn't cripple performance.
    Can I construct my own batcher for my objects so it could stack several of calls to draw them and than automatically flush with the first call from another object to draw that use another batcher? I believe this is how ClanLib batchers organized but I'm not sure.
    Attached Images Attached Images  

  6. #6
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    Yes, that is exactly how the batchers are organized.

    About using cl_program_sprite, it is probably best you write your own program. The sprite program may change if we find a faster way to render a batch of sprites (or if we extend the functionality), and there's also no official documentation for what the vertex attributes mean with this program. In effect this program means "these vertices were sent by CL_Sprite" and the various backends (clanGL, clanGL1, clanSWRender) may recognize it and do something special. For example, clanSWRender ignores every other triangle (by assuming its a box) to speed up the software rendering.

  7. #7
    Lesser Knight
    Join Date
    Jul 2011
    Posts
    57

    Default

    Well, I must say that ClanLib's internal rendering cycle is organized very well. I was surprised myself when my own batcher that I wrote with almost no knowledge of what I was doing actually worked from the first try, did what I expected from it to do and didn't cause any performance problems.
    Feels good, really

  8. #8
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    Simplicity at its finest.

Similar Threads

  1. level 6 and still stuck in tertis
    By in forum Dink Smallwood HD
    Replies: 2
    Last Post: 02-10-2004, 04:42 AM
  2. Level Editor
    By in forum Other RTsoft Games
    Replies: 1
    Last Post: 06-17-2003, 04:39 AM
  3. What level are you?
    By Wolf Warhead in forum Dink Smallwood HD
    Replies: 0
    Last Post: 06-01-2003, 12:14 PM
  4. V1.05 and level editor released
    By Seth in forum Other RTsoft Games
    Replies: 0
    Last Post: 11-18-2002, 10:41 AM
  5. New TLM Ducks Level in Progress!!
    By redink1 in forum Other RTsoft Games
    Replies: 5
    Last Post: 10-22-2002, 11:29 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
  •