def makeNoise3D(self,size=32, c = 1, type = GL_RED): texels=[] for i in range(size): arr2 = [] for j in range(size): arr = [] for k in range(size): arr.append(random()) arr2.append(arr) texels.append(arr2) self.smoothNoise3D(size, 2, texels) for i in range(size): for j in range(size): for k in range(size): texels[i][j][k] = int(255 * texels[i][j][k]) texture = 0 glBindTexture(GL_TEXTURE_3D_EXT, texture) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R_EXT, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, c,size, size, size, 0, type, GL_UNSIGNED_BYTE, texels) return texture
def setTextures(self, program = None): if self.assigned.has_key(program): program = self.assigned[program] if program is None: program = self.active for i in range(len(program["textures"])): glActiveTextureARB(self.multiTex[i]) glBindTexture(program["textures"][i][1], program["textures"][i][2])
def __init__(self, filename): image = pygame.image.load(filename) data = pygame.image.tostring(image, 'RGBX', 1) self.__texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, int(self.__texture)) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.get_width(), image.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
def loadTex2D(self, fname, type = GL_RGB): file = os.path.join(self.workdir,fname) if os.path.exists(file): img = pygame.image.load(file) noise = pygame.image.tostring(img, "RGB") else: Log.debug("Can't load %s; generating random 2D noise instead." % fname) return self.makeNoise2D(16) texture = 0 glBindTexture(GL_TEXTURE_2D, texture) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, 1, img.get_width(), img.get_height(), 0, type, GL_UNSIGNED_BYTE, noise) return texture
def render(self, visibility = 1.0, topMost = False): try: # Save and clear both transformation matrices glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # Draw the polygon and apply texture glBindTexture(GL_TEXTURE_2D, self.animTexs[self.curFrame]) self.animList() # Restore both transformation matrices glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() except: Log.error("AnimationPlayer: Error attempting to play animation")
def loadTex3D(self, fname, type = GL_RED): file = os.path.join(self.workdir,fname) if os.path.exists(file): noise = open(file).read() size = int(len(noise)**(1/3.0)) else: Log.debug("Can't load %s; generating random 3D noise instead." % file) return self.makeNoise3D(16) #self.smoothNoise3D(size, 2, texels) #self.smoothNoise3D(size, 4, texels) texture = 0 glBindTexture(GL_TEXTURE_3D_EXT, texture) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R_EXT, GL_REPEAT) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, 1,size, size, size, 0, type, GL_UNSIGNED_BYTE, noise) return texture
def makeNoise2D(self,size=64, c = 1, type = GL_RED): texels=[] for i in range(size): texels.append([]) for j in range(size): texels[i].append(random()) self.smoothNoise(size, 2, texels) self.smoothNoise(size, 3, texels) self.smoothNoise(size, 4, texels) for i in range(size): for j in range(size): texels[i][j] = int(255 * texels[i][j]) texture = 0 glBindTexture(GL_TEXTURE_2D, texture) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, c,size, size, 0, type, GL_UNSIGNED_BYTE, texels) return texture
def bind(self, glTarget = None): """Bind this texture to self.glTarget in the current OpenGL context""" if not glTarget: glTarget = self.glTarget glBindTexture(glTarget, self.texture) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, self.texEnv)
def textureSetup(self): # Free memory if we already allocated space for textures. if len(self.animTexs) > 0: glDeleteTextures(self.animTexs) self.animTexs = glGenTextures(len(self.animImgs)) for texIdx, img in enumerate(self.animImgs): animSize = img.get_size() if img.get_alpha is None: color = "RGB" colorGL = GL_RGB else: color = "RGBA" colorGL = GL_RGBA # Log.debug("AnimationPlayer: Image %d format: %s (%dx%d)" % (texIdx, color, animSize[0], animSize[1])) glBindTexture(GL_TEXTURE_2D, self.animTexs[texIdx]) surfaceData = pygame.image.tostring(img, color, True) # Linear filtering glTexImage2D(GL_TEXTURE_2D, 0, 3, animSize[0], animSize[1], 0, colorGL, GL_UNSIGNED_BYTE, surfaceData) # MipMapping # gluBuild2DMipmaps(GL_TEXTURE_2D, colorGL, # animSize[0], animSize[1], colorGL, # GL_UNSIGNED_BYTE, surfaceData) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) Log.debug("AnimationPlayer progress: %d%%" % \ ( 100*(texIdx+1+len(self.animImgs)) / \ (2*len(self.animImgs)) )) # Resize animation (polygon) to respect resolution ratio # (The math is actually simple, take the time to draw it down if required) winRes = float(self.winWidth)/float(self.winHeight) animWidth = float(self.animImgs[0].get_size()[0]) animHeight = float(self.animImgs[0].get_size()[1]) animRes = animWidth/animHeight vtxX = 1.0 vtxY = 1.0 if winRes > animRes: r = float(self.winHeight)/animHeight vtxX = 1.0 - abs(self.winWidth-r*animWidth) / (float(self.winWidth)) elif winRes < animRes: r = float(self.winWidth)/animWidth vtxY = 1.0 - abs(self.winHeight-r*animHeight) / (float(self.winHeight)) # Vertices animVtx = array([[-vtxX, vtxY], [ vtxX, -vtxY], [ vtxX, vtxY], [-vtxX, vtxY], [-vtxX, -vtxY], [ vtxX, -vtxY]], dtype=float32) # Texture coordinates texCoord = array([[0.0, 1.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0]], dtype=float32) # Create a compiled OpenGL call list and do array-based drawing # Could have used GL_QUADS but IIRC triangles are recommended self.animList = cmglList() with self.animList: glEnable(GL_TEXTURE_2D) glColor3f(1., 1., 1.) cmglDrawArrays(GL_TRIANGLE_STRIP, vertices=animVtx, texcoords=texCoord) glDisable(GL_TEXTURE_2D)