Advertisement
trishLEX

Untitled

May 29th, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import OpenGL.GL.shaders
  4. import numpy
  5. import pyrr
  6.  
  7.  
  8. def main():
  9.  
  10.     # initialize glfw
  11.     if not glfw.init():
  12.         return
  13.  
  14.     window = glfw.create_window(800, 600, "My OpenGL window", None, None)
  15.  
  16.     if not window:
  17.         glfw.terminate()
  18.         return
  19.  
  20.     glfw.make_context_current(window)
  21.     #        positions        colors
  22.     cube = [-0.5, -0.5,  0.5, 1.0, 0.0, 0.0,
  23.              0.5, -0.5,  0.5, 0.0, 1.0, 0.0,
  24.              0.5,  0.5,  0.5, 0.0, 0.0, 1.0,
  25.             -0.5,  0.5,  0.5, 1.0, 1.0, 1.0,
  26.  
  27.             -0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
  28.              0.5, -0.5, -0.5, 0.0, 1.0, 0.0,
  29.              0.5,  0.5, -0.5, 0.0, 0.0, 1.0,
  30.             -0.5,  0.5, -0.5, 1.0, 1.0, 1.0]
  31.  
  32.     cube = numpy.array(cube, dtype = numpy.float32)
  33.  
  34.     indices = [0, 1, 2, 2, 3, 0,
  35.                4, 5, 6, 6, 7, 4,
  36.                4, 5, 1, 1, 0, 4,
  37.                6, 7, 3, 3, 2, 6,
  38.                5, 6, 2, 2, 1, 5,
  39.                7, 4, 0, 0, 3, 7]
  40.  
  41.     indices = numpy.array(indices, dtype= numpy.uint32)
  42.  
  43.     vertex_shader = """
  44.    #version 330
  45.    in vec3 position;
  46.    in vec3 color;
  47.    uniform mat4 transform;
  48.    out vec3 newColor;
  49.    void main()
  50.    {
  51.        gl_Position = transform * vec4(position, 1.0f);
  52.        newColor = color;
  53.        //newColor = vec3(1.0, 1.0, 1.0);
  54.    }
  55.    """
  56.  
  57.     fragment_shader = """
  58.    #version 330
  59.    in vec3 newColor;
  60.    out vec4 outColor;
  61.    void main()
  62.    {
  63.        outColor = vec4(newColor, 1.0f);
  64.    }
  65.    """
  66.     shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
  67.                                               OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
  68.  
  69.     VBO = glGenBuffers(1)
  70.     glBindBuffer(GL_ARRAY_BUFFER, VBO)
  71.     glBufferData(GL_ARRAY_BUFFER, 192, cube, GL_STATIC_DRAW)
  72.     print(cube.__sizeof__())
  73.     #4 bytes for float, 48 elements * 4
  74.  
  75.     EBO = glGenBuffers(1)
  76.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
  77.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW)
  78.     print(indices.__sizeof__())
  79.     # 4 bytes for float, 48 elements * 4
  80.  
  81.     position = glGetAttribLocation(shader, "position")
  82.     glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
  83.     glEnableVertexAttribArray(position)
  84.  
  85.     color = glGetAttribLocation(shader, "color")
  86.     glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
  87.     glEnableVertexAttribArray(color)
  88.  
  89.  
  90.     glUseProgram(shader)
  91.  
  92.     glClearColor(0.2, 0.3, 0.2, 1.0)
  93.     glEnable(GL_DEPTH_TEST)
  94.     #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
  95.  
  96.     while not glfw.window_should_close(window):
  97.         glfw.poll_events()
  98.  
  99.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  100.  
  101.         rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() )
  102.         rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time() )
  103.         #print(rot_x, rot_y)
  104.         result = pyrr.Matrix44.identity()
  105.         transformLoc = glGetUniformLocation(shader, "transform")
  106.         glUniformMatrix4fv(transformLoc, 1, GL_FALSE, result)
  107.  
  108.         glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
  109.  
  110.         glfw.swap_buffers(window)
  111.  
  112.     glfw.terminate()
  113.  
  114. if __name__ == "__main__":
  115.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement