@Nicholas Thompson
I think you've made the normals more complicated than they need to be. Since you know the function which defines the height of each vertex, it is a simple matter to calculate the function which represents the normal. The following code illustrates what I have in mind. I have merely written a short function createNormal2 to replace yours.
The result doesn't seem quite right though - probably because I've misunderstood the relation between your vertices and the x,z and height values. I think it's moving along the right lines though. Will have another look later on - got a shader wave program of my own to debug!
set display mode 1280, 1024, 32
sync on
sync rate 0
backdrop on
color backdrop 0
autocam off
`Required Vectors..
null = make vector3(1)
null = make vector3(2)
null = make vector3(3)
null = make vector3(4)
null = make vector3(5)
null = make vector3(6)
null = make vector3(7)
null = make vector3(8)
null = make vector3(9)
gosub MakeWaveMesh
Dim Heights(WAVE_PLAIN_WIDTH, WAVE_PLAIN_DEPTH) as float
position camera -100, 50, -100
point camera 0,0,0
frameTime# = 1.0
startTime = timer()
do
frameTime# = (frameTime# * 0.8) + ((timer() - startTime) * 0.2)
startTime = timer()
text 0, 0, "FPS: " + str$(screen fps())
`This is the height calculation
for x = 0 to WAVE_PLAIN_WIDTH-1
for z = 0 to WAVE_PLAIN_DEPTH-1
t# = timer()
Heights(x, z) = sin((t# + (x * z)) * 1.440)
next z
next x
lock vertexdata for limb 1, 0
for x = 1 to WAVE_PLAIN_WIDTH-1
for z = 1 to WAVE_PLAIN_DEPTH-1
`Do Normal
`function createNormal(hUp#, hRight#, hDown#, hLeft#)
`createNormal(Heights(x, z-1), Heights(x+1, z), Heights(x, z+1), Heights(x-1, z))
createNormal2(t#,x,z)
for i = 0 to 5
v = WaveVerts(x + ArrayOffsets(i, 0), z + ArrayOffsets(i, 1), i)
set vertexdata position v, get vertexdata position x(v), Heights(x, z), get vertexdata position z(v)
set vertexdata normals v, x vector3(9), y vector3(9), z vector3(9)
next i
next z
` text 100, x*10, str$(x vector3(9)) + ", " + str$(y vector3(9)) + ", " + str$(z vector3(9)) + ", H: " + str$(Heights(x, 10))
next x
unlock vertexdata
sync
while spacekey() : endwhile
loop
MakeWaveMesh:
#constant WAVE_PLAIN_WIDTH 32
#constant WAVE_PLAIN_DEPTH 32
#constant WAVE_PLAIN_SCALE 4.0
`First we need an array to store the verts position/ID
Dim WaveVerts(WAVE_PLAIN_WIDTH, WAVE_PLAIN_DEPTH, 6)
`3rd dimensions...
` 0 = TL
` 1 = BL
` 2 = TR
` 3 = BL
` 4 = BR
` 5 = TR
Dim ArrayOffsets(5, 2)
` X Z
ArrayOffsets(0, 0) = 0 : ArrayOffsets(0, 1) = 0
ArrayOffsets(1, 0) = 0 : ArrayOffsets(1, 1) = -1
ArrayOffsets(2, 0) = -1 : ArrayOffsets(2, 1) = 0
ArrayOffsets(3, 0) = 0 : ArrayOffsets(3, 1) = -1
ArrayOffsets(4, 0) = -1 : ArrayOffsets(4, 1) = -1
ArrayOffsets(5, 0) = -1 : ArrayOffsets(5, 1) = 0
`Now make the memblock to create a mesh
make memblock 1, 12 + (32 * 3 * 2 * WAVE_PLAIN_WIDTH * WAVE_PLAIN_DEPTH)
write memblock dword 1,0,274 : `FVF FORMAT
write memblock dword 1,4,32 : `SIZE PER VERT
write memblock dword 1,8,WAVE_PLAIN_WIDTH * WAVE_PLAIN_DEPTH * 2 * 3 : `VERT NO
for i = 0 to WAVE_PLAIN_WIDTH-1
for j = 0 to WAVE_PLAIN_DEPTH-1
x = (i - (WAVE_PLAIN_WIDTH * 0.5)) * WAVE_PLAIN_SCALE
z = (j - (WAVE_PLAIN_DEPTH * 0.5)) * WAVE_PLAIN_SCALE
p = ((i * WAVE_PLAIN_DEPTH) + j)
`Write the two triangles for this poly
`POLY 1
MakeMeshEntry(1, 12 + (p * 192) , x , 0, z , 0, 1, 0, 0.0, 0.0) :`TL
MakeMeshEntry(1, 12 + (p * 192) + 32 , x , 0, z + WAVE_PLAIN_SCALE, 0, 1, 0, 0.0, 1.0) :`BLa
MakeMeshEntry(1, 12 + (p * 192) + 64 , x + WAVE_PLAIN_SCALE, 0, z , 0, 1, 0, 1.0, 0.0) :`TRa
`POLY 2
MakeMeshEntry(1, 12 + (p * 192) + 96 , x , 0, z + WAVE_PLAIN_SCALE, 0, 1, 0, 0.0, 1.0) :`BLa
MakeMeshEntry(1, 12 + (p * 192) + 128 , x + WAVE_PLAIN_SCALE, 0, z + WAVE_PLAIN_SCALE, 0, 1, 0, 1.0, 1.0) :`BR
MakeMeshEntry(1, 12 + (p * 192) + 160 , x + WAVE_PLAIN_SCALE, 0, z , 0, 1, 0, 1.0, 0.0) :`TRb
WaveVerts(i, j, 0) = (p * 6) + 0 :`Top Left
WaveVerts(i, j, 1) = (p * 6) + 1 :`Top Right a
WaveVerts(i, j, 2) = (p * 6) + 2 :`Top Right b
WaveVerts(i, j, 3) = (p * 6) + 3 :`Bottom Right
WaveVerts(i, j, 4) = (p * 6) + 4 :`Bottom Left a
WaveVerts(i, j, 5) = (p * 6) + 5 :`Bottom Left b
next j
next i
make mesh from memblock 1, 1
make object 1, 1, 0
return
`Vector 9 holds the result
function createNormal(hUp#, hRight#, hDown#, hLeft#)
set vector3 1, 0.0, hUp#, -WAVE_PLAIN_SCALE
set vector3 2, WAVE_PLAIN_SCALE, hRight#, 0.0
set vector3 3, 0.0, hDown#, WAVE_PLAIN_SCALE
set vector3 4, -WAVE_PLAIN_SCALE, hLeft#, 0.0
cross product vector3 5, 1, 2
cross product vector3 6, 2, 3
cross product vector3 7, 3, 4
cross product vector3 8, 4, 1
set vector3 9, 0, 0, 0
add vector3 9, 9, 5
add vector3 9, 9, 6
add vector3 9, 9, 7
add vector3 9, 9, 8
endfunction
function createNormal2(t#,x,z)
cosxz#=cos(t#+x*z)
set vector3 9, -z*cosxz#,1.0,-x*cosxz#
divide vector3 9, length vector3(9)
endfunction
function MakeMeshEntry(mbNum, n, x#, y#, z#, nx#, ny#, nz#, u#, v#)
write memblock float mbNum, n + 0, x# :`X
write memblock float mbNum, n + 4, y# :`Y
write memblock float mbNum, n + 8, z# :`Z
write memblock float mbNum, n + 12, nx# :`NX
write memblock float mbNum, n + 16, ny# :`NY
write memblock float mbNum, n + 20, nz# :`NZ
write memblock float mbNum, n + 24, u# :`X
write memblock float mbNum, n + 28, v# :`Y
endfunction