~jtaylor/ubuntu/natty/pyfltk/fix-779340

« back to all changes in this revision

Viewing changes to fltk/test/cube.py

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2009-03-13 20:47:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090313204700-ra4hgdlhxzrccas3
Tags: upstream-1.1.3
ImportĀ upstreamĀ versionĀ 1.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Another forms test program for the Python port of
 
2
# the Fast Light Tool Kit (pyFLTK).
 
3
# Modified to have 2 cubes to test multiple OpenGL contexts
 
4
#
 
5
# FLTk is Copyright 1998-2003 by Bill Spitzak and others.
 
6
# pyFLTK is Copyright 2003 by Andreas Held
 
7
#
 
8
# This library is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU Library General Public
 
10
# License as published by the Free Software Foundation; either
 
11
# version 2 of the License, or (at your option) any later version.
 
12
#
 
13
# This library is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
# Library General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Library General Public
 
19
# License along with this library; if not, write to the Free Software
 
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
21
# USA.
 
22
#
 
23
 
 
24
from fltk import *
 
25
import sys, time
 
26
 
 
27
try:
 
28
        from OpenGL.GLUT import *
 
29
        from OpenGL.GL import *
 
30
except:
 
31
        print '''
 
32
ERROR: PyOpenGL not installed properly.  
 
33
                '''
 
34
        sys.exit()
 
35
 
 
36
 
 
37
# requires OpenGL
 
38
 
 
39
class cube_box(Fl_Gl_Window):
 
40
      lasttime = 0.0
 
41
      size = 0.0
 
42
      speed = 0.0
 
43
      wire = 0
 
44
      def __init__(self, x, y, w, h, l):
 
45
          Fl_Gl_Window.__init__(self, x, y, w, h, l)
 
46
          self.lasttime = 0.0
 
47
 
 
48
      def draw(self):
 
49
          self.lasttime = self.lasttime+self.speed      
 
50
          if self.valid() == 0:
 
51
             glLoadIdentity()
 
52
             glViewport(0,0,self.w(),self.h())
 
53
             glEnable(GL_DEPTH_TEST)
 
54
             glFrustum(-1,1,-1,1,2,10000)
 
55
             glTranslatef(0,0,-10)
 
56
             gl_font(FL_HELVETICA_BOLD, 16 )
 
57
 
 
58
          try:
 
59
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 
60
                glPushMatrix()
 
61
          
 
62
                glRotatef(self.lasttime*1.6,0,0,1)
 
63
                glRotatef(self.lasttime*4.2,1,0,0)
 
64
                glRotatef(self.lasttime*2.3,0,1,0)
 
65
                glTranslatef(-1.0, 1.2, -1.5);
 
66
                glScalef(self.size,self.size,self.size)
 
67
                drawcube(self.wire)
 
68
                glPopMatrix()
 
69
                gl_color(FL_GRAY)
 
70
                glDisable(GL_DEPTH_TEST)
 
71
                if self.wire == 1:
 
72
                      gl_draw("Cube: wire", -4.5, -4.5)
 
73
                else:
 
74
                      gl_draw("Cube: flat", -4.5, -4.5)
 
75
                glEnable(GL_DEPTH_TEST)
 
76
          except:
 
77
                print "Exception: ", sys.exc_type, sys.exc_value
 
78
 
 
79
# the cube definition
 
80
v0 = [0.0, 0.0, 0.0]
 
81
v1 = [1.0, 0.0, 0.0]
 
82
v2 = [1.0, 1.0, 0.0]
 
83
v3 = [0.0, 1.0, 0.0]
 
84
v4 = [0.0, 0.0, 1.0]
 
85
v5 = [1.0, 0.0, 1.0]
 
86
v6 = [1.0, 1.0, 1.0]
 
87
v7 = [0.0, 1.0, 1.0]
 
88
 
 
89
 
 
90
def drawcube(wire):
 
91
    # draw a colored cube
 
92
    if wire == 1:
 
93
        glBegin(GL_LINE_LOOP)
 
94
    else:
 
95
        glBegin(GL_POLYGON)
 
96
    glColor3ub(0,0,255);
 
97
    glVertex3fv(v0)
 
98
    glVertex3fv(v1) 
 
99
    glVertex3fv(v2) 
 
100
    glVertex3fv(v3)
 
101
    glEnd()
 
102
 
 
103
    if wire == 1:
 
104
        glBegin(GL_LINE_LOOP)
 
105
    else:
 
106
        glBegin(GL_POLYGON)
 
107
    glColor3ub(0,255,255)
 
108
    glVertex3fv(v4)
 
109
    glVertex3fv(v5)
 
110
    glVertex3fv(v6)     
 
111
    glVertex3fv(v7)
 
112
    glEnd()
 
113
    if wire == 1:
 
114
        glBegin(GL_LINE_LOOP)
 
115
    else:
 
116
        glBegin(GL_POLYGON)
 
117
    glColor3ub(255,0,255)
 
118
    glVertex3fv(v0)
 
119
    glVertex3fv(v1)
 
120
    glVertex3fv(v5)     
 
121
    glVertex3fv(v4)
 
122
    glEnd()
 
123
    if wire == 1:
 
124
        glBegin(GL_LINE_LOOP)
 
125
    else:
 
126
        glBegin(GL_POLYGON)
 
127
    glColor3ub(255,255,0)
 
128
    glVertex3fv(v2)
 
129
    glVertex3fv(v3)
 
130
    glVertex3fv(v7)
 
131
    glVertex3fv(v6)
 
132
    glEnd()
 
133
    if wire == 1:
 
134
        glBegin(GL_LINE_LOOP)
 
135
    else:
 
136
        glBegin(GL_POLYGON)
 
137
    glColor3ub(0,255,0)
 
138
    glVertex3fv(v0)
 
139
    glVertex3fv(v4)
 
140
    glVertex3fv(v7)
 
141
    glVertex3fv(v3)
 
142
    glEnd()
 
143
    if wire == 1:
 
144
        glBegin(GL_LINE_LOOP)
 
145
    else:
 
146
        glBegin(GL_POLYGON)
 
147
    glColor3ub(255,0,0)
 
148
    glVertex3fv(v1)
 
149
    glVertex3fv(v2)
 
150
    glVertex3fv(v6)
 
151
    glVertex3fv(v5)
 
152
    glEnd()     
 
153
 
 
154
 
 
155
form = None
 
156
speed = None
 
157
size = None
 
158
button = None
 
159
wire = None
 
160
flat = None
 
161
cube = None
 
162
cube2 = None
 
163
 
 
164
class MyBox(Fl_Group):
 
165
      def __init__(self, type, x, y, w, h, l):
 
166
            Fl_Group.__init__(self, x, y, w, h, l)
 
167
            Fl_Group.box(self, type)
 
168
            self.end()
 
169
 
 
170
def exit_cb(ptr):
 
171
      sys.exit(0)
 
172
 
 
173
def makeform(name):
 
174
    global form
 
175
    global speed
 
176
    global size
 
177
    global wire
 
178
    global flat
 
179
    global button
 
180
    global cube
 
181
    global cube2
 
182
    form = Fl_Window(510+390,390,name)
 
183
    b1 = Fl_Box(FL_DOWN_FRAME,20,20,350,350,"")
 
184
    b2 = Fl_Box(FL_DOWN_FRAME,510,20,350,350,"")
 
185
    speed = Fl_Slider(FL_VERT_SLIDER,390,90,40,220,"Speed")
 
186
    size = Fl_Slider(FL_VERT_SLIDER,450,90,40,220,"Size")
 
187
    wire = Fl_Radio_Light_Button(390,20,100,30,"Wire")
 
188
    flat = Fl_Radio_Light_Button(390,50,100,30,"Flat")  
 
189
    button = Fl_Button(390,340,100,30,"Exit")
 
190
    button.callback(exit_cb)
 
191
    cube = cube_box(23,23,344,344, "")
 
192
    cube2 = cube_box(513,23,344,344, "")
 
193
    b = Fl_Box(FL_NO_BOX,cube.x(),size.y(), cube.w(),size.h(),"")
 
194
    #b = MyBox(FL_NO_BOX,cube.x(),size.y(), cube.w(),size.h(),"")
 
195
    form.resizable(b)
 
196
    b.hide()
 
197
    form.end()
 
198
 
 
199
 
 
200
if __name__ == '__main__':
 
201
   makeform(sys.argv[0])
 
202
 
 
203
   speed.bounds(4,0)
 
204
   cube.speed = 1.0
 
205
   cube2.speed = 1.0
 
206
   speed.value(1.0)
 
207
   size.bounds(4,0.01)
 
208
   cube.size = 1.0
 
209
   cube2.size = 1.0
 
210
   size.value(1.0)
 
211
   flat.value(1)
 
212
   cube.wire = 0
 
213
   cube2.wire = 1
 
214
   form.label("cube");
 
215
 
 
216
   form.show(len(sys.argv), sys.argv)
 
217
   cube.show()
 
218
   cube2.show()
 
219
 
 
220
   while 1:
 
221
         if form.visible() == 1 and speed.value() != 0:
 
222
            if Fl.check() == 0:
 
223
               break
 
224
         else:
 
225
            if Fl.wait() == 0:
 
226
               break
 
227
 
 
228
         cube.wire = wire.value()
 
229
         cube2.wire = not wire.value()
 
230
         cube.size = size.value()
 
231
         cube2.size = size.value()
 
232
         cube.speed = speed.value()
 
233
         cube2.speed = speed.value()
 
234
         cube.redraw()
 
235
         cube2.redraw()
 
236
         if Fl.readqueue() == button:
 
237
            break
 
238
 
 
239