OPEN GL - 从纹理颜色更改顶点位置

时间:2015-11-14 15:54:01

标签: opengl opengl-es textures shader texture-mapping

我有一个由NURB曲面制成的具有许多顶点的平面,因此它可以根据顶点位置(控制点)创建曲面。

我用两个不同的纹理绑定平面对象,一个是要在对象上显示的颜色纹理,另一个是heightMap(黑色和白色),它必须改变顶点yy的位置。平面取决于相应纹理坐标中的白色。

我知道问题出在我的着色器中。我对OPENGL没有太多经验。

以下是我使用的shader.vert

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);
//----------------------
}

由于图像为黑白,R = G = B.这就是为什么我只检查filter.r

我的shader.frag是:

#ifdef GL_ES
precision highp float;
#endif

varying vec2 vTextureCoord;

uniform sampler2D uSampler;

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

这是高度图(.jpg):

enter image description here

我得到的结果是yy坐标中所有增加1的平面。

我期望的结果是平面的某些顶点在yy坐标中增加0-1值。

1 个答案:

答案 0 :(得分:0)

  

我忘记更改对象顶点的数量

问题出在我解决之后。

相关问题