3

I'm trying to draw a quadcopter model using OpenGL. The quadcopter object has an .obj file, an .mtl file and three .tga file for texturing(one for rotors, one for body, one for guns).

My problem is I don't know how to apply these three textures on the object. Should I split the object in three. Are there more any simpler and more efficient way?

user3817833
  • 97
  • 1
  • 9

2 Answers2

3

The simplest way is to split the object into three distinct objects, otherwise you would need some method of identifying which texture the texture coordinates sample. This would require additional information for every single vertex in the model.

I'm not familiar with the obj format, but most formats I have worked with separate objects with different materials into separate meshes, with each mesh referencing a single material.

Homar
  • 1,520
  • 2
  • 17
  • 26
2

The obj will look roughly like this...

#all vertex data (for all materials)
v 0.020542 1.017087 -0.056279
v -0.000138 0.971408 -1.947060
v 0.017678 -0.000000 -0.017678
...
usemtl material_rotors
#all faces with rotor material
f 125/185/28 142/184/3 127/183/3
f 126/186/29 138/187/29 143/188/28
usemtl material_body
#all faces with body material
f 150/190/32 132/191/32 138/192/29
f 150/190/32 138/192/29 126/193/29

You can store everything in the same mesh object, not splitting as such, but instead use separate draw calls. For example:

foreach (material in model.materials)
    material.bind() //binds textures, sets colours, shader uniforms etc
    glDrawElements(GL_TRIANGLES, material.indexCount, GL_UNSIGNED_INT, model.bufferStart + material.startIndex)

It's the same as treating the whole obj as one model, except you keep track of which faces are drawn with a particular material and bind the appropriate texture while drawing that range of faces.

You could find a way to include per-face material information and bind all your textures at once but this just introduces unnecessary memory and processing overhead. I guess if the mesh had huge numbers of different materials then it might start to be more efficient to store material parameters per-vertex and use a single draw call but this is a pretty extreme hypothetical.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180