-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle.py
143 lines (114 loc) · 4.28 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import sys
import ctypes
import numpy as np
# from glfw import *
import OpenGL.GL as gl
import OpenGL.GLUT as glut
from pipeline import Pipeline, ProjParams
from camera import Camera
vertex_code, fragment_code = None, None
camera_pos = [0.0, 1.0, 0.0] # camera position
camera_target = [0.0, -0.5, 1.0] # "look at" direction
camera_up = [0.0, 1.0, 0.0] # camera vertical axis
WINDOW_WIDTH, WINDOW_HEIGHT = 1920, 1200
CAMERA = Camera(camera_pos, camera_target, camera_up, WINDOW_WIDTH, WINDOW_HEIGHT)
def tiny_glut(args):
global vertex_code, fragment_code
scale = 0.01
def display():
CAMERA.render()
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
nonlocal scale
scale_location = gl.glGetUniformLocation(program, "gScale")
assert scale_location != 0xffffffff
world_location = gl.glGetUniformLocation(program, "gWorld")
assert world_location != 0xffffffff
scale += 0.01
pipeline = Pipeline(rotation=[0.0, 30*scale, 0.0],
# scaling=[math.sin(scale)] * 3,
translation=[0, 0, 6],
projection=ProjParams(WINDOW_WIDTH, WINDOW_HEIGHT, 1.0, 100.0, 60.0))
pipeline.set_camera(CAMERA)
gl.glUniformMatrix4fv(world_location, 1, gl.GL_TRUE, pipeline.get_wvp())
gl.glDrawElements(gl.GL_TRIANGLES, 18, gl.GL_UNSIGNED_INT, ctypes.c_void_p(0))
glut.glutSwapBuffers()
# glut.glutPostRedisplay()
def mouse(x, y):
CAMERA.mouse(x, y)
def keyboard(key, x, y):
if key == glut.GLUT_KEY_F1:
sys.exit(0)
elif key == glut.GLUT_KEY_HOME:
CAMERA.setup()
else:
CAMERA.keyboard(key)
glut.glutInit(args)
glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_3_2_CORE_PROFILE)
glut.glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT)
glut.glutInitWindowPosition(400, 100)
# glut.glutCreateWindow("Camera Tutorial")
glut.glutGameModeString("1920x1200@32")
glut.glutEnterGameMode()
# callbacks initialization
glut.glutDisplayFunc(display)
glut.glutIdleFunc(display)
# glut.glutKeyboardFunc(keyboard)
glut.glutPassiveMotionFunc(mouse)
glut.glutSpecialFunc(keyboard)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glDepthFunc(gl.GL_LESS)
gl.glClearColor(0, 0, 0, 0)
vertices = np.array([
-1, -1, 0,
0, -1, -1,
1, -1, 0,
0, -1, 1,
0, 1, 0
], dtype=np.float32)
indexes = np.array([
0, 1, 2,
1, 2, 3,
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4
], dtype=np.uint32)
vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
gl.glShaderSource(vertex_shader, vertex_code)
gl.glCompileShader(vertex_shader)
if not gl.glGetShaderiv(vertex_shader, gl.GL_COMPILE_STATUS):
info = gl.glGetShaderInfoLog(vertex_shader)
print("vertex shader error occurred")
print(bytes.decode(info))
return
fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
gl.glShaderSource(fragment_shader, fragment_code)
gl.glCompileShader(fragment_shader)
if not gl.glGetShaderiv(fragment_shader, gl.GL_COMPILE_STATUS):
info = gl.glGetShaderInfoLog(fragment_shader)
print("fragment shader error occurred")
print(bytes.decode(info))
return
program = gl.glCreateProgram()
gl.glAttachShader(program, vertex_shader)
gl.glAttachShader(program, fragment_shader)
gl.glLinkProgram(program)
gl.glUseProgram(program)
vao = gl.glGenVertexArrays(1)
gl.glBindVertexArray(vao)
vbo = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, vertices.nbytes, vertices, gl.GL_STATIC_DRAW)
gl.glEnableVertexAttribArray(0)
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, ctypes.c_void_p(0))
ibo = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, ibo)
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, indexes.nbytes, indexes, gl.GL_STATIC_DRAW)
glut.glutMainLoop()
if __name__ == "__main__":
with open(os.path.join("shaders", "vs.glsl")) as f:
vertex_code = f.read()
with open(os.path.join("shaders", "fs.glsl")) as f:
fragment_code = f.read()
tiny_glut(sys.argv)