Statistics
| Revision:

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

History | View | Annotate | Download (2.8 kB)

1 1 equemene
#!/usr/bin/env python
2 1 equemene
# Demonstration of the shaderProgram:
3 1 equemene
# Animated torus with procedural texture.
4 1 equemene
#
5 1 equemene
# Class to simplify the incorporation of GLSL programs.
6 1 equemene
#
7 1 equemene
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
8 1 equemene
#
9 1 equemene
# This code is licensed under the PyOpenGL License.
10 1 equemene
# Details are given in the file license.txt included in this distribution.
11 1 equemene
#import OpenGL
12 1 equemene
#OpenGL.FULL_LOGGING = True
13 1 equemene
import sys
14 1 equemene
from shaderProg import ShaderProgram
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
        from OpenGL.GL.ARB.shader_objects import *
20 1 equemene
        from OpenGL.GL.ARB.fragment_shader import *
21 1 equemene
        from OpenGL.GL.ARB.vertex_shader import *
22 1 equemene
except:
23 1 equemene
        print ''' Fehler: PyOpenGL nicht intalliert !!'''
24 1 equemene
25 1 equemene
        sys.exit(  )
26 1 equemene
27 1 equemene
frameRate = 25
28 1 equemene
time = 0.0
29 1 equemene
30 1 equemene
from time import sleep
31 1 equemene
def animationStep( ):
32 1 equemene
        """Update animated parameters."""
33 1 equemene
        global frameRate
34 1 equemene
        global time
35 1 equemene
        time+=0.05
36 1 equemene
        global sP
37 1 equemene
        if sP and sP.enable():
38 1 equemene
                glUseProgramObjectARB( 1L )
39 1 equemene
                glUniform1fARB( sP.indexOfUniformVariable("Time"), time )
40 1 equemene
        sleep( 1 / float( frameRate ) )
41 1 equemene
        glutPostRedisplay( )
42 1 equemene
43 1 equemene
def display(  ):
44 1 equemene
        """Glut display function."""
45 1 equemene
        if not torusList:
46 1 equemene
                init()
47 1 equemene
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
48 1 equemene
        glMatrixMode( GL_PROJECTION )
49 1 equemene
        glLoadIdentity( )
50 1 equemene
        xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT )
51 1 equemene
        gluPerspective(60, float(xSize) / float(ySize), 0.1, 50)
52 1 equemene
        glMatrixMode( GL_MODELVIEW )
53 1 equemene
        glLoadIdentity( )
54 1 equemene
        glTranslatef( 0, 0, -4 )
55 1 equemene
        glRotatef( 130, 1, 0, 0 )
56 1 equemene
        glCallList( torusList )
57 1 equemene
        glutSwapBuffers( )
58 1 equemene
59 1 equemene
sP = None
60 1 equemene
def initShaders( ):
61 1 equemene
        """Initialise shaderProg object."""
62 1 equemene
        global sP
63 1 equemene
        sP = ShaderProgram( )
64 1 equemene
        sP.addShader( GL_VERTEX_SHADER_ARB, "brick.vert" )
65 1 equemene
        sP.addShader( GL_FRAGMENT_SHADER_ARB, "brick.frag" )
66 1 equemene
        sP.linkShaders( )
67 1 equemene
        sP.enable( )
68 1 equemene
        glUniform1fARB( sP.indexOfUniformVariable("Amplitude"), 0.3)
69 1 equemene
        glUniform3fvARB( sP.indexOfUniformVariable("LightPosition"), 1, \
70 1 equemene
                (0.0, 0.0, 3.0) )
71 1 equemene
        glUniform3fvARB( sP.indexOfUniformVariable("BrickColor"), 1, \
72 1 equemene
                (1.0, 0.3, 0.2) )
73 1 equemene
        glUniform3fvARB( sP.indexOfUniformVariable("MortarColor"), 1, \
74 1 equemene
                (0.85, 0.86, 0.84) )
75 1 equemene
        glUniform2fvARB( sP.indexOfUniformVariable("BrickSize"), 1, \
76 1 equemene
                (0.3, 0.15) )
77 1 equemene
        glUniform2fvARB( sP.indexOfUniformVariable("BrickPct"), 1, \
78 1 equemene
                (0.9, 0.85) )
79 1 equemene
80 1 equemene
torusList = None
81 1 equemene
82 1 equemene
def init(  ):
83 1 equemene
        "Glut init function."""
84 1 equemene
        glClearColor ( 0.3, 0.3, 0.3, 1 )
85 1 equemene
        glEnable( GL_DEPTH_TEST )
86 1 equemene
        glShadeModel( GL_SMOOTH )
87 1 equemene
        global torusList
88 1 equemene
        torusList = glGenLists( 1 )
89 1 equemene
        glNewList( torusList, GL_COMPILE )
90 1 equemene
        glutSolidTorus(0.5, 1, 40, 50);
91 1 equemene
        glEndList( )
92 1 equemene
        initShaders( )
93 1 equemene
94 1 equemene
def main():
95 1 equemene
96 1 equemene
        import logging
97 1 equemene
        logging.basicConfig()
98 1 equemene
        glutInit( sys.argv )
99 1 equemene
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
100 1 equemene
        glutInitWindowSize( 250, 250 )
101 1 equemene
        glutInitWindowPosition( 100, 100 )
102 1 equemene
        glutCreateWindow( sys.argv[0] )
103 1 equemene
        glutDisplayFunc( display )
104 1 equemene
        glutIdleFunc( animationStep )
105 1 equemene
        glutMainLoop(  )
106 1 equemene
107 1 equemene
if __name__ == "__main__":
108 1 equemene
        main()