' Copyright (c) 2006 Ian Cowburn ' ' Permission is hereby granted, free of charge, to any person obtaining a copy of ' this software and associated documentation files (the "Software"), to deal in ' the Software without restriction, including without limitation the rights to ' use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ' of the Software, and to permit persons to whom the Software is furnished to do ' so, subject to the following conditions: ' ' The above copyright notice and this permission notice shall be included in all ' copies or substantial portions of the Software. ' ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ' SOFTWARE. ' ' $Id$ ' Module noddybox.glflag ModuleInfo "Framework: Simple Open GL Backdrop flag" ModuleInfo "Copyright: Ian Cowburn -- released under the MIT License" ModuleInfo "Author: Ian Cowburn" ModuleInfo "Version: $Revision$" 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