Esempio n. 1
0
    def initialize(self):
        logger.info("initialize " + self.__class__.__name__)

        # collect shader files
        for filename in glob.glob(os.path.join(PathShaders, '*.*')):
            try:
                shaderFile = os.path.split(filename)[1]
                shaderName, ext = os.path.splitext(shaderFile)
                # open shader file
                f = open(filename, 'r')
                shaderSource = f.read()
                f.close()
                # create shader
                if ext == '.vs':
                    shader = VertexShader(shaderName, shaderSource)
                    self.vertexShaders[shaderName] = shader
                elif ext == '.fs':
                    shader = FragmentShader(shaderName, shaderSource)
                    self.fragmentShader[shaderName] = shader
                else:
                    logger.warn("Shader error : %s is invalid shader. Shader file extension must be one of '.vs', '.ps', '.cs'..." % filename)
                    continue
                # regist shader
                self.shaders.append(shader)
            except:
                logger.error(traceback.format_exc())
Esempio n. 2
0
def LoadMTL(filepath, filename):
    contents = {}
    mtl = None
    # check is exist file
    filename = os.path.join(filepath, filename)
    if os.path.isfile(filename):
        for line in open(filename, "r"):
            # is comment?
            line = line.strip()
            if line.startswith('#'):
                continue
            
            # split with space
            line = line.split()
            
            # is empty?
            if not line:
                continue
            
            preFix = line[0]
            # create new material
            if preFix == 'newmtl' and len(line) > 1:
                mtlName = line[1]
                mtl = contents[mtlName] = {}
            elif mtl is None:
                logger.warn("mtl file doesn't start with newmtl stmt")
                raise ValueError("mtl file doesn't start with newmtl stmt")
            elif preFix == 'map_Kd':
                # load the texture referred to by this declaration
                texName = os.path.join(filepath, line[1])
                mtl['map_Kd'] = texName
                try:
                    if os.path.exists(texName):
                        # load texture file
                        image = Image.open(texName)
                        ix, iy = image.size
                        image = image.tobytes("raw", "RGBX", 0, -1)

                        # binding texture
                        texid = mtl['texture_Kd'] = glGenTextures(1)
                        glBindTexture(GL_TEXTURE_2D, texid)
                        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
                except:
                    logger.error(traceback.format_exc())
            elif len(line) > 1:
                mtl[preFix] = list(map(float, line[1:]))
    return contents