[LWJGL] How to handle separate OBJ Groups and their materials

OBJ File structure allows for groups of faces to have a single material attached to them.
I have written a parser which separates groups and they each have their own material.

How can I go about getting that data to the shader?
I figure my two options are:

  1. Load them in as a vertex array object attribute
  2. Load them in as uniform variables

The only way I know how to implement option 1 is to load an array of material data for every vertex which seems to be a LOT of extra information. Is there a way to point to the current group’s material while it’s being drawn and then switch to a new material for the next group? Or, is there something I’m missing completely?

Option 2 doesn’t seem viable because it seems to persist throughout the whole draw call.

Any information would be greatly appreciated.



The easiest would be to use separate draw calls with different uniform buffers (for material colors) and texture unit bindings (for the different texture - if your MTL material contains texture maps).
It is also possible to cram all information into a single draw call using indexed access with a uniform array in the shader (write the actual material colors into uniform buffer and encode the index to use as an integer vertex attribute) and sampling from different texture units (encode the texture unit to use as a vertex attribute as well).
If you target ever higher OpenGL versions and more capable hardware, more options open up.

Thank you for the fast response!

So, considering your first option:
Suppose I have a model which has two groups: a character body and a metal arm
I would batch render all of the character bodies and then all of the metal arms (or vice versa)?

Considering the second option:
I would need to store all vertex/normal/texture/index information in buffers for the VAO and then ALSO pass it in to the shader as a uniform buffer object?

(I’ve never used UBO’s before, so pardon me if I don’t understand!)