Pyopengl Shader

pyopengl-shader.png
#! /usr/bin/env python
'''Tests rendering using the ARB shader objects extension...
'''
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
 
from OpenGL.GL.ARB.shader_objects import *
from OpenGL.GL.ARB.vertex_shader import *
from OpenGL.GL.ARB.fragment_shader import *
 
import time, sys
 
program = None
 
def compileShader( source, shaderType ):
    """Compile shader source of given type"""
    shader = glCreateShaderObjectARB(shaderType)
    print "glShaderSourceARB:", bool(glShaderSourceARB)
    glShaderSourceARB( shader, source )
    glCompileShaderARB( shader )
    return shader
 
def compileProgram(vertexSource=None, fragmentSource=None):
    program = glCreateProgramObjectARB()
 
    if vertexSource:
        vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER_ARB)
        glAttachObjectARB(program, vertexShader)
    if fragmentSource:
        fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER_ARB)
        glAttachObjectARB(program, fragmentShader)
 
    glValidateProgramARB( program )
    glLinkProgramARB(program)
 
    if vertexShader:
        glDeleteObjectARB(vertexShader)
    if fragmentShader:
        glDeleteObjectARB(fragmentShader)
 
    return program
 
def init(Width, Height):
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
 
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
 
    glMatrixMode(GL_MODELVIEW)
 
    if not glInitShaderObjectsARB():
        print 'Missing Shader Objects!'
        sys.exit(1)
    if not glInitVertexShaderARB():
        print 'Missing Vertex Shader!'
        sys.exit(1)
    if not glInitFragmentShaderARB():
        print 'Missing Fragment Shader!'
        sys.exit(1)
 
    global program
    program = compileProgram(
[
    '''
    varying vec3 normal;
    void main() {
        normal = gl_NormalMatrix * gl_Normal;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }
    ''',
], 
[
    '''
    varying vec3 normal;
    void main() {
        float intensity;
        vec4 color;
        vec3 n = normalize(normal);
        vec3 l = normalize(gl_LightSource[0].position).xyz;
 
        // quantize to 5 steps (0, .25, .5, .75 and 1)
        intensity = (floor(dot(l, n) * 4.0) + 1.0)/4.0;
        color = vec4(intensity*1.0, intensity*0.5, intensity*0.5,
            intensity*1.0);
 
        gl_FragColor = color;
    }
    ''',
]
)
 
def resize(Width, Height):
    if Height == 0:                        # 0割り防止
        Height = 1
 
    glViewport(0, 0, Width, Height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
 
# The main drawing function. 
def draw():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
 
    glTranslatef(-1.5, 0.0, -6.0)
 
    if program:
        glUseProgramObjectARB(program)
    glutSolidSphere(1.0,32,32)
    glTranslate( 1,0,2 )
    glutSolidCube( 1.0 )
 
    glutSwapBuffers()
 
def key(*args):
    if args[0] == '\x1b':#ESCキー
        sys.exit()
 
def main():
    global window
    glutInit(sys.argv)
 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(640, 480)
    glutInitWindowPosition(0, 0)
    window = glutCreateWindow("pyOpenGL")
 
    glutDisplayFunc(draw)
    glutIdleFunc(draw)
    glutReshapeFunc(resize)
    glutKeyboardFunc(key)
    init(640, 480)
    glutMainLoop()
 
if __name__ == "__main__":
    print "Hit ESC key to quit."
    main()

サポートサイト Wikidot.com