带有顶点着色器和 QGLShaderProgram 的 OpenGL HeightMap

har*_*oth 5 c++ opengl qt

我想渲染地形并根据高度应用颜色。
我正在编写一个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);
Run Code Online (Sandbox Code Playgroud)

着色器来源:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的错误在哪里?
我应该如何将数据加载到着色器然后在那里访问它?

har*_*oth 2

(该代码现在似乎可以工作)

在 izissise 的帮助下,我解决了大部分问题。我使用 GL_TEXTURE_RECTANGLE 而不是 GL_TEXTURE_2D。
它仍然只使用红色通道(这可能会被优化)。

这是我的初始化

QGLShaderProgram    mShader;
QVector< GLfloat> mHeightMap (width * height * state_count, 
                              some_data);
mShader.link();

// init texture
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &mShaderTexture);
glBindTexture(GL_TEXTURE_RECTANGLE, mShaderTexture);
Run Code Online (Sandbox Code Playgroud)


并将数据发送到着色器(这可以根据需要重复多次):

mShader.bind();
// ..
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RED,
             width, depth, 0,
             GL_RED, GL_FLOAT,
             &(mHeightMap.constData()[mHeightMapPos])); // set portion of vector as array to texture / sampler
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_RECTANGLE);
glBindTexture(GL_TEXTURE_RECTANGLE, mShaderTexture);
mShader.setUniformValue( "max_height",  (GLfloat) (250.0) );
mShader.setUniformValue( "x_steps",     (GLint) width);
mShader.setUniformValue( "z_steps",     (GLint) height);

// ..
mShader.release();
Run Code Online (Sandbox Code Playgroud)


以及着色器源

uniform int     x_steps;
uniform int     z_steps;
uniform sampler2DRect heights;
uniform float   max_height;

void main(void)
{
    vec4 tmp         = gl_Vertex;
    vec4 h;

    float x_coord    = gl_Vertex[0] * 0.001 * float(x_steps-1);
    float z_coord    = gl_Vertex[2] * 0.001 * float(z_steps-1);
    h                = texture2DRect( heights, ivec2(int(x_coord), int(z_coord)) );
    tmp.y            = max_height * (h.r);

    gl_FrontColor    = gl_Color;
    gl_FrontColor[1] = h.r;

    gl_Position      = gl_ModelViewProjectionMatrix * tmp;
}
Run Code Online (Sandbox Code Playgroud)