root / PyOpenGL-Demo / proesch / shaderProg / textureShader.py @ 2
Historique | Voir | Annoter | Télécharger (4,34 ko)
| 1 |
#!/usr/bin/env python
|
|---|---|
| 2 |
# Copyright (C) 2007 "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
|
| 3 |
#
|
| 4 |
# This code is licensed under the PyOpenGL License.
|
| 5 |
# Details are given in the file license.txt included in this distribution.
|
| 6 |
#import OpenGL
|
| 7 |
#OpenGL.FULL_LOGGING = True
|
| 8 |
|
| 9 |
import sys |
| 10 |
import array |
| 11 |
import Image |
| 12 |
import random |
| 13 |
from shaderProg import * |
| 14 |
|
| 15 |
try:
|
| 16 |
from OpenGL.GLUT import * |
| 17 |
from OpenGL.GL import * |
| 18 |
from OpenGL.GLU import * |
| 19 |
from OpenGL.GL.ARB.shader_objects import * |
| 20 |
from OpenGL.GL.ARB.fragment_shader import * |
| 21 |
from OpenGL.GL.ARB.vertex_shader import * |
| 22 |
except:
|
| 23 |
print ''' Error PyOpenGL not installed properly !!''' |
| 24 |
sys.exit( ) |
| 25 |
|
| 26 |
|
| 27 |
class Texture( object ): |
| 28 |
def __init__( self ): |
| 29 |
self.xSize, self.ySize = 0, 0 |
| 30 |
self.rawRefence = None |
| 31 |
|
| 32 |
class RandomTexture( Texture ): |
| 33 |
def __init__( self, xSizeP, ySizeP ): |
| 34 |
self.xSize, self.ySize = xSizeP, ySizeP |
| 35 |
tmpList = [ random.randint(0, 255) \ |
| 36 |
for i in range( 3 * self.xSize * self.ySize ) ] |
| 37 |
self.textureArray = array.array( 'B', tmpList ) |
| 38 |
self.rawReference = self.textureArray.tostring( ) |
| 39 |
|
| 40 |
class FileTexture( Texture ): |
| 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 |
frameRate = 30
|
| 48 |
from time import sleep |
| 49 |
def animationStep( *args ): |
| 50 |
global frameRate
|
| 51 |
global sP
|
| 52 |
if not quadList: |
| 53 |
if len(sys.argv) > 1: |
| 54 |
init( sys.argv[1] )
|
| 55 |
else:
|
| 56 |
init( None )
|
| 57 |
assert quadList
|
| 58 |
if sP.isEnabled():
|
| 59 |
global rgbTransformMatrix
|
| 60 |
row = random.randint( 0, 3 ) |
| 61 |
column = random.randint( 0, 3 ) |
| 62 |
rgbTransformMatrix[row*4+column] += random.normalvariate( 0, 0.05 ) |
| 63 |
if rgbTransformMatrix[row*4+column] < 0: |
| 64 |
rgbTransformMatrix[row*4+column] = 0 |
| 65 |
sum=0.
|
| 66 |
for x in rgbTransformMatrix[ row*4:row*4+4 ]: |
| 67 |
sum+=x
|
| 68 |
for i in range( row*4,row*4+4 ): |
| 69 |
rgbTransformMatrix[i] /= sum
|
| 70 |
glUniformMatrix4fvARB(sP.indexOfUniformVariable("RGBTransformationMatrix"),\
|
| 71 |
1, False, rgbTransformMatrix) |
| 72 |
sleep( 1 / float( frameRate ) ) |
| 73 |
glutPostRedisplay( ) |
| 74 |
|
| 75 |
def display( *args ): |
| 76 |
if not quadList: |
| 77 |
if len(sys.argv) > 1: |
| 78 |
init( sys.argv[1] )
|
| 79 |
else:
|
| 80 |
init( None )
|
| 81 |
assert quadList
|
| 82 |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) |
| 83 |
glColor3f( 1, 1, 1 ) |
| 84 |
glMatrixMode( GL_PROJECTION ) |
| 85 |
glLoadIdentity( ) |
| 86 |
xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) |
| 87 |
f = float(ySize) / float(xSize) |
| 88 |
gluOrtho2D( -1.5, 1.5, -1.5*f, 1.5*f) |
| 89 |
glMatrixMode( GL_MODELVIEW ) |
| 90 |
glLoadIdentity( ) |
| 91 |
glPushMatrix( ) |
| 92 |
glTranslatef( -0.6, 0, 0 ) |
| 93 |
sP.disable( ) |
| 94 |
glCallList( quadList ) |
| 95 |
glPopMatrix( ) |
| 96 |
glTranslatef( 0.6, 0, 0) |
| 97 |
sP.enable( ) |
| 98 |
glCallList( quadList ) |
| 99 |
glutSwapBuffers ( ) |
| 100 |
|
| 101 |
sP = None
|
| 102 |
def initShaders( ): |
| 103 |
global sP
|
| 104 |
sP = ShaderProgram( ) |
| 105 |
sP.addShader( GL_FRAGMENT_SHADER_ARB, "rgbMorph.frag" )
|
| 106 |
sP.linkShaders( ) |
| 107 |
sP.enable( ) |
| 108 |
global rgbTransformMatrix
|
| 109 |
glUniformMatrix4fvARB(sP.indexOfUniformVariable("RGBTransformationMatrix"), \
|
| 110 |
1, False, rgbTransformMatrix) |
| 111 |
|
| 112 |
quadList = None
|
| 113 |
rgbTransformMatrix = [ 0.0 for i in range( 16 ) ] |
| 114 |
rgbTransformMatrix[0] = rgbTransformMatrix[5] = rgbTransformMatrix[10] \ |
| 115 |
= rgbTransformMatrix[15] = 1.0 |
| 116 |
def init( fileName ): |
| 117 |
try:
|
| 118 |
texture = FileTexture( fileName ) |
| 119 |
except:
|
| 120 |
print 'could not open ', fileName, '; using random texture' |
| 121 |
texture = RandomTexture( 256, 256 ) |
| 122 |
glClearColor ( 0, 0, 0, 0 ) |
| 123 |
glShadeModel( GL_SMOOTH ) |
| 124 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) |
| 125 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) |
| 126 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) |
| 127 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) |
| 128 |
glTexImage2D( GL_TEXTURE_2D, 0, 3, texture.xSize, texture.ySize, 0, |
| 129 |
GL_RGB, GL_UNSIGNED_BYTE, texture.rawReference ) |
| 130 |
glEnable( GL_TEXTURE_2D ) |
| 131 |
global quadList
|
| 132 |
quadList = glGenLists( 1 )
|
| 133 |
glNewList( quadList, GL_COMPILE ) |
| 134 |
glBegin( GL_QUADS ) |
| 135 |
glTexCoord2f( 0, 1 ) |
| 136 |
glVertex3f( -0.5, 0.5, 0 ) |
| 137 |
glTexCoord2f( 0, 0 ) |
| 138 |
glVertex3f( -0.5, -0.5, 0 ) |
| 139 |
glTexCoord2f( 1, 0 ) |
| 140 |
glVertex3f( 0.5, -0.5, 0 ) |
| 141 |
glTexCoord2f( 1, 1 ) |
| 142 |
glVertex3f( 0.5, 0.5, 0 ) |
| 143 |
glEnd( ) |
| 144 |
glEndList( ) |
| 145 |
initShaders( ) |
| 146 |
|
| 147 |
def main(): |
| 148 |
import logging |
| 149 |
logging.basicConfig() |
| 150 |
glutInit( sys.argv ) |
| 151 |
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ) |
| 152 |
glutInitWindowSize( 250, 250 ) |
| 153 |
glutInitWindowPosition( 100, 100 ) |
| 154 |
glutCreateWindow( sys.argv[0] )
|
| 155 |
glutDisplayFunc( display ) |
| 156 |
glutIdleFunc( animationStep ) |
| 157 |
glutMainLoop( ) |
| 158 |
|
| 159 |
if __name__ == "__main__": |
| 160 |
main() |
| 161 |
|