root / PyOpenGL-Demo / dek / texturesurf2.py @ 2
Historique | Voir | Annoter | Télécharger (3,85 ko)
| 1 |
#!
|
|---|---|
| 2 |
|
| 3 |
# This is statement is required by the build system to query build info
|
| 4 |
if __name__ == '__build__': |
| 5 |
raise Exception |
| 6 |
|
| 7 |
## Texture mapping an image--- derived mainly from texturesurf.c (SGI demo)
|
| 8 |
## Notable feature: uses PIL to load a PPM image, which is then texturemapped
|
| 9 |
## to a bezier surface, using PyOpenGL.
|
| 10 |
|
| 11 |
## Note: your image needs to be RGBA, and the image
|
| 12 |
## dimensions must be powers of two, starting
|
| 13 |
## with 64 (ie, 64x64, 128x128, etc)
|
| 14 |
|
| 15 |
## also, I noticed that from certain directions the surface is
|
| 16 |
## partially transparent. is there a bug in my code,
|
| 17 |
## or is it the way that OpenGL handles concave surfaces?
|
| 18 |
## if you figure it out, let me know.
|
| 19 |
## (it may invlve backface culling)
|
| 20 |
|
| 21 |
## Written by David Konerding (dek@cgl.ucsf.edu)
|
| 22 |
## You are free to modify and redistribute this code.
|
| 23 |
|
| 24 |
## This version differs from texturesurf.py in that it sends the image as
|
| 25 |
## a NumPy array instead of as a string, but that's all.
|
| 26 |
|
| 27 |
|
| 28 |
import sys |
| 29 |
try:
|
| 30 |
import Numeric |
| 31 |
except:
|
| 32 |
print "This demo requires the Numeric extension, sorry" |
| 33 |
sys.exit() |
| 34 |
from Image import * |
| 35 |
from OpenGL.GL import * |
| 36 |
from OpenGL.Tk import * |
| 37 |
|
| 38 |
## Control points for the bezier surface
|
| 39 |
ctrlpoints = [[[ -1.5, -1.5, 4.0], [-0.5, -1.5, 2.0], [0.5, -1.5, |
| 40 |
-1.0], [1.5, -1.5, 2.0]], [[-1.5, -0.5, 1.0], [-0.5, -0.5, 3.0], [0.5, -0.5, |
| 41 |
0.0], [1.5, -0.5, -1.0]], [[-1.5, 0.5, 4.0], [-0.5, 0.5, 0.0],[ 0.5, 0.5, |
| 42 |
3.0], [1.5, 0.5, 4.0]], [[-1.5, 1.5, -2.0], [-0.5, 1.5, -2.0], [0.5, 1.5, |
| 43 |
0.0], [1.5, 1.5, -1.0]]] |
| 44 |
|
| 45 |
## Texture control points
|
| 46 |
texpts = [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [1.0, 1.0]]] |
| 47 |
|
| 48 |
class Surface: |
| 49 |
def Display(self, event=None): |
| 50 |
glClearColor(0.0, 0.0, 0.0, 0) |
| 51 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 52 |
glCallList(self.list)
|
| 53 |
|
| 54 |
def SetupWindow(self): |
| 55 |
self.OglFrame = Frame()
|
| 56 |
self.OglFrame.pack(side = 'top') |
| 57 |
self.QuitButton = Button(self.OglFrame, {'text':'Quit'}) |
| 58 |
self.QuitButton.bind('<ButtonRelease-1>', sys.exit) |
| 59 |
self.QuitButton.pack({'side':'top'}) |
| 60 |
|
| 61 |
def SetupOpenGL(self): |
| 62 |
self.ogl = Opengl(master=self.OglFrame, width = 500, height = 500, double = 1, depth = 1) |
| 63 |
self.ogl.pack(side = 'top', expand = 1, fill = 'both') |
| 64 |
self.ogl.set_centerpoint(0, 0, 0) |
| 65 |
self.ogl.redraw = self.Display |
| 66 |
|
| 67 |
## Note: only works for RGBA images where side length is power of 2
|
| 68 |
def MakeImage(self, filename): |
| 69 |
im = open(filename)
|
| 70 |
self.imageWidth = im.size[0] |
| 71 |
self.imageHeight = im.size[1] |
| 72 |
self.image = Numeric.fromstring(im.tostring("raw", "RGBX", 0, -1), Numeric.Int0) |
| 73 |
self.image = Numeric.reshape(self.image, (self.imageWidth, self.imageHeight, 4)) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
## this hunk of code handles the bezier mapping and texture mapping
|
| 78 |
def Surface(self): |
| 79 |
glDisable(GL_CULL_FACE) |
| 80 |
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 0, 1, ctrlpoints) |
| 81 |
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 0, 1, texpts) |
| 82 |
glEnable(GL_MAP2_TEXTURE_COORD_2) |
| 83 |
glEnable(GL_MAP2_VERTEX_3) |
| 84 |
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0) |
| 85 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) |
| 86 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) |
| 87 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) |
| 88 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
| 89 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) |
| 90 |
glTexImage2Dub(GL_TEXTURE_2D, 0, 3, 0, GL_RGBA, self.image) |
| 91 |
glEnable(GL_TEXTURE_2D) |
| 92 |
glEnable(GL_DEPTH_TEST) |
| 93 |
glEnable(GL_NORMALIZE) |
| 94 |
glShadeModel(GL_FLAT) |
| 95 |
|
| 96 |
|
| 97 |
self.list = glGenLists(1) |
| 98 |
glNewList(self.list, GL_COMPILE)
|
| 99 |
glEvalMesh2(GL_FILL, 0, 20, 0, 20) |
| 100 |
glEndList() |
| 101 |
|
| 102 |
def __init__(self): |
| 103 |
try:
|
| 104 |
filename = sys.argv[1]
|
| 105 |
except:
|
| 106 |
filename = "image.ppm"
|
| 107 |
sys.stderr.write("usage: <name> ppmfilename\n")
|
| 108 |
#sys.exit(1)
|
| 109 |
|
| 110 |
self.SetupWindow()
|
| 111 |
self.MakeImage(filename)
|
| 112 |
self.SetupOpenGL()
|
| 113 |
self.Surface()
|
| 114 |
|
| 115 |
self.ogl.tkRedraw()
|
| 116 |
self.ogl.mainloop()
|
| 117 |
|
| 118 |
if __name__ == '__main__': |
| 119 |
Surface() |
| 120 |
|