Python‎ > ‎

Vectorized Particle System and Geometry Shaders

Source available for download below:



001from OpenGL.GL import *
002from OpenGL.GLU import *
003from OpenGL.GLUT import *
004from OpenGL import GL as gl
005import random
006import time
007import numpy
008import math
009 
010#These three defines exist in OpenGL.GL, but does not correspond to those used here
011GL_GEOMETRY_SHADER_EXT       = 0x8DD9
012GL_GEOMETRY_INPUT_TYPE_EXT   = 0x8DDB
013GL_GEOMETRY_OUTPUT_TYPE_EXT  = 0x8DDC
014GL_GEOMETRY_VERTICES_OUT_EXT = 0x8DDA
015 
016# 1:
017# Define the missing glProgramParameteri method
018_glProgramParameteri = None
019def glProgramParameteri( program, pname, value  ):
020    global _glProgramParameteri
021    if not _glProgramParameteri:
022        import ctypes
023        # Open the opengl32.dll
024        gldll = ctypes.windll.opengl32
025        # define a function pointer prototype of *(GLuint program, GLenum pname, GLint value)
026        prototype = ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_int )
027        # Get the win gl func adress
028        fptr = gldll.wglGetProcAddress( 'glProgramParameteriEXT' )
029        if fptr==0:
030            raise Exception( "wglGetProcAddress('glProgramParameteriEXT') returned a zero adress, which will result in a nullpointer error if used.")
031        _glProgramParameteri = prototype( fptr )
032    _glProgramParameteri( program, pname, value )
033 
034# 2:
035# Define the missing glCreateShaderObject method
036_glCreateShaderObject = None
037def glCreateShaderObject( shadertype ):
038    global _glCreateShaderObject
039    if not _glCreateShaderObject:
040        import ctypes
041        # Open the opengl32.dll
042        gldll = ctypes.windll.opengl32
043        # define a function pointer prototype of *(GLuint program, GLenum pname, GLint value)
044        prototype = ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_uint )
045        # Get the win gl func adress
046        fptr = gldll.wglGetProcAddress( 'glCreateShaderObjectARB' )
047        if fptr==0:
048            raise Exception( "wglGetProcAddress('glCreateShaderObjectARB') returned a zero adress, which will result in a nullpointer error if used.")
049        _glCreateShaderObject = prototype( fptr )
050    return _glCreateShaderObject( shadertype )
051 
052vert = '''
053void main(){
054    gl_FrontColor = gl_Color;
055    gl_Position = ftransform();
056}
057'''
058 
059geom = '''#version 120
060#extension GL_EXT_geometry_shader4 : enable
061const float radius = 0.5;
062varying out vec2 coord;
063 
064void main()
065{
066  gl_FrontColor = gl_FrontColorIn[0];
067 
068  coord = vec2( -1,-1 );
069  gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,-radius,0,0) );
070  EmitVertex();
071  coord = vec2( -1,1 );
072  gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,radius, 0,0) );
073  EmitVertex();
074  coord = vec2( 1,-1 );
075  gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,-radius, 0,0) );
076  EmitVertex();
077  coord = vec2( 1,1 );
078  gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,radius, 0,0) );
079  EmitVertex(); 
080  EndPrimitive();
081}
082'''
083frag = '''
084varying in vec2 coord;
085void main(){
086    vec4 color = gl_Color;
087    color.a = 1.0-length( coord );
088    if (color.a<0.0) discard;
089 
090    // A VERY FAKE "lighting" model
091    float d = dot( normalize(vec3(coord,1.0)), vec3( 0.19, 0.19, 0.96225 ) );
092    color.rgb *= d*d;
093    // end "lighting"
094     
095    gl_FragColor = color;
096}
097'''
098 
099theta = 0.0
100delta = 0.0
101TIMEOUTFACTOR = 5.0
102 
103def my_idle( ):
104    global theta
105    global delta
106    = time.clock()
107    passed = t-delta
108    theta += passed
109    delta = t
110 
111    dirvector = numpy.array( [ -1-(math.sin( theta/2.0 )), -1-(math.cos(theta/