Exemple #1
0
    def load(self, filename):
        contents = {}
        mtl = None
        for line in R.openResource(filename, "rt"):
            if line.startswith("#"):
                continue
            values = line.split()
            if not values:
                continue
            if values[0] == "newmtl":
                mtl = contents[values[1]] = {}
            elif mtl is None:
                raise ValueError, "mtl file doesn't start with newmtl stmt"
            elif values[0] == "map_Kd":
                # load the texture referred to by this declaration
                mtl["texture"] = glt.Texture(values[1])
            elif values[0] in mat_params:
                mtl[values[0]] = map(float, values[1:])

        for k, v in contents.iteritems():
            if len(v) == 0:
                continue
            # don't overwrite materials
            if k not in self._materials_by_name or self._materials_by_name[k] is None:
                self.setNamedMaterial(k, Material(v))
Exemple #2
0
    def loadConfig(self,mapfile):

        cfg = ConfigParser.ConfigParser()
        cfg.readfp(R.openResource(mapfile + ".map"))

        def get(option, default=None, section="Main"):
            try:
                return cfg.get(section, option)
            except:
                return default

        def getColor(option, default=(0,0,0), section="MapMarkerColors"):
            try:
                col = cfg.get(section, option).strip()
            except:
                return default

            if ',' in col:
                col = col.replace("(","").replace(")","")
                col = map(int,col.split(','))
            else:
                col = pygame.Color(col)
                col = col.r, col.g, col.b

            return col

        self._shader_name = get("shader")
        self._height_map = get("height_map")
        self._texture_map = get("texture_map")
        self._detail_map = get("detail_map")

        self._textures = [self._texture_map, self._detail_map]

        for name, color in cfg.items("MapMarkerColors"):
            self._marker_colors[name] = getColor(name)

        print "Markers:"
        print self._marker_colors
Exemple #3
0
	def load(self, filename):
		self._materials = {}
		mat_num = 1
		current_material = None
		mat = Material()

		for n,line in enumerate(R.openResource(filename,"rt")):
			line = line.strip()
			if not line or line.startswith("#"):
				continue

			if "#" in line:
				line = line.partition("#")[0]

			cmd,_,line = line.partition(" ")

			if cmd == "newmtl":
				if current_material:
					self._materials[current_material] = mat
					self._materials["#%s"%mat_num] = mat
					mat_num += 1

				mat = Material()
				current_material = line

			elif cmd == "Ka": # ambient color
				mat._ambient_color[0:3] = self._parseColor(line, filename, n)

			elif cmd == "Kd": # diffuse color
				mat._diffuse_color[0:3] = self._parseColor(line, filename, n)
			
			elif cmd == "Ks": # specular color
				mat._specular_color[0:3] = self._parseColor(line, filename, n)
			
			elif cmd == "Ns": # specular exponent
				mat._specular_exp = self._parseFloat(line, filename, n)

			elif cmd == "d": # opacity
				mat._alpha = self._parseFloat(line, filename, n)

			elif cmd == "Tr": # transparency
				mat._alpha = 1.0 - self._parseFloat(line, filename, n)
		
			elif cmd == "map_Kd" or cmd == "tex0": # diffuse texture (or texture 0)
				mat._texture[0] = line

			elif cmd == "map_Ks" or cmd == "tex1": # specular map (or texture 1)
				mat._texture[1] = line
			
			elif cmd == "map_bump" or cmd == "tex2": # bump map (or texture 2)
				mat._texture[2] = line

			#this is an extension of sPyGlass
			elif cmd == "shader":
				mat._shader = line
			else:
				print "%s:%s - material property ignored: '%s'"%(filename, n, cmd)

		if current_material:
			self._materials[current_material] = mat
			self._materials["#%s"%mat_num] = mat
			mat_num += 1
Exemple #4
0
    def readObj(self, filename, swapyz=False):
        positions = []
        normals = []
        faces = []
        vert_tuple2number = {}
        texcoords = []
        vertices = []

        material = -1
        for line in R.openResource(filename, "rt"):
            if line.startswith("#"):
                continue
            values = line.split()
            if not values:
                continue
            if values[0] == "v":
                v = map(float, values[1:4])
                if swapyz:
                    v = v[0], v[2], v[1]
                positions.append(v)
            elif values[0] == "vn":
                v = map(float, values[1:4])
                if swapyz:
                    v = v[0], v[2], v[1]
                n = math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
                if n > 0:
                    v[0] /= n
                    v[1] /= n
                    v[2] /= n
                normals.append(v)
            elif values[0] == "vt":
                texcoords.append(map(float, values[1:3]))
            elif values[0] in ("usemtl", "usemat"):
                material = self._materials.getMaterialIdByName(values[1])
            elif values[0] == "mtllib":
                self._materials.load(values[1])
            elif values[0] == "f":
                tri_v = [0, 0, 0]
                for i, v in enumerate(values[1:]):
                    w = map(lambda a: int(a) if a else 0, v.split("/"))
                    pos = w[0]
                    tc = w[1] if len(w) > 1 else 0
                    n = w[2] if len(w) > 2 else 0

                    if v not in vert_tuple2number:
                        vert_tuple2number[v] = len(vertices)
                        vertices.append((pos - 1, n - 1, tc - 1))

                    vnum = vert_tuple2number[v]

                    # if a face has more than 3 vertices, split it into triangles
                    if i == 0:
                        tri_v[0] = vnum
                    else:
                        tri_v[1], tri_v[2] = tri_v[2], vnum

                    if i >= 2:
                        faces.append((material, tri_v[:]))

        texcoords = N.array(texcoords, dtype="f")
        positions = N.array(positions, dtype="f")
        normals = N.array(normals, dtype="f")

        x_arr = positions[:, 0]
        y_arr = positions[:, 1]
        z_arr = positions[:, 2]

        self._bounds = N.array(
            ((N.min(x_arr), N.min(y_arr), N.min(z_arr)), (N.max(x_arr), N.max(y_arr), N.max(z_arr))), dtype="f"
        )

        self._has_normals = len(normals) > 0
        self._has_tc = len(texcoords) > 0

        self.createBuffers(texcoords, positions, normals, faces, vertices)
        self.resolveMaterials()