Original Post
Hi all! I know this is a long shot but I am really out of ideas so I hope someone with experience could help me. I am trying out the code from Dave Astle's book "More OpenGL Game Programming" I am at the terrain chapter working on his example code to generate a textured terrain. I decided to add a simple linear interpolation function so that instead of just suddenly displaying the new terrain, I can smoothly show the transition in real time. Here is my simple interpolation function I added to his CHeight class:
void CHeightClass::MorphInit(int type)
{
if(type == CHeightClass::Midpoint)
{
if(tempA == NULL) tempA = new float[Width*Height];
if(tempB == NULL) tempB = new float[Width*Height];
createHeightmapMidpoint();
for(int ix=0; ix<Width; ix++)
for(int iy=0; iy<Height; iy++)
tempA[ix*Height+iy] = columns[ix][iy].y;
createHeightmapMidpoint(rand_seed);
for(int ix=0; ix<Width; ix++)
for(int iy=0; iy<Height; iy++)
tempB[ix*Height+iy] = columns[ix][iy].y;
}
}
void CHeightClass::MorphRun()
{
float mu = 1 ; //0-1
for(int ix=0; ix<Width; ix++)
for(int iy=0; iy<Height; iy++)
{
columns[ix][iy].y = tempA[ix*Height+iy]*(1-mu)+tempB[ix*Height+iy]*mu; //linear interpolation
}
// Create normals.
createNormals();
}
MorphInit stores the current height value in tempA, generates the heightmap using the same seed and stores it in tempB. MorphRun then sets the height after applying linear interpolation for tempA and tempB. I then call MorphInit and MorphRun when the user presses 3 as shown in this code:
// If the '3' key is pressed, create a heightmap using midpoint displacement.
if ((GetAsyncKeyState('3') & 0x8000))
{
//glClear(GL_COLOR_BUFFER_BIT);
//font.display("Creating Heightmap With:", 1, 30);
//font.display("Midpoint Algorithm", 1, 70);
SwapBuffers(ghDC);
heightmap.MorphInit(CHeightClass::Midpoint);
heightmap.MorphRun();
//heightmap.createHeightmap(CHeightClass::Midpoint);
texture.init(heightmap.getHeightData());
terrain.init(heightmap.getHeightData());
}
I haven't made any other changes to his code. I hope someone who came across this example before or someone who can see what's wrong with my code help me out. It doesn't seem to be interpolating but just doing what it used to do. I can provide more code snippets if anyone is kind enough to help. edit: Or anyone that has any idea to generate terrain in real time? I dont think its possible to alter the terrain properties in this code during runtime but I could be wrong. Thank you. [Edited by - jonathanc on April 23, 2009 9:31:51 PM]