Statistics
| Revision:

root / PyOpenGL-Demo / proesch / shaderProg / torusShader.py @ 1

History | View | Annotate | Download (2.8 kB)

1
#!/usr/bin/env python
2
# Demonstration of the shaderProgram:
3
# Animated torus with procedural texture.
4
#
5
# Class to simplify the incorporation of GLSL programs.
6
# 
7
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
8
#
9
# This code is licensed under the PyOpenGL License.
10
# Details are given in the file license.txt included in this distribution.
11
#import OpenGL
12
#OpenGL.FULL_LOGGING = True
13
import sys
14
from shaderProg import ShaderProgram
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 ''' Fehler: PyOpenGL nicht intalliert !!'''
24

    
25
        sys.exit(  )
26

    
27
frameRate = 25
28
time = 0.0
29

    
30
from time import sleep
31
def animationStep( ):
32
        """Update animated parameters."""
33
        global frameRate
34
        global time
35
        time+=0.05
36
        global sP
37
        if sP and sP.enable():
38
                glUseProgramObjectARB( 1L )
39
                glUniform1fARB( sP.indexOfUniformVariable("Time"), time )
40
        sleep( 1 / float( frameRate ) )
41
        glutPostRedisplay( )
42

    
43
def display(  ):
44
        """Glut display function."""
45
        if not torusList:
46
                init()
47
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
48
        glMatrixMode( GL_PROJECTION )
49
        glLoadIdentity( )
50
        xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT )
51
        gluPerspective(60, float(xSize) / float(ySize), 0.1, 50)
52
        glMatrixMode( GL_MODELVIEW )
53
        glLoadIdentity( )
54
        glTranslatef( 0, 0, -4 )
55
        glRotatef( 130, 1, 0, 0 )
56
        glCallList( torusList )
57
        glutSwapBuffers( )
58

    
59
sP = None
60
def initShaders( ):
61
        """Initialise shaderProg object."""
62
        global sP
63
        sP = ShaderProgram( )
64
        sP.addShader( GL_VERTEX_SHADER_ARB, "brick.vert" )
65
        sP.addShader( GL_FRAGMENT_SHADER_ARB, "brick.frag" )
66
        sP.linkShaders( )
67
        sP.enable( )
68
        glUniform1fARB( sP.indexOfUniformVariable("Amplitude"), 0.3)
69
        glUniform3fvARB( sP.indexOfUniformVariable("LightPosition"), 1, \
70
                (0.0, 0.0, 3.0) )
71
        glUniform3fvARB( sP.indexOfUniformVariable("BrickColor"), 1, \
72
                (1.0, 0.3, 0.2) )
73
        glUniform3fvARB( sP.indexOfUniformVariable("MortarColor"), 1, \
74
                (0.85, 0.86, 0.84) )
75
        glUniform2fvARB( sP.indexOfUniformVariable("BrickSize"), 1, \
76
                (0.3, 0.15) )
77
        glUniform2fvARB( sP.indexOfUniformVariable("BrickPct"), 1, \
78
                (0.9, 0.85) )
79

    
80
torusList = None
81

    
82
def init(  ):
83
        "Glut init function."""
84
        glClearColor ( 0.3, 0.3, 0.3, 1 )
85
        glEnable( GL_DEPTH_TEST )
86
        glShadeModel( GL_SMOOTH )
87
        global torusList
88
        torusList = glGenLists( 1 )
89
        glNewList( torusList, GL_COMPILE )
90
        glutSolidTorus(0.5, 1, 40, 50);
91
        glEndList( )
92
        initShaders( )
93

    
94
def main():
95

    
96
        import logging
97
        logging.basicConfig()
98
        glutInit( sys.argv )
99
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
100
        glutInitWindowSize( 250, 250 )
101
        glutInitWindowPosition( 100, 100 )
102
        glutCreateWindow( sys.argv[0] )
103
        glutDisplayFunc( display )
104
        glutIdleFunc( animationStep )
105
        glutMainLoop(  )
106

    
107
if __name__ == "__main__":
108
        main()
109