juangallostra / augmented-reality

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

how to use a colorful AR model? #10

Open LML-566 opened 5 years ago

LML-566 commented 5 years ago

how to use a colorful AR model?

juangallostra commented 5 years ago

@LML-566 the easiest option to include color in the models would be to make use of the property usemtl that is available when defining a model in OBJ format. This enables you to define a color (in hex format) for a specific set of faces.

As an example, see: https://github.com/juangallostra/augmented-reality/blob/19a49c47982df9009c7f6ec06a0eb3613050562b/models/pirate-ship-fat.obj#L20

To get an idea of how the whole model would then look: https://github.com/juangallostra/augmented-reality/blob/master/models/pirate-ship-fat.obj#L20

This is the easy part. You can search for models which already include color.

The hard part is that, after that, you should modify the section of the code that renders the faces of the model (see below) to render first (or not render at all) the faces that lie in the background according to the current perspective and then move forwards to render, as the last one, the face that lies most in the foreground. Otherwise you might end up seeing colors that are not expected to be seen.

https://github.com/juangallostra/augmented-reality/blob/19a49c47982df9009c7f6ec06a0eb3613050562b/src/ar_main.py#L106-L120

I think (although I have not tested it) that it should be enough to order the list of faces to be rendered by decreasing y-coordinate. After computing the projection of the model vertices (Lines 113-114) for all the faces something along the lines of

ordered_faces = sorted(faces, key=lambda face: face[1], reverse=True)

should be done before rendering.