我希望能够呈现地形并根据高度应用颜色。
我正在编写一个Qt项目,使用QGlShaderProgram。
我的地形网格从(0,0,0)到(1000,0,1000),每隔100个长度单位放置一个顶点。我想使用统一数组将数据传输到着色器。
我仍然遇到了将数据发送到着色器的问题。
C++/Qt 中的调用:
QGLShaderProgram mShader;
QVector< GLfloat> mHeightMap (10*10, some_data);
GLfloat mXStepSize = 100;
GLfloat mZStepSize = 100;
// ..
mShader.link();
mShader.bind();
mShader.setUniformValueArray( "heights",
&(mHeightMap[0]), // one line after another
mHeightMap.size(), 1 );
mShader.setUniformValue( "x_res", (GLint) mXStepSize);
mShader.setUniformValue( "z_res", (GLint) mZStepSize);
着色器源代码:
uniform sampler2D heights;
uniform int x_res;
uniform int z_res;
void main(void)
{
vec4 tmp = gl_Vertex;
vec4 h;
float x_coord = gl_Vertex[0] * 0.001;
float z_coord = gl_Vertex[2] * 0.001;
// interprete as 2D:
int element = int( (x_coord + float(x_res)*z_coord) );
h = texture2D( heights, vec2(x_coord, z_coord));
gl_FrontColor = gl_Color;
gl_FrontColor[1] = h[ element]; // set color by height
tmp.y = h[ element]; // write height to grid
gl_Position = gl_ModelViewProjectionMatrix * tmp;
}
我的错误在哪里?
我应该如何将数据加载到着色器中,然后在那里访问它?