- camera = cameraObj
-
- numvert = len(face)
-
- # backface culling
-
- # translate and rotate according to the object matrix
- # and then translate according to the camera position
- #m = obj.getMatrix()
- #m.transpose()
-
- #a = m*Vector(face[0]) - Vector(cameraObj.loc)
- #b = m*Vector(face[1]) - Vector(cameraObj.loc)
- #c = m*Vector(face[numvert-1]) - Vector(cameraObj.loc)
-
- a = []
- a.append(face[0][0])
- a.append(face[0][1])
- a.append(face[0][2])
- a = RotatePoint(a[0], a[1], a[2], obj.RotX, obj.RotY, obj.RotZ)
- a[0] += obj.LocX - camera.LocX
- a[1] += obj.LocY - camera.LocY
- a[2] += obj.LocZ - camera.LocZ
- b = []
- b.append(face[1][0])
- b.append(face[1][1])
- b.append(face[1][2])
- b = RotatePoint(b[0], b[1], b[2], obj.RotX, obj.RotY, obj.RotZ)
- b[0] += obj.LocX - camera.LocX
- b[1] += obj.LocY - camera.LocY
- b[2] += obj.LocZ - camera.LocZ
- c = []
- c.append(face[numvert-1][0])
- c.append(face[numvert-1][1])
- c.append(face[numvert-1][2])
- c = RotatePoint(c[0], c[1], c[2], obj.RotX, obj.RotY, obj.RotZ)
- c[0] += obj.LocX - camera.LocX
- c[1] += obj.LocY - camera.LocY
- c[2] += obj.LocZ - camera.LocZ
-
- norm = Vector([0,0,0])
- norm[0] = (b[1] - a[1])*(c[2] - a[2]) - (c[1] - a[1])*(b[2] - a[2])
- norm[1] = -((b[0] - a[0])*(c[2] - a[2]) - (c[0] - a[0])*(b[2] - a[2]))
- norm[2] = (b[0] - a[0])*(c[1] - a[1]) - (c[0] - a[0])*(b[1] - a[1])
-
- d = norm[0]*a[0] + norm[1]*a[1] + norm[2]*a[2]
- # d = DotVecs(norm, Vector(a))
-
- return (d<0)
-
- def _doClipping(face):
+ # The transformation matrix of the object
+ mObj = Matrix(obj.getMatrix())
+ mObj.transpose()
+
+ # The normal after applying the current object rotation
+ #normal = mObj.rotationPart() * Vector(face.no)
+ normal = Vector(face.no)
+
+ # View vector in orthographics projections can be considered simply s the
+ # camera position
+ #view_vect = Vector(camObj.loc)
+
+ # View vector as in perspective projections
+ # it is the dofference between the camera position and
+ # one point of the face, we choose the first point,
+ # but maybe a better choice may be the farthest point from the camera.
+ point = Vector(face[0].co)
+ #point = mObj * point.resize4D()
+ #point.resize3D()
+ view_vect = Vector(camObj.loc) - point
+
+
+ # if d <= 0 the face is visible from the camera
+ d = view_vect * normal
+
+ if d <= 0:
+ return False
+ else:
+ return True
+
+
+ # Scene methods
+
+ def _doClipping():
+ return
+
+ def _doDepthSorting(self, scene):
+
+ cameraObj = self.camera
+ Objects = scene.getChildren()
+
+ Objects.sort(lambda obj1, obj2:
+ cmp(Vector(Vector(cameraObj.loc) - Vector(obj1.loc)).length,
+ Vector(Vector(cameraObj.loc) - Vector(obj2.loc)).length
+ )
+ )
+
+ # hackish sorting of faces according to the max z value of a vertex
+ for o in Objects:
+
+ if (o.getType() != 'Mesh'):
+ continue
+ #
+
+ mesh = o.data
+ mesh.faces.sort(
+ lambda f1, f2:
+ # Sort faces according to the min z coordinate in a face
+ #cmp(min([v[2] for v in f1]), min([v[2] for v in f2])))
+
+ # Sort faces according to the max z coordinate in a face
+ cmp(max([v[2] for v in f1]), max([v[2] for v in f2])))
+
+ # Sort faces according to the avg z coordinate in a face
+ #cmp(sum([v[2] for v in f1])/len(f1), sum([v[2] for v in f2])/len(f2)))
+ mesh.faces.reverse()
+ mesh.update()
+
+ # update the scene
+ # FIXME: check if it is correct
+ scene.update()
+ #for o in scene.getChildren():
+ # scene.unlink(o)
+ #for o in Objects:
+ # scene.link(o)
+
+ # Per object methods
+
+ def _doModelViewTransformations(self, object):
+ if(object.getType() != 'Mesh'):
+ return
+
+ matMV = object.matrix
+ mesh = object.data
+ mesh.transform(matMV, True)
+ mesh.update()
+
+
+ def _doBackFaceCulling(self, object):
+ if(object.getType() != 'Mesh'):
+ return
+
+ print "doing Backface Culling"
+ mesh = object.data
+
+ # Select all vertices, so edges without faces can be displayed
+ for v in mesh.verts:
+ v.sel = 1
+
+ Mesh.Mode(Mesh.SelectModes['FACE'])
+ # Loop on faces
+ for f in mesh.faces:
+ f.sel = 0
+ if self._isFaceVisible(f, object, self.camera):
+ f.sel = 1
+
+ for f in mesh.faces:
+ if not f.sel:
+ for v in f:
+ v.sel = 0
+
+ for f in mesh.faces:
+ if f.sel:
+ for v in f:
+ v.sel = 1
+
+ mesh.update()
+
+
+
+ #Mesh.Mode(Mesh.SelectModes['VERTEX'])
+
+ def _doColorAndLighting(self, object):