root / PyOpenGL-Demo / dek / tile.py @ 2
Historique | Voir | Annoter | Télécharger (2,52 ko)
| 1 |
#!/usr/bin/python
|
|---|---|
| 2 |
|
| 3 |
# This is statement is required by the build system to query build info
|
| 4 |
if __name__ == '__build__': |
| 5 |
raise Exception |
| 6 |
|
| 7 |
|
| 8 |
## example python/pyopengl script to do tiled texturemapping.
|
| 9 |
## By david konerding (dek@cgl.ucsf.edu)
|
| 10 |
|
| 11 |
import sys |
| 12 |
from Image import * |
| 13 |
from OpenGL.GL import * |
| 14 |
from OpenGL.Tk import * |
| 15 |
##try:
|
| 16 |
## import numpy as Numeric
|
| 17 |
##except ImportError, err:
|
| 18 |
## try:
|
| 19 |
## import Numeric
|
| 20 |
## except ImportError, err:
|
| 21 |
## print "This demo requires the numpy or Numeric extension, sorry"
|
| 22 |
## import sys
|
| 23 |
## sys.exit()
|
| 24 |
import math |
| 25 |
|
| 26 |
|
| 27 |
const = math.pi |
| 28 |
class checker: |
| 29 |
|
| 30 |
def makeImage(self): |
| 31 |
im = open(self.filename) |
| 32 |
self.imageWidth = im.size[0] |
| 33 |
self.imageHeight = im.size[1] |
| 34 |
self.image = im.tostring("raw", "RGBX", 0, -1) |
| 35 |
|
| 36 |
def display(self, event=None): |
| 37 |
glClearColor(0.0, 0.0, 0.0, 0) |
| 38 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 39 |
glBegin(GL_QUADS) |
| 40 |
|
| 41 |
|
| 42 |
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0) |
| 43 |
glTexCoord2f(0.0, 2.0); glVertex3f(0.0, 10., 0.0) |
| 44 |
glTexCoord2f(2.0, 2.0); glVertex3f(10. , 10., 0.0) |
| 45 |
glTexCoord2f(2.0, 0.0); glVertex3f(10., 0.0, 0.0) |
| 46 |
|
| 47 |
glEnd() |
| 48 |
glFlush() |
| 49 |
|
| 50 |
def SetupWindow(self): |
| 51 |
self.OglFrame = Frame()
|
| 52 |
self.OglFrame.pack(side = 'top') |
| 53 |
self.QuitButton = Button(self.OglFrame, {'text':'Quit'}) |
| 54 |
self.QuitButton.bind('<ButtonRelease-1>', sys.exit) |
| 55 |
self.QuitButton.pack({'side':'top'}) |
| 56 |
self.ogl = Opengl(master=self.OglFrame, width = 500, height = 500, double = 1) |
| 57 |
self.ogl.pack(side = 'top', expand = 1, fill = 'both') |
| 58 |
## self.ogl.set_eyepoint(900.)
|
| 59 |
## self.ogl.set_centerpoint(0, 0, 0)
|
| 60 |
self.ogl.redraw = self.display |
| 61 |
|
| 62 |
|
| 63 |
def SetupTexture(self): |
| 64 |
self.makeImage()
|
| 65 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
|
| 66 |
glTexImage2D(GL_TEXTURE_2D, 0, 3, self.imageWidth, self.imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, self.image) |
| 67 |
## glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
|
| 68 |
## glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
|
| 69 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) |
| 70 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) |
| 71 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
| 72 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) |
| 73 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) |
| 74 |
glEnable(GL_TEXTURE_2D) |
| 75 |
glShadeModel(GL_FLAT) |
| 76 |
|
| 77 |
|
| 78 |
def __init__(self): |
| 79 |
try:
|
| 80 |
self.filename = sys.argv[1] |
| 81 |
except:
|
| 82 |
self.filename = "image.ppm" |
| 83 |
sys.stderr.write("usage: <name> ppmfilename\n")
|
| 84 |
#sys.exit(1)
|
| 85 |
self.SetupWindow()
|
| 86 |
self.SetupTexture()
|
| 87 |
self.ogl.mainloop()
|
| 88 |
|
| 89 |
if __name__ == '__main__': |
| 90 |
checker() |
| 91 |
|
| 92 |
|