How to render models using .OBJ and .MTL files

Started by tynerk92, May 17, 2019, 18:08:39

Previous topic - Next topic

tynerk92

I have loaders for each of the file types (.obj and .mtl).

MTL File
The .mtl file has several materials in it which define different types of lighting reflective values, alpha, etc. etc.
newmtl Bark
Kd 0.207595 0.138513 0.055181
Ns 256
d 1
illum 1
Ka 0 0 0
Ks 0 0 0
newmtl Tree
Kd 0.256861 0.440506 0.110769
Ns 256
d 1
illum 1
Ka 0 0 0
Ks 0 0 0


OBJ File
The .obj file has "usemtl <materialName>" in several locations throughout the faces section.
...
mtllib tree.mtl
usemtl Bark
g Default
f 7/1/1 8/2/1 14/3/1 13/4/1
f 9/5/2 15/6/2 14/3/2 8/2/2
f 10/7/3 16/8/3 15/6/3 9/5/3
...
f 83/90/310 89/392/310 88/394/310 81/86/310
f 79/94/311 87/395/311 90/393/311 85/92/311
f 79/87/312 81/86/312 88/394/312 87/396/312
usemtl Tree
f 189/215/141 190/216/141 191/217/141
f 192/218/142 189/215/142 191/219/142
f 193/220/143 192/218/143 191/221/143
...

My question is:
When using drawElements(), is there a way to switch the texture that I'm using or to update color values during the draw process?

If not, is there a common solution for this problem?

Thank you in advance,

Kylon

KaiHH

You can either issue multiple draw calls per OpenGL pipeline state (bound texture, bound shader) or you can also use a texture atlas to store multiple textures and remap the texture coordinates in the OBJ file to the allocated regions in the texture atlas. A third possibility is to use an array texture, assign each vertex an integer in the vertex data identifying which texture it belongs to and then write a shader that selects the corresponding texture layer to sample from. Array textures have the restriction that all layers must have the same resolution, though.

tynerk92

Thanks for the quick reply!

When you say multiple draw calls per OpenGL pipeline state:

Do you mean that in the example of a tree which has a trunk texture and a leaf texture,
I would draw all trunks in the scene and then all leaves in the scene?

tynerk92

I've decided not to worry about switching textures as each model that I use should have its own mapped texture.

I'm going to handle the different colors/reflective values/etc. through a VBO which contains all of that information as an array.
That way I can still use glDrawElements and not have to worry about switching things manually.