View Full Version : getting z-coordinate from elevation map

07-27-2010, 04:12 PM
Hi All,
please can you help me with this: - I'm trying to get a z-coordinate from a elevation map in vertex shader, but I'm missing something. After first drawElements I'm getting glError 1282. I think that something is wrong in my vertex shader, but can not figure out that what.

Here is my code:
fragment shader:

uniform vec4 uFSLevelColor;
void main(void) {
gl_FragColor = uFSLevelColor;
vertex shader:

attribute vec3 aVertexPosition;
// elevation map variables definition:
attribute vec2 aDEMcoordinate; // holding coordinate for elevation map
uniform sampler2D uDEM_sampler; // DEM sampler
// -----------------
uniform vec2 blockOffset;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

void main(void) {
vec3 vtx = aVertexPosition;
// getting only first parameter (since in grayscale rgb is the same)
float zCoord = texture2D(uDEM_sampler, aDEMcoordinate).r;
vtx.z = zCoord;
gl_Position = uPMatrix * uMVMatrix * (vec4(vtx, 1.0) + vec4(blockOffset, 0.0, 1.0));
getting attributes and uniform variables: - attrib arrays are created correctly - debuged and checked

shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPos itionAttribute);
shaderProgram.DEMpositionAttribute = gl.getAttribLocation(shaderProgram, "aDEMcoordinate");
gl.enableVertexAttribArray(shaderProgram.DEMpositi onAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.blockOffset = gl.getUniformLocation(shaderProgram, "blockOffset");
shaderProgram.DEM_sampler = gl.getUniformLocation(shaderProgram, "uDEM_sampler");
// function for loading texture I'm not going to copy here (I think that there should be not the problem)
// texture VBO:

DEM_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, DEM_buffer);
textureCoordinates = [ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0];
gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(textureCoordinates), gl.STATIC_DRAW);
// and finally the draw() / redraw() function

gl.bindBuffer(gl.ARRAY_BUFFER, DEM_buffer);
gl.vertexAttribPointer(shaderProgram.DEMpositionAt tribute, 2, gl.FLOAT, false, 0, 0);
gl.bindTexture(gl.TEXTURE_2D, testTexture);
gl.uniform1i(shaderProgram.DEM_sampler, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, vbo_MxM);
gl.vertexAttribPointer(shaderProgram.vertexPositio nAttribute, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo_MxM);
gl.uniform2f(shaderProgram.blockOffset, 64.0, 1.0);
gl.drawElements(gl.TRIANGLE_STRIP, 8064, gl.UNSIGNED_SHORT, 0);

After the last command - drawElements I'm getting the glError 1282
any help/idea is more then welcomed, since I really don't know what is wrong..

07-27-2010, 11:24 PM
one thing - if I comment:

gl.enableVertexAttribArray(shaderProgram.DEMpositi onAttribute);
and comment the texture related code Iīm not getting the 1282 glError after the drawElements(). The shaderProgram.DEMpositionAttribute variable is generated correctly - that I have debuged.

So most probably something in the aDEMcoordinate attribute? - can not figure out what :(

08-04-2010, 10:25 PM
So most probably something in the aDEMcoordinate attribute? - can not figure out what :(
no, vertex shader was correct. The 1282 glError problem was that I didnīt disable/enable vertexAttribArray between the attribute usage calls.

So now no glError, but still some problem with drawing/getting the z-coord.