Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SceneGraph + Layers Concept  (Read 3297 times)

0 Members and 2 Guests are viewing this topic.

Cory Parsnipson

  • Newbie
  • *
  • Posts: 8
    • View Profile
SceneGraph + Layers Concept
« on: April 08, 2016, 08:38:35 pm »
Hi, I'm thinking about implementing a scene graph with layers capability. I'm planning on making the scene graph as a general tree with a draw method that descends the prefix and draws everything to the render window (eventually).

I'm trying to figure out how to implement layers while still doing this traversal and I've decided on having 1 sf::RenderTexture per layer with each node in the scene graph having a integer to identify which layer they are on. So while I'm traversing the scene graph, instead of drawing every node directly to the render window, I query it's layer "z-index" and then draw it to the right render texture.

Lastly, when I update the render window, right before I display it to the screen, I'm going to iterate in order of z-index and draw all the render textures to the render window.

Whew, so my question actually is this: Will using Render Textures as an intermediate step really, really slow down my game loop? I'm not sure of the implementation details at this point so I'm also concerned that I'll be swapping the OpenGL context too often with no way of adding in a sprite batch capability later.

Also, if there is a better conceptual way or something in the docs that already implements this stuff, I'd totally be open to learning about it...

Thanks!

Cory Parsnipson

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SceneGraph + Layers Concept
« Reply #1 on: April 08, 2016, 08:58:08 pm »
Also I forgot to add that most likely there will be maybe 3 -5 layers at any given point, so there shouldn't be very many render textures being generated, if that makes a difference.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: SceneGraph + Layers Concept
« Reply #2 on: April 08, 2016, 11:19:22 pm »
For the concept of layers I use multiple root nodes. Assuming your graph is set up so that draw(node) traverses the node's tree drawing each child node from the bottom up, then drawing multiple root nodes one after each other effectively draws in layers. For example:

std::array<Node, 4u> layers;
for(const auto& layer : layers)
{
    draw(layer);
}
 

Each of these is a root node, so layers[0] draws all its children, followed by layers[1] and so on. If it helps you can label them with an enum:

enum LayerID
{
    Back = 0,
    Middle,
    Front,
    UI
}

layers[LayerID::UI].addChild(myUINode);
 

I have a practical example of it here.

Cory Parsnipson

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SceneGraph + Layers Concept
« Reply #3 on: April 09, 2016, 05:26:31 am »
Thanks for the example! Do you ever run into a situation where you want a single node to be in multiple layers?

For example, let's say you have a house or something where you want the back wall to be behind the player character, but the front wall to block it. Do you make two separate nodes for these items?

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: SceneGraph + Layers Concept
« Reply #4 on: April 09, 2016, 12:12:21 pm »
I've not tried it with a scene graph, but you might be able to adapt this technique.

Cory Parsnipson

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SceneGraph + Layers Concept
« Reply #5 on: April 10, 2016, 04:02:32 am »
Whoa, cool. This is really informative. Thanks!

 

anything