def createObject(self, node, parent=None, flags=GEOM_INIT_ALL): """ \param node (\c Lib3dsNode) Current node \param parent (\c WorldObject) Parent object or None \param flags (\c int) Flags for mesh creation \todo worldtransform als Slot \todo Pivot so setzen wie in 3DS """ if node==None: return worldobj = None auto_insert = True if parent!=None: auto_insert = False # Object? if node.type==TYPE_OBJECT: if node.name!="$$$DUMMY" and node.name!="_Quader01": data = node.object_data mesh = self.ddds.meshByName(node.name) if self.meshes.has_key(mesh.name): del self.meshes[mesh.name] # print "###",node.name,"###" # print "Node matrix:" # print node.matrix # print "Pivot:",data.pivot # print "Mesh matrix:" # print mesh.matrix tm = TriMeshGeom() mesh.initGeom(tm, flags) if parent==None: PT = mat4().translation(-data.pivot) m = node.matrix*PT*mesh.matrix.inverse() else: PT = mat4().translation(-data.pivot) m = node.matrix*PT*mesh.matrix.inverse() # worldtransform von Parent bestimmen... pworldtransform = parent.worldtransform # pworldtransform = parent.localTransform() # p = parent.parent # while p!=None: # pworldtransform = p.localTransform()*pworldtransform # p = p.parent m = pworldtransform.inverse()*m worldobj = TriMesh(name=node.name, # pivot=data.pivot, transform=m, auto_insert=auto_insert) worldobj.geom = tm # Set the materials matnames = mesh.materialNames() worldobj.setNumMaterials(len(matnames)) for i in range(len(matnames)): if matnames[i]=="": material = GLMaterial() # Was the material already instantiated? elif matnames[i] in self.materials: material = self.materials[matnames[i]] else: mat = self.ddds.materialByName(matnames[i]) # print "Material:",matnames[i] # print " self_illum:",mat.self_illum # print " self_ilpct:",mat.self_ilpct material = Material3DS(name = matnames[i], ambient = mat.ambient, diffuse = mat.diffuse, specular = mat.specular, shininess = mat.shininess, shin_strength = mat.shin_strength, use_blur = mat.use_blur, transparency = mat.transparency, falloff = mat.falloff, additive = mat.additive, use_falloff = mat.use_falloff, self_illum = mat.self_illum, self_ilpct = getattr(mat, "self_ilpct", 0), shading = mat.shading, soften = mat.soften, face_map = mat.face_map, two_sided = mat.two_sided, map_decal = mat.map_decal, use_wire = mat.use_wire, use_wire_abs = mat.use_wire_abs, wire_size = mat.wire_size, texture1_map = self._createTexMap(mat.texture1_map), texture2_map = self._createTexMap(mat.texture2_map), opacity_map = self._createTexMap(mat.opacity_map), bump_map = self._createTexMap(mat.bump_map), specular_map = self._createTexMap(mat.specular_map), shininess_map = self._createTexMap(mat.shininess_map), self_illum_map = self._createTexMap(mat.self_illum_map), reflection_map = self._createTexMap(mat.reflection_map) ) self.materials[matnames[i]] = material worldobj.setMaterial(material, i) # Camera? elif node.type==TYPE_CAMERA: cam = self.ddds.cameraByName(node.name) # print "Camera:",node.name # print " Roll:",cam.roll # print node.matrix # Convert the FOV from horizontal to vertical direction # (Todo: What aspect ratio to use?) fov = degrees(atan(480/640.0*tan(radians(cam.fov/2.0))))*2.0 worldobj = TargetCamera(name = node.name, pos = cam.position, target = cam.target, fov = fov, auto_insert = auto_insert) # Light? elif node.type==TYPE_LIGHT: lgt = self.ddds.lightByName(node.name) worldobj = self._createLight(node, lgt, auto_insert) if worldobj!=None and parent!=None: parent.addChild(worldobj) if worldobj!=None: self.createObject(node.childs(), worldobj, flags=flags) else: self.createObject(node.childs(), parent, flags=flags) self.createObject(node.next(), parent, flags=flags)
def setup(self): """Setup the scene. """ scene = getScene() # Set up direction scene.up = (0,1,0) # Camera self.cam = TargetCamera( name = "SlideShow_Cam", pos = (0,0,5.6), fov = 30 ) # Set background... gradient = Image.new("RGB", (2,128)) draw = ImageDraw.Draw(gradient) colbottom = vec3(0.1, 0.1, 0.3) colmid = vec3(0.95,0.95,1) coltop = vec3(0.2, 0.2, 0.4) colormap = [colbottom, colbottom, colmid, coltop, coltop] for y in range(128): t = float(y)/128 c = spline(t, colormap) draw.line([0,y, 1,y], fill=(int(c.x*255), int(c.y*255), int(c.z*255))) back = Plane( lx=4, ly=3, pos=(0,0,-5), scale=2.4, material = GLMaterial( diffuse = (0,1,1), texture = GLTexture(image = gradient, mode = GL_REPLACE)) ) # Create plate materials... initial = Image.new("RGB", (4,4)) invY = mat4(1).translate(vec3(0,1,0)).scale(vec3(1,-1,1)) self.mat1 = GLMaterial( diffuse = (1,1,1,1), texture = GLTexture( image = initial, mode = GL_REPLACE, # size = (512,512), transform = invY, wrap_s = GL_CLAMP, wrap_t = GL_CLAMP ) ) self.backmaterial = self.mat1 self.setBackImage(self.images[0][0]) self.mat2 = GLMaterial( diffuse = (1,1,1,1), texture = GLTexture( image = initial, mode = GL_REPLACE, # size = (512,512), transform = invY, wrap_s = GL_CLAMP, wrap_t = GL_CLAMP ) ) self.backmaterial = self.mat2 self.setBackImage(self.images[1][0]) self.frontmaterial = self.mat1 self.backmaterial = self.mat2 # Create plates... self.frontplate = Plane(lx = 4, ly = 3, material = self.frontmaterial) self.backplate = Plane(lx = 4, ly = 3, pos = (0,0,-0.1), material = self.backmaterial ) self.helper = Sphere(radius=0.1, pos=(0,0,-1))
def __init__( self, name="Material3DS", ambient=(0, 0, 0, 0), diffuse=(1.0, 1.0, 1.0, 1.0), specular=(1.0, 1.0, 1.0, 1.0), shininess=1, shin_strength=0, use_blur=0, transparency=0.0, falloff=0, additive=0, use_falloff=0, self_illum=False, self_ilpct=0.0, shading=0, soften=0, face_map=0, two_sided=0, map_decal=0, use_wire=0, use_wire_abs=0, wire_size=0, density=1.0, texture1_map=None, texture1_mask=None, texture2_map=None, texture2_mask=None, opacity_map=None, opacity_mask=None, bump_map=None, bump_mask=None, specular_map=None, specular_mask=None, shininess_map=None, shininess_mask=None, self_illum_map=None, self_illum_mask=None, reflection_map=None, reflection_mask=None, bump_size=1.0 # Extra parameter to control the bump map ): Material.__init__(self, name=name, density=density) self.ambient = ambient self.diffuse = diffuse self.specular = specular self.shininess = shininess self.shin_strength = shin_strength self.transparency = transparency self.self_illum = self_illum self.self_ilpct = self_ilpct self.texture1_map = texture1_map self.texture1_mask = texture1_mask self.texture2_map = texture2_map self.texture2_mask = texture2_mask self.opacity_map = opacity_map self.opacity_mask = opacity_mask self.bump_map = bump_map self.bump_mask = bump_mask self.specular_map = specular_map self.specular_mask = specular_mask self.shininess_map = shininess_map self.shininess_mask = shininess_mask self.self_illum_map = self_illum_map self.self_illum_mask = self_illum_mask self.reflection_map = reflection_map self.reflection_mask = reflection_mask self.bump_size = bump_size if texture1_map == None: self._gltexture = None glambient = ambient gldiffuse = diffuse else: map = texture1_map T = mat4(1, 0, 0, -map.offset[0] - 0.5, 0, -1, 0, 0.5 - map.offset[1], 0, 0, 1, 0, 0, 0, 0, 1) a = sl.radians(map.rotation) ca = math.cos(a) sa = math.sin(a) R = mat4(ca, -sa, 0, 0, sa, ca, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) S = mat4(map.scale[0], 0, 0, 0.5, 0, map.scale[1], 0, 0.5, 0, 0, 1, 0, 0, 0, 0, 1) self._gltexture = GLTexture( imagename=map.name, mode=GL_MODULATE, # transform = mat4().scaling(vec3(1,-1,1)) transform=S * R * T) glambient = map.percent * vec4( 1, 1, 1, 1) + (1 - map.percent) * vec4(ambient) gldiffuse = map.percent * vec4( 1, 1, 1, 1) + (1 - map.percent) * vec4(diffuse) self._glmaterial = GLMaterial(ambient=glambient, diffuse=gldiffuse, specular=shin_strength * specular, shininess=25 * shininess, texture=self._gltexture)