簡體   English   中英

更新 OPENGL 中的頂點和索引

[英]update vertices and indices in OPENGL

hi everyone I new to OPENGL and I want to update the vertices and indices when I translate the 3d object I make vertices vector and indices vector these vector hold all the data of the 3d object is this possible the code I use:

    // Create buffers/arrays
    glGenVertexArrays(1, &this->VAO);
    glGenBuffers(1, &this->VBO);
    glGenBuffers(1, &this->EBO);

    glBindVertexArray(this->VAO);
    // Load data into vertex buffers
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    // A great thing about structs is that their memory layout is sequential for all its items.
    // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
    // again translates to 3/2 floats which translates to a byte array.
    glBufferData(GL_ARRAY_BUFFER,this->vertices.size() * sizeof(vertex), &this->vertices[0], GL_STATIC_DRAW);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(int), &this->indices[0], GL_STATIC_DRAW);

    // Set the vertex attribute pointers
    // Vertex Positions
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)0);
    // Vertex Normals
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, normal));
    // Vertex Texture Coords
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, uv));

    glBindVertexArray(0);

我不知道這段代碼是否更改了頂點和索引,或者它只是復制這兩個向量並渲染我想要的不是復制這個向量並渲染我想直接使用這個向量並改變這個向量是這可能

通常在圖形上,我們使用所謂的變換矩陣來進行空間操作,例如平移/旋轉/縮放。

這使我們可以避免在 3D 世界中的 model 特定數據(例如頂點、法線等)和他的 state 之間混合數據(轉換)。

所以我的建議是使用變換矩陣並在頂點着色器中更新 model position。

你可以在這里找到更多信息https://learnopengl.com/Getting-started/Transformations

暫無
暫無

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

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