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
195
196
197
198
|
Module noddybox.glflag
ModuleInfo "Framework: Simple Open GL Backdrop flag"
ModuleInfo "Copyright: Public Domain"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"
' $Id$
Import brl.Basic
Import pub.OpenGL
Strict
Type GLFlag
' These are considered private
'
Const THRESHOLD:Float=0.0001
Field v:GLFlagVertex[,]
Field width:Int
Field height:Int
Field txt:Int
Field zpos:Float
' Creates a new flag
'
' texture - Flag texture
' horiz - Number of horizontal vertices
' vert - Number of vertical vertices
' size - Size of each square
' tile - Whether the texture is tiled or not
' z - Distance of flag (flags always created in the XY plane)
'
' Returns null for invalid parameters.
'
Function Create:GLFlag(texture:Int, horiz:Int, vert:Int, size:Float, tile:Int, z:Float)
Local o:GLFlag
If (horiz<2 Or vert<2)
Return Null
EndIf
o=New GLFlag
o.txt=texture
o.zpos=z
o.width=horiz
o.height=vert
o.v=New GLFlagVertex[horiz,vert]
Local ox:Float
Local oy:Float
ox=-(size*horiz)/2
oy=-(size*horiz)/2
For Local x=0 Until horiz
For Local y=0 Until vert
Local v:GLFlagVertex=New GLFlagVertex
v.Set(ox+x*size,oy+y*size,0)
If (tile)
v.u=(x&1)
v.v=(y&1)
Else
v.u=1.0/(horiz-1)*x
v.v=1.0/(vert-1)*(vert-1-y)
EndIf
o.v[x,y]=v
Next
Next
Return o
End Function
' Call to render the flag
'
' dampen - Scale to dampen movements by when bouncing movement
' friction - Scale to slow down movements
'
Method Render(dampen:Float=1.0, friction:Float=0.95)
glPushMatrix()
glTranslatef(0,0,zpos)
glBindTexture(GL_TEXTURE_2D,txt)
For Local vert:GLFlagVertex=EachIn v
If (vert.xi<>0 Or vert.yi<>0 Or vert.zi<>0)
Local s:Float
s=Sgn(vert.xi)
vert.xi:*friction
If (Abs(vert.xi)<THRESHOLD)
vert.initxi=vert.initxi*dampen
If (vert.initxi<THRESHOLD)
vert.xi=0
vert.x=vert.initx
Else
vert.xi=-s*vert.initxi*2
EndIf
Else
vert.x:+vert.xi
EndIf
s=Sgn(vert.yi)
vert.yi:*friction
If (Abs(vert.yi)<THRESHOLD)
vert.inityi=vert.inityi*dampen
If (vert.inityi<THRESHOLD)
vert.yi=0
vert.y=vert.inity
Else
vert.yi=-s*vert.inityi*2
EndIf
Else
vert.y:+vert.yi
EndIf
s=Sgn(vert.zi)
vert.zi:*friction
If (Abs(vert.zi)<THRESHOLD)
vert.initzi=vert.initzi*dampen
If (vert.initzi<THRESHOLD)
vert.zi=0
vert.z=vert.initx
Else
vert.zi=-s*vert.initzi*2
EndIf
Else
vert.z:+vert.zi
EndIf
EndIf
Next
For Local x:Int=0 Until width-1
glBegin(GL_TRIANGLE_STRIP)
For Local y:Int=0 Until height
glTexCoord2f(v[x,y].u,v[x,y].v)
glVertex3f(v[x,y].x,v[x,y].y,v[x,y].z)
glTexCoord2f(v[x+1,y].u,v[x+1,y].v)
glVertex3f(v[x+1,y].x,v[x+1,y].y,v[x+1,y].z)
Next
glEnd()
Next
glPopMatrix()
End Method
' Sets the movements for a vertex
'
' x - The X co-ord of the vertex
' y - The Y co-ord of the vertex
' xi - The X movement
' yi - The Y movement
' zi - The Z movement
'
Method Nudge(x:Int, y:Int, xi:Float, yi:Float, zi:Float)
v[x,y].initxi=Abs(xi)
v[x,y].inityi=Abs(yi)
v[x,y].initzi=Abs(zi)
v[x,y].xi=xi
v[x,y].yi=yi
v[x,y].zi=zi
End Method
End Type
Type GLFlagVertex
Field x#,y#,z#
Field u#,v#
Field initx#,inity#,initz#
Field initxi#,inityi#,initzi#
Field xi#,yi#,zi#
Method New()
x=0
y=0
z=0
initx=0
inity=0
initz=0
initxi=0
inityi=0
initzi=0
xi=0
yi=0
zi=0
End Method
Method Set(xc:Float, yc:Float, zc:Float)
x=xc
initx=xc
y=yc
inity=yc
z=zc
initz=zc
End Method
End Type
|