summaryrefslogtreecommitdiff
path: root/glflag.mod/glflag.bmx
blob: e0993cf7c580a93ce9860567d75f364d89abbc42 (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
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 TGLFlag

	' These are public and can be used to modify the flag
	'
	Field xpos:Float
	Field ypos:Float
	Field zpos:Float
	Field xrot:Float
	Field yrot:Float
	Field zrot:Float
	
	' These are considered private
	'
	Global static:Int=False
	Global si:Float[]=Null
	Field v:TGLFlagVertex[,]
	Field cell:Int
	Field move:Int
	Field width:Int
	Field height:Int
	Field txt:Int

	' 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:TGLFlag(texture:Int, horiz:Int, vert:Int, size:Float, tile:Int, z:Float)
		Local o:TGLFlag
		
		If (horiz<2 Or vert<2)
			Return Null
		EndIf
		
		o=New TGLFlag
		
		If (Not o.static)
			o.si=New Float[3600]
			For Local a:Float=0 Until 3600
				o.si[a]=Sin(a/10)
			Next
			o.static=True
		EndIf
		
		o.xpos=0
		o.ypos=0
		o.zpos=z
		o.xrot=0
		o.yrot=0
		o.zrot=0
		
		o.txt=texture
		o.width=horiz
		o.height=vert
		o.cell=size
		o.move=size/2
		o.v=New TGLFlagVertex[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:TGLFlagVertex=New TGLFlagVertex
				v.x=ox+x*size
				v.y=oy+y*size
				v.z=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
	'	
	Method Render()
		glPushMatrix()
		glTranslatef(xpos,ypos,zpos)
		glRotatef(xrot,1,0,0)
		glRotatef(yrot,0,1,0)
		glRotatef(zrot,0,0,1)
		glBindTexture(GL_TEXTURE_2D,txt)
		
		For Local vert:TGLFlagVertex=EachIn v
			vert.ax=(vert.ax+vert.aix) Mod 3600
			vert.ay=(vert.ay+vert.aiy) Mod 3600
			vert.az=(vert.az+vert.aiz) Mod 3600
			vert.cx=vert.x+si[vert.ax]*move
			vert.cy=vert.y+si[vert.ay]*move
			vert.cz=vert.z+si[vert.az]*move
		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].cx,v[x,y].cy,v[x,y].cz)
				glTexCoord2f(v[x+1,y].u,v[x+1,y].v)
				glVertex3f(v[x+1,y].cx,v[x+1,y].cy,v[x+1,y].cz)
			Next
			glEnd()
		Next
		glPopMatrix()
	End Method
	
	' Sets the amount of movement for vertex movement (defaults to half the cell size)
	'
	Method SetMove(m:Int)
		move=m
	End Method
	
	' Sets the movements for a vertex
	'
	' x			- The X co-ord of the vertex
	' y			- The Y co-ord of the vertex
	' angz		- The initial angle (movement is done on a sine wave with 3600 positions) for the Z co-ord
	' angiz		- The angle increment for the Z co-ord
	' angx		- Ditto for X
	' angix		- Ditto for X
	' angy		- And Y
	' angiy		- And Y
	'
	' Note increments must be positive - to simulate negative ones pass a large increment (eg. 3599 for -1)
	'
	Method Nudge(x:Int, y:Int, angz:Float, angiz:Float=10, angx:Float=0, angix:Float=0, angy:Float=0, angiy:Float=0)
		v[x,y].ax=angx
		v[x,y].ay=angy
		v[x,y].az=angz
		v[x,y].aix=angix
		v[x,y].aiy=angiy
		v[x,y].aiz=angiz
	End Method
	
End Type

Type TGLFlagVertex
	Field x#,y#,z#
	Field cx#,cy#,cz#
	Field u#,v#
	Field ax#,ay#,az#
	Field aix#,aiy#,aiz#
	
	Method New()
		x=0
		y=0
		z=0
		cx=0
		cy=0
		cz=0
		ax=0
		ay=0
		az=0
		aix=0
		aiy=0
		aiz=0
	End Method
End Type