| 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 | #   zrgb  (Requires Z buffer.) | 
|  | 4 | # | 
|  | 5 | # This program demostrates zbuffering 3 intersecting RGB polygons while | 
|  | 6 | # in doublebuffer mode where, movement of the mouse with the LEFTMOUSE | 
|  | 7 | # button depressed will, rotate the 3 polygons. This is done by compound | 
|  | 8 | # rotations allowing continuous screen-oriented rotations. | 
|  | 9 | # | 
|  | 10 | #    Press the "Esc" key to exit. | 
|  | 11 |  | 
|  | 12 | from gl import * | 
|  | 13 | from GL import * | 
|  | 14 | from DEVICE import * | 
|  | 15 |  | 
|  | 16 |  | 
|  | 17 | idmat=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1] | 
|  | 18 |  | 
|  | 19 | def main() : | 
|  | 20 | # | 
|  | 21 | # old and new mouse position | 
|  | 22 | # | 
|  | 23 | # | 
|  | 24 | mode = 0 | 
|  | 25 | omx = 0 | 
|  | 26 | mx = 0 | 
|  | 27 | omy = 0 | 
|  | 28 | my = 0 | 
|  | 29 | # | 
|  | 30 | objmat=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1] | 
|  | 31 | # | 
|  | 32 | initialize () | 
|  | 33 | # | 
|  | 34 | draw_scene (objmat) | 
|  | 35 | # | 
|  | 36 | while (1) : | 
|  | 37 | # | 
|  | 38 | dev, val = qread() | 
|  | 39 | # | 
|  | 40 | if dev == ESCKEY : | 
|  | 41 | if val : | 
|  | 42 | break | 
|  | 43 | # exit when key is going up, not down | 
|  | 44 | # this avoids the scenario where a window | 
|  | 45 | # underneath this program's window | 
|  | 46 | # would otherwise "eat up" the up- | 
|  | 47 | # event of the Esc key being released | 
|  | 48 | return | 
|  | 49 | # | 
|  | 50 | elif dev == REDRAW : | 
|  | 51 | reshapeviewport() | 
|  | 52 | draw_scene(objmat) | 
|  | 53 | # | 
|  | 54 | elif dev == LEFTMOUSE: | 
|  | 55 | omx = mx | 
|  | 56 | omy = my | 
|  | 57 | if val : | 
|  | 58 | mode = 1 | 
|  | 59 | else : | 
|  | 60 | mode = 0 | 
|  | 61 | elif dev == MOUSEX : | 
|  | 62 | omx = mx | 
|  | 63 | mx = val | 
|  | 64 | #print omx, mx | 
|  | 65 | objmat = update_scene(objmat,mx,my,omx,omy,mode) | 
|  | 66 | # | 
|  | 67 | elif dev == MOUSEY : | 
|  | 68 | omy = my | 
|  | 69 | my = val | 
|  | 70 | #print omy, my | 
|  | 71 | objmat = update_scene(objmat,mx,my,omx,omy,mode) | 
|  | 72 | # | 
|  | 73 |  | 
|  | 74 |  | 
|  | 75 | def initialize () : | 
|  | 76 | # | 
|  | 77 | foreground () | 
|  | 78 | keepaspect(5, 4) | 
|  | 79 | w = winopen('Zbuffered RGB') | 
|  | 80 | # | 
|  | 81 | doublebuffer() | 
|  | 82 | RGBmode() | 
|  | 83 | gconfig() | 
|  | 84 | zbuffer(1) | 
|  | 85 | lsetdepth(0x0, 0x7FFFFF) | 
|  | 86 | # | 
|  | 87 | qdevice(ESCKEY) | 
|  | 88 | qdevice(LEFTMOUSE) | 
|  | 89 | qdevice(MOUSEX) | 
|  | 90 | qdevice(MOUSEY) | 
|  | 91 |  | 
|  | 92 | def update_scene (mat, mx, my, omx, omy, mode) : | 
|  | 93 | # | 
|  | 94 | if mode == 1 : | 
|  | 95 | mat = orient(mat, mx, my, omx, omy) | 
|  | 96 | draw_scene(mat) | 
|  | 97 | return mat | 
|  | 98 |  | 
|  | 99 | def orient (mat, mx, my, omx, omy) : | 
|  | 100 | # | 
|  | 101 | # | 
|  | 102 | pushmatrix() | 
|  | 103 | loadmatrix(idmat) | 
|  | 104 | # | 
|  | 105 | if mx - omx : rot (float (mx - omx), 'y') | 
|  | 106 | if omy - my : rot (float (omy - my), 'x') | 
|  | 107 | # | 
|  | 108 | multmatrix(mat) | 
|  | 109 | mat = getmatrix() | 
|  | 110 | # | 
|  | 111 | popmatrix() | 
|  | 112 | # | 
|  | 113 | return mat | 
|  | 114 |  | 
|  | 115 | def draw_scene (mat) : | 
|  | 116 | RGBcolor(40, 100, 200) | 
|  | 117 | clear() | 
|  | 118 | zclear() | 
|  | 119 | # | 
|  | 120 | perspective(400, 1.25, 30.0, 60.0) | 
|  | 121 | translate(0.0, 0.0, -40.0) | 
|  | 122 | multmatrix(mat) | 
|  | 123 | # | 
|  | 124 | # skews original view to show all polygons | 
|  | 125 | # | 
|  | 126 | rotate(-580, 'y') | 
|  | 127 | draw_polys() | 
|  | 128 | # | 
|  | 129 | swapbuffers() | 
|  | 130 |  | 
|  | 131 | polygon1 = [(-10.0,-10.0,0.0),(10.0,-10.0,0.0),(-10.0,10.0,0.0)] | 
|  | 132 |  | 
|  | 133 | polygon2 = [(0.0,-10.0,-10.0),(0.0,-10.0,10.0),(0.0,5.0,-10.0)] | 
|  | 134 |  | 
|  | 135 | polygon3 = [(-10.0,6.0,4.0),(-10.0,3.0,4.0),(4.0,-9.0,-10.0),(4.0,-6.0,-10.0)] | 
|  | 136 |  | 
|  | 137 | def draw_polys(): | 
|  | 138 | bgnpolygon() | 
|  | 139 | cpack(0x0) | 
|  | 140 | v3f(polygon1[0]) | 
|  | 141 | cpack(0x007F7F7F) | 
|  | 142 | v3f(polygon1[1]) | 
|  | 143 | cpack(0x00FFFFFF) | 
|  | 144 | v3f(polygon1[2]) | 
|  | 145 | endpolygon() | 
|  | 146 | # | 
|  | 147 | bgnpolygon() | 
|  | 148 | cpack(0x0000FFFF) | 
|  | 149 | v3f(polygon2[0]) | 
|  | 150 | cpack(0x007FFF00) | 
|  | 151 | v3f(polygon2[1]) | 
|  | 152 | cpack(0x00FF0000) | 
|  | 153 | v3f(polygon2[2]) | 
|  | 154 | endpolygon() | 
|  | 155 | # | 
|  | 156 | bgnpolygon() | 
|  | 157 | cpack(0x0000FFFF) | 
|  | 158 | v3f(polygon3[0]) | 
|  | 159 | cpack(0x00FF00FF) | 
|  | 160 | v3f(polygon3[1]) | 
|  | 161 | cpack(0x00FF0000) | 
|  | 162 | v3f(polygon3[2]) | 
|  | 163 | cpack(0x00FF00FF) | 
|  | 164 | v3f(polygon3[3]) | 
|  | 165 | endpolygon() | 
|  | 166 |  | 
|  | 167 |  | 
|  | 168 | main () |