home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / sgi / gl / kites.py < prev    next >
Text File  |  1996-11-27  |  4KB  |  195 lines

  1. #! /usr/bin/env python
  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. #     ESC     -> exit program
  21. #     MOUSE3     -> freeze toggle
  22. #     MOUSE2     -> one step (use this in freeze state)
  23.  
  24. from GL import *
  25. from gl import *
  26. import DEVICE
  27. from math import *
  28.  
  29. #
  30. # viewobj : sets the rotation, translation and scaling
  31. # set appropiate material, call drawobject()
  32. #
  33. def viewobj (r, s, t, mat) :
  34.     pushmatrix()
  35.     rot (r * 10.0, 'X')
  36.     rot (r * 10.0, 'Y')
  37.     rot (r * 10.0, 'Z')
  38.     scale (s[0], s[1], s[2])
  39.     translate (t[0], t[1], t[2])
  40.     lmbind(MATERIAL, mat)
  41.     drawobject()
  42.     popmatrix()
  43.  
  44. #
  45. # makeobj : the contructor of the object
  46. #
  47. def mkobj () :
  48.     v0 = (-5.0 ,0.0, 0.0)
  49.     v1 = (0.0 ,5.0, 0.0)
  50.     v2 = (5.0 ,0.0, 0.0)
  51.     v3 = (0.0 ,2.0, 0.0)
  52.     n0 = (sqrt(2.0)/2.0, sqrt(2.0)/2.0, 0.0)
  53.     vn = ((v0, n0), (v1, n0), (v2, n0), (v3, n0))
  54.     #
  55.     return vn
  56.  
  57. #
  58. # the object itself as an array of vertices and normals
  59. #
  60. kite = mkobj ()
  61.  
  62. #
  63. # drawobject : draw a triangle. with bgnpolygon
  64. #
  65. def drawobject () :
  66.     #
  67.     bgnpolygon()
  68.     vnarray (kite)
  69.     endpolygon()
  70.  
  71. #
  72. # identity matrix
  73. #
  74. 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]
  75.  
  76. #
  77. # the rgb-value of light-blue 
  78. #
  79. LightBlue = (43,169,255)
  80.  
  81. #
  82. # the different materials.
  83. #
  84. m1=[SPECULAR,0.0,0.0,0.6,DIFFUSE,0.0,0.0,0.8,SHININESS,20.0,LMNULL]
  85. m2=[SPECULAR,0.8,0.0,0.1,DIFFUSE,0.8,0.0,0.3,SHININESS,120.0,LMNULL]
  86. m3=[SPECULAR,0.0,1.0,0.0,DIFFUSE,0.0,0.6,0.0,SHININESS,120.0,LMNULL]
  87.  
  88. #
  89. # lightsources
  90. #
  91. light1 = [LCOLOR,1.0,1.0,1.0,POSITION,15.0,15.0,0.0,1.0,LMNULL]
  92. light2 = [LCOLOR,1.0,1.0,1.0,POSITION,-15.0,15.0,0.0,1.0,LMNULL]
  93.  
  94. #
  95. # the lightmodel
  96. #
  97. model = [AMBIENT,0.2,0.2,0.2,LMNULL]
  98.  
  99. #
  100. # initgl : opens the window, configures the pipeline to 2buf and zbuf,
  101. # sets the viewing, defines and binds the materials
  102. #
  103. def initgl () :
  104.     #
  105.     # open window
  106.     #
  107.     foreground ()
  108.     keepaspect (1, 1)
  109.     prefposition (100, 500, 100, 500)
  110.     w = winopen ('PYTHON lights')
  111.     keepaspect (1, 1)
  112.     winconstraints()
  113.     #
  114.     # configure pipeline (zbuf, 2buf, GOURAUD and RGBmode)
  115.     #
  116.     zbuffer (1)
  117.     doublebuffer ()
  118.     shademodel (GOURAUD)
  119.     RGBmode ()
  120.     gconfig ()
  121.     #
  122.     # define and bind materials (set perspective BEFORE loadmat !)
  123.     #
  124.     mmode(MVIEWING)
  125.     perspective (900, 1.0, 1.0, 20.0)
  126.     loadmatrix(idmat)
  127.     lmdef(DEFMATERIAL, 1, m1)
  128.     lmdef(DEFMATERIAL, 2, m2)
  129.     lmdef(DEFMATERIAL, 3, m3)
  130.     lmdef(DEFLIGHT, 1, light1)
  131.     lmdef(DEFLIGHT, 2, light2)
  132.     lmdef(DEFLMODEL, 1, model)
  133.     lmbind(LIGHT0,1)
  134.     lmbind(LIGHT1,2)
  135.     lmbind(LMODEL,1)
  136.     #
  137.     # set viewing
  138.     #
  139.     lookat (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0)
  140.     #
  141.     # ask for the REDRAW and ESCKEY events
  142.     #
  143.     qdevice(DEVICE.MOUSE3)
  144.     qdevice(DEVICE.MOUSE2)
  145.     qdevice(DEVICE.REDRAW)
  146.     qdevice(DEVICE.ESCKEY)
  147.  
  148. #
  149. # GoForIT : use 2buf to redraw the object 2n times. index i is used as 
  150. # the (smoothly changing) rotation angle
  151. #
  152. def GoForIt(i) :
  153.     freeze = 1
  154.     while 1 :
  155.         if freeze <> 0 :
  156.             i = i + 1
  157.         #
  158.         # clear z-buffer and clear background to light-blue
  159.         #
  160.         zclear()
  161.         c3i (LightBlue)
  162.         clear()
  163.         #
  164.         # draw the 3 traiangles scaled above each other.
  165.         #
  166.         viewobj(float(i),[1.0,1.0,1.0],[1.0,1.0,1.0],1)
  167.         viewobj(float(i),[0.75,0.75,0.75],[0.0,2.0,2.0],2)
  168.         viewobj(float(i),[0.5,0.5,0.5],[0.0,4.0,4.0],3)
  169.         #
  170.         swapbuffers()
  171.         #
  172.         if qtest() <> 0 :
  173.             dev, val = qread()
  174.             if dev == DEVICE.ESCKEY :
  175.                 break
  176.             elif dev == DEVICE.REDRAW :
  177.                 reshapeviewport ()
  178.             elif dev == DEVICE.MOUSE3 and val <> 0 :
  179.                 freeze = 1 - freeze
  180.             elif dev == DEVICE.MOUSE2 and val <> 0 :
  181.                 i = i + 1
  182.  
  183.  
  184. # the main program
  185. #
  186. def main () :
  187.     initgl ()
  188.     GoForIt (0)
  189.  
  190. #
  191. # exec main
  192. #
  193. main  ()
  194.