2
\$\begingroup\$

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++) {
verticesList.add((float) j);
verticesList.add((float)Math.sin(j) * 1.5f);
verticesList.add((float) i);

normalsList.add((float) j);
normalsList.add((float)Math.sin(j) * 1.5f);
normalsList.add((float) i);

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

verticesList.add((float) j);
verticesList.add((float)Math.sin(j) * 1.5f);
verticesList.add((float) i+1);

normalsList.add((float) j);
normalsList.add((float)Math.sin(j) * 1.5f);
normalsList.add((float) i+1);

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

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

P.S.: I need something like this: enter image description here

edit: Result with GL_TRIANGLES draw mode

Better Result

How do I compute the normals?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Please show us your "very scary results". We need to see your errors to be able to see what you could be doing wrong. \$\endgroup\$
    – Philipp
    Commented Nov 2, 2014 at 19:06
  • 1
    \$\begingroup\$ HI. I added a picture in the post. \$\endgroup\$
    – Kronos
    Commented Nov 3, 2014 at 8:34
  • \$\begingroup\$ 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. \$\endgroup\$
    – badweasel
    Commented Jan 2, 2015 at 19:49
  • \$\begingroup\$ youtube.com/… This guy explains how to generate terrain from heightmaps, and add normals. \$\endgroup\$
    – KaareZ
    Commented Feb 1, 2015 at 19:49

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ You're right I had to make Integer of it. And How do I compute the normals the right way? \$\endgroup\$
    – Kronos
    Commented Nov 3, 2014 at 15:25
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jun 2, 2015 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .