簡體   English   中英

使用 glDrawElements 繪制 std::vector

[英]Drawing a std::vector with glDrawElements

我正在嘗試使用我擁有的高度圖繪制地形。 通過使用 std::vector,我現在有一個位置數組和一個位置索引數組,以便在繪制地形時使用glDrawElements(GL_TRIANGLE_STRIP,...) 但是,由於某種原因,我的簡單代碼不起作用。 請注意,我將glm用於數學庫。

以下是 VAO、VBO 和 EBO 的配置。 我仔細檢查了頂點數據和索引數據,它們都是正確的。

// positions and indices
    vector<glm::vec3> positions;
    vector<int> indices;
    for (int z = 0; z < gridZNum; z++) {
        for (int x = 0; x < gridXNum; x++) {
            positions.push_back(glm::vec3(x, _terrain->getHeight(x,z),z));
            //normals.push_back(_terrain->getNormal(x, z));
            indices.push_back(z*gridXNum + (x+1));
            indices.push_back(z*gridXNum + (x+1) + gridXNum);
            if (x == gridXNum-1 && z != gridZNum-1) {
                indices.push_back(z*gridXNum + 2*gridXNum);
                indices.push_back((z+1)*gridXNum+1);
            }
        }
    }

// VAO, VBO configuration
    unsigned int VAO, VBO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindBuffer(GL_ARRAY_BUFFER, VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), &positions[0] , GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int), &indices[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(float), 0);

最后,這是我的繪圖功能

//Inside the render loop I call
drawScene(shader, VAO, indices.size());
//......

void drawScene(Shader &shader, unsigned int VAO, unsigned int numIndices)   //draw the scene with the parameter data.
{
    glm::mat4 model;
    //model = glm::translate(model, glm::vec3(-(_terrain->width())/2, 0.0f, -(_terrain->length())/2));
    glm::mat4 view = camera.GetViewMatrix();
    glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);

    shader.use();
    shader.setMat4("model", model);
    shader.setMat4("view", view);
    shader.setMat4("projection", projection);

    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

我假設像 size 參數這樣的參數有問題,但目前我找不到任何問題。

在定義通用頂點屬性數據數組之前,您必須綁定頂點數組對象。 請參閱頂點數組對象

glBindVertexArray(VAO);

頂點緩沖區對象的名稱是VBO而不是VAO

glBindBuffer(GL_ARRAY_BUFFER, VBO);

以某種方式更改您的代碼:

unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), &positions[0] , GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int), &indices[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(float), 0);

暫無
暫無

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

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