-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_triangle.py
60 lines (46 loc) · 1.58 KB
/
02_triangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import glfw
from OpenGL.GL import *
import numpy as np
from math import sin, cos
def main():
# Initialize the library , window, & context
if not glfw.init():
raise Exception('glfw can\'t be initialized')
window = glfw.create_window(640, 480, "Grafkomku", None, None)
if not window:
glfw.terminate()
raise Exception('glfw window can\'t be created')
glfw.set_window_pos(window, 100, 100)
glfw.make_context_current(window)
glClearColor(0, 0, 0.1, 0)
# Define vertices and color
vertices = [0, 0.5, 0,
- 0.5, -0.5, 0,
0.5, -0.5, 0]
vertices = np.array(vertices, dtype=float)
colors = [1, 0, 0,
0, 1, 0,
0, 0, 1]
colors = np.array(colors, dtype=float)
# Make Shape from Array
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, vertices)
glColorPointer(3, GL_FLOAT, 0, colors)
# Loop until the user closes the window
while not glfw.window_should_close(window):
# Swap and clear buffer and Poll process events
glfw.swap_buffers(window)
glClear(GL_COLOR_BUFFER_BIT)
glfw.poll_events()
# Draw & Tranformation the objext
glDrawArrays(GL_TRIANGLES, 0, 3)
# returns the elapsed time, since init was called
ct = glfw.get_time()
glLoadIdentity()
glScale(abs(sin(ct)), abs(sin(ct)), 1)
glRotatef(sin(ct) * 45, 0, 0, 1)
glTranslatef(sin(ct), cos(ct), 0)
glfw.terminate()
if __name__ == "__main__":
main()