• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Scene graph catagorizing

## Recommended Posts

I know that material changes are slow, so you dont want to be changing materials all over the place. Do you organize your scene graph by material or by entity? psuedo: Material:
for each material
set material
for each model using this material
orient matrix
draw this portion of the mesh

Entity:
for each entity
orient matrix
for each material
set material
draw this portion of the mesh

The second one seems obvious but just how slow is a material change compared to say a matrix push translate 3rotates and a pop. Another thing, how many materials is an exceptable count for models? A person it seems you''d need only one, but a car you''ll need one for the glass, body, tailights, headlights, maybe license plate. Thanks

##### Share on other sites
By Material, assuming they include ''shaders'' (Vertex & Fragment Programs).
It''ll be way more efficient.

-* So many things to do, so little time to spend. *-

##### Share on other sites
They don''t, im just talking about like:

glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, #);
glMateriali( GL_FRONT, GL_SHININESS, #);
glBindTexture(GL_TEXTURE_2D, #);

And isn''t it faster if you compile those into a display list?

##### Share on other sites
The best way I think is to ignore the matrix stack and calculate the combined matrices yourself by hand. Then, walk the scenegraph putting the visible objects into a renderqueue, which can be sorted as the items are inserted. Of course this means that you need to have some sort of predicate that decides that one particular geometry''s material properties is less than the next.

The way I do this is each geometry ''chunk'' has a class/set of functions used to render it. The first sort criteria is the class/set of functions that it uses to render it e.g. I have a class that renders a gouraud shaded polygon. I then have a class that can render a diffuse textured polygon and so on. Then each of these functions have a callback function for comparing two objects that are to be rendered. This allows each class to sort it''s lists differently. This way I can sort by texture id in one case, but in the case of gouraud shading the textureID makes no sense as a sort key(it simply isn''t there).

So to answer the original question I sort my scenegraph by the heirarchial relationship of the entities. Incidently depending upon your rendering method you may want to maintain both trees at the same time(or even interlink the two in some weird and wonderful way). This will enable you to reap the benefits of both layouts for specific tasks to give ''optimal'' performance.

James