It went slower in wire frame? That should be the opposite. Check you setup.ini file. There should be a line that reads
blitflipmode=
Make sure it reads
blitflipmode=1
and
vbcreate=0
vbusage=0
3doverlay=0
On my AMD running at 1.5 ghz (single core) it'll run at about 115 fps in 32 bit and at about 144 in 16 bit. Though the example is coded to only run in 32 bit at a 60 fps cap. The 16 bit version looks fine actually... Hmmmmm
Quote: "I don't get this, how is it doing that? is it editing the textures or what?"
2 textures overlayed.
Quote: "I heard something about using dark lights, is that to make shadows?"
I forgot about that. For dynamic shadows, I could use dark lights. Though they would only be shaped like the type of light they are. A spot light, point light, or directional light. A dark light does the opposite of a regular light. Instead of emphasizing a particular color, it de-emphasizes it; essentially abosorbing it.
But for shadows and dark lights, you need polygons so the lighting isn't flat shaded. Here's an example of a dark light shadow effect using two spheres. The first sphere has a red light attached to it going down a hallway. The second sphere has a dark light attached to it. The idea was to block out the light in the hallway around the dark sphere. I used some of Kelebrindae's matrix functions to create the hallway so that the walls had multiple polygons.
remstart
==============================================================
= Title : dark light down hallway
= Author : latch
= Date : may 7 2009
= Update :
= Version:
==============================================================
Comments
==============================================================
remend
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
autocam off
set display mode 800,600,32
sync on
sync rate 60
hide mouse
rem =============================================================
rem = MAIN
rem =============================================================
gosub _init
gosub _lighting
do
gosub _track_lights_to_spheres
sync
loop
end
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
_init:
rem make a wall texture
ink rgb(192,192,192),0
box 2,2,126,126
get image 1,0,0,128,128
sync
rem make a hallway using object matrix commands
memNum=1
width#=100
depth#=1000
tilesX=10
tilesz=100
makeMemMatrix(memNum,width#,depth#,tilesX,tilesZ)
ConfigureMemMatrixNormals(memNum)
make mesh from memblock 1,memNum
make object 1,1,1
`set object 1,1,1,0
add limb 1,1,1
add limb 1,2,1
add limb 1,3,1
offset limb 1,1,100,0,0
rotate limb 1,1,0,0,90
offset limb 1,2,0,100,0
rotate limb 1,2,0,0,270
offset limb 1,3,100,100,0
rotate limb 1,3,0,0,180
scale object texture 1,3,30
set object ambient 1,rgb(64,64,64)
rem make a couple of spheres to move down the tunnel
make object sphere 2,96
color object 2,rgb(255,0,0)
make object sphere 3,96
color object 3,rgb(32,32,32)
position object 2,50,48,0
position object 3,50,48,-500
position camera 50,20,0
return
`----------------------------------------------------------------
_lighting:
set ambient light 0
color light 0,rgb(60,60,60)
make light 1
make light 2
color light 1,255,0,0
set light range 1,200
color light 2,-255,-255,-255
set light range 2,400
return
`----------------------------------------------------------------
_track_lights_to_spheres:
for obj=2 to 3
move object obj,3
if object position z(obj) > 1080
position object obj,50,48,-96
endif
set point light obj-1,object position x(obj),object position y(obj),object position z(obj)
next obj
return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
rem *******************************************************************
rem * Create and manage a "matrix-like-object" built from a memblock *
rem * Author: Kelebrindae *
rem * November 2004 *
rem *******************************************************************
rem Build a memblock containing the mesh of a "matrix-like" object.
rem This mesh will be [width#] by [depth#], divided in a grid of [tilesX] by [tilesZ] squares.
rem Each square has 4 vertices, so the mesh will contain [tilesX] * [tilesZ] * 4 vertices, numeroted from 0 to [Nb verteces - 1].
rem This means that a 2x2 grid will have 16 verteces, like shown here:
rem 09--10 13--14
rem | | |
rem 08__11 12__15
rem 01 02 05 06
rem | | |
rem 00--03 04--07
rem The mesh's pivot (0,0,0 coordinates) is located on vertex n°0 (in the "lower-left" corner).
rem The UV coords 0,0 are located on the "upper left" corner of the matrix (in the 3x3 matrix, on vertex n°12).
rem It is so because the Y axis in images is "inverted" (pixel 0,0 is in the upper left corner of the bitmap).
rem So, to avoid a "vertical mirror" effect on the texture, I had to invert the V axis of UVmapping...
rem Matrix number of tiles and dimensions are stored in memblock 255 for fast and easy access.
` Parameters:
` Memblock number, matrix width (pixels), matrix depth (pixels)
function makeMemMatrix(memNum,width#,depth#,tilesX,tilesZ)
if memblock exist(memNum) = 1 then exitfunction
rem If memblock n°255 doesn't exist yet, create it.
if memblock exist(255) = 0
make memblock 255,(memNum+1)*16
else
rem If it exists but is too small, copy its content, delete and re-create it, then copy its datas back
size=get memblock size(255)
if size<(memNum+1)*16
make memblock 254,size
copy memblock 255,254,0,0,size
delete memblock 255
make memblock 255,(memNum+1)*16
copy memblock 254,255,0,0,size
endif
endif
rem Compute offsets and datas sizes
vertNum = tilesX*tilesZ*4
faceNum = (tilesX * tilesZ) * 2
vertOffset = 32
normNum = vertNum
normOffset = 32+(vertNum*12)
faceOffset = 32+(vertNum*12)+(normNum*12)
faceSize = 7*faceNum: rem this size is in "Dwords", not in "Bytes"! (1 Dword = 4 bytes)
UVOffset = 32+(vertNum*12)+(normNum*12)+(faceNum*28)
rem Create memblock
memSize = 32 + (12*vertNum) + (12*normNum) + (28*faceNum) + (8*vertNum)
make memblock memNum,memSize
rem Write mesh header
write memblock dword memNum,0,vertNum
write memblock dword memNum,4,vertOffset
write memblock dword memNum,8,normNum
write memblock dword memNum,12,normOffset
write memblock dword memNum,16,faceNum
write memblock dword memNum,20,faceOffset
write memblock dword memNum,24,faceSize
write memblock dword memNum,28,UVOffset
rem Size of matrix tile, Size of UV tile
tileWidth# = width#/(tilesX*1.0)
tileDepth# = depth#/(tilesZ*1.0)
UVtileWidth# = 1.00/(tilesX*1.0)
UVtileDepth# = 1.00/(tilesZ*1.0)
rem Store number of tiles and tiles size in memblock 255
write memblock Dword 255,memNum*16,tilesX
write memblock Dword 255,(memNum*16)+4,tilesZ
write memblock float 255,(memNum*16)+8,tileWidth#
write memblock float 255,(memNum*16)+12,tileDepth#
rem Create 4 vertex and 2 faces by tile
vertCtr=0:faceCtr=0
for cz = 0 to tilesZ-1
for cx = 0 to tilesX-1
rem Vertex 0 coords
write memblock float memNum,vertOffset+(vertCtr*12),cx*tileWidth#
write memblock float memNum,vertOffset+(vertCtr*12)+4,0.00
write memblock float memNum,vertOffset+(vertCtr*12)+8,cz*tileDepth#
rem Normals infos
write memblock float memNum,normOffset+(vertCtr*12),0.0
write memblock float memNum,normOffset+(vertCtr*12)+4,1.00
write memblock float memNum,normOffset+(vertCtr*12)+8,0.0
rem UV coords
write memblock float memNum,UVoffset+(vertCtr*8),cx*UVtileWidth#
write memblock float memNum,UVoffset+(vertCtr*8)+4,1.00-(cz*UVtileDepth#)
rem Store vertex number and go on
v0 = vertCtr
inc vertCtr
rem Vertex 1 coords
write memblock float memNum,vertOffset+(vertCtr*12),cx*tileWidth#
write memblock float memNum,vertOffset+(vertCtr*12)+4,0.00
write memblock float memNum,vertOffset+(vertCtr*12)+8,(cz+1)*tileDepth#
rem Normals infos
write memblock float memNum,normOffset+(vertCtr*12),0.0
write memblock float memNum,normOffset+(vertCtr*12)+4,1.00
write memblock float memNum,normOffset+(vertCtr*12)+8,0.0
rem UV coords
write memblock float memNum,UVoffset+(vertCtr*8),cx*UVtileWidth#
write memblock float memNum,UVoffset+(vertCtr*8)+4,1.00-((cz+1)*UVtileDepth#)
rem Store vertex number and go on
v1 = vertCtr
inc vertCtr
rem Vertex 2 coords
write memblock float memNum,vertOffset+(vertCtr*12),(cx+1)*tileWidth#
write memblock float memNum,vertOffset+(vertCtr*12)+4,0.00
write memblock float memNum,vertOffset+(vertCtr*12)+8,(cz+1)*tileDepth#
rem Normals infos
write memblock float memNum,normOffset+(vertCtr*12),0.0
write memblock float memNum,normOffset+(vertCtr*12)+4,1.00
write memblock float memNum,normOffset+(vertCtr*12)+8,0.0
rem UV coords
write memblock float memNum,UVoffset+(vertCtr*8),(cx+1)*UVtileWidth#
write memblock float memNum,UVoffset+(vertCtr*8)+4,1.00-((cz+1)*UVtileDepth#)
rem Store vertex number and go on
v2 = vertCtr
inc vertCtr
rem Vertex 3 coords
write memblock float memNum,vertOffset+(vertCtr*12),(cx+1)*tileWidth#
write memblock float memNum,vertOffset+(vertCtr*12)+4,0.00
write memblock float memNum,vertOffset+(vertCtr*12)+8,cz*tileDepth#
rem Normals infos
write memblock float memNum,normOffset+(vertCtr*12),0.0
write memblock float memNum,normOffset+(vertCtr*12)+4,1.00
write memblock float memNum,normOffset+(vertCtr*12)+8,0.0
rem UV coords
write memblock float memNum,UVoffset+(vertCtr*8),(cx+1)*UVtileWidth#
write memblock float memNum,UVoffset+(vertCtr*8)+4,1.00-(cz*UVtileDepth#)
rem Store vertex number and go on
v3 = vertCtr
inc vertCtr
rem First triangle of tile
write memblock dword memNum,faceOffset+(faceCtr*28),3
write memblock dword memNum,faceOffset+(faceCtr*28)+4,v1
write memblock dword memNum,faceOffset+(faceCtr*28)+8,v1
write memblock dword memNum,faceOffset+(faceCtr*28)+12,v2
write memblock dword memNum,faceOffset+(faceCtr*28)+16,v2
write memblock dword memNum,faceOffset+(faceCtr*28)+20,v0
write memblock dword memNum,faceOffset+(faceCtr*28)+24,v0
inc faceCtr
rem Second triangle
write memblock dword memNum,faceOffset+(faceCtr*28),3
write memblock dword memNum,faceOffset+(faceCtr*28)+4,v3
write memblock dword memNum,faceOffset+(faceCtr*28)+8,v3
write memblock dword memNum,faceOffset+(faceCtr*28)+12,v0
write memblock dword memNum,faceOffset+(faceCtr*28)+16,v0
write memblock dword memNum,faceOffset+(faceCtr*28)+20,v2
write memblock dword memNum,faceOffset+(faceCtr*28)+24,v2
inc faceCtr
next cx
next cz
endfunction
rem Set the height of a vertex in the matrix memblock
` Parameters:
` Memblock number, x coord of the vertex to move, Z coord, new height
function setMemMatrixHeight(memNum,cx,cz,height#)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
if cx>0 and cz>0 then vertCtr=((cz-1)*(4*tilesX)) + ((cx-1)*4) + 2:write memblock float memNum,32+(vertCtr*12)+4,height#
if cx>0 and cz<TilesZ then vertCtr=(cz*(4*tilesX)) + ((cx-1)*4) + 3:write memblock float memNum,32+(vertCtr*12)+4,height#
if cx<TilesX and cz<TilesZ then vertCtr=(cz*(4*tilesX)) + (cx*4):write memblock float memNum,32+(vertCtr*12)+4,height#
if cx<TilesX and cz>0 then vertCtr=((cz-1)*(4*tilesX)) + (cx*4)+1:write memblock float memNum,32+(vertCtr*12)+4,height#
endfunction
rem Get the height of a vertex in the matrix memblock
` Parameters:
` Memblock number, x coord of the vertex, Z coord
` Return: height of the vertex
function getMemMatrixHeight(memNum,cx,cz)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
height#=0
if cx>0 and cz>0 then vertCtr=((cz-1)*(4*tilesX)) + ((cx-1)*4) + 2:exitfunction memblock float(memNum,32+(vertCtr*12)+4)
if cx>0 and cz<TilesZ then vertCtr=(cz*(4*tilesX)) + ((cx-1)*4) + 3:exitfunction memblock float(memNum,32+(vertCtr*12)+4)
if cx<TilesX and cz<TilesZ then vertCtr=(cz*(4*tilesX)) + (cx*4):exitfunction memblock float(memNum,32+(vertCtr*12)+4)
if cx<TilesX and cz>0 then vertCtr=((cz-1)*(4*tilesX)) + (cx*4)+1:exitfunction memblock float(memNum,32+(vertCtr*12)+4)
endfunction height#
rem Get the height of a point in the matrix memblock
` Parameters:
` Memblock number, x coord, Z coord
` Return: height of the point
` (DBpro code by Phaelax, slightly adapted for DBC)
function getMemMatrixGroundHeight(memNum,x#,z#)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem retrieve tiles size
sizex#=memblock float(255,(memnum*16)+8)
sizez#=memblock float(255,(memnum*16)+12)
rem If the point is in the matrix, return its height
if x#>=0 and z#>=0 and x#<tilesX*sizex# and z#<tilesZ*sizez#
xt=int(x#/sizex#)
zt=int(z#/sizez#)
if (((xt+1)*sizex#)-x#)+(z#-(zt*sizez#))<=sizex#
dx#=getMemMatrixHeight(memNum,xt+1,zt)-getMemMatrixHeight(memNum,xt,zt)
dz#=getMemMatrixHeight(memNum,xt+1,zt+1)-getMemMatrixHeight(memNum,xt+1,zt)
else
dx#=getMemMatrixHeight(memNum,xt+1,zt+1)-getMemMatrixHeight(memNum,xt,zt+1)
dz#=getMemMatrixHeight(memNum,xt,zt+1)-getMemMatrixHeight(memNum,xt,zt)
endif
height#=(((x#-(xt*sizex#))/sizex#)*dx#)+(((z#-(zt*sizez#))/sizez#)*dz#)+getMemMatrixHeight(memNum,xt,zt)
endif
endfunction height#
rem Set the normal of a vertex in the matrix memblock
rem => called in the ConfigureMemMatrixNormal function
` Parameters:
` Memblock number, x coord of the vertex, Z coord, x,y,z of the new normal vector
function setMemMatrixNormal(memNum,cx,cz,nx#,ny#,nz#)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
vertNum=memblock dword(memNum,0)
normOffset = 32+(vertNum*12)
if cx>0 and cz>0
vertCtr=((cz-1)*(4*tilesX)) + ((cx-1)*4) + 2
write memblock float memNum,normOffset+(vertCtr*12),nx#
write memblock float memNum,normOffset+(vertCtr*12)+4,ny#
write memblock float memNum,normOffset+(vertCtr*12)+8,nz#
endif
if cx>0 and cz<TilesZ
vertCtr=(cz*(4*tilesX)) + ((cx-1)*4) + 3
write memblock float memNum,normOffset+(vertCtr*12),nx#
write memblock float memNum,normOffset+(vertCtr*12)+4,ny#
write memblock float memNum,normOffset+(vertCtr*12)+8,nz#
endif
if cx<TilesX and cz<TilesZ
vertCtr=(cz*(4*tilesX)) + (cx*4)
write memblock float memNum,normOffset+(vertCtr*12),nx#
write memblock float memNum,normOffset+(vertCtr*12)+4,ny#
write memblock float memNum,normOffset+(vertCtr*12)+8,nz#
endif
if cx<TilesX and cz>0
vertCtr=((cz-1)*(4*tilesX)) + (cx*4)+1
write memblock float memNum,normOffset+(vertCtr*12),nx#
write memblock float memNum,normOffset+(vertCtr*12)+4,ny#
write memblock float memNum,normOffset+(vertCtr*12)+8,nz#
endif
endfunction
rem Re-calculate the normal of each vertex of the matrix
` Parameters:
` Memblock number
function ConfigureMemMatrixNormals(memNum)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem retrieve tiles size
sizex#=memblock float(255,(memnum*16)+8)
sizez#=memblock float(255,(memnum*16)+12)
for z=1 to tilesZ-1
for x=1 to tilesX-1
rem Get matrix heights
h8#=getMemMatrixHeight(memNum,x,z-1)
h4#=getMemMatrixHeight(memNum,x-1,z)
h#=getMemMatrixHeight(memNum,x,z)
h2#=getMemMatrixHeight(memNum,x,z)
rem Calculate projected angle X using heights
x1#=(x-1)*sizex# : y1#=h#
x2#=x*sizex# : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
rem Calculate projected angle Z using heights
z1#=(z-1)*sizez# : y1#=h2#
z2#=z*sizez# : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
rem Make normal from projected angle
nx#=sin(ax#)
ny#=cos(ax#)
nz#=sin(az#)
rem Setting matrix normal for smoothness
setMemMatrixNormal(memNum,x,z,nx#,ny#,nz#)
next x
next z
endfunction
rem Re-calculate the normal of ONE vertex of the matrix
` Parameters:
` Memblock number, x coord of the vertex, Z coord
function ConfigureOneMemMatrixNormal(memNum,x,z)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem retrieve tiles size
sizex#=memblock float(255,(memnum*16)+8)
sizez#=memblock float(255,(memnum*16)+12)
rem Get matrix heights
h8#=getMemMatrixHeight(memNum,x,z-1)
h4#=getMemMatrixHeight(memNum,x-1,z)
h#=getMemMatrixHeight(memNum,x,z)
h2#=getMemMatrixHeight(memNum,x,z)
rem Calculate projected angle X using heights
x1#=(x-1)*sizex# : y1#=h#
x2#=x*sizex# : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
rem Calculate projected angle Z using heights
z1#=(z-1)*sizez# : y1#=h2#
z2#=z*sizez# : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
rem Make normal from projected angle
nx#=sin(ax#)
ny#=cos(ax#)
nz#=sin(az#)
rem Setting matrix normal for smoothness
setMemMatrixNormal(memNum,x,z,nx#,ny#,nz#)
endfunction
rem "Smooth" the matrix, giving it a more "round" look.
rem This smoothes the mesh; it does not affect the normals.
rem The more iterations you set, the more intense the smooth effect will be (it'll be slower, too).
` Parameters:
` Memblock number, nb iterations of smoothing
` (DBpro code by Phaelax, slightly adapted for DBC)
function smoothMemMatrix(memNum,iterations)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem store original heights
dim heights#(tilesX,tilesZ)
for z = 0 to tilesZ
for x = 0 to tilesX
heights#(x,z) = getMemMatrixHeight(memNum,x,z)
next x
next z
rem Smooth the matrix by "averaging" the height of each vertex with the height of its neighbours
for t = 1 to iterations
for z = 0 to tilesZ
for x = 0 to tilesX
count = 0
h1# = 0
h2# = 0
h3# = 0
h4# = 0
h5# = 0
h6# = 0
h7# = 0
h8# = 0
if z < tilesZ
if x > 0 then h1# = heights#(x-1,z+1) : inc count
h2# = heights#(x,z+1) : inc count
if x < tilesX then h3# = heights#(x+1,z+1) : inc count
endif
if x > 0 then h4# = heights#(x-1,z) : inc count
if x < tilesX then h5# = heights#(x+1,z) : inc count
if z > 0
if x > 0 then h6# = heights#(x-1,z-1) : inc count
h7# = heights#(x,z-1) : inc count
if x < tilesX then h8# = heights#(x+1,z-1) : inc count
endif
avg# = (h1#+h2#+h3#+h4#+h5#+h6#+h7#+h8#) / count
setMemMatrixHeight(memNum,x,z,avg#)
next x
next z
next t
rem delete array
undim heights#()
endfunction
rem Take a rectangle in the matrix (defined by x1,z1,x2,z2 parameters) and change its UVmapping
rem to show a certain area of the texture (defined by xtex1,ytex1,xtex2,ytex2 parameters).
` Parameters:
` memblock,image used to texture the matrix,
` width,depth of the matrix
` x1,z1,x2,z2 (in tiles coordinates, which define a rectangle wherein the mapping will be changed)
` xtex1,ytex1,xtex2,ytex2 (in pixels, which define a rectangle in the texture that will be mapped on the rectangle defined above)
` rotate (0 = no rotation; 1 = 90° ; 2 = 180° ; 3 = 270°. Rotation is trigonometric -> anti-clockwise)
` flip (0 = no flip; 1 = horizontal flip; 2 = vertical flip. You don't need "flip both" 'cos it's just like "rotate 180°")
` (Man, is it complicated! I must find a way to make it simplier...)
` Return: 0 - if you did everything right
` 1 - Memblock doesn't exist
` 2 - Texture image doesn't exist (did you load it before running this function?)
` 3 - Tiles coords given for mapping (x1,z1,x2,z2) exceed matrix limits
` 4 - Tiles coords are inverted (x1>x2 and/or z1>z2)
` 5 - Texture coords exceed texture limits (<0 or >texture size)
` 6 - Texture coords are inverted (xtex1>xtex2 and/or ytex1>ytex2)
` 7 - Rotate/flipping params exceeds limits (<0 or >3 for rotate or >2 for flipping)
function UVmapMemMatrix(memNum,imageNum,x1,z1,x2,z2,xtex1,ytex1,xtex2,ytex2,rotate,flip)
if memblock exist(memNum) = 0 then exitfunction 1
if image exist(ImageNum) = 0 then exitfunction 2
rem Control parameter integrity
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
if x1<0 or x2>tilesX or z1<0 or z2>tilesZ then exitfunction 3
if x1>x2 or z1>z2 then exitfunction 4
if rotate<0 or rotate>3 or flip<0 or flip>2 then exitfunction 7
rem Load texture into a memblock, to get its size
make memblock from image 254,imageNum
imageWidth#=memblock dword(254,0):imageHeight#=memblock dword(254,4)
delete memblock 254
if xtex2>imageWidth# or ytex2>imageHeight# or xtex1<0 or ytex1<0 then exitfunction 5
if xtex1>=xtex2 or ytex1>=ytex2 then exitfunction 6
rem Rotating and flipping
dim rotx(4):dim rotz(4)
rotx(1)=0:rotx(2)=0:rotx(3)=1:rotx(4)=1
rotz(1)=0:rotz(2)=1:rotz(3)=1:rotz(4)=0
nrotstart=1:mirrorboth=0
if rotate=1 then nrotstart=2
if rotate=2 then mirrorboth=1:rem 180° rotation = flip both axis
if rotate=3 then nrotstart=2:mirrorboth=1:rem 270° rotation = 90° rotation + flip both axis
if mirrorboth=1
select flip
case 0
newflip=3
endcase
case 1
newflip=2
endcase
case 2
newflip=1
endcase
endselect
flip=newflip
endif
rem Useful values: Nb Tiles to remap, size of UV tiles, etc...
UVOffset=memblock dword(memNum,28)
nbtilesX=abs(x2-x1):nbtilesZ=abs(z2-z1)
xtex1#=xtex1:xtex2#=xtex2:ytex1#=ytex1:ytex2#=ytex2
subImageRatiox#=((1.00+xtex2#-xtex1#)/imageWidth#)
subImageRatioy#=((1.00+ytex2#-ytex1#)/imageHeight#)
Ustart#=xtex1#/imageWidth#
Vstart#=(ytex2#+1.00)/imageHeight#
UVtileWidth# = subImageRatiox#/(abs(nbtilesX)+1)
UVtileDepth# = subImageRatioy#/(abs(nbtilesZ)+1)
rem Mirroring: U and/or V directions are inverted
if flip=1 or flip=3 then Ustart#=1.00-(subImageRatiox#+Ustart#):UVtileWidth#=UVtileWidth#*-1.00
if flip=2 or flip=3 then Vstart#=1.00-(subImageRatioy#-Vstart#):UVtileDepth#=UVtileDepth#*-1.00
rem remapping of the rectangle x1,z1 -> x2,z2
for cz=0 to nbtilesZ
for cx=0 to nbtilesX
rem UV coords
if rotate=1 or rotate=3
vertCtr=((x1+cx)*tilesZ*4)+((z2-cz)*4)
else
vertCtr=((z1+cz)*tilesX*4)+((x1+cx)*4)
endif
nrot=nrotstart
rem v0
write memblock float memNum,UVoffset+(vertCtr*8),Ustart#+((cx+rotx(nrot))*UVtileWidth#)
write memblock float memNum,UVoffset+(vertCtr*8)+4,Vstart#-((cz+rotz(nrot))*UVtileDepth#)
inc vertCtr
inc nrot:if nrot=5 then nrot=1
rem v1
write memblock float memNum,UVoffset+(vertCtr*8),Ustart#+((cx+rotx(nrot))*UVtileWidth#)
write memblock float memNum,UVoffset+(vertCtr*8)+4,Vstart#-((cz+rotz(nrot))*UVtileDepth#)
inc vertCtr
inc nrot:if nrot=5 then nrot=1
rem v2
write memblock float memNum,UVoffset+(vertCtr*8),Ustart#+((cx+rotx(nrot))*UVtileWidth#)
write memblock float memNum,UVoffset+(vertCtr*8)+4,Vstart#-((cz+rotz(nrot))*UVtileDepth#)
inc vertCtr
inc nrot:if nrot=5 then nrot=1
rem v3
write memblock float memNum,UVoffset+(vertCtr*8),Ustart#+((cx+rotx(nrot))*UVtileWidth#)
write memblock float memNum,UVoffset+(vertCtr*8)+4,Vstart#-((cz+rotz(nrot))*UVtileDepth#)
next cx
next cz
endfunction 0
rem Randomize the height of all vertices in the matrix, within the range of 0 to heightrange#
` Parameters:
` Memblock number, height range
function randomizeMemMatrix(memNum,heightrange#)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
for i=0 to tilesX
for j=0 to tilesZ
setMemMatrixHeight(memNum,i,j,(rnd(heightrange#*256.00)/256.00))
next j
next i
endfunction
rem Move the pivot point of the matrix (usually the lower left corner) to the x,z center of the object
function centerMemMatrixPivot(memnum)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem retrieve tiles size
sizex#=memblock float(255,(memnum*16)+8)
sizez#=memblock float(255,(memnum*16)+12)
rem Calculate x and z displacement we must apply to each vertex
vertOffset=memblock dword(memNum,4)
vertNum=memblock dword(memNum,0)
xmove#=( memblock float(memNum,vertOffset)-memblock float(memNum,vertOffset+((vertnum-2)*12)) )/2.00
zmove#=( memblock float(memNum,vertOffset+8)-memblock float(memNum,vertOffset+((vertnum-2)*12)+8) )/2.00
rem Move vertex to their new position
vertCtr=0
for cz = 0 to tilesZ-1
for cx = 0 to tilesX-1
rem Vertex 0 coords
write memblock float memNum,vertOffset+(vertCtr*12),xmove#+(cx*sizex#)
write memblock float memNum,vertOffset+(vertCtr*12)+8,zmove#+(cz*sizez#)
inc vertCtr
rem Vertex 1 coords
write memblock float memNum,vertOffset+(vertCtr*12),xmove#+(cx*sizex#)
write memblock float memNum,vertOffset+(vertCtr*12)+8,zmove#+((cz+1)*sizez#)
inc vertCtr
rem Vertex 2 coords
write memblock float memNum,vertOffset+(vertCtr*12),xmove#+((cx+1)*sizex#)
write memblock float memNum,vertOffset+(vertCtr*12)+8,zmove#+((cz+1)*sizez#)
inc vertCtr
rem Vertex 3 coords
write memblock float memNum,vertOffset+(vertCtr*12),xmove#+((cx+1)*sizex#)
write memblock float memNum,vertOffset+(vertCtr*12)+8,zmove#+(cz*sizez#)
inc vertCtr
next cx
next cz
endfunction
rem Move the pivot point of the matrix to its lower left corner (in case it has been centered before)
function restoreMemMatrixPivot(memnum)
tilesX=memblock Dword(255,memnum*16)
tilesZ=memblock Dword(255,(memnum*16)+4)
rem retrieve tiles size
sizex#=memblock float(255,(memnum*16)+8)
sizez#=memblock float(255,(memnum*16)+12)
rem Calculate x and z displacement we must apply to each vertex
vertOffset=memblock dword(memNum,4)
vertNum=memblock dword(memNum,0)
rem Move vertex to their new position
vertCtr=0
for cz = 0 to tilesZ-1
for cx = 0 to tilesX-1
rem Vertex 0 coords
write memblock float memNum,vertOffset+(vertCtr*12),cx*sizex#
write memblock float memNum,vertOffset+(vertCtr*12)+8,cz*sizez#
inc vertCtr
rem Vertex 1 coords
write memblock float memNum,vertOffset+(vertCtr*12),cx*sizex#
write memblock float memNum,vertOffset+(vertCtr*12)+8,(cz+1)*sizez#
inc vertCtr
rem Vertex 2 coords
write memblock float memNum,vertOffset+(vertCtr*12),(cx+1)*sizex#
write memblock float memNum,vertOffset+(vertCtr*12)+8,(cz+1)*sizez#
inc vertCtr
rem Vertex 3 coords
write memblock float memNum,vertOffset+(vertCtr*12),(cx+1)*sizex#
write memblock float memNum,vertOffset+(vertCtr*12)+8,cz*sizez#
inc vertCtr
next cx
next cz
endfunction
Enjoy your day.