summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/gl/kites.py
blob: 6e3dea6f49a4e39ed26052e6a11d99db799d8472 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/local/bin/python

# *** This only works correctly on a 24 bit-plane machine. ***
#
# A simple Python program that tests the some parts of the
# GL library. It shows the speed that can be obtained when
# doing simple graphics.
#
# The bottleneck in this program is NOT Python but the graphics
# engine; i.e Python can feed the graphics pipeline fast enough
# on the 4D/25G.
#
# This program show 3 kites flying around the screen. It uses
#
# 	* bgnpolygon, endpolygon
# 	* v3, n3
# 	* lmdef, lmbind
#
# Usage :
# 
# 	ESC 	-> exit program
# 	MOUSE3 	-> freeze toggle
# 	MOUSE2 	-> one step (use this in freeze state)

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

#
# viewobj : sets the rotation, translation and scaling
# set appropiate material, call drawobject()
#
def viewobj (r, s, t, mat) :
	pushmatrix()
	rot (r * 10.0, 'X')
	rot (r * 10.0, 'Y')
	rot (r * 10.0, 'Z')
	scale (s[0], s[1], s[2])
	translate (t[0], t[1], t[2])
	lmbind(MATERIAL, mat)
	drawobject()
	popmatrix()

#
# makeobj : the contructor of the object
#
def mkobj () :
	v0 = (-5.0 ,0.0, 0.0)
	v1 = (0.0 ,5.0, 0.0)
	v2 = (5.0 ,0.0, 0.0)
	v3 = (0.0 ,2.0, 0.0)
	n0 = (sqrt(2.0)/2.0, sqrt(2.0)/2.0, 0.0)
	vn = ((v0, n0), (v1, n0), (v2, n0), (v3, n0))
	#
	return vn

#
# the object itself as an array of vertices and normals
#
kite = mkobj ()

#
# drawobject : draw a triangle. with bgnpolygon
#
def drawobject () :
	#
	bgnpolygon()
	vnarray (kite)
	endpolygon()

#
# identity matrix
#
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]

#
# the rgb-value of light-blue 
#
LightBlue = (43,169,255)

#
# the different materials.
#
m1=[SPECULAR,0.0,0.0,0.6,DIFFUSE,0.0,0.0,0.8,SHININESS,20.0,LMNULL]
m2=[SPECULAR,0.8,0.0,0.1,DIFFUSE,0.8,0.0,0.3,SHININESS,120.0,LMNULL]
m3=[SPECULAR,0.0,1.0,0.0,DIFFUSE,0.0,0.6,0.0,SHININESS,120.0,LMNULL]

#
# lightsources
#
light1 = [LCOLOR,1.0,1.0,1.0,POSITION,15.0,15.0,0.0,1.0,LMNULL]
light2 = [LCOLOR,1.0,1.0,1.0,POSITION,-15.0,15.0,0.0,1.0,LMNULL]

#
# the lightmodel
#
model = [AMBIENT,0.2,0.2,0.2,LMNULL]

#
# initgl : opens the window, configures the pipeline to 2buf and zbuf,
# sets the viewing, defines and binds the materials
#
def initgl () :
	#
	# open window
	#
	foreground ()
	keepaspect (1, 1)
	prefposition (100, 500, 100, 500)
	w = winopen ('PYTHON lights')
	keepaspect (1, 1)
	winconstraints()
	#
	# configure pipeline (zbuf, 2buf, GOURAUD and RGBmode)
	#
	zbuffer (1)
	doublebuffer ()
	shademodel (GOURAUD)
	RGBmode ()
	gconfig ()
	#
	# define and bind materials (set perspective BEFORE loadmat !)
	#
	mmode(MVIEWING)
	perspective (900, 1.0, 1.0, 20.0)
	loadmatrix(idmat)
	lmdef(DEFMATERIAL, 1, m1)
	lmdef(DEFMATERIAL, 2, m2)
	lmdef(DEFMATERIAL, 3, m3)
	lmdef(DEFLIGHT, 1, light1)
	lmdef(DEFLIGHT, 2, light2)
	lmdef(DEFLMODEL, 1, model)
	lmbind(LIGHT0,1)
	lmbind(LIGHT1,2)
	lmbind(LMODEL,1)
	#
	# set viewing
	#
	lookat (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0)
	#
	# ask for the REDRAW and ESCKEY events
	#
	qdevice(DEVICE.MOUSE3)
	qdevice(DEVICE.MOUSE2)
	qdevice(DEVICE.REDRAW)
	qdevice(DEVICE.ESCKEY)

#
# GoForIT : use 2buf to redraw the object 2n times. index i is used as 
# the (smoothly changing) rotation angle
#
def GoForIt(i) :
	freeze = 1
	while 1 :
		if freeze <> 0 :
			i = i + 1
		#
		# clear z-buffer and clear background to light-blue
		#
		zclear()
		c3i (LightBlue)
		clear()
		#
		# draw the 3 traiangles scaled above each other.
		#
		viewobj(float(i),[1.0,1.0,1.0],[1.0,1.0,1.0],1)
		viewobj(float(i),[0.75,0.75,0.75],[0.0,2.0,2.0],2)
		viewobj(float(i),[0.5,0.5,0.5],[0.0,4.0,4.0],3)
		#
		swapbuffers()
		#
		if qtest() <> 0 :
			dev, val = qread()
			if dev == DEVICE.ESCKEY :
				break
			elif dev == DEVICE.REDRAW :
				reshapeviewport ()
			elif dev == DEVICE.MOUSE3 and val <> 0 :
				freeze = 1 - freeze
			elif dev == DEVICE.MOUSE2 and val <> 0 :
				i = i + 1


# the main program
#
def main () :
	initgl ()
	GoForIt (0)

#
# exec main
#
main  ()