-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcube.py
More file actions
161 lines (119 loc) · 3.9 KB
/
cube.py
File metadata and controls
161 lines (119 loc) · 3.9 KB
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
# author: Somsubhra Bairi (201101056)
# Draws a polyhedron by cutting corners of cube
# Controls: UP - rotate up
# DOWN - rotate down
# LEFT - rotate left
# RIGHT - rotate right
# 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"
# The cube class
class Cube:
# Constructor for the cube class
def __init__(self):
self.rotate_y = 0.0
self.rotate_x = 0.0
self.scale = 2.0
# Initialize
def init(self):
# Set background to black
glClearColor(0.0, 0.0, 0.0, 0.0)
# Set the shade model to flat
glShadeModel(GL_FLAT)
# Draw half of the cube with corners cut
def draw_half(self, mirror):
# The plane equations cutting corners of cube
eqn = [-1.0, 0.0, 0.0, 0.0]
eqn1 = [1.0, 1.0, 1.0, 1.25]
eqn2 = [1.0, -1.0, 1.0, 1.25]
eqn3 = [1.0, 1.0, -1.0, 1.25]
eqn4 = [1.0, -1.0, -1.0, 1.25]
eqn5 = [-1.0, 1.0, 1.0, 1.25]
# Set the color to white
glColor3f(1.0, 1.0, 1.0)
# Reset the matrix
glLoadIdentity()
# Set the camera
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
if mirror:
glScalef(-self.scale, self.scale, self.scale)
glRotatef(-self.rotate_y, 0.0, 1.0, 0.0)
else:
glScalef(self.scale, self.scale, self.scale)
glRotatef(self.rotate_y, 0.0, 1.0, 0.0)
glRotatef(self.rotate_x, 1.0, 0.0, 0.0)
# Draw solid cube
glutSolidCube(1.0)
# Draw a red wire cube to highlight the background
glColor3f(1.0, 0.0, 0.0)
glutWireCube(1.0)
# Clip the corners of the cube with these equations
glClipPlane(GL_CLIP_PLANE0, eqn1)
glEnable(GL_CLIP_PLANE0)
glClipPlane(GL_CLIP_PLANE1, eqn2)
glEnable(GL_CLIP_PLANE1)
glClipPlane(GL_CLIP_PLANE2, eqn3)
glEnable(GL_CLIP_PLANE2)
glClipPlane(GL_CLIP_PLANE3, eqn4)
glEnable(GL_CLIP_PLANE3)
glClipPlane(GL_CLIP_PLANE4, eqn5)
glEnable(GL_CLIP_PLANE4)
# Cut the cube into half
glClipPlane(GL_CLIP_PLANE5, eqn)
glEnable(GL_CLIP_PLANE5)
glFlush()
# The display function
def display(self):
glClear(GL_COLOR_BUFFER_BIT)
# Draw half the cube with corners cut
self.draw_half(False)
# Draw a mirror image of the above half cube
self.draw_half(True)
# The reshape function
def reshape(self, w, h):
glViewport(0, 0, GLsizei(w), GLsizei(h))
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
glMatrixMode(GL_MODELVIEW)
# The keyboard controls
def special(self, key, x, y):
# Rotate cube according to keys pressed
if key == GLUT_KEY_RIGHT:
self.rotate_y += 5
if key == GLUT_KEY_LEFT:
self.rotate_y -= 5
if key == GLUT_KEY_UP:
self.rotate_x += 5
if key == GLUT_KEY_DOWN:
self.rotate_x -= 5
glutPostRedisplay()
# The main function
def main():
# Initialize OpenGL
glutInit(sys.argv)
# Set display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
# Set size and position of window size
glutInitWindowSize(400, 400)
glutInitWindowPosition(100, 100)
# Create window with given title
glutCreateWindow("Cube")
# Instantiate the cube
cube = Cube()
cube.init()
# The callback for display function
glutDisplayFunc(cube.display)
# The callback for reshape function
glutReshapeFunc(cube.reshape)
# The callback function for keyboard controls
glutSpecialFunc(cube.special)
# Start the main loop
glutMainLoop()
# Call the main function
if __name__ == '__main__':
main()