一段绘制水面的源代码,谁能帮忙指点一下其中的数学知识
procedure DrawWater;
var I, J : Integer;
VectLength : glFloat;
begin
// Calculate new velocity
For I :=2 to GridSize-2 do
For J :=2 to GridSize-2 do
Velocity[I, J] := Velocity[I, J] + (Position[I, J] -
(4*(Position[I-1,J] + Position[I+1,J] + Position[I,J-1] + Position[I,J+1]) + // left, right, above, below
Position[I-1,J-1] + Position[I+1,J-1] + Position[I-1,J+1] + Position[I+1,J+1])/25) / 7; // diagonally across
// Calculate the new ripple positions
For I:=2 to GridSize-2 do
For J:=2 to GridSize-2 do
Begin
Position[I, J] := Position[I, J] - Velocity[I,J];
Velocity[I, J] := Velocity[I, J] * Viscosity;
End;
// Calculate the new vertex coordinates
For I :=0 to GridSize do
For J :=0 to GridSize do
begin
Vertex[I, J].X :=(I - GridSize/2)/GridSize*5;
Vertex[I, J].Y :=(Position[I, J] / 1024)/GridSize*3;
Vertex[I, J].Z :=(J - GridSize/2)/GridSize*5;
end;
// Calculate the new vertex normals.
// Do this by using the points to each side to get the right angle
For I :=0 to GridSize do
begin
For J :=0 to GridSize do
begin
If (I > 0) and (J > 0) and (I < GridSize) and (J < GridSize) then
begin
with Normals[I, J] do
begin
X := Position[I+1, J] - Position[I-1,J];
Y := -2048;
Z := Position[I, J+1] - Position[I, J-1];
VectLength :=sqrt(x*x + y*y + z*z);
if VectLength <> 0 then
begin
X :=X/VectLength;
Y :=Y/VectLength;
Z :=Z/VectLength;
end;
end;
end
else
begin
Normals[I, J].X :=0;
Normals[I, J].Y :=1;
Normals[I, J].Z :=0;
end;
end;
end;
// Draw the water texture
glBindTexture(GL_TEXTURE_2D, WaterTexture);
For J :=0 to GridSize-1 do
begin
glBegin(GL_QUAD_STRIP);
for I :=0 to GridSize do
begin
glNormal3fv(@Normals[I, J+1]);
// glTexCoord2f(I/GridSize, (J+1)/GridSize);
glVertex3fv(@Vertex[I, J+1]);
glNormal3fv(@Normals[I, J]);
// glTexCoord2f(I/GridSize, J/GridSize);
glVertex3fv(@Vertex[I, J]);
end;
glEnd;
end;
end;