码迷,mamicode.com
首页 > 其他好文 > 详细

pyglet and opengl -- 纹理映射以及动画

时间:2015-06-28 14:12:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

  1 #-*- coding:gbk -*-
  2 #from pyglet.gl import *
  3 from OpenGL.GL import *
  4 import pyglet
  5 from pyglet import clock
  6 from PIL import Image
  7 from ctypes import *
  8 
  9 def draw_rect(x, y,z,width, height,ang):
 10     """
 11     实际矩形代码
 12     """
 13     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 14     glPushMatrix()
 15     glLoadIdentity()
 16     glTranslatef(x+width/2,y+height/2,0.0)
 17     glRotatef(ang,0.0,1.0,0.0)
 18     glBegin(GL_QUADS)
 19     glTexCoord2f(0.0,0.0)  #纹理坐标
 20     glVertex3f(x-width/2, y-height/2,z)
 21     glTexCoord2f(1.0,0.0)
 22     glVertex3f(x + width/2, y-height/2,z)
 23     glTexCoord2f(1.0,1.0)
 24     glVertex3f(x + width/2, y + height/2,z)
 25     glTexCoord2f(0.0,1.0)
 26     glVertex3f(x-width/2, y + height/2,z)
 27     glEnd()
 28     glPopMatrix()
 29     
 30 
 31 class Button():
 32     x=y=z=0
 33     width=height=10.0
 34     ang=0
 35     TEXTUREWIDTH=100
 36     TEXTUREHEIGHT=100
 37     image_data=[] #保存纹理数据
 38     def draw(self):
 39         """
 40         画矩形
 41         """
 42         draw_rect(self.x,self.y,self.z,self.width,self.height,self.ang)
 43 
 44     def loadTexture(self):
 45         """
 46         载入纹理
 47         """
 48         #创建纹理对象
 49         texid=glGenTextures(1)        #绑定纹理
 50         glBindTexture(GL_TEXTURE_2D,texid)
 51         glPixelStorei(GL_UNPACK_ALIGNMENT,1)
 52         #开启纹理功能
 53         glEnable(GL_TEXTURE_2D)
 54         #说明映射方式
 55         glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL)
 56         #设置滤波方式
 57         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
 58         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
 59         #纹理图
 60         glTexImage2D(GL_TEXTURE_2D,0,3,self.TEXTUREWIDTH,self.TEXTUREHEIGHT,0,GL_RGBA,GL_UNSIGNED_BYTE,self.image_data)
 61 
 62 
 63     def load_image(self,imagePath):
 64         """
 65         读取图像的数据
 66         """
 67         imageData=Image.open(imagePath)
 68         try:
 69             imageData=imageData.convert(RGB)
 70             self.TEXTUREWIDTH,self.TEXTUREHEIGHT,self.image_data=imageData.size[0],imageData.size[1],imageData.tostring(raw,RGBA,0,-1)
 71         except SystemError:
 72             self.TEXTUREWIDTH,self.TEXTUREHEIGHT,self.image_data=imageData.size[0],imageData.size[1],imageData.tostring(raw,RGBX,0,-1)
 73         assert self.TEXTUREWIDTH*self.TEXTUREHEIGHT*4 == len(self.image_data)
 74 
 75 
 76 
 77         
 78 class MyWindow(pyglet.window.Window):
 79     def __init__(self):
 80         super(MyWindow,self).__init__()
 81         #按钮
 82         self.button=Button()
 83         self.button.x=10
 84         self.button.y=10
 85         self.button.z=0
 86         self.button.width=self.width/2
 87         self.button.height=self.height/2
 88         self.button.ang=0
 89 
 90         #所有要画的图元
 91         self.need_draw=[
 92                 self.button,
 93                             ]
 94     def on_draw(self):
 95         """
 96         画图,刷新
 97         """
 98         self.clear()
 99         for draw_object in self.need_draw:
100             draw_object.draw()
101     def value_change(self,dt):
102         self.button.x+=10.0
103         
104 
105     
106         
107 
108 if __name__ == "__main__":
109     wn=MyWindow()
110     wn.button.load_image(tex.jpg)
111     wn.button.loadTexture()
112     clock.schedule_interval(wn.value_change,0.5)
113 pyglet.app.run()

 

pyglet and opengl -- 纹理映射以及动画

标签:

原文地址:http://www.cnblogs.com/yinwei-space/p/4605438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!