-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicusp3d.py
197 lines (155 loc) · 4.02 KB
/
bicusp3d.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
import sys
try:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
except:
print('Error: PyOpenGL not installed properly')
sys.exit( )
import array
import signal
import random
def signal_handler(signal, frame):
print('u pressed ctrl-c')
sys.exit(0)
#
signal.signal(signal.SIGINT, signal_handler)
###signal.pause()
import math
fullscreen = False
mouseDown = False
xrot = 0.2
yrot = 0.0
zrot = 0.0
xdiff = 0.0
ydiff = 0.0
def col(c):
# return a color variable computed from a escape value
return (c % 4 * 64, c % 8 * 32, c % 16 * 16)
def init():
# glClearColor(0.93, 0.93, 0.93, 0.0)
glClearColor(1.0,1.0,1.0,0.0)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glClearDepth(1.0)
return True
def DrawEllipsoid(uistacks, uislices, fA, fB, fC):
global counter
tstep = 2*math.pi/uislices
sstep = 2*math.pi/uistacks
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
#t = -math.pi/2
for i in range(4*uislices):
t = tstep*i
glBegin(GL_TRIANGLE_STRIP)
# s = -2*math.pi/2
vart=4
myvars=1
for j in range(uistacks+1):
s = sstep*j
x=math.cos(vart*t) * math.cos(vart*t) * math.cos(myvars*t)
y=math.cos(vart*t) * math.cos(vart*t) * math.sin(myvars*t)
z=math.sin(vart*t) * math.sin(vart*t)
glVertex3f(0.0, 0.0,0.0)
glVertex3f(x,y,z)
cx = int (x * 256)
cy = int (y * 256)
cz = int (z * 256)
glColor3b(cx % 4 * 64, cy % 8 * 32, cz % 16 * 16)
#glVertex3f(fA * math.cos(vart*t) * math.cos(myvars*s) * math.sin(t), fB * math.cos(vart*t) * math.sin(myvars*s) * math.sin(t), fC * math.sin(vart*t) * math.cos(t))
#glVertex3f(fA * math.cos(vart*t+tstep) * math.cos(myvars*s)*math.sin(t), fB *math.cos(vart*t+tstep) * math.sin(myvars*s)*math.sin(t), fC * math.sin(vart*t+tstep)*math.cos(t))
glEnd()
def display():
global xrot, yrot, zrot
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(
0.0, 0.0, 10.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0)
glRotatef(xrot, 1.0, 0.0, 0.0)
glRotatef(yrot, 0.0, 1.0, 0.0)
glRotatef(zrot, 0.0, 0.0, 1.0)
#glutWireCube(1)
glColor3f(0.5, 0.0, 1.0)
#glutWireSphere(4,10,10)
DrawEllipsoid(30, 120, 1.1, 2.1, 5.1)
#void glutSolidTorus(GLdouble innerRadius,
# GLdouble outerRadius,
# GLint nsides, GLint rings);
#void glutWireTorus(GLdouble innerRadius,
# GLdouble outerRadius,
# GLint nsides, GLint rings);
glFlush()
glutSwapBuffers()
def resize(*args):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glViewport(0, 0, args[0], args[1])
gluPerspective(45.0, 1.0 * args[0] / args[0], 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def idle():
global xrot, yrot, zrot
global mouseDown
if not (mouseDown):
xrot += 0.3
yrot += 0.3
zrot += 1.0
glutPostRedisplay()
#def keyboard(*args):
# ##print args[0]
# if (args[0]==27):
# ## print args[0]
# sys.exit(1)
#
#def specialKeyboard(*args):
# ##print args[0]
# if (args[0] == GLUT_KEY_F1):
# fullscreen = not(fullscreen)
#
# if (fullscreen):
# glutFullScreen()
# else:
# glutReshapeWindow(500, 500)
# glutPositionWindow(50, 50)
#
def mouse(*args):
global xrot, yrot
global xdiff, ydiff
global mouseDown
if (args[0] == GLUT_LEFT_BUTTON and args[1] == GLUT_DOWN):
mouseDown = True
xdiff = args[2] - yrot
ydiff = -args[3] + xrot
else:
mouseDown = False
def mouseMotion(*args):
global xrot, yrot
global xdiff, ydiff
global mouseDown
if (mouseDown):
yrot = args[0] - xdiff
xrot = args[1] + ydiff
glutPostRedisplay()
def main():
glutInit(sys.argv)
glutInitWindowPosition(50, 50)
glutInitWindowSize(1024, 768)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
glutCreateWindow("13 - Solid Shapes")
glutDisplayFunc(display)
#glutKeyboardFunc(keyboard)
#glutSpecialFunc(specialKeyboard)
glutMouseFunc(mouse)
glutMotionFunc(mouseMotion)
glutReshapeFunc(resize)
if not (init()):
return 1
glutIdleFunc(idle)
glutMainLoop()
return 0
if __name__ == "__main__":
main()