#### Archived

This topic is now archived and is closed to further replies.

# Terrain Generater

This topic is 5083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay I have a basic terrain generater done. All it does is load a height map, maps a default texture on the terrain and renders it the Terrains look very nice. But I would like to write the infomation to a text file so I can load it into some other programs I've tried several methods and the terrain is coming back missing almost all the points. Here's the code for my Terrain
///////////////////////////////////////////////////////////

//cTerrain

//Class for terrain

//Created March 10, 2004

///////////////////////////////////////////////////////////

#define MAP_X	32				// size of map along x-axis

#define MAP_Z	32				// size of map along z-axis

#define MAP_SCALE	20.0f		// the scale of the terrain map

class cTerrain
{
public:
float terrain[MAP_X][MAP_Z][3];
void CreateTerrain(char* HeightMap);
void Render();
void CleanUp();
void WriteToFile(char *Path);
cTexture Texture;
Vector Position;
private:
unsigned char* imageData;
};
///////////////////////////////////////////////////////////

void cTerrain::CleanUp()
{
Texture.CleanUp();
}
///////////////////////////////////////////////////////////

void cTerrain::CreateTerrain(char* HeightMap)
{

for(int z = 0; z < MAP_Z; z++)
{
for(int x = 0; x < MAP_X; x++)
{
terrain[x][z][0] = float(x)*MAP_SCALE;
terrain[x][z][1] = (float)imageData[(z*MAP_Z+x)*3];
terrain[x][z][2] = -float(z)*MAP_SCALE;
}
}
}
///////////////////////////////////////////////////////////

void cTerrain::Render()
{
glPushMatrix();
glTranslatef(Position.x,Position.y,Position.z);

Texture.Render();

for (int z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < MAP_X-1; x++)
{
// draw vertex 0

glColor3f(terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);

// draw vertex 1

glTexCoord2f(1.0f, 0.0f);
glColor3f(terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f);
glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);

// draw vertex 2

glTexCoord2f(0.0f, 1.0f);
glColor3f(terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f);
glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);

// draw vertex 3

glColor3f(terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
}
glEnd();
}
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, 0);
}
///////////////////////////////////////////////////////////

void cTerrain::WriteToFile(char *Path)
{

}
///////////////////////////////////////////////////////////


any ideas? By the way how do you get that nice code box thing? ...Nevermind now [edited by - GoldMage on March 20, 2004 7:37:28 PM]

##### Share on other sites
to get code boxes use the word source and /source inside of these brackets []

[ source ][ /source ]but without spaces

##### Share on other sites
Would the fact that your "WriteToFile()" function is empty mean anything?

You have to remember that you''re unique, just like everybody else.

##### Share on other sites
Yes that''s the point. I''ve tried to write one but it didn''t work. I need help WRITING a WriteToFile() function. I''m not that stupid . But any way the way I tried was writing the terrain array to the file but when I loaded it, it was missing major parts of the terrain. I posted the source code of the class so you know what I need to save and have a better chance of coming up with ideas. Also for people not needing to save the terrain it is a rather nice class (for my small skill any way)

##### Share on other sites
Well, if you just want to save the array as a RAW file, you can just do something like this:

void WriteFile( char *path ){    ofstream file( path );    if( !file )        return;    file.write( image_data, MAP_X * MAP_Z );    file.close();}

Thats roughly it, I can''t remember the exact parameter lists or anything for the functions, but it can''t be that far off. All you''re doing is saving the image data to a file.

You have to remember that you''re unique, just like everybody else.