| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
from fltk import * |
| 24 |
import sys, time |
| 25 |
|
| 26 |
try: |
| 27 |
from OpenGL.GLUT import * |
| 28 |
from OpenGL.GL import * |
| 29 |
except: |
| 30 |
print(''' |
| 31 |
ERROR: PyOpenGL not installed properly. |
| 32 |
''') |
| 33 |
sys.exit() |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class cube_box(Fl_Gl_Window): |
| 39 |
lasttime = 0.0 |
| 40 |
size = 0.0 |
| 41 |
speed = 0.0 |
| 42 |
wire = 0 |
| 43 |
def __init__(self, x, y, w, h, l): |
| 44 |
Fl_Gl_Window.__init__(self, x, y, w, h, l) |
| 45 |
self.lasttime = 0.0 |
| 46 |
|
| 47 |
def draw(self): |
| 48 |
self.lasttime = self.lasttime+self.speed |
| 49 |
if self.valid() == 0: |
| 50 |
glLoadIdentity() |
| 51 |
glViewport(0,0,self.w(),self.h()) |
| 52 |
glEnable(GL_DEPTH_TEST) |
| 53 |
glFrustum(-1,1,-1,1,2,10000) |
| 54 |
glTranslatef(0,0,-10) |
| 55 |
gl_font(FL_HELVETICA_BOLD, 16 ) |
| 56 |
|
| 57 |
try: |
| 58 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 59 |
glPushMatrix() |
| 60 |
|
| 61 |
glRotatef(self.lasttime*1.6,0,0,1) |
| 62 |
glRotatef(self.lasttime*4.2,1,0,0) |
| 63 |
glRotatef(self.lasttime*2.3,0,1,0) |
| 64 |
glTranslatef(-1.0, 1.2, -1.5); |
| 65 |
glScalef(self.size,self.size,self.size) |
| 66 |
drawcube(self.wire) |
| 67 |
glPopMatrix() |
| 68 |
gl_color(FL_GRAY) |
| 69 |
glDisable(GL_DEPTH_TEST) |
| 70 |
if self.wire == 1: |
| 71 |
gl_draw("Cube: wire", -4.5, -4.5) |
| 72 |
else: |
| 73 |
gl_draw("Cube: flat", -4.5, -4.5) |
| 74 |
glEnable(GL_DEPTH_TEST) |
| 75 |
except: |
| 76 |
print("Exception: ", sys.exc_info()[0], sys.exc_info()[1]) |
| 77 |
|
| 78 |
|
| 79 |
v0 = [0.0, 0.0, 0.0] |
| 80 |
v1 = [1.0, 0.0, 0.0] |
| 81 |
v2 = [1.0, 1.0, 0.0] |
| 82 |
v3 = [0.0, 1.0, 0.0] |
| 83 |
v4 = [0.0, 0.0, 1.0] |
| 84 |
v5 = [1.0, 0.0, 1.0] |
| 85 |
v6 = [1.0, 1.0, 1.0] |
| 86 |
v7 = [0.0, 1.0, 1.0] |
| 87 |
|
| 88 |
|
| 89 |
def drawcube(wire): |
| 90 |
|
| 91 |
if wire == 1: |
| 92 |
glBegin(GL_LINE_LOOP) |
| 93 |
else: |
| 94 |
glBegin(GL_POLYGON) |
| 95 |
glColor3ub(0,0,255); |
| 96 |
glVertex3fv(v0) |
| 97 |
glVertex3fv(v1) |
| 98 |
glVertex3fv(v2) |
| 99 |
glVertex3fv(v3) |
| 100 |
glEnd() |
| 101 |
|
| 102 |
if wire == 1: |
| 103 |
glBegin(GL_LINE_LOOP) |
| 104 |
else: |
| 105 |
glBegin(GL_POLYGON) |
| 106 |
glColor3ub(0,255,255) |
| 107 |
glVertex3fv(v4) |
| 108 |
glVertex3fv(v5) |
| 109 |
glVertex3fv(v6) |
| 110 |
glVertex3fv(v7) |
| 111 |
glEnd() |
| 112 |
if wire == 1: |
| 113 |
glBegin(GL_LINE_LOOP) |
| 114 |
else: |
| 115 |
glBegin(GL_POLYGON) |
| 116 |
glColor3ub(255,0,255) |
| 117 |
glVertex3fv(v0) |
| 118 |
glVertex3fv(v1) |
| 119 |
glVertex3fv(v5) |
| 120 |
glVertex3fv(v4) |
| 121 |
glEnd() |
| 122 |
if wire == 1: |
| 123 |
glBegin(GL_LINE_LOOP) |
| 124 |
else: |
| 125 |
glBegin(GL_POLYGON) |
| 126 |
glColor3ub(255,255,0) |
| 127 |
glVertex3fv(v2) |
| 128 |
glVertex3fv(v3) |
| 129 |
glVertex3fv(v7) |
| 130 |
glVertex3fv(v6) |
| 131 |
glEnd() |
| 132 |
if wire == 1: |
| 133 |
glBegin(GL_LINE_LOOP) |
| 134 |
else: |
| 135 |
glBegin(GL_POLYGON) |
| 136 |
glColor3ub(0,255,0) |
| 137 |
glVertex3fv(v0) |
| 138 |
glVertex3fv(v4) |
| 139 |
glVertex3fv(v7) |
| 140 |
glVertex3fv(v3) |
| 141 |
glEnd() |
| 142 |
if wire == 1: |
| 143 |
glBegin(GL_LINE_LOOP) |
| 144 |
else: |
| 145 |
glBegin(GL_POLYGON) |
| 146 |
glColor3ub(255,0,0) |
| 147 |
glVertex3fv(v1) |
| 148 |
glVertex3fv(v2) |
| 149 |
glVertex3fv(v6) |
| 150 |
glVertex3fv(v5) |
| 151 |
glEnd() |
| 152 |
|
| 153 |
|
| 154 |
form = None |
| 155 |
speed = None |
| 156 |
size = None |
| 157 |
button = None |
| 158 |
wire = None |
| 159 |
flat = None |
| 160 |
cube = None |
| 161 |
cube2 = None |
| 162 |
|
| 163 |
class MyBox(Fl_Group): |
| 164 |
def __init__(self, type, x, y, w, h, l): |
| 165 |
Fl_Group.__init__(self, x, y, w, h, l) |
| 166 |
Fl_Group.box(self, type) |
| 167 |
self.end() |
| 168 |
|
| 169 |
def exit_cb(ptr): |
| 170 |
sys.exit(0) |
| 171 |
|
| 172 |
def makeform(name): |
| 173 |
global form |
| 174 |
global speed |
| 175 |
global size |
| 176 |
global wire |
| 177 |
global flat |
| 178 |
global button |
| 179 |
global cube |
| 180 |
global cube2 |
| 181 |
form = Fl_Window(510+390,390,name) |
| 182 |
b1 = Fl_Box(FL_DOWN_FRAME,20,20,350,350,"") |
| 183 |
b2 = Fl_Box(FL_DOWN_FRAME,510,20,350,350,"") |
| 184 |
speed = Fl_Slider(FL_VERT_SLIDER,390,90,40,220,"Speed") |
| 185 |
size = Fl_Slider(FL_VERT_SLIDER,450,90,40,220,"Size") |
| 186 |
wire = Fl_Radio_Light_Button(390,20,100,30,"Wire") |
| 187 |
flat = Fl_Radio_Light_Button(390,50,100,30,"Flat") |
| 188 |
button = Fl_Button(390,340,100,30,"Exit") |
| 189 |
button.callback(exit_cb) |
| 190 |
cube = cube_box(23,23,344,344, "") |
| 191 |
cube2 = cube_box(513,23,344,344, "") |
| 192 |
b = Fl_Box(FL_NO_BOX,cube.x(),size.y(), cube.w(),size.h(),"") |
| 193 |
|
| 194 |
form.resizable(b) |
| 195 |
b.hide() |
| 196 |
form.end() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == '__main__': |
| 200 |
makeform(sys.argv[0]) |
| 201 |
|
| 202 |
speed.bounds(4,0) |
| 203 |
cube.speed = 1.0 |
| 204 |
cube2.speed = 1.0 |
| 205 |
speed.value(1.0) |
| 206 |
size.bounds(4,0.01) |
| 207 |
cube.size = 1.0 |
| 208 |
cube2.size = 1.0 |
| 209 |
size.value(1.0) |
| 210 |
flat.value(1) |
| 211 |
cube.wire = 0 |
| 212 |
cube2.wire = 1 |
| 213 |
form.label("cube"); |
| 214 |
|
| 215 |
form.show(len(sys.argv), sys.argv) |
| 216 |
cube.show() |
| 217 |
cube2.show() |
| 218 |
|
| 219 |
while 1: |
| 220 |
if form.visible() == 1 and speed.value() != 0: |
| 221 |
if Fl.check() == 0: |
| 222 |
break |
| 223 |
else: |
| 224 |
if Fl.wait() == 0: |
| 225 |
break |
| 226 |
|
| 227 |
cube.wire = wire.value() |
| 228 |
cube2.wire = not wire.value() |
| 229 |
cube.size = size.value() |
| 230 |
cube2.size = size.value() |
| 231 |
cube.speed = speed.value() |
| 232 |
cube2.speed = speed.value() |
| 233 |
cube.redraw() |
| 234 |
cube2.redraw() |
| 235 |
if Fl.readqueue() == button: |
| 236 |
break |
| 237 |
|
| 238 |
|