Question

I am trying to create terrain which takes height values from txt file .

During my search I realized that generally the terrains are created from bmp files or another kind of picture taking values from pixels. I read height values from file into a 2d array.

void File(){
    FILE* file = fopen("data.txt", "r"); // data.txt opened with read only mode to get heights
    int i,j;
    for (i = 0; i < 17; i++){
        for (j = 0; j < 21; j++){
            fscanf(file, "%d", &data[i][j]);
        }
    }
    fclose(file);
}

and then load these values to vertex to create triangle.

But there are triangles everywhere when I change the x y z values .

The intended project is to create a terrain.

Is there a special way or code to create terrain by using just height values from a txt file?

Was it helpful?

Solution

OpenGL renders primitives like triangles and triangle strips. You'll need to convert your terrain heightmap into primitives that OpenGL understands.

Perhaps this tutorial found by a quick google search can help you.
http://www.codeproject.com/Articles/14154/OpenGL-Terrain-Generation-An-Introduction

OTHER TIPS

Depending on how the data looks in the heightmap, say its in the format of vertex/height.

You could read in a line, say its

v 1.7/2.1/3.7 h 10

store off the vertex and translate that vertex up 10 (height).

Repeat for each vertex and connect them with faces.

If it's just height values then you could substitue in arbitrary verticies for each height value. I.E.

heightmap:

h 10
h 20
h 30

Then as a function:

void generateTerrain(int length, int width)
{
    for(;length>= 0; --length)
    {
       for(; width >= 0; --width)
       {
         //read line from file
         //store the height in a temp hieght variable
         //createVertex(length, width, height);
         //store this vertex somewhere for later
       }
    }
}

There are probably way more efficient ways of doing that, but for simple terrain generation that just popped in my head :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top