Statistics
| Revision:

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

History | View | Annotate | Download (3.7 kB)

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

    
25

    
26
import sys
27
from Image import *
28
from OpenGL.GL import *
29
from OpenGL.Tk import *
30

    
31

    
32
## Control points for the bezier surface
33
ctrlpoints = [[[ -1.5, -1.5, 4.0], [-0.5, -1.5, 2.0], [0.5, -1.5,
34
        -1.0], [1.5, -1.5, 2.0]], [[-1.5, -0.5, 1.0], [-0.5, -0.5, 3.0], [0.5, -0.5,
35
        0.0], [1.5, -0.5, -1.0]], [[-1.5, 0.5, 4.0], [-0.5, 0.5, 0.0], [0.5, 0.5,
36
        3.0], [1.5, 0.5, 4.0]], [[-1.5, 1.5, -2.0], [-0.5, 1.5, -2.0], [0.5, 1.5,
37
        0.0], [1.5, 1.5, -1.0]]]
38

    
39
## Texture control points
40
texpts = [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [1.0, 1.0]]]
41

    
42
class Surface:
43
        def Display(self, event=None):
44
                glClearColor(0.0, 0.0, 0.0, 0)
45
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
46
                glCallList(self.list)
47

    
48
        def SetupWindow(self):
49
                self.OglFrame = Frame()
50
                self.OglFrame.pack(side = 'top')
51
                self.QuitButton = Button(self.OglFrame, {'text':'Quit'})
52
                self.QuitButton.bind('<ButtonRelease-1>', sys.exit)
53
                self.QuitButton.pack({'side':'top'})
54

    
55
        def SetupOpenGL(self):
56
                self.ogl = Opengl(master=self.OglFrame, width = 500, height = 500, double = 1, depth = 1)
57
                self.ogl.pack(side = 'top', expand = 1, fill = 'both')
58
                self.ogl.set_centerpoint(0, 0, 0)
59
                self.ogl.redraw = self.Display
60

    
61
## Note: only works for RGBA images where side length is power of 2
62
        def MakeImage(self, filename):
63
                im = open(filename)
64
                self.imageWidth = im.size[0]
65
                self.imageHeight = im.size[1]
66
                self.image = im.tostring("raw", "RGBX", 0, -1)
67
#                print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image)
68

    
69

    
70
## this hunk of code handles the bezier mapping and texture mapping
71
        def Surface(self):
72
                glDisable(GL_CULL_FACE)
73
                glMap2f(GL_MAP2_VERTEX_3, 0, 1, 0, 1, ctrlpoints)
74
                glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 0, 1, texpts)
75
                glEnable(GL_MAP2_TEXTURE_COORD_2)
76
                glEnable(GL_MAP2_VERTEX_3)
77
                glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0)
78
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
79
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
80
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
81
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
82
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
83
                print type(self.image)
84
                glTexImage2D(GL_TEXTURE_2D, 0, 3, self.imageWidth, self.imageHeight, 0,
85
                                         GL_RGBA, GL_UNSIGNED_BYTE, self.image)
86
                glEnable(GL_TEXTURE_2D)
87
                glEnable(GL_DEPTH_TEST)
88
                glEnable(GL_NORMALIZE)
89
                glShadeModel(GL_FLAT)
90

    
91

    
92
                self.list = glGenLists(1)
93
                glNewList(self.list, GL_COMPILE)
94
                glEvalMesh2(GL_FILL, 0, 20, 0, 20)
95
                glEndList()
96

    
97
        def __init__(self):
98
                try:
99
                        filename = sys.argv[1]
100
                except:
101
                        filename = "image.ppm"
102
                        sys.stderr.write("usage: <name> ppmfilename\n")
103
                        #sys.exit(1)
104

    
105
                self.SetupWindow()
106
                self.MakeImage(filename)
107
                self.SetupOpenGL()
108
                self.Surface()
109

    
110
                self.ogl.tkRedraw()
111
                self.ogl.mainloop()
112

    
113
if __name__ == '__main__':
114
        Surface()
115
                
116
demo = Surface