Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- global rotate_x
- rotate_x = 0
- global rotate_y
- rotate_y = 0
- def keyCallback(window, key, scancode, action, mods):
- global rotate_x
- global rotate_y
- if key == glfw.KEY_UP and action:
- rotate_x += 10.0
- elif key == glfw.KEY_DOWN and action:
- rotate_x -= 10.0
- elif key == glfw.KEY_LEFT and action:
- rotate_y += 10.0
- elif key == glfw.KEY_RIGHT and action:
- rotate_y -= 10.0
- def create_shader(shader_type, source):
- shader = glCreateShader(shader_type)
- glShaderSource(shader, source)
- glCompileShader(shader)
- compilestatus = glGetShaderiv(shader, GL_COMPILE_STATUS)
- if compilestatus != GL_TRUE:
- raise RuntimeError(glGetShaderInfoLog(shader)
- .decode('ASCII'))
- return shader
- def shader():
- vertex = create_shader(GL_VERTEX_SHADER, """
- varying vec4 vertex_color;
- void main(){
- gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
- vertex_color = gl_Color;
- }""")
- # фрагментный шейдер
- fragment = create_shader(GL_FRAGMENT_SHADER, """
- varying vec4 vertex_color;
- void main() {
- gl_FragColor = vertex_color;
- }""")
- program = glCreateProgram()
- glAttachShader(program, vertex)
- glAttachShader(program, fragment)
- glBindFragDataLocation(program, 0, "outColor")
- glLinkProgram(program)
- glUseProgram(program) ###должно идти перед draw elements
- # Определяем массив вершин (три вершины по три координаты)
- #pointdata = [[0, 0.5, 0], [-0.5, -0.5, 0], [0.5, -0.5, 0]]
- pointdata = [[- 0.5, - 0.5, - 0.5], [0.5, - 0.5, - 0.5], [0.5, 0.5, - 0.5], [- 0.5, 0.5, - 0.5],
- [- 0.5, - 0.5, 0.5], [0.5, - 0.5, 0.5], [0.5, 0.5, 0.5], [- 0.5, 0.5, 0.5],
- [0.5, - 0.5, - 0.5], [0.5, - 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, - 0.5],
- [- 0.5, - 0.5, - 0.5], [- 0.5, - 0.5, 0.5], [- 0.5, 0.5, 0.5], [- 0.5, 0.5, - 0.5],
- [- 0.5, 0.5, - 0.5], [0.5, 0.5, - 0.5], [0.5, 0.5, 0.5], [- 0.5, 0.5, 0.5],
- [- 0.5, - 0.5, - 0.5], [0.5, - 0.5, - 0.5], [0.5, - 0.5, 0.5], [- 0.5, - 0.5, 0.5]]
- # Определяем массив цветов (по одному цвету для каждой вершины)
- #pointcolor = [[1, 1, 0], [0, 1, 1], [1, 0, 1]]
- pointcolor = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1],
- [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0],
- [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0],
- [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1],
- [1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0],
- [1, 0, 1], [1, 0, 1], [1, 0, 1], [1, 0, 1]]
- def background():
- glClearColor(0.8, 0.8, 0.8, 1.0)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- #shader()
- def draw():
- glLoadIdentity()
- glMatrixMode(GL_MODELVIEW)
- global rotate_x
- global rotate_y
- glRotatef(rotate_x, 1, 0, 0)
- glRotatef(rotate_y, 0, 1, 0)
- glEnableClientState(GL_VERTEX_ARRAY) # Включаем использование массива вершин
- glEnableClientState(GL_COLOR_ARRAY) # Включаем использование массива цветов
- glVertexPointer(3, GL_FLOAT, 0, pointdata)
- glColorPointer(3, GL_FLOAT, 0, pointcolor)
- glDrawArrays(GL_QUADS, 0, 24)
- glDisableClientState(GL_VERTEX_ARRAY) # Отключаем использование массива вершин
- glDisableClientState(GL_COLOR_ARRAY) # Отключаем использование массива цветов
- class Drawer:
- window = False
- def __init__(self):
- if not glfw.init():
- return
- self.window = glfw.create_window(800, 800, "OpenGL", None, None)
- glfw.set_key_callback(self.window, keyCallback)
- if not self.window:
- glfw.terminate()
- return
- glfw.make_context_current(self.window)
- shader()
- def startLoop(self):
- while not glfw.window_should_close(self.window):
- background()
- draw()
- glfw.swap_buffers(self.window)
- glfw.poll_events()
- glfw.terminate()
- drawer = Drawer()
- drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement