Memblock matrix by kevil15th Dec 2003 14:03
|
---|
Summary This code shows how you can use memblocks to simulate a matrix. It has the basic commands the normal matrix also has and more. Description This code shows how you can use memblocks to simulate a matrix. It has the basic commands the normal matrix also has and more. If you have any questions about the functions or the code or if you have requests for more functions, plz let me know. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com sync on : sync rate 0 randomize timer() set display mode 1024,768,32 load image "grass2.bmp",1 rem read heightmap dim height#(64,64) open to read 1,"height.dat" for x=0 to 64 for z=0 to 64 read float 1,height#(x,z) height#(x,z)=height#(x,z)*600.0 next z next x close file 1 rem needed vars! dim tilex(128) dim tilez(128) dim tilesizex#(128) dim tilesizez#(128) dim texture(128) dim xpart#(128) dim ypart#(128) dim object(128) rem make sea texture make memblock 10,(64*64*4)+12 write memblock dword 10,0,64 write memblock dword 10,4,64 write memblock dword 10,8,32 for x=1 to 64 for y=1 to 64 height#=((height#(x-1,(65-y)-1)+height#(x,(65-y)-1)+height#(x-1,(65-y))+height#(x,(65-y)))/4.00)*1.25 if height#>350 then color=rgb(250,250,250) if height#<150 then color=rgb(0,0,250) if height#<=350 and height#>=150 then color=rgb((height#-150)*1.25,(height#-150)*1.25,250) write memblock dword 10,12+((((y-1)*64)+(x-1))*4),color next y next x make image from memblock 2,10 delete memblock 10 rem make the object makematrix(1,64*60,64*60,64,64) : makematrix(2,64*60,64*60,8,8) : makematrix(3,64*60,64*60,8,8) texturematrix(1,1,32,32) : texturematrix(2,2,1,1) : texturematrix(3,2,1,1) color backdrop rgb(150,150,255) position camera 0,50,0 set camera range 1,20000 rem setup matrix for x=0 to tilex(1) for z=0 to tilez(1) if height#(x,z)>=200 and height#(x,z)<300 then color=rgb(int(200-((height#(x,z)-200)*1.50)),int(150+((height#(x,z)-200)*1.00)),50) if height#(x,z)>=300 and height#(x,z)<400 then color=rgb(int(50+((height#(x,z)-300)*1.00)),int(250-((height#(x,z)-300)*1.00)),int(50+((height#(x,z)-300)*1.00))) if height#(x,z)>=400 then color=rgb(150,150,150) if height#(x,z)<200 then color=rgb(200,150,50) setmatrixheight(1,x,z,height#(x,z)*1.50) setmatrixcolor(1,x,z,color) next z next x normalizematrix(1,0,0,tilex(1),tilez(1)) updatematrix(1) : updatematrix(2) : updatematrix(3) rem position the matrix positionmatrix(2,0,300,0) positionmatrix(3,0,300,0) ghostmatrix(2,0) ghostmatrix(3,1) set object cull 50002,0 rem vars time=timer() fase#=0.00 fogged=0 do text 0,0,str$(screen fps()) text 0,20,str$(statistic(1)) rem get time dt#=(timer()-time)/1000.0 time=timer() yrotate camera wrapvalue(camera angle y()+mousemovex()) xrotate camera wrapvalue(camera angle x()+mousemovey()) if mouseclick()=1 then move camera 200*dt# if mouseclick()=2 then move camera -200*dt# height#=getgroundheight(1,camera position x(),camera position z())+5.0 if camera position y()<height# then position camera camera position x(),height#,camera position z() rem waving effect positionmatrix(2,0,310+(sin(fase#)*10),0) positionmatrix(3,0,310+(sin(fase#)*10),0) fase#=fase#+(45*dt#) rem fogeffect if camera position y()<310+(sin(fase#)*10) and fogged=0 fogged=1 fog on fog color rgb(50,50,250) fog distance 200 color backdrop rgb(75,75,255) color ambient light rgb(75,75,250) endif if camera position y()>=310+(sin(fase#)*10) and fogged=1 fogged=0 fog off color backdrop rgb(150,150,255) color ambient light rgb(255,255,255) endif sync loop function makematrix(mem,x#,z#,tilex,tilez) tilex(mem)=tilex tilez(mem)=tilez tilesizex#(mem)=x#/tilex(mem) tilesizez#(mem)=z#/tilez(mem) polys=2*tilex*tilez size=(polys*36*3)+12 make memblock mem,size write memblock dword mem,0,338 write memblock dword mem,4,36 write memblock dword mem,8,polys*3 pos=12 for x=1 to tilex(mem) for z=1 to tilez(mem) writevectordata(mem,pos,(x-1.0)*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,1.000) writevectordata(mem,pos+36,x*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,0.000) writevectordata(mem,pos+72,x*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,1.000) writevectordata(mem,pos+108,(x-1.0)*tilesizex#(mem),0.000,(z-1.0)*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,1.000) writevectordata(mem,pos+144,(x-1.0)*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),0.000,0.000) writevectordata(mem,pos+180,x*tilesizex#(mem),0.000,z*tilesizez#(mem),0.000,1.000,0.000,rgb(255,255,255),1.000,0.000) pos=pos+216 next z next x object(mem)=mem+50000 make mesh from memblock object(mem),mem make object object(mem),object(mem),0 endfunction function writevectordata(mem,pos,X# as float,Y# as float,Z# as float,NX# as float,NY# as float,NZ# as float,color as dword,TU# as float,TV# as float) write memblock float mem,pos,X# write memblock float mem,pos+4,Y# write memblock float mem,pos+8,Z# write memblock float mem,pos+12,NX# write memblock float mem,pos+16,NY# write memblock float mem,pos+20,NZ# write memblock dword mem,pos+24,color write memblock float mem,pos+28,TU# write memblock float mem,pos+32,TV# endfunction function setmatrixheight(mem,x,z,y#) if x-1>=0 and z-1>=0 write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+40,y# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+184,y# endif if x-1>=0 and z<tilez(mem) write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+76,y# endif if x<tilex(mem) and z-1>=0 write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+148,y# endif if x<tilex(mem) and z<tilez(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+4,y# write memblock float mem,12+(216*((x*tilez(mem))+z))+112,y# endif endfunction function setmatrixcolor(mem,x,z,color) if x-1>=0 and z-1>=0 write memblock dword mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+60,color write memblock dword mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+204,color endif if x-1>=0 and z<tilez(mem) write memblock dword mem,12+(216*(((x-1)*tilez(mem))+z))+96,color endif if x<tilex(mem) and z-1>=0 write memblock dword mem,12+(216*((x*tilez(mem))+(z-1)))+168,color endif if x<tilex(mem) and z<tilez(mem) write memblock dword mem,12+(216*((x*tilez(mem))+z))+24,color write memblock dword mem,12+(216*((x*tilez(mem))+z))+132,color endif endfunction function setmatrixnormal(mem,x,z,nx#,ny#,nz#) if x-1>=0 and z-1>=0 write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+48,nx# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+52,ny# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+56,nz# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+192,nx# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+196,ny# write memblock float mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+200,nz# endif if x-1>=0 and z<tilez(mem) write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+84,nx# write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+88,ny# write memblock float mem,12+(216*(((x-1)*tilez(mem))+z))+92,nz# endif if x<tilex(mem) and z-1>=0 write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+156,nx# write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+160,ny# write memblock float mem,12+(216*((x*tilez(mem))+(z-1)))+164,nz# endif if x<tilex(mem) and z<tilez(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+12,nx# write memblock float mem,12+(216*((x*tilez(mem))+z))+16,ny# write memblock float mem,12+(216*((x*tilez(mem))+z))+20,nz# write memblock float mem,12+(216*((x*tilez(mem))+z))+120,nx# write memblock float mem,12+(216*((x*tilez(mem))+z))+124,ny# write memblock float mem,12+(216*((x*tilez(mem))+z))+128,nz# endif endfunction function getmatrixheight(mem,x,z) if x-1>=0 and z-1>=0 height#=memblock float(mem,12+(216*(((x-1)*tilez(mem))+(z-1)))+40) else if x<tilex(mem) and z<tilez(mem) height#=memblock float(mem,12+(216*((x*tilez(mem))+z))+4) else height#=memblock float(mem,12+(216*(((x-1)*tilez(mem))+z))+76) endif endif endfunction height# function randomizematrix(mem,bx,bz,ex,ez,value#) for x=bx to ex for z=bz to ez setmatrixheight(mem,x,z,(rnd(int(value#*100))/100.0)-(0.5*value#)) next z next x endfunction function normalizematrix(mem,bx,bz,ex,ez) if bx<=0 then bx=1 if bz<=0 then bz=1 if ex>=tilex(mem) then ex=tilex(mem)-1 if ez>=tilez(mem) then ez=tilez(mem)-1 rem Use matrix normals to make it smooth for z=bz to ez for x=bx to ex rem Get matrix heights h8#=getmatrixheight(mem,x,z-1) h4#=getmatrixheight(mem,x-1,z) h#=getmatrixheight(mem,x,z) h2#=getmatrixheight(mem,x,z) rem Calculate projected angle X using heights x1#=(x-1)*25.0 : y1#=h# x2#=(x+0)*25.0 : 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)*25.0 : y1#=h2# z2#=(z+0)*25.0 : 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 setmatrixnormal(1,x,z,nx#,ny#,nz#) next x next z endfunction function updatematrix(mem) change mesh from memblock object(mem),mem change mesh object(mem),0,object(mem) texture object object(mem),texture(mem) endfunction function getgroundheight(mem,x#,z#) if x#>=0 and z#>=0 and x#<tilex(mem)*tilesizex#(mem) and z#<tilez(mem)*tilesizez#(mem) xt=int(x#/tilesizex#(mem)) zt=int(z#/tilesizez#(mem)) if (((xt+1)*tilesizex#(mem))-x#)+(z#-(zt*tilesizez#(mem)))<=tilesizex#(mem) dx#=getmatrixheight(mem,xt+1,zt)-getmatrixheight(mem,xt,zt) dz#=getmatrixheight(mem,xt+1,zt+1)-getmatrixheight(mem,xt+1,zt) else dx#=getmatrixheight(mem,xt+1,zt+1)-getmatrixheight(mem,xt,zt+1) dz#=getmatrixheight(mem,xt,zt+1)-getmatrixheight(mem,xt,zt) endif height#=(((x#-(xt*tilesizex#(mem)))/tilesizex#(mem))*dx#)+(((z#-(zt*tilesizez#(mem)))/tilesizez#(mem))*dz#)+getmatrixheight(mem,xt,zt) endif endfunction height# function preparematrixtexture(mem,image,xsplit,ysplit) texture(mem)=image xpart#(mem)=1.000/xsplit ypart#(mem)=1.000/ysplit for x=0 to tilex(mem)-1 for z=0 to tilez(mem)-1 setmatrixtile(mem,x,z,1,1) next z next x endfunction function setmatrixtile(mem,x,z,xtex,ytex) write memblock float mem,12+(216*((x*tilez(mem))+z))+28,(xtex-1)*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+32,ytex*ypart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+64,xtex*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+68,(ytex-1)*ypart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+100,xtex*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+104,ytex*ypart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+136,(xtex-1)*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+140,ytex*ypart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+172,(xtex-1)*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+176,(ytex-1)*ypart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+208,xtex*xpart#(mem) write memblock float mem,12+(216*((x*tilez(mem))+z))+212,(ytex-1)*ypart#(mem) endfunction function texturematrix(mem,image,xtile#,ztile#) texture(mem)=image texx#=xtile#/tilex(mem) texz#=ztile#/tilez(mem) for x=0 to tilex(mem)-1 for z=0 to tilez(mem)-1 z2=(tilez(mem)-z)-1 write memblock float mem,12+(216*((x*tilez(mem))+z2))+28,(x*texx#) value#=((z+1)*texz#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+32,value# value#=((x+1)*texx#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+64,value# write memblock float mem,12+(216*((x*tilez(mem))+z2))+68,(z*texz#) value#=((x+1)*texx#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+100,value# value#=((z+1)*texz#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+104,value# write memblock float mem,12+(216*((x*tilez(mem))+z2))+136,(x*texx#) value#=((z+1)*texz#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+140,value# write memblock float mem,12+(216*((x*tilez(mem))+z2))+172,(x*texx#) write memblock float mem,12+(216*((x*tilez(mem))+z2))+176,(z*texz#) value#=((x+1)*texx#) : if value#=0.00 then value#=1.00 write memblock float mem,12+(216*((x*tilez(mem))+z2))+208,value# write memblock float mem,12+(216*((x*tilez(mem))+z2))+212,(z*texz#) next z next x endfunction function positionmatrix(mem,x#,y#,z#) position object object(mem),x#,y#,z# endfunction function ghostmatrix(mem,ghost) ghost object on object(mem),ghost endfunction |