Statistics
| Revision:

root / PyOpenGL-Demo / proesch / simpleTexture / texturedQuad.py @ 1

History | View | Annotate | Download (2.5 kB)

1 1 equemene
#!/usr/bin/python2.4
2 1 equemene
#
3 1 equemene
# Load a texture from an image file and map it to a quad.
4 1 equemene
#
5 1 equemene
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
6 1 equemene
#
7 1 equemene
# This code is licensed under the PyOpenGL License.
8 1 equemene
# Details are given in the file license.txt included in this distribution.
9 1 equemene
10 1 equemene
import sys
11 1 equemene
import array
12 1 equemene
import Image
13 1 equemene
import random
14 1 equemene
15 1 equemene
try:
16 1 equemene
  from OpenGL.GLUT import *
17 1 equemene
  from OpenGL.GL import *
18 1 equemene
  from OpenGL.GLU import *
19 1 equemene
except:
20 1 equemene
  print ''' Error PyOpenGL not installed properly !!'''
21 1 equemene
  sys.exit(  )
22 1 equemene
23 1 equemene
24 1 equemene
class Texture( object ):
25 1 equemene
        """Texture either loaded from a file or initialised with random colors."""
26 1 equemene
        def __init__( self ):
27 1 equemene
                self.xSize, self.ySize = 0, 0
28 1 equemene
                self.rawRefence = None
29 1 equemene
30 1 equemene
class RandomTexture( Texture ):
31 1 equemene
        """Image with random RGB values."""
32 1 equemene
        def __init__( self, xSizeP, ySizeP ):
33 1 equemene
                self.xSize, self.ySize = xSizeP, ySizeP
34 1 equemene
                tmpList = [ random.randint(0, 255) \
35 1 equemene
                        for i in range( 3 * self.xSize * self.ySize ) ]
36 1 equemene
                self.textureArray = array.array( 'B', tmpList )
37 1 equemene
                self.rawReference = self.textureArray.tostring( )
38 1 equemene
39 1 equemene
class FileTexture( Texture ):
40 1 equemene
        """Texture loaded from a file."""
41 1 equemene
        def __init__( self, fileName ):
42 1 equemene
                im = Image.open( fileName )
43 1 equemene
                self.xSize = im.size[0]
44 1 equemene
                self.ySize = im.size[1]
45 1 equemene
                self.rawReference = im.tostring("raw", "RGB", 0, -1)
46 1 equemene
47 1 equemene
def display(  ):
48 1 equemene
        """Glut display function."""
49 1 equemene
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
50 1 equemene
        glColor3f( 1, 1, 1 )
51 1 equemene
        glBegin( GL_QUADS )
52 1 equemene
        glTexCoord2f( 0, 1 )
53 1 equemene
        glVertex3f( -0.5, 0.5, 0 )
54 1 equemene
        glTexCoord2f( 0, 0 )
55 1 equemene
        glVertex3f( -0.5, -0.5, 0 )
56 1 equemene
        glTexCoord2f( 1, 0 )
57 1 equemene
        glVertex3f( 0.5, -0.5, 0 )
58 1 equemene
        glTexCoord2f( 1, 1 )
59 1 equemene
        glVertex3f( 0.5, 0.5, 0 )
60 1 equemene
        glEnd(  )
61 1 equemene
        glutSwapBuffers (  )
62 1 equemene
63 1 equemene
def init( fileName ):
64 1 equemene
        """Glut init function."""
65 1 equemene
        try:
66 1 equemene
                texture = FileTexture( fileName )
67 1 equemene
        except:
68 1 equemene
                print 'could not open ', fileName, '; using random texture'
69 1 equemene
                texture = RandomTexture( 256, 256 )
70 1 equemene
        glClearColor ( 0, 0, 0, 0 )
71 1 equemene
        glShadeModel( GL_SMOOTH )
72 1 equemene
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
73 1 equemene
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
74 1 equemene
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
75 1 equemene
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
76 1 equemene
        glTexImage2D( GL_TEXTURE_2D, 0, 3, texture.xSize, texture.ySize, 0,
77 1 equemene
                                 GL_RGB, GL_UNSIGNED_BYTE, texture.rawReference )
78 1 equemene
        glEnable( GL_TEXTURE_2D )
79 1 equemene
80 1 equemene
glutInit( sys.argv )
81 1 equemene
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB )
82 1 equemene
glutInitWindowSize( 250, 250 )
83 1 equemene
glutInitWindowPosition( 100, 100 )
84 1 equemene
glutCreateWindow( sys.argv[0] )
85 1 equemene
if len(sys.argv) > 1:
86 1 equemene
        init( sys.argv[1] )
87 1 equemene
else:
88 1 equemene
        init( None )
89 1 equemene
glutDisplayFunc( display )
90 1 equemene
glutMainLoop(  )