Sign in to follow this  
snk_kid

OpenGL Scene Graph Resources

Recommended Posts

snk_kid    1312
As scene graphs are a such an important and frequently questioned topic here (and else where) i decided to begin compiling a comprehensive listing of resource links about them for those interested in implementing/using them. Hopefully this will become a sticky for all to find of great use. To those who have not done so already and are new to the topic of scene graphs i recommend reading up on some/all design patterns in particular:
  • Composite - reason being that scene graphs are typically implementated with this pattern
  • Decorator - reason being it's related to Composite and is in fact a degenerate form/case of Composite where decorators only have a single or fixed number of children. It is also frequently used with the Composite pattern. In the case of scene graphs implementated via the Composite pattern we have Grouping node types (they are the Composites) typically there will be an instantiable base Group node type who's sole purpose is child containment and child management, this instantiable base is just a plain instance of the composite pattern and the most general grouping node type of all in a scene graph. Then we add more specific kinds of grouping nodes, like transforms, switch nodes, LOD nodes etc these are sub-types of the base group node type thus inherit child containment and child management (code-reuse) but also adding new behaviours/state to internal nodes of a tree/DAG (the reason why i emphasis this is it does not make sense to have something like a Switch/LOD node who's sole purpose is to select/switch between other nodes as terminating/leaf nodes of a tree/DAG). The specific kinds of grouping node types are Composites aswell they are also instances of the decorator pattern too!.
  • Interpreter - reason being it's related to composite but the main point is the idea of a "global" context i.e SG could have global state or rendering context.
  • Observer - Scene Graphs are/use the Observer pattern implicitly without the developer realizing it but it can be used explicitly when scene graphs are DAGs because there maybe occasions where leaf nodes need to notify state changes to all parents (and there parent's parents and so on all the way upto the root/roots if necessary). Therefore leaf node types are subjects and grouping node types are the observers (they are also subjects when viewed in a different context since grouping node types typically share a common base with leaf node types).
  • flyweight - reason being you can permit node sharing in an n-ary tree without resorting to use a directed acyclic graph (DAG) which are harder to deal with but more flexible.
  • Visitor - reason being seperating the operation and data of scene graphs, it will beome more apparent when this pattern is read

Caveat: The main point/advantage of the composite pattern is the distinction between internal (non-terminal) and leaf (terminal) node types (yet they have a common super-type), Composite intances are internal nodes of a tree/DAG, concreate component instances are terminating, leaf nodes of a tree/DAG. The advantage of this there is no need todo a run-time check whether a node is a leaf/terminal, there is no redundant data of child containment in leaf node types (aswell as other state/behaviour that should only ever be in internal nodes of a tree/DAG). Here is the Caveat, Composite pattern is generally applied to OO designs indeed Gang Of Four (GOF) design patterns are concise & general designs/concepts to recurring solutions that arise in OO systems/languages. As such Composite pattern is implementated in-terms/via sub-type polymorphism (the type of polymorphism most widely known). However there is another method to achieve virtually the same (that is making a distinction between leafs and internal nodes) thing without sub-type polymorphism, without the need of a common polymorphic base, even no need for pointers & heap allocation by using something called recursive variants. Virtually all Functional programming languages (i don't mean imperative but some of them do also) natively support variants AKA disjoint disjoint (discriminated) unions, one value - finite set of types, they also can also be recursive (this is the key here). For example in SML we can describe a binary tree as such:
datatype 'a tree = Leaf of 'a | Node of (('a tree) * ('a tree));
This just basically says that a "tree" is either leaf of some type or an internal node. It's what we want. We can do this in C/C++, in C++ the best method is by using boost::variant, we an use boost::variant to achieve what the composite does and implement the basic structure of a typical scene graph interms of it:
#include <deque>
#include <boost/variant.hpp>

typedef boost::variant<
    struct leaf,
    struct leaf2,
    struct leaf3,
    boost::recursive_wrapper<struct group>,
> scene_node;

//...

struct leaf { /*...*/ };
struct leaf2 { /*...*/ };
struct leaf3 { /*...*/ };

struct group {
  
  typedef std::deque<scene_node> child_list; // notice recursive composition

private:  

  child_list kids;

public:

   void add_child(const scene_node& sn) { kids.push_back(sn); }
};

int main() {

    group root;

    root.add_child(leaf());
    root.add_child(leaf2());
    root.add_child(leaf3());
    root.add_child(group());

    group& inner_grp = boost::get<group>(grp.kids.back());

    inner_grp.add_child(leaf());
    inner_grp.add_child(leaf2());

}
You get the idea, read boost::variant docs on how to use it properly, use boost::static/apply_visitor etc. The interesting thing about this method you will still end up using inheritance but not for sub-type polymorphism or interfaces but purely for code reuse, no need for virtual destructors, not only that you will end up replacing sub-type polymorphism with static polymorphism using Curiously Recurring Template Pattern (CRTP). This idea is even possible in standard C (with some extra work) because C/C++ have union user-defined types but it is not a discriminated union (like boost::variant is) though, in C you can simulate discriminated unions using a simple technique called a tagged unions and enforcing state invariants through an interface. All tagged unions is is a union plus a type tag identifier, in pure standard C it will look like this:
typedef enum tag_type { FOO, BAR, .... } tag_type; // enumrated set of types

typedef struct my_variant_ {

    tag_type type_id;

    union { foo f, bar b, ... } var;

} my_variant;
Many C compilers have a non-standard extension that make this slightly less verbose:
typedef enum tag_type { FOO, BAR, .... } tag_type; // enumrated set of types

typedef struct my_variant_ {

    tag_type type_id;

    union { foo f, bar b, ... };

} my_variant;
Now we have variants in C we can apply this again to implement the composite pattern in C with recursive variants:
#include <stddef.h> // size_t
typedef enum tag_type { LEAF, LEAF2, GROUP };

typedef struct scene_node {
    tag_type type_id;
     union {
        struct leaf*   l1;
        struct leaf2*  l2;
        struct group*  grp;
     };
} scene_node;

typedef struct group {
     scene_node* kids; // pointer to an array of scene_node,
                       // notice recursive composition again
     size_t num_of_kids;
} group;

typedef struct leaf {} leaf;
typedef struct leaf2 {} leaf2;
Another advantage of this specifically to C it's slightly more type-safe, you can (almost) completely avoid void pointer syndrome. The obvious disadvantage to using recursive variants in C/C++ is the lack of pattern matching that statically typed functional languages enjoy aswell as the method completely automated, and type-checked. Saying that though its not to bad with boost::variant's static/apply_visitor. Lastly i'm not suggesting this method to be best/better than the traditional OO method of the Composite. i'm just stating it's not the only method. You could probably get the best of both worlds in OCaml.
I'll continuously add/update this, please PM me if you have any more links and i'll stick up here for all to see [smile]. [Edited by - snk_kid on November 4, 2005 6:29:34 AM]

Share this post


Link to post
Share on other sites
dmatter    4844
Yay, I wrote most of that Wikipedia entry only the other week! - nice to see that its getting some publicity, the article only just hinges on some of the things that make up a good scene-graph but I think it is quite good as read-first article - thanks snk_kid [smile].

Share this post


Link to post
Share on other sites
dimebolt    440
Quote:
Original post by snk_kid
New! added a "Caveat" section + stuff [wink].


Man, you should write an article. If you continue in this rate, it will be way too big for the FAQ :)

Tom

Share this post


Link to post
Share on other sites
acid2    451
THANK YOU! This is possibly one of the most valuable posts I've seen on gamedev. I can't thank you enough ^_^

Share this post


Link to post
Share on other sites
Qrikko    137
Quote:
Original post by acid2
THANK YOU! This is possibly one of the most valuable posts I've seen on gamedev. I can't thank you enough ^_^


and I would move for "my most wanted" post in any forum! thank you so much this will certainly come in handy!

Share this post


Link to post
Share on other sites
jon723    168
I once wrote a scene graph when I worked at a VR company (based on some articles I read on GameDev). When I left the company I wanted to write my own from scratch but couldn't seem to wrap my head around the various design concepts (and i couldnt use things I did for a company). None the less thank you for all of the topics you've compiled I'm pretty sure I'll be able to tackle this problem once and for all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
    • By dpadam450
      FINALLY, upgrading my engine to openGL 4. I was having some trouble so I started with a stripped down application and was wondering if VAO's are required, because I have a sample working, but if I remove the VAO then it doesn't seem to like drawing my triangle.
  • Popular Now