Search code examples
openglopengl-estexturesshadertexture-mapping

OPEN GL - Change Vertex position from a texture color


I have a plane, made from a NURB surface, with many vertex so it can create a curved surface depending on the vertex positions ( control points ).

I bind the plane object with two different textures, one is the color texture to be displayed on the object, the other is an heightMap, ( black and white ), which has to alter de vertex yy positions of the plane depending of the color white in the correspondent texture coordinate.

I know the problem is in my shaders. I do not have many experience with OPENGL.

Here is the shader.vert that I use:

attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

varying vec2 vTextureCoord;

uniform sampler2D uSampler2;

uniform float heightScale;

void main() {

//change texture coordinates
vec2 texInver=vec2(1.0, -1.0);

vTextureCoord = aTextureCoord*texInver;
//--------------------------

//change vertex position
vec4 filter = texture2D(uSampler2, vTextureCoord);

float offset = filter.r;

vec3 inc = vec3(0.0, offset, 0.0);

gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition + inc, 1.0);
//----------------------
}

Since the image is black and white, R = G = B. That is why I only check the filter.r

And my shader.frag is:

#ifdef GL_ES
precision highp float;
#endif

varying vec2 vTextureCoord;

uniform sampler2D uSampler;

void main() {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}

This is the height map ( .jpg ):

enter image description here

The result I get is a plane all incremented by 1 in the yy coordinate.

The result I expect is SOME vertex of the plane to be incremented by a 0-1 value in the yy coordinate.


Solution

  • I was forgetting to change the number of the object's vertexes

    This was the problem, after I did that it was solved.