confusion about drawing meshes per material and uniform buffer for constants

Hi there, I’ve been trying to build a simple rendering engine on my own in the hope of learning more about graphics. Now I have two confusions.

  1. To keep GL state change as few as possible, my idea now is that I render meshes that share the same material all together. But I don’t know whether I should keep an array of mesh pointer in each material or create a map with key as material pointer and mapped value as an array of mesh pointer. Or is there other better ways? how is it implemented commonly? what about the case where many objects share the same mesh?

  2. I would like to use uniform buffer objects to store various constants for shaders to use, I know I can use one uniform buffer object per scene to store matrices like view and perspective, but I don’t know how many should I create for objects and materials, one for each? or few shared by them?

Thanks.

To minimise the number of draw calls, you want to be able to use multiple materials within a single mesh. Make the material ID a “flat” vertex attribute, with material properties stored in an array and textures stored as layers of an array texture.

That way, you only need to use separate draw calls for parts of the scene which are structurally different (i.e. those which require completely different shaders, not just different values).