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

  1. #! /usr/bin/env python
  2.  
  3. #   backface
  4. #
  5. #   draw a cube that can run with backface() turned on or off.
  6. #   cube is moved when LEFTMOUSE is pressed and mouse itself is moved.
  7.  
  8. from gl import *
  9. from DEVICE import *
  10. from GL import *
  11.  
  12. CUBE_SIZE = 200.0
  13. CUBE_OBJ = 1
  14.  
  15. def main () :
  16.     #
  17.     x = 0
  18.     y = 0
  19.     moveit = 0
  20.     #
  21.     initialize()
  22.     #
  23.     while (1) :
  24.         #
  25.         while (qtest()) :
  26.             dev, val = qread()
  27.             #
  28.             if dev == ESCKEY :
  29.                 backface(0)
  30.                 return
  31.                 #
  32.             elif dev == REDRAW :
  33.                 reshapeviewport()
  34.                 drawcube(x,y)
  35.                 #
  36.             elif dev == LEFTMOUSE :
  37.                 #
  38.                 # LEFTMOUSE down
  39.                 moveit = val
  40.                 #
  41.             elif dev == BKEY :
  42.                 backface(1)
  43.                 drawcube(x,y)
  44.                 #
  45.             elif dev == FKEY :
  46.                 backface(0)
  47.                 drawcube(x,y)
  48.                 #
  49.         if moveit :
  50.             x = getvaluator(MOUSEX)
  51.             y = getvaluator(MOUSEY)
  52.             drawcube(x,y)
  53.  
  54.  
  55. def initialize () :
  56.     foreground ()
  57.     keepaspect (1, 1)
  58.     gid = winopen('backface')
  59.     winset(gid)
  60.     winconstraints()
  61.     #
  62.     doublebuffer()
  63.     gconfig()
  64.     shademodel(FLAT)
  65.     #
  66.     ortho(-1024.0, 1024.0, -1024.0, 1024.0, -1024.0, 1024.0)
  67.     #
  68.     qdevice(ESCKEY)
  69.     qdevice(REDRAW)
  70.     qdevice(LEFTMOUSE)
  71.     qdevice(BKEY)
  72.     qdevice(FKEY)
  73.     qenter(REDRAW,gid)
  74.     #
  75.     backface(1)
  76.  
  77. #
  78. # define a cube
  79. def cube () :
  80.     #
  81.     # front face
  82.     pushmatrix()
  83.     translate(0.0,0.0,CUBE_SIZE)
  84.     color(RED)
  85.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  86.     popmatrix()
  87.     #
  88.     # right face
  89.     pushmatrix()
  90.     translate(CUBE_SIZE, 0.0, 0.0)
  91.     rotate(900, 'y')
  92.     color(GREEN)
  93.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  94.     popmatrix()
  95.     #
  96.     # back face
  97.     pushmatrix()
  98.     translate(0.0, 0.0, -CUBE_SIZE)
  99.     rotate(1800, 'y')
  100.     color(BLUE)
  101.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  102.     popmatrix()
  103.     #
  104.     # left face
  105.     pushmatrix()
  106.     translate(-CUBE_SIZE, 0.0, 0.0)
  107.     rotate(-900, 'y')
  108.     color(CYAN)
  109.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  110.     popmatrix()
  111.     #
  112.     # top face
  113.     pushmatrix()
  114.     translate(0.0, CUBE_SIZE, 0.0)
  115.     rotate(-900, 'x')
  116.     color(MAGENTA)
  117.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  118.     popmatrix()
  119.     #
  120.     # bottom face
  121.     pushmatrix()
  122.     translate(0.0, -CUBE_SIZE, 0.0)
  123.     rotate(900, 'x')
  124.     color(YELLOW)
  125.     rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  126.     popmatrix()
  127.  
  128. def drawcube(x,y) :
  129.     #
  130.     pushmatrix()
  131.     rotate(2*x, 'x')
  132.     rotate(2*y, 'y')
  133.     color(BLACK)
  134.     clear()
  135.     cube()        
  136.     popmatrix()
  137.     swapbuffers()
  138.  
  139.  
  140. main ()
  141.