- constructor TMap.Create(FileName: String; Resolution: Integer; Position, Rotation: TVector; Size,
- ProcentualHeight: Single; PerPlaneTexture: Boolean; var nWorld: PNewtonWorld);
- var
- Bmp: TBitMap;
- x, z, i: Integer;
- Ratio: Single;
- FieldSize: Single;
- Vertex: Array[0..3] of Array[0..2] of Single;
- VertPos: Array[0..3] of Array[0..1] of Integer;
- collision: PNewtonCollision;
- BoxA, BoxB: TVector;
- count: Integer;
- begin
- VertPos[0][0] := 0;
- VertPos[0][1] := 0;
- VertPos[1][0] := 1;
- VertPos[1][1] := 0;
- VertPos[2][0] := 1;
- VertPos[2][1] := 1;
- VertPos[3][0] := 0;
- VertPos[3][1] := 1;
- Matrix_SetIdentity(MapMatrix);
- Matrix_SetTransform(MapMatrix, V4(Position.x, Position.y, Position.z));
- Matrix_SetRotation(MapMatrix, V3(Rotation.x, Rotation.y, Rotation.z));
- MapResolution := Resolution;
- MapSize := Size;
- MapProcentualHeight := ProcentualHeight;
- Bmp := TBitMap.Create;
- try
- Bmp.LoadFromFile(FileName);
- Ratio := Bmp.Width / Bmp.Height;
- SetLength(HeightData, Resolution+1);
- for x := 0 to Resolution do
- begin
- SetLength(HeightData[x], Resolution+1);
- for z := 0 to Resolution do
- begin
- HeightData[x, z] := Trunc(Bmp.Canvas.Pixels[Trunc(X/Resolution*Bmp.Width),Trunc(Z/Resolution*Bmp.Height)] / clWhite * 255);
- end;
- end;
- finally
- Bmp.Free
- end;
- FieldSize := MapSize / MapResolution;
- collision := NewtonCreateTreeCollision(nWorld, nil);
- NewtonTreeCollisionBeginBuild(collision);
- count := (Resolution-1)*(Resolution-1)*4;
- DisplayList := glGenLists(1);
- glNewList(DisplayList, GL_COMPILE);
- glBegin(GL_QUADS);
- for z := 0 to MapResolution - 1 do
- for x := 0 to MapResolution - 1 do
- begin
- for i := 0 to 3 do
- begin
- Vertex[i][0] := (x + VertPos[i, 0]) * FieldSize;
- Vertex[i][2] := (z + VertPos[i, 1]) * FieldSize;
- Vertex[i][1] := HeightData[x + VertPos[i, 0], z + VertPos[i, 1]] * ProcentualHeight;
- if PerPlaneTexture then
- glTexCoord2f(VertPos[i][0], VertPos[i][1])
- else
- glTexCoord2f((x + VertPos[i, 0]) / MapResolution, (z + VertPos[i, 1]) / MapResolution);
- glVertex3fv(@Vertex[i]);
- end;
- NewtonTreeCollisionAddFace(collision, 4, @Vertex, SizeOf(TVector), 1);
- end;
- glEnd;
- glEndList;
- NewtonTreeCollisionEndBuild(collision, 1);
- MapBody := NewtonCreateBody(nWorld, collision);
- NewtonBodySetMatrix(MapBody, @MapMatrix);
- NewtonCollisionCalculateAABB(collision, @MapMatrix, @BoxA, @BoxB);
- // set the world size
- NewtonSetWorldSize(nWorld, @BoxA, @BoxB);
- // release Collision
- NewtonReleaseCollision(nWorld, collision);
- end;