Statistics
| Revision:

root / PyOpenGL-Demo / dek / texturesurf2.py @ 1

History | View | Annotate | Download (3.8 kB)

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