-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.py
132 lines (110 loc) · 3.57 KB
/
9.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
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
# draw a cube of side 1, centered at the origin.
def drawUnitCube():
glBegin(GL_QUADS)
glVertex3f( 0.5, 0.5,-0.5)
glVertex3f(-0.5, 0.5,-0.5)
glVertex3f(-0.5, 0.5, 0.5)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5)
glVertex3f(-0.5,-0.5, 0.5)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f( 0.5,-0.5,-0.5)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f(-0.5, 0.5, 0.5)
glVertex3f(-0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5,-0.5)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f(-0.5, 0.5,-0.5)
glVertex3f( 0.5, 0.5,-0.5)
glVertex3f(-0.5, 0.5, 0.5)
glVertex3f(-0.5, 0.5,-0.5)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f(-0.5,-0.5, 0.5)
glVertex3f( 0.5, 0.5,-0.5)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5,-0.5)
glEnd()
def drawCubeArray():
for i in range(5):
for j in range(5):
for k in range(5):
glPushMatrix()
glTranslatef(i,j,-k-1)
glScalef(.5,.5,.5)
drawUnitCube()
glPopMatrix()
def drawFrame():
glBegin(GL_LINES)
glColor3ub(255, 0, 0)
glVertex3fv(np.array([0.,0.,0.]))
glVertex3fv(np.array([1.,0.,0.]))
glColor3ub(0, 255, 0)
glVertex3fv(np.array([0.,0.,0.]))
glVertex3fv(np.array([0.,1.,0.]))
glColor3ub(0, 0, 255)
glVertex3fv(np.array([0.,0.,0]))
glVertex3fv(np.array([0.,0.,1.]))
glEnd()
def render():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
glLoadIdentity()
myFrustum(-1,1, -1,1, 1,10)
myLookAt(np.array([5,3,5]), np.array([1,1,-1]), np.array([0,1,0]))
# Above two lines must behave exactly same as the below two lines
#glFrustum(-1,1, -1,1, 1,10)
#gluLookAt(5,3,5, 1,1,-1, 0,1,0)
drawFrame()
glColor3ub(255, 255, 255)
drawCubeArray()
def myFrustum(left, right, bottom, top, near, far):
# implement here
Mpers = np.array([ [2*near/(right-left), 0, (right+left)/(right-left), 0],
[0, 2*near/(top-bottom), (top+bottom)/(top-bottom), 0],
[0, 0, -(far+near)/(far-near), -2*far*near/(far-near)],
[0, 0, -1, 0] ] )
glMultMatrixf(Mpers.T)
def myLookAt(eye, at, up):
# implement here
w = (eye-at) / np.sqrt( np.dot(eye-at, eye-at) )
u = np.cross(up, w) / np.sqrt( np.dot(np.cross(up, w), np.cross(up, w)) )
v = np.cross(w, u)
Mv = np.array([ [u[0], u[1], u[2], -u@eye],
[v[0], v[1], v[2], -v@eye],
[w[0], w[1], w[2], -w@eye],
[0, 0, 0, 1] ] )
glMultMatrixf(Mv.T)
def key_callback(window, key, scancode, action, mods):
global gCamAng, gCamHeight
if action==glfw.PRESS or action==glfw.REPEAT:
if key==glfw.KEY_1:
gCamAng += np.radians(-10)
elif key==glfw.KEY_3:
gCamAng += np.radians(10)
elif key==glfw.KEY_2:
gCamHeight += .1
elif key==glfw.KEY_W:
gCamHeight += -.1
def main():
if not glfw.init():
return
window = glfw.create_window(480,480,'A', None,None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
glfw.set_key_callback(window, key_callback)
while not glfw.window_should_close(window):
glfw.poll_events()
render()
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()