0

For a smoother surface appearance of the terrain, the solution will be to associate a single not normal to a triangle, but a normal to associate to each of three vertices of the triangle.

So, when I'm trying to implement it, I found some problems like blur

https://i.stack.imgur.com/6oPcW.png

This is a scheme for explicit the thing I'm trying to do : https://i.stack.imgur.com/7T04y.png

I'm using this code :

    for (int i=0;i<nb_pt_z;i++)
{
    for (int j=0;j<nb_pt_x;j++)
    {
        Vector N(0.0, 0.0, 0.0);

        if (i != 0)
        {
            if (j != 0)
            {
                Vector v1(points_terrain[((i-1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i-1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i-1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v2(points_terrain[(i*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[(i*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[(i*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);

                N += v1^v2;
            }

            if (j != nb_pt_z-1)
            {
                Vector v1(points_terrain[((i-1)*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i-1)*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i-1)*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v2(points_terrain[((i-1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i-1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i-1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);

                N += v1^v2;

                Vector v3(points_terrain[(i*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[(i*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[(i*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v4(points_terrain[((i-1)*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i-1)*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i-1)*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);

                N+= v3^v4;
            }
        }

        if (i != nb_pt_x-1)
        {
            if (j != 0)
            {
                Vector v1(points_terrain[(i*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[(i*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[(i*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v2(points_terrain[((i+1)*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i+1)*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i+1)*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);

                N+= v1^v2;

                Vector v3(points_terrain[((i+1)*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i+1)*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i+1)*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v4(points_terrain[((i+1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i+1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i+1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);

                N+= v3^v4;
            }

            if (j != nb_pt_z-1)
            {
                Vector v3(points_terrain[((i+1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[((i+1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[((i+1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);

                Vector v4(points_terrain[(i*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
                          points_terrain[(i*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
                          points_terrain[(i*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);

                N+= v3^v4;
            }
        }
        N.normalize();
        points_terrain[(i*nb_pt_x)+j].nx = N.x;
        points_terrain[(i*nb_pt_x)+j].ny = N.y;
        points_terrain[(i*nb_pt_x)+j].nz = N.z;

    }
}

typedef struct                                  
{
    GLfloat s, t;
    GLfloat nx, ny, nz;
    GLfloat x, hauteur,z;                               

} Point_terrain; /* Me, I have a pointer of that already initialized and well fill *

I'm using vertex-array for drawing these points.

num = 0;
for( z=0; z<nb_pt_z-1; z++ )
{
    for( x=0; x<nb_pt_x-1; x++ )
    {
        int ind=z*nb_pt_x+x;
        points_indice[num++] =ind;
        points_indice[num++] = ind+nb_pt_x;
        points_indice[num++] = ind+nb_pt_x+1;
        points_indice[num++] = ind;
        points_indice[num++] = ind+nb_pt_x+1;
        points_indice[num++] = ind+1;
    }
}

and ..

 glInterleavedArrays( GL_T2F_N3F_V3F, 0, points_terrain );
glDrawElements( GL_TRIANGLES, nbIndice,
               GL_UNSIGNED_INT, points_indice );

So , someone know where the problem can be from ?

Pépito
  • 57
  • 4
  • 11
  • Actually, this looks like you have swapped some coordinates. Check if the `for` loops are in the correct order. The problem you are facing is not the blur, but discontinuities of the normals. – Nico Schertler Mar 14 '14 at 22:26
  • Yep I already checked the for, it seems correct.I'm trying to correct this problem during 2-3 nights lol – Pépito Mar 14 '14 at 22:33
  • Your loops suggest that the vertices are aligned x-row by x-row (inner loop goes over x), but the indexing suggests otherwise (left neighbour is `num-nb_pt_x` instead of `num-1`). This does not seem correct. – Nico Schertler Mar 15 '14 at 09:21
  • I updated my first post. After edited my code because of your remarque, it persist. The points_indice tab can be the problem ? ( see first post ) – Pépito Mar 15 '14 at 16:38
  • Your range check is wrong (`if (j != nb_pt_z-1)`). `j` belongs to the x-axis and `i` to the z-axis. But this should not be *the* problem. You could try removing triangles from your scheme. So start with triangle 1 only and see if the normals are smooth. – Nico Schertler Mar 16 '14 at 09:59
  • I tried a lot of function of my friend, it's the same result, so I really think the problem come from an another function. Like I said, it can be the index calcul and the drawing of the vertex array ? – Pépito Mar 16 '14 at 13:52

0 Answers0