Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: Python  |  size: 2.95 KB  |  views: 65  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import pygame
  2. import numpy
  3. import ctypes
  4. import sys
  5.  
  6. from OpenGL.GL import *
  7. from OpenGL.GLU import *
  8.  
  9. pygame.display.init()
  10.  
  11. pygame.display.set_mode((640, 480), pygame.OPENGL|pygame.DOUBLEBUF)
  12.  
  13. glViewport(0, 0, 640, 480)
  14. glClearColor(0.0, 0.0, 0.0, 1.0)
  15.  
  16. triangle_verticies = numpy.array([-20.0, -20.0, 0.0,
  17.                                    20.0, -20.0, 0.0,
  18.                                    0.0, 20.0, 0.0], dtype=numpy.float32)
  19. projection_matrix = numpy.array([1.0, 0.0, 0.0, 0.0,
  20.                                  0.0, 1.0, 0.0, 0.0,
  21.                                  0.0, 0.0, 0.0, 0.0,
  22.                                  0.0, 0.0, 0.0, 1.0], dtype=numpy.float32)
  23. view_matrix = numpy.array([1.0, 0.0, 0.0, 320.0,
  24.                            0.0, 1.0, 0.0, 240.0,
  25.                            0.0, 0.0, 1.0, 0.0,
  26.                            0.0, 0.0, 0.0, 1.0], dtype=numpy.float32)
  27. mvp_matrix = projection_matrix * view_matrix
  28.  
  29. triangle_buffer_handle = glGenBuffers(1)
  30. glBindBuffer(GL_ARRAY_BUFFER, triangle_buffer_handle)
  31. glBufferData(GL_ARRAY_BUFFER, triangle_verticies, GL_STATIC_DRAW)
  32.  
  33. v_shader_source = "attribute vec4 v_position;\n" +\
  34.                   "uniform mat4 uMVPMatrix;\n" +\
  35.                   "void main()\n" +\
  36.                   "{\n"+\
  37.                   " gl_Position = uMVPMatrix * v_position;\n" +\
  38.                   "}\n"
  39.  
  40. f_shader_source = "void main()\n" +\
  41.                   "{\n" +\
  42.                   " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" +\
  43.                   "}\n"
  44.  
  45. v_shader_handle = glCreateShader(GL_VERTEX_SHADER)
  46. glShaderSource(v_shader_handle, v_shader_source)
  47. glCompileShader(v_shader_handle)
  48.  
  49. vs_compile_status = ctypes.c_int(-24)
  50. glGetShaderiv(v_shader_handle, GL_COMPILE_STATUS, vs_compile_status)
  51. if vs_compile_status.value == 0:
  52.     print glGetShaderInfoLog(v_shader_handle)
  53.     sys.exit(0)
  54.  
  55. f_shader_handle = glCreateShader(GL_FRAGMENT_SHADER)
  56. glShaderSource(f_shader_handle, f_shader_source)
  57. glCompileShader(f_shader_handle)
  58.  
  59. s_program_handle = glCreateProgram()
  60. glAttachShader(s_program_handle, v_shader_handle)
  61. glAttachShader(s_program_handle, f_shader_handle)
  62. glLinkProgram(s_program_handle)
  63.  
  64. link_status = ctypes.c_int(-24)
  65. glGetProgramiv(s_program_handle, GL_LINK_STATUS, link_status)
  66.  
  67. glUseProgram(s_program_handle)
  68.  
  69. v_position_handle = glGetAttribLocation(s_program_handle, "v_position")
  70. mvp_matrix_handle = glGetUniformLocation(s_program_handle, "uMVPMatrix")
  71.  
  72. clock = pygame.time.Clock()
  73. running = True
  74. while running:
  75.     for event in pygame.event.get():
  76.         if event.type == pygame.QUIT:
  77.             running = False
  78.     clock.tick(40)
  79.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  80.     glVertexAttribPointer(v_position_handle, 3, GL_FLOAT, GL_FALSE, 0,
  81.             triangle_verticies)
  82.     glEnableVertexAttribArray(v_position_handle)
  83.  
  84.     glUniformMatrix4fv(mvp_matrix_handle, 1, GL_FALSE, mvp_matrix)
  85.     glDrawArrays(GL_TRIANGLES, 0, 3)
  86.     pygame.display.flip()
clone this paste RAW Paste Data