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