Shortened URL: http://ur1.ca/e9siu

  1. #! /usr/bin/env python
  2. from OpenGL.GL import *
  3. from OpenGL.GLU import *
  4. from OpenGL.GLUT import *
  5.  
  6. from OpenGL.GL.ARB.shader_objects import *
  7. from OpenGL.GL.ARB.vertex_shader import *
  8. from OpenGL.GL.ARB.fragment_shader import *
  9.  
  10. import time, sys
  11.  
  12. program = None
  13.  
  14. def compileShader( source, shaderType ):
  15.     """Compile shader source of given type"""
  16.     shader = glCreateShaderObjectARB(shaderType)
  17.     print "glShaderSourceARB:", bool(glShaderSourceARB)
  18.     glShaderSourceARB( shader, source )
  19.     glCompileShaderARB( shader )
  20.     return shader
  21.  
  22. def compileProgram(vertexSource=None, fragmentSource=None):
  23.  
  24.     program = glCreateProgramObjectARB()
  25.     if vertexSource:
  26.         vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER_ARB)
  27.         glAttachObjectARB(program, vertexShader)
  28.     if fragmentSource:
  29.         fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER_ARB)
  30.         glAttachObjectARB(program, fragmentShader)
  31.     glValidateProgramARB( program )
  32.     glLinkProgramARB(program)
  33.     if vertexShader:
  34.         glDeleteObjectARB(vertexShader)
  35.     if fragmentShader:
  36.         glDeleteObjectARB(fragmentShader)
  37.     return program
  38.  
  39. def InitGL(Width, Height):                
  40.     glClearColor(0.0, 0.0, 0.0, 0.0)    
  41.     glClearDepth(1.0)                    
  42.     glDepthFunc(GL_LESS)              
  43.     glEnable(GL_DEPTH_TEST)              
  44.     glShadeModel(GL_SMOOTH)                
  45.     glMatrixMode(GL_PROJECTION)
  46.     glLoadIdentity()                    
  47.     gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
  48.     glMatrixMode(GL_MODELVIEW)
  49.  
  50.     if not glInitShaderObjectsARB():
  51.         print 'Missing Shader Objects!'
  52.         sys.exit(1)
  53.     if not glInitVertexShaderARB():
  54.         print 'Missing Vertex Shader!'
  55.         sys.exit(1)
  56.     if not glInitFragmentShaderARB():
  57.         print 'Missing Fragment Shader!'
  58.         sys.exit(1)
  59.  
  60.     global program
  61.     program = compileProgram(
  62. [
  63.     '''
  64.    varying vec3 normal;
  65.    void main() {
  66.        normal = gl_NormalMatrix * gl_Normal;
  67.        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  68.    }
  69.    ''',
  70. ],
  71. [
  72.     '''
  73.    varying vec3 normal;
  74.    void main() {
  75.        float intensity;
  76.        vec4 color;
  77.        vec3 n = normalize(normal);
  78.        vec3 l = normalize(gl_LightSource[0].position).xyz;
  79.  
  80.        // quantize to 5 steps (0, .25, .5, .75 and 1)
  81.        intensity = (floor(dot(l, n) * 4.0) + 1.0)/4.0;
  82.        color = vec4(intensity*1.0, intensity*0.5, intensity*0.5,
  83.            intensity*1.0);
  84.  
  85.        gl_FragColor = color;
  86.    }
  87.    ''',
  88. ]
  89. )
  90.  
  91.  
  92. def ReSizeGLScene(Width, Height):
  93.     if Height == 0:                        
  94.         Height = 1
  95.     glViewport(0, 0, Width, Height)        
  96.     glMatrixMode(GL_PROJECTION)
  97.     glLoadIdentity()
  98.     gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
  99.     glMatrixMode(GL_MODELVIEW)
  100.  
  101. def DrawGLScene():
  102.  
  103.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  104.     glLoadIdentity()                    
  105.     glTranslatef(-1.5, 0.0, -6.0)
  106.     if program:
  107.         glUseProgramObjectARB(program)
  108.     glutSolidSphere(1.0,32,32)
  109.     glTranslate( 1,0,2 )
  110.     glutSolidCube( 1.0 )
  111.     glutSwapBuffers(window)
  112.    
  113. def clear_gl_callbacks():
  114.      glutDisplayFunc(DrawGLScene)
  115.      glutMotionFunc(None)
  116.      glutKeyboardFunc(keyPressed)
  117.  
  118. def start_game_mode():
  119.      if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
  120.          return # already in game mode
  121.      glutGameModeString("1024x768:32@75")
  122.      if glutGameModeGet(GLUT_GAME_MODE_POSSIBLE):
  123.          clear_gl_callbacks()
  124.          glutEnterGameMode()
  125.          DrawGLScene()
  126.  
  127. def start_windowed_mode():
  128.      if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
  129.          clear_gl_callbacks()
  130.          glutLeaveGameMode()
  131.          DrawGLScene()
  132.          glutSetWindow(window)
  133.  
  134. def keyPressed(key, x, y):
  135.      if key == '\033':
  136.          if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
  137.              start_windowed_mode()
  138.      elif key == "f":
  139.          if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
  140.              start_windowed_mode()
  141.          else:
  142.              start_game_mode()
  143. def main():
  144.     global window
  145.     glutInit(sys.argv)
  146.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
  147.     window = glutCreateWindow("Test glut game ")
  148.     glutInitWindowPosition(100, 100)
  149.     glutInitWindowSize(1024, 768)
  150.     glutInitWindowPosition(0, 0)
  151.     glutGameModeString( "1024x768:32@75" )
  152.     glutEnterGameMode()
  153.     glutDisplayFunc(DrawGLScene)
  154.     glutIdleFunc(DrawGLScene)
  155.     glutReshapeFunc(ReSizeGLScene)
  156.     glutKeyboardFunc(keyPressed)
  157.     InitGL(1024, 768)
  158.     glutMainLoop()
  159.  
  160. if __name__ == "__main__":
  161.     print "Hit ESC key to quit."
  162.     main()
  163.  

Posted by catafest at 10 Jun 2013, 06:34:52 UTC
Language: python


Powered by Sticky Notes. Using Fedora skin.
"Sticky Notes" (the web application) is released under the BSD license, Copyright © 2012 Sayak Banerjee.

"Fedora" and the Fedora logo are trademarks of Red Hat, Inc. The Fedora project is maintained and driven by the community. This is a community maintained site. Red Hat is not responsible for content.

Sponsors | Legal | Trademark Guidelines