summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/gl/nurbs.py
blob: fb76aa9f3958ca783478b07cc2fba730decf4a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /usr/local/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()