我想绘制地形并根据高度应用颜色。
我正在写一个Qt项目,所以使用QGlShaderProgram。具有顶点着色器和QGLShaderProgram的OpenGL高度图
我的地形网格是从(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;
}
哪里是我的错?
我应该如何将数据加载到着色器,然后在那里访问它?
可以在使用mHeightMap.constData()而不是&(mHeightMap [0]) – Archie
使用mHeightMap.constData()中,由于矢量实际上是较长和我传递&(mHeigtMap [DATA_START]) – hardmooth
您可以使用&(mHeightMap.constData()[data_start]),更安全 – izissise