I don't mind perspective's post. Its nice to see other peoples work (as long as it pertains to the subject).

I would like to compare notes with you perspective; like what method do you use to generate the terrain? These are my two functions (VERY messy):

Code:

//This method is to create simi random terrain patches.
void Terrain::Init(float width, float height, int rows, int cols, float tscale)
{
m_vertices = new sVERTEX*[rows];
for( int r = 0; r < rows; r++ )
m_vertices[r] = new sVERTEX[cols];
m_rows = rows;
m_cols = cols;
m_width = width;
m_height = height;
m_scale = tscale;
m_isProc = false;
float tdist = m_width/m_cols;
float tdist2 = m_height/m_rows;
/*
0_1_2
|/|/|
3 4 5
*/
int disp = 0;
float max = 1.0f;
int center = m_rows/2;
// initial
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
m_vertices[r][c].X = r*tdist;
if( rand()%3 >= 2 )
m_vertices[r][c].Y = ((float)(rand()%10)/10)*max;
else
m_vertices[r][c].Y = 0.0f;
m_vertices[r][c].Z = c*tdist2;
}
}
// smooth, rolling hills :)
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
//if( !(m_vertices[r][c].Y > 0.0f) ) {
if( 1 ) {
float sum = 0;
int avgc = 0;
if( r > 1 ) {
if(m_vertices[r-1][c].Y != 0.0f ) {
sum += m_vertices[r-1][c].Y;
avgc++; }
}
if( r < m_rows-1 ) {
if(m_vertices[r+1][c].Y != 0.0f ) {
sum += m_vertices[r+1][c].Y;
avgc++;}
}
if( c > 1 ) {
if(m_vertices[r][c-1].Y != 0.0f ) {
sum += m_vertices[r][c-1].Y;
avgc++; }
}
if( c < m_cols-1 ) {
if(m_vertices[r][c+1].Y != 0.0f ) {
sum += m_vertices[r][c+1].Y;
avgc++;}
}
if((r>1)&&(c>1)) {
if(m_vertices[r-1][c-1].Y != 0.0f ) {
sum += m_vertices[r-1][c-1].Y;
avgc++; }
}
if((r<m_rows-1)&&(c<m_cols-1)) {
if(m_vertices[r+1][c+1].Y != 0.0f ) {
sum += m_vertices[r+1][c+1].Y;
avgc++;}
}
if((r>1)&&(c<m_cols-1)) {
if(m_vertices[r-1][c+1].Y != 0.0f ) {
sum += m_vertices[r-1][c+1].Y;
avgc++; }
}
if((r<m_rows-1)&&(c>1)) {
if(m_vertices[r+1][c+1].Y != 0.0f ) {
sum += m_vertices[r+1][c-1].Y;
avgc++; }
}
m_vertices[r][c].Y = ((float)sum/avgc)/1.2;
}
}
}
};
// This is for heightmap based creation of terrain
bool Terrain::Init(char* heightMapFilename, int width, int height, float heightDivider, float scale)
{
FILE* file;
unsigned char* heightMap;
m_vertices = new sVERTEX*[height];
for( int r = 0; r < height; r++ )
m_vertices[r] = new sVERTEX[width];
m_rows = width;
m_cols = height;
m_width = width;
m_height = height;
m_scale = 1.0f;
m_pScale = scale;
m_isProc = true;
float tdist = (m_width/m_cols)*m_pScale;
float tdist2 = (m_height/m_rows)*m_pScale;
file= fopen(heightMapFilename, "rb");
if(file==NULL)
return false;
heightMap= new unsigned char [width*height];
if(heightMap==NULL)
return false; // didnt allocate
fread(heightMap, 1, width*height, file);
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
m_vertices[r][c].X = r*tdist;
int a = heightMap[(r*height)+c];
m_vertices[r][c].Y = (a/4);
m_vertices[r][c].Z = c*tdist2;
}
}
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
float sum = 0;
int avgc = 0;
if(r > 1) {
sum+= m_vertices[r-1][c].Y;
avgc++;
}
if(r < m_rows-1) {
sum+= m_vertices[r+1][c].Y;
avgc++;
}
sum /= avgc;
if( m_vertices[r][c].Y < sum/2) {
m_vertices[r][c].Y = sum;
}
}
}
fclose(file);
int b = m_vertices[5][2].Y;
char* text = cus_printf("y: %i", b);
SetError( text );
if(heightMap)
delete[] heightMap;
// smooth, rolling hills :)
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
//if( !(m_vertices[r][c].Y > 0.0f) ) {
if( 1 ) {
float sum = 0;
int avgc = 0;
if( r > 1 ) {
if(m_vertices[r-1][c].Y != 0.0f ) {
sum += m_vertices[r-1][c].Y;
avgc++; }
}
if( r < m_rows-1 ) {
if(m_vertices[r+1][c].Y != 0.0f ) {
sum += m_vertices[r+1][c].Y;
avgc++;}
}
if( c > 1 ) {
if(m_vertices[r][c-1].Y != 0.0f ) {
sum += m_vertices[r][c-1].Y;
avgc++; }
}
if( c < m_cols-1 ) {
if(m_vertices[r][c+1].Y != 0.0f ) {
sum += m_vertices[r][c+1].Y;
avgc++;}
}
if((r>1)&&(c>1)) {
if(m_vertices[r-1][c-1].Y != 0.0f ) {
sum += m_vertices[r-1][c-1].Y;
avgc++; }
}
if((r<m_rows-1)&&(c<m_cols-1)) {
if(m_vertices[r+1][c+1].Y != 0.0f ) {
sum += m_vertices[r+1][c+1].Y;
avgc++;}
}
if((r>1)&&(c<m_cols-1)) {
if(m_vertices[r-1][c+1].Y != 0.0f ) {
sum += m_vertices[r-1][c+1].Y;
avgc++; }
}
if((r<m_rows-1)&&(c>1)) {
if(m_vertices[r+1][c+1].Y != 0.0f ) {
sum += m_vertices[r+1][c-1].Y;
avgc++; }
}
m_vertices[r][c].Y = (sum/avgc)/1.2;
}
}
}
return true;
};