nurbs.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Demo » sgi » gl » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Demo » sgi » gl » nurbs.py
#! /usr/bin/env python

# Rotate a 3D surface created using NURBS.
#
# Press left mouse button to toggle surface trimming.
# Press ESC to quit.
#
# See the GL manual for an explanation of NURBS.

from gl import *
from GL import *
from DEVICE import *

TRUE = 1
FALSE = 0
ORDER = 4

idmat = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]

surfknots = [-1, -1, -1, -1, 1, 1, 1, 1]

def make_ctlpoints():
  c = []
  #
  ci = []
  ci.append((-2.5,  -3.7,  1.0))
  ci.append((-1.5,  -3.7,  3.0))
  ci.append((1.5,  -3.7, -2.5))
  ci.append((2.5,  -3.7,  -0.75))
  c.append(ci)
  #
  ci = []
  ci.append((-2.5,  -2.0,  3.0))
  ci.append((-1.5,  -2.0,  4.0))
  ci.append((1.5,  -2.0,  -3.0))
  ci.append((2.5,  -2.0,  0.0))
  c.append(ci)
  #
  ci = []
  ci.append((-2.5, 2.0,  1.0))
  ci.append((-1.5, 2.0,  0.0))
  ci.append((1.5,  2.0,  -1.0))
  ci.append((2.5,  2.0,  2.0))
  c.append(ci)
  #
  ci = []
  ci.append((-2.5,  2.7,  1.25))
  ci.append((-1.5,  2.7,  0.1))
  ci.append((1.5,  2.7,  -0.6))
  ci.append((2.5,  2.7,  0.2))
  c.append(ci)
  #
  return c

ctlpoints = make_ctlpoints()

trimknots = [0., 0., 0.,  1., 1.,  2., 2.,  3., 3.,   4., 4., 4.]

def make_trimpoints():
  c = []
  c.append((1.0, 0.0, 1.0))
  c.append((1.0, 1.0, 1.0))
  c.append((0.0, 2.0, 2.0))
  c.append((-1.0, 1.0, 1.0))
  c.append((-1.0, 0.0, 1.0))
  c.append((-1.0, -1.0, 1.0))
  c.append((0.0, -2.0, 2.0))
  c.append((1.0, -1.0, 1.0) )
  c.append((1.0, 0.0, 1.0))
  return c

trimpoints = make_trimpoints()

def main():
  init_windows()
  setup_queue()
  make_lights()
  init_view()
  #
  set_scene()
  setnurbsproperty( N_ERRORCHECKING, 1.0 )
  setnurbsproperty( N_PIXEL_TOLERANCE, 50.0 )
  trim_flag = 0
  draw_trim_surface(trim_flag)
  #
  while 1:
    while qtest():
      dev, val = qread()
      if dev == ESCKEY:
        return
      elif dev == WINQUIT:
        dglclose(-1)  # this for DGL only
        return
      elif dev == REDRAW:
        reshapeviewport()
        set_scene()
        draw_trim_surface(trim_flag)
      elif dev == LEFTMOUSE:
        if val:
          trim_flag = (not trim_flag)
    set_scene()
    draw_trim_surface(trim_flag)

def init_windows():
  foreground()
  #prefposition(0, 500, 0, 500)
  wid = winopen('nurbs')
  wintitle('NURBS Surface')
  doublebuffer()
  RGBmode()
  gconfig()
  lsetdepth(0x000, 0x7fffff)
  zbuffer( TRUE )

def setup_queue():
  qdevice(ESCKEY)
  qdevice(REDRAW)
  qdevice(RIGHTMOUSE)
  qdevice(WINQUIT)
  qdevice(LEFTMOUSE) #trimming

def init_view():
  mmode(MPROJECTION)
  ortho( -4., 4., -4., 4., -4., 4. )
  #
  mmode(MVIEWING)
  loadmatrix(idmat)
  #
  lmbind(MATERIAL, 1)

def set_scene():
  lmbind(MATERIAL, 0)
  RGBcolor(150,150,150)
  lmbind(MATERIAL, 1)
  clear()
  zclear()
  #
  rotate( 100, 'y' )
  rotate( 100, 'z' )

def draw_trim_surface(trim_flag):
  bgnsurface()
  nurbssurface(surfknots, surfknots, ctlpoints, ORDER, ORDER, N_XYZ)
  if trim_flag:
    bgntrim()
    nurbscurve(trimknots, trimpoints, ORDER-1, N_STW)
    endtrim()
  endsurface()
  swapbuffers()

def make_lights():
  lmdef(DEFLMODEL,1,[])
  lmdef(DEFLIGHT,1,[])
  #
  # define material #1
  #
  a = []
  a = a + [EMISSION, 0.0, 0.0, 0.0]
  a = a + [AMBIENT,  0.1, 0.1, 0.1]
  a = a + [DIFFUSE,  0.6, 0.3, 0.3]
  a = a + [SPECULAR,  0.0, 0.6, 0.0]
  a = a + [SHININESS, 2.0]
  a = a + [LMNULL]
  lmdef(DEFMATERIAL, 1, a)
  #
  # turn on lighting
  #
  lmbind(LIGHT0, 1)
  lmbind(LMODEL, 1)

main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.