Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 453bd40 | 1992-03-30 13:18:37 +0000 | [diff] [blame] | 2 | |
| 3 | # *** This only works correctly on a 24 bit-plane machine. *** |
| 4 | # |
| 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. |
| 8 | # |
| 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. |
| 12 | # |
| 13 | # This program show 3 kites flying around the screen. It uses |
| 14 | # |
| 15 | # * bgnpolygon, endpolygon |
| 16 | # * v3, n3 |
| 17 | # * lmdef, lmbind |
| 18 | # |
| 19 | # Usage : |
| 20 | # |
| 21 | # ESC -> exit program |
| 22 | # MOUSE3 -> freeze toggle |
| 23 | # MOUSE2 -> one step (use this in freeze state) |
| 24 | |
| 25 | from GL import * |
| 26 | from gl import * |
| 27 | import DEVICE |
| 28 | from math import * |
| 29 | |
| 30 | # |
| 31 | # viewobj : sets the rotation, translation and scaling |
| 32 | # set appropiate material, call drawobject() |
| 33 | # |
| 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() |
| 44 | |
| 45 | # |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 46 | # makeobj : the constructor of the object |
Guido van Rossum | 453bd40 | 1992-03-30 13:18:37 +0000 | [diff] [blame] | 47 | # |
| 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)) |
| 55 | # |
| 56 | return vn |
| 57 | |
| 58 | # |
| 59 | # the object itself as an array of vertices and normals |
| 60 | # |
| 61 | kite = mkobj () |
| 62 | |
| 63 | # |
| 64 | # drawobject : draw a triangle. with bgnpolygon |
| 65 | # |
| 66 | def drawobject () : |
| 67 | # |
| 68 | bgnpolygon() |
| 69 | vnarray (kite) |
| 70 | endpolygon() |
| 71 | |
| 72 | # |
| 73 | # identity matrix |
| 74 | # |
| 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] |
| 76 | |
| 77 | # |
| 78 | # the rgb-value of light-blue |
| 79 | # |
| 80 | LightBlue = (43,169,255) |
| 81 | |
| 82 | # |
| 83 | # the different materials. |
| 84 | # |
| 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] |
| 88 | |
| 89 | # |
| 90 | # lightsources |
| 91 | # |
| 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] |
| 94 | |
| 95 | # |
| 96 | # the lightmodel |
| 97 | # |
| 98 | model = [AMBIENT,0.2,0.2,0.2,LMNULL] |
| 99 | |
| 100 | # |
| 101 | # initgl : opens the window, configures the pipeline to 2buf and zbuf, |
| 102 | # sets the viewing, defines and binds the materials |
| 103 | # |
| 104 | def initgl () : |
| 105 | # |
| 106 | # open window |
| 107 | # |
| 108 | foreground () |
| 109 | keepaspect (1, 1) |
| 110 | prefposition (100, 500, 100, 500) |
| 111 | w = winopen ('PYTHON lights') |
| 112 | keepaspect (1, 1) |
| 113 | winconstraints() |
| 114 | # |
| 115 | # configure pipeline (zbuf, 2buf, GOURAUD and RGBmode) |
| 116 | # |
| 117 | zbuffer (1) |
| 118 | doublebuffer () |
| 119 | shademodel (GOURAUD) |
| 120 | RGBmode () |
| 121 | gconfig () |
| 122 | # |
| 123 | # define and bind materials (set perspective BEFORE loadmat !) |
| 124 | # |
| 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) |
| 137 | # |
| 138 | # set viewing |
| 139 | # |
| 140 | lookat (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0) |
| 141 | # |
| 142 | # ask for the REDRAW and ESCKEY events |
| 143 | # |
| 144 | qdevice(DEVICE.MOUSE3) |
| 145 | qdevice(DEVICE.MOUSE2) |
| 146 | qdevice(DEVICE.REDRAW) |
| 147 | qdevice(DEVICE.ESCKEY) |
| 148 | |
| 149 | # |
| 150 | # GoForIT : use 2buf to redraw the object 2n times. index i is used as |
| 151 | # the (smoothly changing) rotation angle |
| 152 | # |
| 153 | def GoForIt(i) : |
| 154 | freeze = 1 |
| 155 | while 1 : |
| 156 | if freeze <> 0 : |
| 157 | i = i + 1 |
| 158 | # |
| 159 | # clear z-buffer and clear background to light-blue |
| 160 | # |
| 161 | zclear() |
| 162 | c3i (LightBlue) |
| 163 | clear() |
| 164 | # |
| 165 | # draw the 3 traiangles scaled above each other. |
| 166 | # |
| 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) |
| 170 | # |
| 171 | swapbuffers() |
| 172 | # |
| 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 |
| 183 | |
| 184 | |
| 185 | # the main program |
| 186 | # |
| 187 | def main () : |
| 188 | initgl () |
| 189 | GoForIt (0) |
| 190 | |
| 191 | # |
| 192 | # exec main |
| 193 | # |
| 194 | main () |