diff options
author | Ian C <ianc@noddybox.co.uk> | 2005-08-15 01:55:25 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2005-08-15 01:55:25 +0000 |
commit | f37162283c31f6a48dc8f2dd9e7059c741618fc9 (patch) | |
tree | 0198201058b158a227d0637b201eb93ef54e4426 | |
parent | a0ea730f4028df72eb2c13b1275a2d6511c013d0 (diff) |
*** empty log message ***
-rw-r--r-- | glflag.mod/glflag.bmx | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/glflag.mod/glflag.bmx b/glflag.mod/glflag.bmx new file mode 100644 index 0000000..5b62530 --- /dev/null +++ b/glflag.mod/glflag.bmx @@ -0,0 +1,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
\ No newline at end of file |