Hi all!

I've been trying to convert http://www.gametutorials.com Height map tutorial number 1 to Delphi (it's almost the same as the one on NeHe). However when I compile it I get a framerate of ~2 fps.

Needless to say I don't know why and I need some help. Here are the part of the code that seem to mess it up.

[pascal]
//////////////////////////////////////////////////////
// This returns the Height into the Height Map
//////////////////////////////////////////////////////

function Height(pHeightMap: array of Byte; x, y: Integer): Integer;
var
x2,y2: Integer;
begin
x2 := x mod MAP_SIZE;
y2 := y mod MAP_SIZE;

Result := pHeightMap[x2 + (y2 * MAP_SIZE)];
end;

////////////////////////////////////////////////////////////////////////////////////
// This sets the color value for a particular index, depending on the height index
////////////////////////////////////////////////////////////////////////////////////

procedure SetVertexColor(pHeightMap: array of Byte; x, y: Integer);
var
fColor: Single;
begin
fColor := -0.15 + (Height(pHeightMap, x, y) / 256.0);
glColor3f(0, fColor, 0);
end;

////////////////////////////////////////////
// This renders the height map as QUADS
////////////////////////////////////////////

procedure RenderHeightMap(pHeightMap: array of Byte);
var
x1, y1: Integer;
x2, y2, z2: Integer;
// fColor: Single; // never used
begin
x1 := 0; y1 := 0;
if Length(pHeightMap) = 0 then Exit;

glBegin(GL_QUADS);
//x1 := 0;
while x1 < MAP_SIZE do
begin
//y1 := 0; // commented out or testing purposes (it get alot faster from 1/10 fps -> 2 fps)
while y1 < MAP_SIZE do
begin
// bottom left vertex
x2 := x1;
y2 := Height(pHeightMap, x1, y1);
z2 := y1;

SetVertexColor(pHeightMap, x2, z2);

glVertex3i(x2, y2, z2);

// commented out for testing purposes (it gets a bit faster, from 2 fps -> 5 fps)
{ // top left vertex
x2 := x1;
y2 := Height(pHeightMap, x1, y1 + STEP_SIZE);
z2 := y1 + STEP_SIZE;

SetVertexColor(pHeightMap, x2, z2);

glVertex3i(x2, y2, z2);

// top right vertex
x2 := x1 + STEP_SIZE;
y2 := Height(pHeightMap, x1 + STEP_SIZE, y1 + STEP_SIZE);
z2 := y1 + STEP_SIZE;

SetVertexColor(pHeightMap, x2, z2);

glVertex3i(x2, y2, z2);

// bottom right vertex
x2 := x1 + STEP_SIZE;
y2 := Height(pHeightMap, x1 + STEP_SIZE, y1);
z2 := y1;

SetVertexColor(pHeightMap, x2, z2);

glVertex3i(x2, y2, z2); }

y1 := y1 + STEP_SIZE;
end;
x1 := x1 + STEP_SIZE
end;
glEnd;

// reset the color
glColor4f(1.0, 1.0, 1.0, 1.0);
end;

///////////////////////////////////////////////////////////////////////////////////
// This loads a .raw file into an array of bytes. Each value is a height value
///////////////////////////////////////////////////////////////////////////////////

procedure LoadRawFile(strName: String; nSize: Integer; var pHeightMap: array of Byte);
var
f: File;
result: Integer;
begin
if not FileExists(strName) then
begin
MessageBox(0, 'Cannot find the height map!', 'Error', MB_OK);
Exit;
end;

AssignFile(f, strName);
try
{$I-}
Reset(f, 1);
{$I+}

result := IOResult;
if result <> 0 then
begin
MessageBox(0, 'Cannot get data!', 'Error', MB_OK);
end;

BlockRead(f, pHeightMap[0], SizeOf(pHeightMap[0]) * nSize);
finally
CloseFile(f);
end;
end;
[/pascal]

Another thing that's a bit strange is that when MAP_SIZE = 1024, I get a stack overflow error. I don't know if these two problems are related.

If this code isn't the problem I can email the whole thing (~120 kB) if someone is kind enough to want to help.