def convert(self): if self._converted: return self._converted = True self.ambient = utils.convert_ctypes(self.ambient, ctypes.c_float, (3, )) self.diffuse = utils.convert_ctypes(self.diffuse, ctypes.c_float, (3, )) self.specular = utils.convert_ctypes(self.specular, ctypes.c_float, (3, )) imageName = self.imageName if imageName in imageNameToTexid: self.texid = imageNameToTexid[imageName] return self.texid = glGenTextures(1) imageNameToTexid[imageName] = self.texid image = pygame.image.load(imageName) w, h = image.get_rect().size image = pygame.image.tostring(image, 'RGBA', 1) glBindTexture(GL_TEXTURE_2D, self.texid) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
def convert(self): if self._converted: return self._converted = True self.ambient = utils.convert_ctypes(self.ambient, ctypes.c_float, (3,)) self.diffuse = utils.convert_ctypes(self.diffuse, ctypes.c_float, (3,)) self.specular = utils.convert_ctypes(self.specular, ctypes.c_float, (3,)) imageName = self.imageName if imageName in imageNameToTexid: self.texid = imageNameToTexid[imageName] return self.texid = glGenTextures(1) imageNameToTexid[imageName] = self.texid image = pygame.image.load(imageName) w, h = image.get_rect().size image = pygame.image.tostring(image, 'RGBA', 1) glBindTexture(GL_TEXTURE_2D, self.texid) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
def __init__(self, name, vdata, material): """ vdata: GL_T2F_N3F_V3F """ global _objID self.objID = _objID _objID += 1 self.vdata = utils.convert_ctypes(vdata, ctypes.c_float, (len(vdata), )) self.indices = utils.convert_ctypes( range(len(vdata)/8), ctypes.c_uint, (len(vdata)/8, )) self.name = name self.material = material
def __init__(self, filename, sprite): self.sprite = sprite fullpath = os.path.join(config.MODEL_DIR, filename) self.data = {} lineID = 0 for line in open(fullpath, 'r'): line = line.split() lineID += 1 opr = line[0] if opr == 'f': objName = line[1] frameID = int(line[2]) mat = utils.convert_ctypes(map(float, line[3:]), ctypes.c_float, (16, )) self.data[objName, frameID] = mat elif opr == 'ts': self.timeStep = float(line[1]) elif opr == 'n': self.nFrames = int(line[1]) else: print 'In line {}, unknown command "{}"'.format(lineID, opr) self.loop = True self._pause = False self.start()
def __init__(self, filename, sprite): self.sprite = sprite fullpath = os.path.join(config.MODEL_DIR, filename) self.data = {} lineID = 0 for line in open(fullpath, 'r'): line = line.split() lineID += 1 opr = line[0] if opr == 'f': objName = line[1] frameID = int(line[2]) mat = utils.convert_ctypes(map(float, line[3:]), ctypes.c_float, (16,)) self.data[objName, frameID] = mat elif opr == 'ts': self.timeStep = float(line[1]) elif opr == 'n': self.nFrames = int(line[1]) else: print 'In line {}, unknown command "{}"'.format(lineID, opr) self.loop = True self._pause = False self.start()
def __init__ (self): cylindarV = [] cylindarN = [] splitNum = 20 for i in range(splitNum): x = math.cos(2*PI*i/splitNum) / 2 y = math.sin(2*PI*i/splitNum) / 2 nx = math.cos(2*PI*(i+1)/splitNum) / 2 ny = math.sin(2*PI*(i+1)/splitNum) / 2 #rectangle cylindarV.append((x, y, .5)) cylindarV.append((nx, ny, .5)) cylindarV.append((nx, ny, -.5)) cylindarV.append((x, y, .5)) cylindarV.append((nx, ny, -.5)) cylindarV.append((x, y, -.5)) vc = calculateNormal((.0, .0, -1.), (nx-x, ny-y, .0)) for j in range(6): cylindarN.append(vc) #upper triangle cylindarV.append((.0, .0, .5)) cylindarV.append((x, y, .5)) cylindarV.append((nx, ny, .5)) vc = calculateNormal((x, y, .0), (nx, ny, .0)) for j in range(3): cylindarN.append(vc) #lower triangle cylindarV.append((.0, .0, -.5)) cylindarV.append((x, y, -.5)) cylindarV.append((nx, ny, -.5)) vc = calculateNormal((nx, ny, .0), (x, y, .0)) for j in range(3): cylindarN.append(vc) self.cylindarV = utils.convert_ctypes(cylindarV, ctypes.c_float, (len(cylindarV), 3)) self.cylindarN = utils.convert_ctypes(cylindarN, ctypes.c_float, (len(cylindarN), 3)) self.cylindarI = utils.convert_ctypes(range(len(cylindarV)), ctypes.c_uint, (len(cylindarV), ))
def init_shadow_matrix(self): l = list(self.lightPos) factor = math.sqrt(l[0]**2 + l[1]**2 + l[2]**2) l[0] /= factor l[1] /= factor l[2] /= factor l[3] = 0 n = (0, 0, 1, 0) dot = l[0]*n[0] + l[1]*n[1] + l[2]*n[2] + l[3]*n[3] shadowMat = [dot-l[0]*n[0],-l[1]*n[0],-l[2]*n[0],-l[3]*n[0], -l[0]*n[1],dot-l[1]*n[1],-l[2]*n[1],-l[3]*n[1], -l[0]*n[2],-l[1]*n[2],dot-l[2]*n[2],-l[3]*n[2], -l[0]*n[3],-l[1]*n[3],-l[2]*n[3],dot-l[3]*n[3]] self.shadowMat = utils.convert_ctypes( shadowMat, ctypes.c_float, (len(shadowMat), ))
def init_shadow_matrix(self): l = list(self.lightPos) factor = math.sqrt(l[0]**2 + l[1]**2 + l[2]**2) l[0] /= factor l[1] /= factor l[2] /= factor l[3] = 0 n = (0, 0, 1, 0) dot = l[0] * n[0] + l[1] * n[1] + l[2] * n[2] + l[3] * n[3] shadowMat = [ dot - l[0] * n[0], -l[1] * n[0], -l[2] * n[0], -l[3] * n[0], -l[0] * n[1], dot - l[1] * n[1], -l[2] * n[1], -l[3] * n[1], -l[0] * n[2], -l[1] * n[2], dot - l[2] * n[2], -l[3] * n[2], -l[0] * n[3], -l[1] * n[3], -l[2] * n[3], dot - l[3] * n[3] ] self.shadowMat = utils.convert_ctypes(shadowMat, ctypes.c_float, (len(shadowMat), ))