Skip to main content
added 1471 characters in body
Source Link
gotanod
  • 341
  • 2
  • 7

Camera position

Currently located at (vec3)(0.0f, 0.0f, 3.0f); y=0

In the tessellation evaluation shader, the height (y coord) is added using the heightMap and it is scaled and shifted.

// Comment from the reference learnopengl.com page
// lookup texel at patch coordinate for height and scale + shift as desired
// height_map provides values in range [0.0-1.0]
// scaled by 64.0,  range [0.0,64.0]
// offset by -16.0, range [-16.0, 48.0]
height = texture(height_map, tex_coord).g * 64.0 - 16.0;    


vec4 p_00 = gl_in[0].gl_Position;
vec4 p_01 = gl_in[1].gl_Position;
vec4 p_10 = gl_in[2].gl_Position;
vec4 p_11 = gl_in[3].gl_Position;

vec4 u_vec = p_01 - p_00;
vec4 v_vec = p_10 - p_00;
vec4 normal = normalize( vec4(cross(v_vec.xyz, u_vec.xyz), 0) );

vec4 p_0 = (p_01 - p_00) * u + p_00;
vec4 p_1 = (p_11 - p_10) * u + p_10;
vec4 p = (p_1 - p_0) * v + p_0 + normal * height;  // normal is Y axis, scaled by range [-16.0,48.0]

gl_Position = projection * view * model * p;      // position has a y coord in range [-16.0, 48.0]

The camera should be above y=48.0 to avoid looking the mesh from below. The map is very huge 2624x1744 (height_map size). You should also increase the Far plane (100.0f) to look more terrain and avoiding the clipping with far plane.

 glm_perspective(glm_rad(ccamera->zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f, projection);

Camera position

Currently located at (vec3)(0.0f, 0.0f, 3.0f); y=0

In the tessellation evaluation shader, the height (y coord) is added using the heightMap and it is scaled and shifted.

// Comment from the reference learnopengl.com page
// lookup texel at patch coordinate for height and scale + shift as desired
// height_map provides values in range [0.0-1.0]
// scaled by 64.0,  range [0.0,64.0]
// offset by -16.0, range [-16.0, 48.0]
height = texture(height_map, tex_coord).g * 64.0 - 16.0;    


vec4 p_00 = gl_in[0].gl_Position;
vec4 p_01 = gl_in[1].gl_Position;
vec4 p_10 = gl_in[2].gl_Position;
vec4 p_11 = gl_in[3].gl_Position;

vec4 u_vec = p_01 - p_00;
vec4 v_vec = p_10 - p_00;
vec4 normal = normalize( vec4(cross(v_vec.xyz, u_vec.xyz), 0) );

vec4 p_0 = (p_01 - p_00) * u + p_00;
vec4 p_1 = (p_11 - p_10) * u + p_10;
vec4 p = (p_1 - p_0) * v + p_0 + normal * height;  // normal is Y axis, scaled by range [-16.0,48.0]

gl_Position = projection * view * model * p;      // position has a y coord in range [-16.0, 48.0]

The camera should be above y=48.0 to avoid looking the mesh from below. The map is very huge 2624x1744 (height_map size). You should also increase the Far plane (100.0f) to look more terrain and avoiding the clipping with far plane.

 glm_perspective(glm_rad(ccamera->zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f, projection);
added 2178 characters in body
Source Link
gotanod
  • 341
  • 2
  • 7

Vertices array size

Please, review the array float vertices[1000];


Try changing the order of vertex01 and vertex10 inside the for loops, so they match what shaders are expecting.

float vertices[1000];  // Increase to rez * rez * 20
unsigned rez = 20;
for(unsigned i = 0; i <= rez-1; i++)
{
    for(unsigned j = 0; j <= rez-1; j++)
    {
        // Vertex00
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex10
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex01
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v

        // Vertex11
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v
    }
}
printf("Loaded %i patches of 4 control points each\n", rez*rez);
printf("Processing %i vertices in vertex shader\n", rez*rez*4);

Please, review the array float vertices[1000];

float vertices[1000];
unsigned rez = 20;
for(unsigned i = 0; i <= rez-1; i++)
{
    for(unsigned j = 0; j <= rez-1; j++)
    {
        // Vertex00
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex10
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex01
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v

        // Vertex11
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v
    }
}
printf("Loaded %i patches of 4 control points each\n", rez*rez);
printf("Processing %i vertices in vertex shader\n", rez*rez*4);

Vertices array size

Please, review the array float vertices[1000];


Try changing the order of vertex01 and vertex10 inside the for loops, so they match what shaders are expecting.

float vertices[1000];  // Increase to rez * rez * 20
unsigned rez = 20;
for(unsigned i = 0; i <= rez-1; i++)
{
    for(unsigned j = 0; j <= rez-1; j++)
    {
        // Vertex00
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex10
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex01
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v

        // Vertex11
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v
    }
}
printf("Loaded %i patches of 4 control points each\n", rez*rez);
printf("Processing %i vertices in vertex shader\n", rez*rez*4);
added 2178 characters in body
Source Link
gotanod
  • 341
  • 2
  • 7

Vertex01 and vertex10 swapped

The order they are defined inside the loop do not match the way they are used inside the shaders.


vec4 p_00 = gl_in[0].gl_Position;
vec4 p_01 = gl_in[1].gl_Position;       // gl_in in the VBO is the 10
vec4 p_10 = gl_in[2].gl_Position;       // gl_in in the VBO is the 01
vec4 p_11 = gl_in[3].gl_Position;

vec4 u_vec = p_01 - p_00;
vec4 v_vec = p_10 - p_00;
vec4 normal = normalize( vec4(cross(v_vec.xyz, u_vec.xyz), 0) );        // cross product with opposite direction

vec4 p_0 = (p_01 - p_00) * u + p_00;
vec4 p_1 = (p_11 - p_10) * u + p_10;
vec4 p = (p_1 - p_0) * v + p_0 + normal * height;

gl_Position = projection * view * model * p;

float vertices[1000];
unsigned rez = 20;
for(unsigned i = 0; i <= rez-1; i++)
{
    for(unsigned j = 0; j <= rez-1; j++)
    {
        // Vertex00
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex10
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex01
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v

        // Vertex11
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v
    }
}
printf("Loaded %i patches of 4 control points each\n", rez*rez);
printf("Processing %i vertices in vertex shader\n", rez*rez*4);

Vertex01 and vertex10 swapped

The order they are defined inside the loop do not match the way they are used inside the shaders.


vec4 p_00 = gl_in[0].gl_Position;
vec4 p_01 = gl_in[1].gl_Position;       // gl_in in the VBO is the 10
vec4 p_10 = gl_in[2].gl_Position;       // gl_in in the VBO is the 01
vec4 p_11 = gl_in[3].gl_Position;

vec4 u_vec = p_01 - p_00;
vec4 v_vec = p_10 - p_00;
vec4 normal = normalize( vec4(cross(v_vec.xyz, u_vec.xyz), 0) );        // cross product with opposite direction

vec4 p_0 = (p_01 - p_00) * u + p_00;
vec4 p_1 = (p_11 - p_10) * u + p_10;
vec4 p = (p_1 - p_0) * v + p_0 + normal * height;

gl_Position = projection * view * model * p;

float vertices[1000];
unsigned rez = 20;
for(unsigned i = 0; i <= rez-1; i++)
{
    for(unsigned j = 0; j <= rez-1; j++)
    {
        // Vertex00
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex10
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*j/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float(j / (float)rez, vertices); // v

        // Vertex01
        push_back_float(-width/2.0f + width*i/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float(i / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v

        // Vertex11
        push_back_float(-width/2.0f + width*(i+1)/(float)rez, vertices); // v.x
        push_back_float(0.0f, vertices); // v.y
        push_back_float(-height/2.0f + height*(j+1)/(float)rez, vertices); // v.z
        push_back_float((i+1) / (float)rez, vertices); // u
        push_back_float((j+1) / (float)rez, vertices); // v
    }
}
printf("Loaded %i patches of 4 control points each\n", rez*rez);
printf("Processing %i vertices in vertex shader\n", rez*rez*4);
Source Link
gotanod
  • 341
  • 2
  • 7
Loading