Giter VIP home page Giter VIP logo

Comments (1)

juangallostra avatar juangallostra commented on August 17, 2024

@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:

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.

for face in obj.faces:
face_vertices = face[0]
points = np.array([vertices[vertex - 1] for vertex in face_vertices])
points = np.dot(points, scale_matrix)
# render model in the middle of the reference surface. To do so,
# model points must be displaced
points = np.array([[p[0] + w / 2, p[1] + h / 2, p[2]] for p in points])
dst = cv2.perspectiveTransform(points.reshape(-1, 1, 3), projection)
imgpts = np.int32(dst)
if color is False:
cv2.fillConvexPoly(img, imgpts, (137, 27, 211))
else:
color = hex_to_rgb(face[-1])
color = color[::-1] # reverse
cv2.fillConvexPoly(img, imgpts, color)

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.

from augmented-reality.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.