-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
195 lines (165 loc) · 5.42 KB
/
12.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
###################################################
# [Practice] OpenGL Lighting
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
from OpenGL.arrays import vbo
import ctypes
gCamAng = 0.
gCamHeight = 1.
gVertexArraySeparate = None
gIndexArray = None
gNormalArray = None
a = 0
b = 0
c = 0
def createVertexAndIndexArrayIndexed():
varr = np.array([
( -1 , 1 , 1 ), # v0
( 1 , 1 , 1 ), # v1
( 1 , -1 , 1 ), # v2
( -1 , -1 , 1 ), # v3
( -1 , 1 , -1 ), # v4
( 1 , 1 , -1 ), # v5
( 1 , -1 , -1 ), # v6
( -1 , -1 , -1 ), # v7
], 'float32')
iarr = np.array([
(0,2,1),
(0,3,2),
(4,5,6),
(4,6,7),
(0,1,5),
(0,5,4),
(3,6,2),
(3,7,6),
(1,2,6),
(1,6,5),
(0,7,3),
(0,4,7),
])
narr = np.array([
(-0.5773502691896258, 0.5773502691896258, 0.5773502691896258),
(0.8164965809277261 , 0.4082482904638631 , 0.4082482904638631),
(0.4082482904638631 , -0.4082482904638631 , 0.8164965809277261),
(-0.4082482904638631 , -0.8164965809277261 , 0.4082482904638631),
(-0.4082482904638631 , 0.4082482904638631 , -0.8164965809277261),
(0.4082482904638631 , 0.8164965809277261 , -0.4082482904638631),
(0.5773502691896258 , -0.5773502691896258 , -0.5773502691896258),
(-0.8164965809277261 , -0.4082482904638631 , -0.4082482904638631),
], 'float32')
return varr, iarr, narr
def drawCube_glDrawElements():
global gVertexArrayIndexed, gIndexArray, gNormalArray
varr = gVertexArrayIndexed
iarr = gIndexArray
narr = gNormalArray
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glNormalPointer(GL_FLOAT, 3*narr.itemsize, narr)
glVertexPointer(3, GL_FLOAT, 3*varr.itemsize, varr)
glDrawElements(GL_TRIANGLES, iarr.size, GL_UNSIGNED_INT, iarr)
def render():
global gCamAng, gCamHeight
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1, 1,10)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(5*np.sin(gCamAng),gCamHeight,5*np.cos(gCamAng), 0,0,0, 0,1,0)
drawFrame()
glEnable(GL_LIGHTING) # try to uncomment: no lighting
glEnable(GL_LIGHT0)
glEnable(GL_NORMALIZE) # try to uncomment: lighting will be incorrect if you scale the object
# glEnable(GL_RESCALE_NORMAL)
# light position
#glPushMatrix()
t = glfw.get_time()
# glRotatef(t*(180/np.pi),0,1,0) # try to uncomment: rotate light
lightPos = (3.,4.,5.,1.) # try to change 4th element to 0. or 1.
glLightfv(GL_LIGHT0, GL_POSITION, lightPos)
#glPopMatrix()
# light intensity for each color channel
lightColor = (1.,1.,1.,1.)
ambientLightColor = (.1,.1,.1,1.)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor)
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor)
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightColor)
# material reflectance for each color channel
objectColor = (a ,b , c, 1.)
specularObjectColor = (1.,1.,1.,1.)
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, objectColor)
glMaterialfv(GL_FRONT, GL_SHININESS, 10)
glMaterialfv(GL_FRONT, GL_SPECULAR, specularObjectColor)
#glPushMatrix()
# glRotatef(t*(180/np.pi),0,1,0) # try to uncomment: rotate object
# glScalef(1.,.2,1.) # try to uncomment: scale object
glColor3ub(0, 0, 255) # glColor*() is ignored if lighting is enabled
# drawCube_glVertex()
#drawCube_glDrawArray()
drawCube_glDrawElements()
#glPopMatrix()
glDisable(GL_LIGHTING)
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 key_callback(window, key, scancode, action, mods):
global gCamAng, gCamHeight
global a, b, c
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
elif key==glfw.KEY_R:
if a == 1:
a = 0
elif a == 0:
a = 1
elif key==glfw.KEY_G:
if b == 1:
b = 0
elif b == 0:
b = 1
elif key==glfw.KEY_B:
if c == 1:
c = 0
elif c == 0:
c = 1
def main():
global gVertexArrayIndexed
global gIndexArray
global gNormalArray
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)
glfw.swap_interval(1)
gVertexArrayIndexed, gIndexArray, gNormalArray = createVertexAndIndexArrayIndexed()
while not glfw.window_should_close(window):
glfw.poll_events()
render()
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()