GLBasic forum

Codesnippets => Code Snippets => Topic started by: matty47 on 2007-Oct-29

Title: Plane Lib
Post by: matty47 on 2007-Oct-29
Hi, after being inspired by the Entity Library/System on the German forum I have been experimenting with a plane lib. Probably best just to look at the code. You need a texture for the water in the example. Can't remember where I got the camera code but it was on this forum.
The plane library
Code (glbasic) Select
// --------------------------------- //
// Project: Plane
// Start: Sunday, October 28, 2007
// IDE Version: 5.055
//Matthew Ingle - October 2007
//A set of functions to implement a plane object
//Add to as you see fit however share the code!!
TYPE Plane_vertex
plx
ply
plz
tx
tz
col
ENDTYPE

TYPE TPlane
Plane_pts[] AS Plane_vertex
xverts
zverts
smooth
ENDTYPE
//--------------------------------------------//
//! Create a plane of z length, x width and having x and z divisions
//! Use smooth= TRUE to smooth the plane
//! you can reference any vertex by it's x,z number 0 to division -1
//--------------------------------------------//
FUNCTION CreatePlane AS TPlane:length,width,secx,secz,smooth
LOCAL name AS TPlane
LOCAL z_count,x_count
LOCAL delta_x=width/secx
LOCAL delta_z=length/secz
REDIM name.Plane_pts[secx+1][secz+1]
FOR x_count=0 TO secx
FOR z_count=0 TO secz
name.Plane_pts[x_count][z_count].plx=delta_x*x_count
name.Plane_pts[x_count][z_count].ply=0
name.Plane_pts[x_count][z_count].plz=delta_z*z_count
name.Plane_pts[x_count][z_count].tx=MOD(x_count,2)
name.Plane_pts[x_count][z_count].tz=MOD(z_count,2)
name.Plane_pts[x_count][z_count].col=RGB(255,255,255)
NEXT
NEXT
name.smooth=smooth
name.xverts=secx+1
name.zverts=secz+1
RETURN name
ENDFUNCTION

//--------------------------------------//
//!Draw the plane
//--------------------------------------//
FUNCTION DrawPlane:name AS TPlane,objnum
LOCAL ix,iz
X_OBJSTART objnum
FOR ix=0 TO name.xverts-2
X_OBJNEWGROUP
FOR iz= 0 TO name.zverts-1
X_OBJADDVERTEX name.Plane_pts[ix][iz].plx,name.Plane_pts[ix][iz].ply,name.Plane_pts[ix][iz].plz,name.Plane_pts[ix][iz].tx,name.Plane_pts[ix][iz].tz,name.Plane_pts[ix][iz].col
X_OBJADDVERTEX name.Plane_pts[ix+1][iz].plx,name.Plane_pts[ix+1][iz].ply,name.Plane_pts[ix+1][iz].plz,name.Plane_pts[ix+1][iz].tx,name.Plane_pts[ix+1][iz].tz,name.Plane_pts[ix+1][iz].col
NEXT
NEXT
X_OBJEND
IF name.smooth=TRUE
X_AUTONORMALS 2
ELSE
X_AUTONORMALS 1
ENDIF

X_DRAWOBJ objnum,0
RETURN 1
ENDFUNCTION

//-------------------------------------//
//! Set the height of a particular plane vertex
//-------------------------------------//
FUNCTION SetPlaneHeight: name AS TPlane,x,z,height
name.Plane_pts[x][z].ply=height
ENDFUNCTION

//------------------------------------//
//! Get the height of the plane at x,z
//------------------------------------//
FUNCTION GetPlaneHeight: name AS TPlane,x,z
RETURN name.Plane_pts[x][z].ply
ENDFUNCTION

//------------------------------------//
//! Assign a random value between 0 and h to each vertex in the plane
//------------------------------------//
FUNCTION RandomisePlane: name AS TPlane,h
LOCAL ix,iz
FOR ix=0 TO name.xverts-1
FOR iz=0 TO name.zverts-1
SetPlaneHeight(name,ix,iz,RND(h))
NEXT
NEXT
ENDFUNCTION
The camera code
Code (glbasic) Select
UNCTION MoveCam:
INC phiXZ,MOUSEAXIS(0)/10 //left-right turn
INC phiXY,MOUSEAXIS(1)/10 //up-down turn

IF phiXZ > 360 THEN phiXZ=phiXZ-360
IF phiXZ < 0 THEN  phiXZ=phiXZ+360

IF phiXY < -up_down_limit THEN phiXY=-up_down_limit
IF phiXY > up_down_limit  THEN phiXY=up_down_limit

IF KEY(17) //forward -- w key
INC camz,SIN(phiXZ)*WalkSpeed
INC camx,COS(phiXZ)*WalkSpeed
ENDIF

IF KEY(31) //back -- s key
DEC camz,SIN(phiXZ)*WalkSpeed
DEC camx,COS(phiXZ)*WalkSpeed
ENDIF

IF KEY(30) //left -- a key
INC camz,SIN(phiXZ-90)*WalkSpeed
INC camx,COS(phiXZ-90)*WalkSpeed
ENDIF

IF KEY(32) //right --d key
INC camz,SIN(phiXZ+90)*WalkSpeed
INC camx,COS(phiXZ+90)*WalkSpeed
ENDIF

ENDFUNCTION
And a program to test the code
Code (glbasic) Select
// --------------------------------- //
// Project: Plane
// Start: Saturday, October 27, 2007
// IDE Version: 5.047
//Test of the plane functions as an ocean swell
//Matthew Ingle October 2007
//Code adapted from a Darkbasic example
//Use and improve!!!
//------------Declarations------------//
//Variables for camera
LIMITFPS 25
GLOBAL up_down_limit=30
GLOBAL camy=20,camx=15,camz=15
GLOBAL WalkSpeed=0.5
GLOBAL phiXZ=0
GLOBAL phiXY=0

//Variable for ground plane
LOCAL ground AS TPlane
LOCAL size=2000 // the x and z size of the plane
LOCAL divisions=20 //the number of divisions in the plane
LOCAL waveheight=2
DIM ripple[divisions*divisions] // an array to hold ripple data for the plane

//Fill the ripple array with data
LOCAL t
FOR t=0 TO divisions*divisions-1
ripple[t]=RND(359)
NEXT

//Create a plane object and assign random heights to the vertices
ground=CreatePlane(size,size,divisions,divisions,TRUE)
RandomisePlane(ground,waveheight)
//load a sprite FOR the water
LOADSPRITE "water.bmp",500

//draw a plain blue background
GETSCREENSIZE sx,sy
DRAWRECT 0,0,sx,sy,RGB(0x38, 0x8e, 0xc2)
GRABSPRITE 100,0,0,sx,sy

//main loop
WHILE TRUE
DRAWSPRITE 100,0,0
X_MAKE3D 0.1,1000,45
X_CAMERA camx, camy, camz,   camx+COS(phiXZ), camy+SIN(-phiXY), camz+SIN(phiXZ)
X_AMBIENT_LT 0,RGB(255,255,255)
X_FOG RGB(240,240,255),FALSE,200,1000
X_DRAWAXES 0,0,0
//update the ripple data for the plane
LOCAL ix,iz
FOR ix =1 TO divisions-2
FOR iz=1 TO divisions-2
//Get the current height of the plane vertex
h=GetPlaneHeight(ground,ix,iz)
//Match the vertex to a member of the ripple array
tt=ix+(iz*divisions-1)
//set the new height of the vertex
SetPlaneHeight(ground,ix,iz,h+(COS(ripple[tt])*1))
//and change the ripple height for next time
ripple[tt]=MOD(ripple[tt]+5,360)
NEXT
NEXT
//Set the water texture and draw the plane
X_SETTEXTURE 500,-1
DrawPlane(ground,500)
//check the camera for movement
MoveCam()
X_MAKE2D
PRINT "Camera X:"+camx,0,30
PRINT "Camera Z:"+camz,0,50
PRINT "Camera Y:"+camy,0,70
//show it
SHOWSCREEN
WEND
END
About time I contributed hope this is OK (I am not flameproof!!)
Thanks
Matthew
Title: Plane Lib
Post by: Schranz0r on 2007-Oct-29
I wrote the Cameracode ;)
Good work, hope you make more functions like that !
Title: Plane Lib
Post by: bigsofty on 2008-Jan-10
Do you have the . BMPs for this Matty?
Title: Plane Lib
Post by: matty47 on 2008-Jan-12
The only bmp I used was a tileable water texture. Any would do. Just adjust the name in the code. I think the original was 256 x 256 pixels but you could use larger. Possible could add a bump texture to make the water look more realistic?
Thanks
Matthew