渲染多个元素与OpenGL不显示

时间:2016-06-05 19:32:45

标签: opengl visual-c++

我一直在研究一个简单的渲染器而且无法解决这个问题。 如果我渲染1个立方体没问题,但之后它每3渲染一次(第1,第4,第7,第10,......)。如果有人有一个很好的建议或我错过的地方,我真的很感激帮助。

class Renderer
{
public:
    Renderer();
    ~Renderer();

    void init();
    void addCube(Cube cube);
    void draw();
    void render();

private:
    std::vector<Cube> m_cubes; /*Class builds 12 Voxels and 36 Indices*/
    std::vector<Voxel> m_voxels;
    std::vector<GLushort> m_indices;
    GLuint m_vao;
    GLuint m_veo;
    GLuint m_vbo;
    para::TextureCache m_textureCache; /*Texture map to avoid multiple loading*/
};
/*Renderer.cpp*/
Renderer::Renderer() {
}

Renderer::~Renderer() {
    glDeleteBuffers(1, &m_vbo);
    glDeleteBuffers(1, &m_veo);
    glDeleteVertexArrays(1, &m_vao);
}

void Renderer::init() {
    glGenVertexArrays(1, &m_vao);
    glBindVertexArray(m_vao);
    glGenBuffers(1, &m_vbo);
    glGenBuffers(1, &m_veo);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Voxel), (void*)offsetof(Voxel, pos));
    glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Voxel), (void*)offsetof(Voxel, color));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Voxel), (void*)offsetof(Voxel, uv));
}

void Renderer::addCube(Cube cube) {
        m_cubes.push_back(cube);
        int tmp = m_indices.size();
        /*loads or retrieves already mapped texture - returns GLuint*/
        m_cubes.back().texture = m_textureCache.getTexture(cube.textureName);
        for (size_t i = 0; i < cube.voxels.size(); i++) {
            m_voxels.push_back(cube.voxels[i]);
        }
        for (size_t j = 0; j < cube.indices.size(); j++) {
            GLushort temp = cube.indices[j] + tmp;
            m_indices.push_back(temp);
        }
}

void Renderer::draw() {
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glBufferData(GL_ARRAY_BUFFER, m_voxels.size() * sizeof(Voxel), &m_voxels.front(), GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_veo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLushort), &m_indices.front(), GL_STATIC_DRAW);
}

void Renderer::render() {
    glBindVertexArray(m_vao);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_cubes[0].texture); /*fixed value just for testing*/
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_SHORT, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindVertexArray(0);
}

1 个答案:

答案 0 :(得分:0)

这看起来像是一个问题,您可能已经通过逐步调试调试器中的代码并查看变量值来识别自己。我看到的问题是你在这个代码片段中使用了错误的值来从“local”到“global”索引进行调整:

    int tmp = m_indices.size();
    /*loads or retrieves already mapped texture - returns GLuint*/
    m_cubes.back().texture = m_textureCache.getTexture(cube.textureName);
    for (size_t i = 0; i < cube.voxels.size(); i++) {
        m_voxels.push_back(cube.voxels[i]);
    }
    for (size_t j = 0; j < cube.indices.size(); j++) {
        GLushort temp = cube.indices[j] + tmp;
        m_indices.push_back(temp);
    }

索引值表示顶点数组中的条目,在您的情况下,该条目名为m_voxels。因此,当您添加对象时,要调整已从先前对象存储的顶点的索引,您需要将先前添加的顶点的数量添加到索引,而不是之前的索引数。

因此,该代码片段中的第一行需要更改为:

    int tmp = m_voxels.size();