# OpenGL generate hills looking vertices

I want to generate a terrain which looks like hills. Some sort of sine waves. Should be as simple as possible. But I can not figure out how. I get very scary results, which are nothing worth. I need to have vertices, normals and the indices of the vertices.

for(int i=0; i < size; i++) {
for(int j=0;j < size; j++) {

}
}


I know that this is not the way how normals a "calculated".

P.S.: I need something like this:

edit:

How do I compute the normals?

• Please show us your "very scary results". We need to see your errors to be able to see what you could be doing wrong. Nov 2 '14 at 19:06
• HI. I added a picture in the post. Nov 3 '14 at 8:34
• First, you should not be using triangle fans. Thats the first thing that's wrong in the bottom example. See that all triangles share the first point as one of the three points. Jan 2 '15 at 19:49
• youtube.com/… This guy explains how to generate terrain from heightmaps, and add normals. Feb 1 '15 at 19:49

I recall reading this: alternative method (there a few) for terrain assignment; I suggest you review and adopt this approach instead of your initial attempts. This approach differs from with your method in that the example uses an existing greyscale bitmap as the heightfield and reads the image-data to assign the elevation of the 'hills.'

However, this difference is easily replaced with your method for sinusoidal calculation of the heightfield. In the author's code, the block where the imagedata is retrieved and assigned into the terrain matrix looks like this:

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;
}
}


You should replace the middle assignment expression terrain[x][z][1] = with your sine-map instead of the imagedata call so it looks like this:

terrain[x][z][1] = float(sin(myradians*(z*MAP_Z+x)))*MAP_SCALE;

  and **BEFORE** the loop, assign the variable myradians:


myradians = float(PI*(angle-90.0f)/180.0f);

so that the sin() function operates correctly in radians.

After that change to the codeproject example, I think you will be able to render a hilly terrain. Then you can make adjustments to the single line to vary the magnitude and offset of the sine-wave.

this looks suspect:

indicesList.add((byte)(i+j));


I'd expect the index to be something like:

indicesList.add((byte)(size*i+j));


but if size*size>255 then you need to upgrade the indicesList to contain a short or int, otherwise they just won't fit.

• You're right I had to make Integer of it. And How do I compute the normals the right way? Nov 3 '14 at 15:25
• This information is right, but it does not make it an answer. It should have been a comment on the question itself, or you should update the answer to have full information on how to solve the question Jun 2 '15 at 10:47