// SurfaceMacro.inc - Defines macro DrawSurf() which creates a mesh surface defined by a function definedin // Macro SurfaceFunction. The mesh is comprised of smooth triangles. /* Surface is drawn as x-z plane with height in y direction. SurfPos - Position of centre of surface. SurfSize - Size in x and z directions, y coordinate is vertical scale. SurfPatches - Number of patches in x and z direction. Each patch is made up of 2 triangles. SurfFunction - Function number. Use this to pass to the surface function to define the function to use SurfTexture - Texture applied to surface. */ // Define surface function macro // FuncNum is passed from the call to DrawSurf // FuncAction exists incase you need to do some initialization. The macro is called once with this set to 0 // before it is called to get height with this set to 1. // Pos is the position at which to calculate a height. // Macro returns Y value at Pos. // Enclose each function in a #case..#break wrapper like those below. // Change the number to suit. #macro SurfaceFunction(FuncNum,FuncAction,Pos) #local Y = Pos.y; #switch (FuncNum) #case (0) // Simple Sine wave #if (FuncAction=0) // Initialize #else // Calculate Y value from X and Z #local D=sqrt((X*X) + (Z*Z)); // Calculate distance #local Y = sin(((D/4)-(clock*6))*2*pi); #end #break #case (1) // Average surface through array of control points, requires arrays ContPoint and ContSize. #local Y = 0; #local I = 0; #local NumContPoints = dimension_size(ContPoint,1); #while (I; // Initialize function #local SurfFuncAction = 0; #local Y = SurfaceFunction(SurfFuncNum,SurfFuncAction,<0,0,0>); #local SurfFuncAction = 1; #local SurfStep = <(SurfSize.x/SurfPatchs.x),1,(SurfSize.z/SurfPatchs.z)>; // Prime Heights array #local X = (SurfPos.x-(SurfSize.x/2))+(SurfStep.x*-1); #local Z = (SurfPos.z-(SurfSize.z/2))+(SurfStep.z*-1); #local XI = 0; #while (XI); #local XI = XI + 1; #local X = X + SurfStep.x; #end #local Z = (SurfPos.z-(SurfSize.z/2)); #local X = (SurfPos.x-(SurfSize.x/2))+(SurfStep.x*-1); #local XI = 0; mesh{ #while (XI); #local XI = XI + 1; #local X = X + SurfStep.x; #end #local ZI = 2; #local Z = (SurfPos.z-(SurfSize.z/2))+SurfStep.z; #while (ZI); #if(XI>0) #if (XI-, -)); #local V1 = vnormalize(vcross(-, -)); #local V2 = vnormalize(vcross(-, -)); #local V3 = vnormalize(vcross(-, -)); #local NewNormal = vnormalize(V0+V1+V2+V3); #end #end #if (ZI>2) #if (XI>1) #if (XI, NewNormal, , Normals[XI-1], , Normals[XI] texture { SurfTexture } } #end #end #if (XI>0) #if (XI, Normals[XI], , Normals[XI+1], , NewNormal texture { SurfTexture } } #end #end #end #local Heights[XI][0] = Heights[XI][1]; #local Heights[XI][1] = Y; #local Normals[XI] = NewNormal; #local XI = XI + 1; #local X = XNext; #end #local ZI = ZI + 1; #local Z = Z + SurfStep.z; #end scale <1,SurfSize.y,1> } #end