   Sorting by materials?
  I posted this in Game Development, but i figured after it went off the front page with no replys its not a cross post.---My materials consist of a texture (Sometimes), ambient, diffuse, specular, emissive, and shininess. Right now I have it setup so that it sets each material only once, which is good, but isn''t there an order to set them in? least amount of changes. If material1 and material2 are only differnt by a texture right now i''m setting all the other settings too.I was thinking of compiling a display list for my material when i create it then my shader can just call the list. But then that will set every property again like im doing now, but maybe it will still be faster?How are you guys doing it?  
  You can have like a little cache that holds the last used texture ID, and if the material you are going to use contains the same texture ID, then just dont set the texture.Or maybe Im not understanding your problem... ;  Yea thats the system im doing now, well sorta, i have a list of my materials, and when i "render" something i stick its indices and vertex pointers on the list under that material, then when i Render, i set each material with anything in its list then draw its list.But what im asking is how do I sort my list of materials. Right now its just the order they are created. When i switch materials, even if the only difference is the texture, i''m still calling glmaterial on all the diffuse and ambient and stuff.I know everytime I switch a material I will have to compare the current diffuse (and the like) with the one i may be changing it too. I will preprocess this into booleans so that i dont have to scan 4 floats to see if i need to change it. But i dont want to go back and forth. If I have 4 textures, and they alternate diffuses, I want to only set it twice. So I guess basicaly what im asking is, Is there a difference in the time it takes to call glmaterial on the differnt properties? (this would affect which prop i sort by) And how would I go about sorting something with multiple properties, It seems like it would be like alphabetizing the list.Sorry about the grammer and I''s. ;Well heres the system I'm trying to create, please any comments or suggestions.I have a shader object (I dunno if shaders the correct word but its what I used), a material manager object, and various rendered objects.When I load my objects, they load their materials first. Calls to the material manager ensure that only one instance of a material or texture is loaded, and returns an index (a long) to the material which is stored for each object. (they are only stored for deletion and for this next step, after that they are not used.)After the object loads it's vertex data, it calls the shader to add it's vertex, normal, indices, uv pointers to a list under each material in a list (i want this second list to be sorted). The shader returns an index (a long) for that objectVisual:  Material 1 - Material 2 - Material 3obj1    obj2    obj3obj4  obj5obj6   I also have an array of booleans.When I actualy call Draw for my objects, they pass in a matrix, which is copied to the obj, and the boolean is set to true for that index.When I actualy render my scene, I will (psuedo code)for each material   setmaterial   'here?   if has objects setmaterial  'or here? for each object    if booleanlist = true  set matrix  drawprimitives    end next   endnext   Alright so thats what I'm trying to do.Any comments on the speed of passing longs instead of pointers or anything like that?Also I have a question about where to set my material.If their no objects for a certain material (this case would only happen if say an object was loaded but not drawn (is that word?) due to culling or something) then there is no reason for me to set that texture BUT.I want to preprocess my materials and have a boolean for each property (ambient, diffuse ...) so that I dont have to compare 4 floats with the 4 floats I last used, just if ambchange then change it. If I never load that material then that data wont be acurate.I hope im not way off on a tangent with my ideas...Once again sorry for my typing lazyness.   [edited by - honayboyz on February 19, 2004 9:24:50 PM];  I'm not sure sorting is the fastest way to handle the problem, since the materials in a scene often change as you add or remove them. The way I do it is I simply remember each property setting, and only change it if the material's property is different from what was last set.So if there are two materials that are identical except with different textures, after the first one is rendered, only the currently bound texture will be changed before rendering the second material. There are a few 'if' statements to do this but it's probably faster than any attempt at sorting can be.EDIT: Of course there is the worst-case scenario where two very different materials are rendered, followed by one very similar to the first, but since most materials are identical except for the texture, this isn't much of an issue.   ~CGameProgrammer( ); -- Post screenshots of your projects. There's already 134 screenshot posts.   [edited by - CGameProgrammer on February 20, 2004 3:06:56 AM];  CGameProgrammer> no, it definitely is _very_ important to sort your materials, and even more if you use vertex/fragment programs. you can get more than 100% fps increase depending on your shaders (got nearly 300% on a test scene with lots of different shaders mixed together. once sorted, it all came down to switching shaders 10 times per frame, instead of around 1500...)honayboyz> search the forums for render state sorting/shader sorting, you''ll find some threads where YannL explained some things, you might find that interesting and btw, CGameProgrammer, sorting geometry chunks according to their materials/shaders, isn''t an overhead at all. I too didn''t quite like the idea of sorting every chunk before rendering, and I thought the overhead might be greater than the speed gain due to less shader switching, but I was completely wrong, and now that I''ve tryed it, I simply can''t go back you should try too...;  I don''t use shaders. I should, I guess, but I don''t like the minimum hardware to be that high... so for me, all it is is basic render-mode changes. But you make a good point that sorting is important when using shaders.   ~CGameProgrammer( ); -- Post screenshots of your projects. There''s already 134 screenshot posts. ;  sBibi (or anyone), thanks for the comments.Do you have any about how im storing pointers and returning indices, how are you doing this?edit: can anyone else search the forums?   [edited by - honayboyz on February 20, 2004 8:24:49 AM];  There you go:http://triplebuffer.devmaster.net/misc/yannl.php 