簡體   English   中英

OpenGL:地形不繪圖(高度圖)

[英]OpenGL: Terrain not drawing (heightmap)

編輯:我在想我的問題可能是在加載頂點和索引時。 也許專注於那一節:)

我正在嘗試從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::initGL()
{
    // load vertices from heights data
    vertices = new Vector4f[w * l];
    int vertIndex = 0;
    for(unsigned y = 0; y < l; y++)
    {
        for(unsigned x = 0; x < w; x++)
        {
            vertices[vertIndex++] = Vector4f((float)x, (float)y, heights[y][x], 1.0f);
        }
    }

    // generate indices for indexed drawing
    indices = new GLshort[(w - 1) * (l - 1) * 6]; // patch count * 6 (2 triangles per terrain patch)
    int indicesIndex = 0;
    for(unsigned y = 0; y < (l - 1); ++y)
    {
        for(unsigned x = 0; x < (w - 1); ++x)
        {
            int start = y * w + x;
            indices[indicesIndex++] = (GLshort)start;
            indices[indicesIndex++] = (GLshort)(start + 1);
            indices[indicesIndex++] = (GLshort)(start + w);

            indices[indicesIndex++] = (GLshort)(start + 1);
            indices[indicesIndex++] = (GLshort)(start + 1 + w);
            indices[indicesIndex++] = (GLshort)(start + w);
        }
    }

    // generate colours for the vertices
    colours = new Vector4f[w * l];
    for(unsigned i = 0; i < w * l; i++)
    {
        colours[i] = Vector4f(0.0f, 1.0f, 0.0f, 1.0f); // let's make the entire terrain green
    }

    // THIS CODE WORKS FOR CUBES (BEGIN)

    // vertex buffer object
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // index buffer object
    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // colours vertex buffer object
    glGenBuffers(1, &colour_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, colour_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // create vertex array object
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, colour_vbo);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

    glBindVertexArray(0);

    // THIS CODE WORKS FOR CUBES (END)
}

我創建VBO,IBO和VAO的部分適用於立方體,它們繪制得很好。

渲染地形:

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

透視矩陣

以下是“相機”的設置:

struct CameraSettings
{
    static const float FRUSTUM_SCALE = 1.0f;
    static const float Z_NEAR = 0.5f;
    static const float Z_FAR = 3.0f;

    static float perspective_matrix[16];
};

float CameraSettings::perspective_matrix[16] = {
    FRUSTUM_SCALE,
        0, 0, 0, 0,
        FRUSTUM_SCALE,
        0, 0, 0, 0,
        (Z_FAR + Z_NEAR) / (Z_NEAR - Z_FAR),
        -1.0f,
        0, 0,
        (2 * Z_FAR * Z_NEAR) / (Z_NEAR - Z_FAR),
        0
};

在調用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);

(與顏色緩沖區等相同)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM