Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL:地形未绘制(高度地图)_C++_Opengl_Heightmap - Fatal编程技术网

C++ OpenGL:地形未绘制(高度地图)

C++ OpenGL:地形未绘制(高度地图),c++,opengl,heightmap,C++,Opengl,Heightmap,编辑:我想问题可能是在加载顶点和索引时。也许可以关注该部分:) 我正在尝试从bmp文件加载高度贴图并在OpenGL中显示它。与我尝试的大多数东西一样,所有东西都编译并运行,没有错误,但屏幕上没有绘制任何东西。我似乎不能很好地隔离这个问题,因为所有的代码都是独立工作的,但是当结合起来绘制地形时,什么都不起作用 地形等级 我有一门地形课。它有2个VBO、1个IBO和1个VAO。它还存储顶点、索引、顶点颜色和高度。它是从bmp文件加载的 装载地形: Terrain* Terrain::loadTerr

编辑:我想问题可能是在加载顶点和索引时。也许可以关注该部分:)

我正在尝试从bmp文件加载高度贴图并在OpenGL中显示它。与我尝试的大多数东西一样,所有东西都编译并运行,没有错误,但屏幕上没有绘制任何东西。我似乎不能很好地隔离这个问题,因为所有的代码都是独立工作的,但是当结合起来绘制地形时,什么都不起作用

地形等级

我有一门地形课。它有2个VBO、1个IBO和1个VAO。它还存储顶点、索引、顶点颜色和高度。它是从bmp文件加载的

装载地形:

Terrain* Terrain::loadTerrain(const std::string& filename, float height)
{
    BitMap* bmp = BitMap::load(filename);
    Terrain* t = new Terrain(bmp->width, bmp->length);
    for(unsigned y = 0; y < bmp->length; y++)
    {
        for(unsigned x = 0; x < bmp->width; x++)
        {
            unsigned char color =
                (unsigned char)bmp->pixels[3 * (y * bmp->width + x)];
            float h = height * ((color / 255.0f) - 0.5f);
            t->setHeight(x, y, h);
        }
    }
    delete bmp;
    t->initGL();
    return t;
}
void Terrain::render()
{
    glUseProgram(shaderProgram);

    glBindVertexArray(vao);
    int indices_length = (w - 1) * (l - 1) * 6;
    glDrawElements(GL_TRIANGLES, indices_length, GL_UNSIGNED_SHORT, 0);
}
着色器

这些是顶点着色器和片段着色器

顶点:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 vertexColour;

out vec4 fragmentColour;

uniform vec3 offset;
uniform mat4 perspectiveMatrix;

void main()
{
    vec4 cameraPos = position + vec4(offset.x, offset.y, offset.z, 0.0);

    gl_Position = perspectiveMatrix * cameraPos;

    fragmentColour = vertexColour;
}
片段:

#version 330

in vec4 fragmentColour;

out vec4 outputColour;

void main()
{
    outputColour = fragmentColour;
}
透视矩阵

以下是“摄像头”的设置:

调用
initGL()
后,将填充制服:

// get offset uniform
    offsetUniform = ShaderManager::getUniformLocation(shaderProgram, "offset");
    perspectiveMatrixUniform = ShaderManager::getUniformLocation(shaderProgram, "perspectiveMatrix");

    // set standard uniform data
    glUseProgram(shaderProgram);
    glUniform3f(offsetUniform, xOffset, yOffset, zOffset);
    glUniformMatrix4fv(perspectiveMatrixUniform, 1, GL_FALSE, CameraSettings::perspective_matrix);
    glUseProgram(0);

有人能看看我的代码并给出建议吗?

当你说:

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
你真的想说:

    glBufferData(GL_ARRAY_BUFFER, sizeof (Vector4f) * w * l, vertices, GL_STATIC_DRAW);

(与颜色缓冲区等相同)

您能将所有高度设置为
0
并查看结果吗?是否启用背面剔除?也许你的三角形有错误的顶点顺序?我试过使用和不使用背面剔除,结果相同。将“所有高度”设置为0也没有任何作用。你不应该在顶点着色器中使用模型视图矩阵变换顶点吗?另外,你说这样做没有任何错误,但我没有看到对
glGetError()
的任何调用。当你添加它时,有没有显示?@user1118321我现在检查过了,没有任何总账错误。是的。否则,只需输入指针的大小(4字节)。当然!解引用顶点也可以吗?sizeof(*顶点)?只是出于好奇。sizeof(*顶点)=sizeof(Vector4f)
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof (Vector4f) * w * l, vertices, GL_STATIC_DRAW);