Zulko/vapory

vapory with mesh

mpkuse opened this issue · 2 comments

I am trying to render a scene from mesh. I have a textured mesh which I am loading with assimp. Could you give me an hint as how to render a mesh scene with vapory.

That's actually already possible with two (fairly small) extension classes. Let's look at a Mesh consisting of smooth triangles. Then you need to create the following two (empty) extensions of the
POVRayElement class:

class SmoothTriangle(POVRayElement):
   """SmoothTriangle([x,y,z], [nx, ny, nz])"""

and

class Mesh(POVRayElement):
    """Mesh( ... smooth triangles ..., texture )"""

You can then call Mesh(*args) where args is an array of smooth triangles and the Texture (for example: Texture(Pigment('color', [1, 0, 0])))

For instance you can construct args as follows:

mesh_elements = list()
for tri in self.mesh:
    mesh_elements.append(tri.as_vapory())

mesh_elements.append(Texture(Pigment('color', [1, 0, 0])))
Mesh(*mesh_elements)

That's all folks

I didn't use the two empty class ; instead I used Triangle class provided in vapory and it works like a charm. Here's the code:

def load_3d_model(model_vertices, model_faces):
   """load meshes into a list of vapory Triangle object.
      :param model_vertices: a list of list; each list provides coordinates of one vertex in 3d space. For example, it might look like this: [[-0.282614,-0.0720009,-0.170291], [-0.282874,-0.0745509,-0.1703], [-0.281393,-0.0725343,-0.170298]]
      :param model_faces: a list that contains 6 float [vertex_index1, vertex_index2, vertex_index3, r, g, b]  and r,g,b here is in a 0-1 scale.
   """
   mesh_elements = []
   for face in model_faces:
       tri = Triangle(model_vertices[  face[0]  ],
                      model_vertices[  face[1]  ],
                      model_vertices[  face[2]  ],
                      Texture(Pigment('rgb', face[3:]) ))
       mesh_elements.append(tri)
   return mesh_elements

Rendering is easy too:

object_model = load_3d_model(model_vertices, model_faces)
light = LightSource([0, 0, 1], 'color', 1)
camera = Camera('location',  [0, 0, 1],
                       'direction', [0, 0, -1],
                       'look_at',  [0, 0, 0])
scene = Scene(camera, objects = [light] +  object_model)
scene.render('rendered.png', remove_temp = False)