View Full Version : Trouble Generating Terrain Correctly

08-30-2013, 09:27 AM
Hello Everybody (my first post),

I am attempting to generate a terrain. For the most part I have it working; however, only half of the grid draws with the other half underneath. Therefore, half the height map gets applied to the top layer, and then the other half to the bottom layer. I want the grid to be continuous (no layering). I use the vertex shader to sample the height map to determine the height of the current pixel. I am not sure is it's possibly my grid generation that is wrong. The size of the height map is 256 x 256, and GROUND_X_SIZE is 256.

Here is how I generated the grid:

M3DVector3f* vVertBuff = new M3DVector3f[GROUND_X_SIZE * GROUND_X_SIZE * 4]; // array to hold vertex data
int counter = 0;
for(int i = 0; i < GROUND_Z_SIZE; i++) // loop to generate the vertices
for(int j = 0; j < GROUND_X_SIZE; j++)
m3dLoadVector3(vVertBuff[counter++], i*GROUND_QUAD_SIZE, GROUND_HEIGHT, j * GROUND_QUAD_SIZE);
m3dLoadVector3(vVertBuff[counter++], (i+1)*GROUND_QUAD_SIZE, GROUND_HEIGHT, j * GROUND_QUAD_SIZE);
m3dLoadVector3(vVertBuff[counter++], i*GROUND_QUAD_SIZE, GROUND_HEIGHT, (j+1) * GROUND_QUAD_SIZE);
m3dLoadVector3(vVertBuff[counter++], (i+1)*GROUND_QUAD_SIZE, GROUND_HEIGHT, (j+1) * GROUND_QUAD_SIZE);

glGenBuffers(1, &_glhGroundVertBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _glhGroundVertBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(M3DVector3f) * GROUND_X_SIZE * GROUND_Z_SIZE * 4, vVertBuff, GL_STATIC_DRAW);

And here are my vert and fragment shaders:

in vec4 vVertexPos;

uniform mat4 mMVP;
uniform sampler2D heightMap;

out vec2 vTexVary;

void main(void)
vTexVary = vVertexPos.xz/256.0f;
float height = texture(heightMap, vTexVary).r;

// Alter the height
vec4 locPos = vVertexPos;

locPos.y = height * 40.0f - 40.0f;
gl_Position = mMVP * locPos;

in vec2 vTexVary;

uniform vec4 vColor;
uniform sampler2D textureMap;

out vec4 vColorOfMyDamnPixel;

void main(void)
vColorOfMyDamnPixel = vColor * texture(textureMap, vTexVary * 100.0f); // vColor;