Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import OpenGL.GL.shaders
- import numpy
- import pyrr
- def main():
- # initialize glfw
- if not glfw.init():
- return
- window = glfw.create_window(800, 600, "My OpenGL window", None, None)
- if not window:
- glfw.terminate()
- return
- glfw.make_context_current(window)
- # positions colors
- cube = [-0.5, -0.5, 0.5, 1.0, 0.0, 0.0,
- 0.5, -0.5, 0.5, 0.0, 1.0, 0.0,
- 0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
- -0.5, 0.5, 0.5, 1.0, 1.0, 1.0,
- -0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
- 0.5, -0.5, -0.5, 0.0, 1.0, 0.0,
- 0.5, 0.5, -0.5, 0.0, 0.0, 1.0,
- -0.5, 0.5, -0.5, 1.0, 1.0, 1.0]
- cube = numpy.array(cube, dtype = numpy.float32)
- indices = [0, 1, 2, 2, 3, 0,
- 4, 5, 6, 6, 7, 4,
- 4, 5, 1, 1, 0, 4,
- 6, 7, 3, 3, 2, 6,
- 5, 6, 2, 2, 1, 5,
- 7, 4, 0, 0, 3, 7]
- indices = numpy.array(indices, dtype= numpy.uint32)
- vertex_shader = """
- #version 330
- in vec3 position;
- in vec3 color;
- uniform mat4 transform;
- out vec3 newColor;
- void main()
- {
- gl_Position = transform * vec4(position, 1.0f);
- newColor = color;
- //newColor = vec3(1.0, 1.0, 1.0);
- }
- """
- fragment_shader = """
- #version 330
- in vec3 newColor;
- out vec4 outColor;
- void main()
- {
- outColor = vec4(newColor, 1.0f);
- }
- """
- shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
- OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
- VBO = glGenBuffers(1)
- glBindBuffer(GL_ARRAY_BUFFER, VBO)
- glBufferData(GL_ARRAY_BUFFER, 192, cube, GL_STATIC_DRAW)
- print(cube.__sizeof__())
- #4 bytes for float, 48 elements * 4
- EBO = glGenBuffers(1)
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW)
- print(indices.__sizeof__())
- # 4 bytes for float, 48 elements * 4
- position = glGetAttribLocation(shader, "position")
- glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
- glEnableVertexAttribArray(position)
- color = glGetAttribLocation(shader, "color")
- glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
- glEnableVertexAttribArray(color)
- glUseProgram(shader)
- glClearColor(0.2, 0.3, 0.2, 1.0)
- glEnable(GL_DEPTH_TEST)
- #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
- while not glfw.window_should_close(window):
- glfw.poll_events()
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() )
- rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time() )
- #print(rot_x, rot_y)
- result = pyrr.Matrix44.identity()
- transformLoc = glGetUniformLocation(shader, "transform")
- glUniformMatrix4fv(transformLoc, 1, GL_FALSE, result)
- glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
- glfw.swap_buffers(window)
- glfw.terminate()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement