juangallostra / augmented-reality

Augmented reality proof of concept project with Python and OpenCV
MIT License
335 stars 118 forks source link

How to display the texture of 3Dmodel? #20

Closed Allfu closed 3 years ago

Allfu commented 3 years ago

thanks for your work! Now i get a question: Is a single color space covering the texture of the model? Or should we combine the MTL file to display the texture of the model? thanks !

juangallostra commented 3 years ago

Hi @Allfu , glad you like it!

Current Status

As you can see here

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/ar_main.py#L116-L117

if color is False, which it is by default, I am using a single color to render the model, ignoring whatever colors have been defined in the model file.

The default color used to render the model is defined here: https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/ar_main.py#L21

Render Model with colors

If you only want to render the model with colors - assuming they have been defined in the .obj file - and not textures, you should call the render function with the last parameter set to True:

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/ar_main.py#L99

The call to the render function is done here:

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/ar_main.py#L80

an example of a model with colors is the Pirate Ship (https://github.com/juangallostra/augmented-reality/blob/master/models/pirate-ship-fat.obj). Colors are defined in hex format:

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/models/pirate-ship-fat.obj#L20

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/models/pirate-ship-fat.obj#L104

To load colors you should also uncomment the lines: https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/objloader_simple.py#L25-L26

See below an example result of the Pirate Ship and Fox colored models:

imagen

test

The second one is taken from piotrekk94's fork: https://github.com/piotrekk94/tx2-ar, which has some other improvements.

Render model with textures

If what you want is to display a texture for the model, then yes, you should combine the OBJ model with the MTL file.

The code I am using to load OBJ models is based on this one: https://www.pygame.org/wiki/OBJFileLoader As you can see in the link, there is a second class I removed which loads MTL files. In addition to that, lines

https://github.com/juangallostra/augmented-reality/blob/970b0cfd569aededa24f68fbc07fe8442212b853/src/objloader_simple.py#L27-L28

should be uncommented. The problem with textures is that you will then need OpenGL (or something similar) to render the model, which was work I didn't want to do at the time. It is possible, however, to do so.

This is a useful resource if you are wishing to dig deeper on how to do it: https://rdmilligan.wordpress.com/2015/10/15/augmented-reality-using-opencv-opengl-and-blender/

Allfu commented 3 years ago

got it! thanks for your help! thanks!