1 #-*-CODING:GBK-*-2 #From pyglet.gl Import *3 fromOpengl.glImport*4 ImportPyglet5 fromPygletImportClock6 fromPILImportImage7 fromcTYPESImport*8 9 defdraw_rect (x, Y,z,width, Height,ang):Ten """ One Actual Rectangle Code A """ -Glclear (Gl_color_buffer_bit |gl_depth_buffer_bit) - Glpushmatrix () the glloadidentity () -Gltranslatef (x+width/2,y+height/2,0.0) -Glrotatef (ang,0.0,1.0,0.0) - Glbegin (gl_quads) +GLTEXCOORD2F (0.0,0.0)#Texture coordinates -glvertex3f (X-WIDTH/2, Y-HEIGHT/2, Z) +GLTEXCOORD2F (1.0,0.0) Aglvertex3f (x + WIDTH/2, Y-HEIGHT/2, Z) atGLTEXCOORD2F (1.0,1.0) -glvertex3f (x + width/2, y + height/2, Z) -GLTEXCOORD2F (0.0,1.0) -glvertex3f (X-WIDTH/2, y + height/2, Z) - glend () - Glpopmatrix () in - to classButton (): +x=y=z=0 -width=height=10.0 theang=0 *texturewidth=100 $textureheight=100Panax NotoginsengImage_data=[]#Saving Texture Data - defDraw (self): the """ + Draw a rectangle A """ the Draw_rect (Self.x,self.y,self.z,self.width,self.height,self.ang) + - defloadtexture (self): $ """ $ Loading Textures - """ - #Create a Texture object theTexid=glgentextures (1)#Binding Textures - glbindtexture (GL_TEXTURE_2D,TEXID)WuyiGlpixelstorei (gl_unpack_alignment,1) the #turn on texture function - glenable (gl_texture_2d) Wu #Description Mapping Method - GLTEXENVF (gl_texture_env,gl_texture_env_mode,gl_decal) About #Set Filter Mode $ Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_nearest) - Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_nearest) - #Texture Map -Glteximage2d (gl_texture_2d,0,3, self. Texturewidth,self. Textureheight,0,gl_rgba,gl_unsigned_byte,self.image_data) A + the defload_image (self,imagepath): - """ $ reading data from an image the """ theImagedata=Image.open (ImagePath) the Try: theImagedata=imagedata.convert ('RGB') -Self. Texturewidth,self. Textureheight,self.image_data=imagedata.size[0],imagedata.size[1],imagedata.tostring ('Raw','RGBA', 0,-1) in exceptSystemerror: theSelf. Texturewidth,self. Textureheight,self.image_data=imagedata.size[0],imagedata.size[1],imagedata.tostring ('Raw','RGBX', 0,-1) the assertSelf. Texturewidth*self. Textureheight*4 = =Len (self.image_data) About the the the + classMywindow (Pyglet.window.Window): - def __init__(self): theSuper (Mywindow,self).__init__()Bayi #Button theself.button=Button () theself.button.x=10 -self.button.y=10 -self.button.z=0 theSelf.button.width=self.width/2 theSelf.button.height=self.height/2 theself.button.ang=0 the - #all the elements to be drawn theself.need_draw=[ the Self.button, the ]94 defOn_draw (self): the """ the Paint, Refresh the """98 self.clear () About forDraw_objectinchSelf.need_draw: - Draw_object.draw ()101 defValue_change (SELF,DT):102self.button.x+=10.0103 104 the 106 107 108 if __name__=="__main__":109wn=Mywindow () theWn.button.load_image ('tex.jpg')111 wn.button.loadTexture () theClock.schedule_interval (wn.value_change,0.5)113Pyglet.app.run ()
Pyglet and OpenGL--texture mapping and animation