py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Demo / sgi / gl / kites.py
blobcde69cb2e80938003e081940053fe90646b44d19
1 #! /usr/bin/env python
3 # *** This only works correctly on a 24 bit-plane machine. ***
5 # A simple Python program that tests the some parts of the
6 # GL library. It shows the speed that can be obtained when
7 # doing simple graphics.
9 # The bottleneck in this program is NOT Python but the graphics
10 # engine; i.e Python can feed the graphics pipeline fast enough
11 # on the 4D/25G.
13 # This program show 3 kites flying around the screen. It uses
15 # * bgnpolygon, endpolygon
16 # * v3, n3
17 # * lmdef, lmbind
19 # Usage :
21 # ESC -> exit program
22 # MOUSE3 -> freeze toggle
23 # MOUSE2 -> one step (use this in freeze state)
25 from GL import *
26 from gl import *
27 import DEVICE
28 from math import *
31 # viewobj : sets the rotation, translation and scaling
32 # set appropiate material, call drawobject()
34 def viewobj (r, s, t, mat) :
35 pushmatrix()
36 rot (r * 10.0, 'X')
37 rot (r * 10.0, 'Y')
38 rot (r * 10.0, 'Z')
39 scale (s[0], s[1], s[2])
40 translate (t[0], t[1], t[2])
41 lmbind(MATERIAL, mat)
42 drawobject()
43 popmatrix()
46 # makeobj : the constructor of the object
48 def mkobj () :
49 v0 = (-5.0 ,0.0, 0.0)
50 v1 = (0.0 ,5.0, 0.0)
51 v2 = (5.0 ,0.0, 0.0)
52 v3 = (0.0 ,2.0, 0.0)
53 n0 = (sqrt(2.0)/2.0, sqrt(2.0)/2.0, 0.0)
54 vn = ((v0, n0), (v1, n0), (v2, n0), (v3, n0))
56 return vn
59 # the object itself as an array of vertices and normals
61 kite = mkobj ()
64 # drawobject : draw a triangle. with bgnpolygon
66 def drawobject () :
68 bgnpolygon()
69 vnarray (kite)
70 endpolygon()
73 # identity matrix
75 idmat=[1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0]
78 # the rgb-value of light-blue
80 LightBlue = (43,169,255)
83 # the different materials.
85 m1=[SPECULAR,0.0,0.0,0.6,DIFFUSE,0.0,0.0,0.8,SHININESS,20.0,LMNULL]
86 m2=[SPECULAR,0.8,0.0,0.1,DIFFUSE,0.8,0.0,0.3,SHININESS,120.0,LMNULL]
87 m3=[SPECULAR,0.0,1.0,0.0,DIFFUSE,0.0,0.6,0.0,SHININESS,120.0,LMNULL]
90 # lightsources
92 light1 = [LCOLOR,1.0,1.0,1.0,POSITION,15.0,15.0,0.0,1.0,LMNULL]
93 light2 = [LCOLOR,1.0,1.0,1.0,POSITION,-15.0,15.0,0.0,1.0,LMNULL]
96 # the lightmodel
98 model = [AMBIENT,0.2,0.2,0.2,LMNULL]
101 # initgl : opens the window, configures the pipeline to 2buf and zbuf,
102 # sets the viewing, defines and binds the materials
104 def initgl () :
106 # open window
108 foreground ()
109 keepaspect (1, 1)
110 prefposition (100, 500, 100, 500)
111 w = winopen ('PYTHON lights')
112 keepaspect (1, 1)
113 winconstraints()
115 # configure pipeline (zbuf, 2buf, GOURAUD and RGBmode)
117 zbuffer (1)
118 doublebuffer ()
119 shademodel (GOURAUD)
120 RGBmode ()
121 gconfig ()
123 # define and bind materials (set perspective BEFORE loadmat !)
125 mmode(MVIEWING)
126 perspective (900, 1.0, 1.0, 20.0)
127 loadmatrix(idmat)
128 lmdef(DEFMATERIAL, 1, m1)
129 lmdef(DEFMATERIAL, 2, m2)
130 lmdef(DEFMATERIAL, 3, m3)
131 lmdef(DEFLIGHT, 1, light1)
132 lmdef(DEFLIGHT, 2, light2)
133 lmdef(DEFLMODEL, 1, model)
134 lmbind(LIGHT0,1)
135 lmbind(LIGHT1,2)
136 lmbind(LMODEL,1)
138 # set viewing
140 lookat (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0)
142 # ask for the REDRAW and ESCKEY events
144 qdevice(DEVICE.MOUSE3)
145 qdevice(DEVICE.MOUSE2)
146 qdevice(DEVICE.REDRAW)
147 qdevice(DEVICE.ESCKEY)
150 # GoForIT : use 2buf to redraw the object 2n times. index i is used as
151 # the (smoothly changing) rotation angle
153 def GoForIt(i) :
154 freeze = 1
155 while 1 :
156 if freeze <> 0 :
157 i = i + 1
159 # clear z-buffer and clear background to light-blue
161 zclear()
162 c3i (LightBlue)
163 clear()
165 # draw the 3 traiangles scaled above each other.
167 viewobj(float(i),[1.0,1.0,1.0],[1.0,1.0,1.0],1)
168 viewobj(float(i),[0.75,0.75,0.75],[0.0,2.0,2.0],2)
169 viewobj(float(i),[0.5,0.5,0.5],[0.0,4.0,4.0],3)
171 swapbuffers()
173 if qtest() <> 0 :
174 dev, val = qread()
175 if dev == DEVICE.ESCKEY :
176 break
177 elif dev == DEVICE.REDRAW :
178 reshapeviewport ()
179 elif dev == DEVICE.MOUSE3 and val <> 0 :
180 freeze = 1 - freeze
181 elif dev == DEVICE.MOUSE2 and val <> 0 :
182 i = i + 1
185 # the main program
187 def main () :
188 initgl ()
189 GoForIt (0)
192 # exec main
194 main ()