-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest3.py
243 lines (185 loc) · 6.14 KB
/
test3.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# author: Somsubhra Bairi (201101056)
# Draw sphere with QUAD_STRIP
# Controls: UP/DOWN - scale up/down
# LEFT/RIGHT - rotate left/right
# F1 - Toggle surface as SMOOTH or FLAT
# Python imports
from math import *
# OpenGL imports for python
try:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
except:
print "OpenGL wrapper for python not found"
# Last time when sphere was re-displayed
last_time = 0
COLOR_YELLOW = (1, 1 , 0)
COLOR_WHITE = (1, 1, 1)
COLOR_BLUE = (0, 0, 1)
COLOR_RED = (1, 0, 0)
COLOR_GREEN = (0, 0.6, 0)
COLOR_ORANGE = (1, 0.49, 0.14)
COLOR_BLACK = (0, 0, 0)
# The sphere class
class Sphere:
# Constructor for the sphere class
def __init__(self, radius):
# Radius of sphere
self.radius = radius
# Number of latitudes in sphere
self.lats = 100
# Number of longitudes in sphere
self.longs = 100
self.user_theta = 0
self.user_height = 0
# Direction of light
self.direction = [0.0, 2.0, -1.0, 1.0]
# Intensity of light
self.intensity = [0.7, 0.7, 0.7, 1.0]
# Intensity of ambient light
self.ambient_intensity = [0.3, 0.3, 0.3, 1.0]
# The surface type(Flat or Smooth)
self.surface = GL_FLAT
# Initialize
def init(self):
# Set background color to black
glClearColor(0.0, 0.0, 0.0, 0.0)
self.compute_location()
# Set OpenGL parameters
glEnable(GL_DEPTH_TEST)
# Enable lighting
glEnable(GL_LIGHTING)
# Set light model
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, self.ambient_intensity)
# Enable light number 0
glEnable(GL_LIGHT0)
# Set position and intensity of light
glLightfv(GL_LIGHT0, GL_POSITION, self.direction)
glLightfv(GL_LIGHT0, GL_DIFFUSE, self.intensity)
# Setup the material
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
# Compute location
def compute_location(self):
x = 2 * cos(self.user_theta)
y = 2 * sin(self.user_theta)
z = self.user_height
d = sqrt(x * x + y * y + z * z)
# Set matrix mode
glMatrixMode(GL_PROJECTION)
# Reset matrix
glLoadIdentity()
#glFrustum(-d * 0.5, d * 0.5, -d * 0.5, d * 0.5, d - 1.1, d + 1.1)
# Set camera
#gluLookAt(x, y, z, 0, 0, 0, 0, 0, 1)
gluPerspective(45.0,float(640)/float(400),0.1,200.0)
gluLookAt(1, 1.5, -3, 0, 0, 0, 0, 0, 1)
# Display the sphere
def display(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Set color to white
glColor3f(1.0, 1.0, 1.0)
# Set shade model
glShadeModel(self.surface)
self.draw()
glutSwapBuffers()
# Draw the sphere
def draw(self):
for i in range(0, self.lats + 1):
lat0 = pi * (-0.5 + float(float(i - 1) / float(self.lats)))
z0 = sin(lat0)
zr0 = cos(lat0)
lat1 = pi * (-0.5 + float(float(i) / float(self.lats)))
z1 = sin(lat1)
zr1 = cos(lat1)
# Use Quad strips to draw the sphere
# glBegin(GL_QUAD_STRIP)
# for j in range(0, self.longs + 1):
# lng = 2 * pi * float(float(j - 1) / float(self.longs))
# x = cos(lng)
# y = sin(lng)
# glNormal3f(x * zr0, y * zr0, z0)
# glVertex3f(x * zr0, y * zr0, z0)
# glNormal3f(x * zr1, y * zr1, z1)
# glVertex3f(x * zr1, y * zr1, z1)
# glEnd()
glColor3f(*COLOR_GREEN)
glBegin(GL_POLYGON)
glVertex3f(0.3,0.3,0.8)
glVertex3f(0.3,0.9,0.8)
glVertex3f(0.9, 0.9, 0.8)
glVertex3f(0.9, 0.3, 0.8)
glEnd()
glColor3f(*COLOR_RED)
glBegin(GL_POLYGON)
glVertex3f(0.5,0.5,1.3)
glVertex3f(0.5,1.0,1.3)
glVertex3f(1.0, 1.0, 1.3)
glVertex3f(1.0, 0.5, 1.3)
glEnd()
glColor3f(*COLOR_YELLOW)
glBegin(GL_POLYGON)
glVertex3f(0.1,0.1,0.2)
glVertex3f(0.1,0.7,0.2)
glVertex3f(0.7, 0.7, 0.2)
glVertex3f(0.7, 0.1, 0.2)
glEnd()
# Keyboard controller for sphere
def special(self, key, x, y):
# Scale the sphere up or down
if key == GLUT_KEY_UP:
self.user_height += 0.1
if key == GLUT_KEY_DOWN:
self.user_height -= 0.1
# Rotate the cube
if key == GLUT_KEY_LEFT:
self.user_theta += 0.1
if key == GLUT_KEY_RIGHT:
self.user_theta -= 0.1
# Toggle the surface
if key == GLUT_KEY_F1:
if self.surface == GL_FLAT:
self.surface = GL_SMOOTH
else:
self.surface = GL_FLAT
self.compute_location()
glutPostRedisplay()
# The idle callback
def idle(self):
global last_time
time = glutGet(GLUT_ELAPSED_TIME)
if last_time == 0 or time >= last_time + 40:
last_time = time
glutPostRedisplay()
# The visibility callback
def visible(self, vis):
if vis == GLUT_VISIBLE:
glutIdleFunc(self.idle)
else:
glutIdleFunc(None)
# The main function
def main():
# Initialize the OpenGL pipeline
glutInit(sys.argv)
# Set OpenGL display mode
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
# Set the Window size and position
glutInitWindowSize(300, 300)
glutInitWindowPosition(50, 100)
# Create the window with given title
glutCreateWindow('Sphere')
# Instantiate the sphere object
s = Sphere(1.0)
s.init()
# Set the callback function for display
glutDisplayFunc(s.display)
# Set the callback function for the visibility
glutVisibilityFunc(s.visible)
# Set the callback for special function
glutSpecialFunc(s.special)
# Run the OpenGL main loop
glutMainLoop()
# Call the main function
if __name__ == '__main__':
main()