-# backface cutting
- a = []
- a.append(OBJmesh.faces[face][0][0])
- a.append(OBJmesh.faces[face][0][1])
- a.append(OBJmesh.faces[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(OBJmesh.faces[face][1][0])
- b.append(OBJmesh.faces[face][1][1])
- b.append(OBJmesh.faces[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(OBJmesh.faces[face][numvert-1][0])
- c.append(OBJmesh.faces[face][numvert-1][1])
- c.append(OBJmesh.faces[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 = [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]
-
- if d < 0:
- file.write("<polygon points=\"")
- for vert in range(numvert):
-
- objekat[0] += 3
-
- vertxyz = []
-
- if vert != 0: file.write(", ")
-
- vertxyz.append(OBJmesh.faces[face][vert][0])
- vertxyz.append(OBJmesh.faces[face][vert][1])
- vertxyz.append(OBJmesh.faces[face][vert][2])
-
-# rotate object
-
- vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], obj.RotX, obj.RotY, obj.RotZ)
-
- vertxyz[0] += obj.LocX - camera.LocX
- vertxyz[1] += obj.LocY - camera.LocY
- vertxyz[2] += obj.LocZ - camera.LocZ
-
-# rotate camera
-
- vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], -camera.RotX, -camera.RotY, -camera.RotZ)
-
- objekat.append(Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
-# dist = Distance(vertxyz[0], vertxyz[1], vertxyz[2])
- xy = flatern(vertxyz[0], vertxyz[1], vertxyz[2])
- px = int(xy[0])
- py = int(xy[1])
- objekat.append(px)
- objekat.append(py)
- # add/sorting in Z' direction
- #Dodaj(px,py,Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
- file.write(`px` + ", " + `py`)
- ambient = -200
- svetlo = [1,1,-1]
- vektori = (norm[0]*svetlo[0]+norm[1]*svetlo[1]+norm[2]*svetlo[2])
- vduzine = fabs(sqrt(pow(norm[0],2)+pow(norm[1],2)+pow(norm[2],2))*sqrt(pow(svetlo[0],2)+pow(svetlo[1],2)+pow(svetlo[2],2)))
- intensity = floor(ambient + 255 * acos(vektori/vduzine))
- print vektori/vduzine
- if intensity < 0: intensity = 0
- file.write("\"\n style=\"fill:rgb("+str(intensity)+","+str(intensity)+","+str(intensity)+");stroke:rgb(0,0,0);stroke-width:1\"/>\n")
- if startFrm <> endFrm:
- file.write("<animate attributeName=\"visibility\" begin=\""+str(f*0.08)+"s\" dur=\"0.08s\" fill=\"remove\" to=\"visible\">\n")
- file.write("</animate>\n")
- file.write("</g>\n")
-
-#flatern()
-#writesvg(objekat)
-file.write("</svg>")
-file.close()
-print file
-DrawProgressBar (1.0,"Finished.")
-print "Finished\n"
+ The only instance attribute here is the canvas size, which can be
+ queryed to the renderer by other entities.
+ """
+ self.canvasSize = (0.0, 0.0)
+
+
+ # Public Methods
+ #
+
+ def getCanvasSize(self):
+ """Return the current canvas size read from Blender rendering context"""
+ return self.canvasSize
+
+ def doRendering(self, scene, cameraObj=None):
+ """Control the rendering process.
+
+ Here we control the entire rendering process invoking the operation
+ needed to transforma project the 3D scene in two dimensions.
+
+ Parameters:
+ scene --- the Blender Scene to render
+ cameraObj --- the camera object to use for the viewing processing
+ """
+
+ if cameraObj == None:
+ cameraObj = scene.getCurrentCamera()
+
+ # TODO: given the camera get the Wold-to-camera transform and the
+ # projection matrix
+
+ context = scene.getRenderingContext()
+ self.canvasSize = (context.imageSizeX(), context.imageSizeY())
+
+ Objects = scene.getChildren()
+
+ # A mesh to store the transformed geometrical structure
+ mesh = []
+
+ for obj in Objects:
+
+ if (obj.getType() != "Mesh"):
+ print "Type:", obj.getType(), "\tSorry, only mesh Object supported!"
+ continue
+
+ OBJmesh = obj.getData() # Get the mesh data for the object
+ meshfaces = OBJmesh.faces # The number of faces in the object
+
+ transformed_object = []
+
+ for face in meshfaces:
+
+ # TODO: per face color calculation
+ # TODO: add/sorting in Z' direction (per face??)
+
+ # if the face is visible flatten it on the "picture plane"
+ if isFaceVisible(face, obj, cameraObj):
+
+ # Store transformed face
+ transformed_face = []
+
+ for vert in face:
+
+ vertxyz = list(vert)
+
+ p1 = flatten_new(vert, cameraObj, self.canvasSize,
+ obj)
+ transformed_face.append(p1)
+ continue
+
+ # rotate camera
+ vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2],
+ cameraObj.RotX, cameraObj.RotY, cameraObj.RotZ)
+ #-cameraObj.RotX, -cameraObj.RotY, -cameraObj.RotZ)
+
+
+ # original setting for translate
+ vertxyz[0] -= (obj.LocX - cameraObj.LocX)
+ vertxyz[1] -= (obj.LocY - cameraObj.LocY)
+ vertxyz[2] -= (obj.LocZ - cameraObj.LocZ)
+
+
+ # rotate object
+ vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], obj.RotX, obj.RotY, obj.RotZ)
+
+
+
+ p1 = flatten(vertxyz[0], vertxyz[1], vertxyz[2],
+ cameraObj, self.canvasSize)
+
+ transformed_face.append(p1)
+
+ # just some fake lighting...
+
+ transformed_object.append(transformed_face)
+
+ # at the end of the loop on obj
+ mesh.append(transformed_object)
+ return mesh
+
+
+ # Private Methods
+ #
+
+ def _removehiddenFaces(obj):
+ return
+
+ def _testClipping(face):
+ return
+
+
+# ---------------------------------------------------------------------
+#
+## Main Program
+#
+# ---------------------------------------------------------------------
+
+
+scene = Scene.GetCurrent()
+renderer = Renderer()
+
+projectedMesh = renderer.doRendering(scene)
+canvasSize = renderer.getCanvasSize()
+
+# hackish sorting of faces according to the max z value of a vertex
+for o in projectedMesh:
+ o.sort(lambda f1, f2:
+ cmp(sum([v[2] for v in f1])/len(f1), sum([v[2] for v in f2])/len(f2)))
+ o.reverse()
+
+writer = SVGVectorWriter("proba.svg", canvasSize)
+writer.printCanvas(projectedMesh)