为什么有些顶点没有绘制?

时间:2015-06-05 03:04:46

标签: c++ opengl

我正在尝试使用OpenGL在C ++中制作体素地形生成器,代码正确添加元素,但有些未显示。我检查了它们是否超出了屏幕范围,但事实并非如此。构建多维数据集的代码是。

CubeData createCube(float x, float y, float z, float size) {
    float r = (float) (rand() % 255 + 1);
    float g = (float)(rand() % 255 + 1);
    float b = (float)(rand() % 255 + 1);
    float a = 1.0f;
    if (r > 1.0f)
        r = 100.0f / r;
    if (g > 1.0f)
        g = 100.0f / g;
    if (b > 1.0f)
        b = 100.0f / b;
    std::vector<GLfloat> _cube_verts = {
        -size + x, -size + y, size + z,
        size + x, -size + y, size + z,
        size + x, size + y, size + z,
        -size + x, size + y, size + z,
        -size + x, -size + y, -size + z,
        size + x, -size + y, -size + z,
        size + x, size + y, -size + z,
        -size + x, size + y, -size + z
    };
    std::vector<unsigned int> _indices = {
        // front
        last_verts_len, (last_verts_len + 1), (last_verts_len + 2),
        (last_verts_len + 2), (last_verts_len + 3), last_verts_len,
        // top
        (last_verts_len + 3), (last_verts_len + 2), (last_verts_len + 6),
        (last_verts_len + 6), (last_verts_len + 7), (last_verts_len + 3),
        // back
        (last_verts_len + 7), (last_verts_len + 6), (last_verts_len + 5),
        (last_verts_len + 5), (last_verts_len + 4), (last_verts_len + 7),
        // bottom
        (last_verts_len + 4), (last_verts_len + 5), (last_verts_len + 1),
        (last_verts_len + 1), last_verts_len, (last_verts_len + 4),
        // left
        (last_verts_len + 4), last_verts_len, (last_verts_len + 3),
        (last_verts_len + 3), (last_verts_len + 7), (last_verts_len + 4),
        // right
        (last_verts_len + 1), (last_verts_len + 5), (last_verts_len + 6),
        (last_verts_len + 6), (last_verts_len + 2), (last_verts_len + 1)
    };
    std::vector<GLfloat> _colors = {
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a,
        r, g, b, a
    };
    CubeData data;
    data.verts = _cube_verts;
    data.inds = _indices;
    data.colors = _colors;
    last_verts_len += _cube_verts.size();
    cubeIndex++;
    return data;
}

添加和分配多维数据集的代码是

void Recalibrated::Init() {
    camera = PerspectiveCamera(75.0f, (float)GameApplication::GetWidth() / (float)GameApplication::GetHeight(), 0.01f, 2000.0f);
    camera.Translate(0.0f, 0.0f, 16.0f * 8.0f * 2.0f);
    shader = Shader("../Recalibrated/vertex.glsl", "../Recalibrated/frag.glsl");
    std::vector<GLfloat> verts;
    std::vector<unsigned int> inds;
    std::vector<GLfloat> colors;
    CubeData cube1 = createCube(10 * 8, 0, 0, 4);
    appendFloatVector(&verts, cube1.verts);
    appendFloatVector(&colors, cube1.colors);
    appendUnsignedIntVector(&inds, cube1.inds);
    CubeData cube2 = createCube(10 * 8, 0, 0, 4);
    appendFloatVector(&verts, cube2.verts);
    appendFloatVector(&colors, cube2.colors);
    appendUnsignedIntVector(&inds, cube2.inds);
    //int x, y, z;
    //for (x = 0; x < 32; x++) {
    //}
    drawer = VAODrawer();
    drawer.SetVerticesWithIndicesAndColors(verts, inds, colors);
    drawer.Init();
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
}

CubeData只是一个包含三个向量的结构。并且两个append*函数只是插入到向量的末尾。

任何帮助?

编辑:

我忘记了VAODrawer课程。这可能很重要,因此代码是。

#include "VAODrawer.h"

void VAODrawer::SetVerticesWithIndicesAndColors(std::vector<GLfloat> verts, std::vector<GLuint> indices, std::vector<GLfloat> colors) {
    this->_VERTS = std::vector<GLfloat>(verts);
    this->_INDICES = std::vector<unsigned int>(indices);
    this->_COLORS = std::vector<GLfloat>(colors);
}

void VAODrawer::Init() {
    _MODEL_MATRIX = glm::mat4(1.0f);
    if (_VAO_ID == -1 && _VBO_VERT_ID == -1 && _VBO_COL_ID == -1 && _VAO_ID == -1 && _VERTS.size() != 0 && _INDICES.size() != 0 && _COLORS.size() != 0)
    {
        glGenVertexArrays(1, &_VAO_ID);
        glGenBuffers(1, &_VBO_VERT_ID);
        glGenBuffers(1, &_VBO_COL_ID);
        glGenBuffers(1, &_IND_ID);
        glBindVertexArray(_VAO_ID);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
        glBufferData(GL_ARRAY_BUFFER, _VERTS.size() * sizeof(GLfloat), _VERTS.data(), GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
        glBufferData(GL_ARRAY_BUFFER, _COLORS.size() * sizeof(GLfloat), _COLORS.data(), GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, _INDICES.size() * sizeof(GLuint), &_INDICES[0], GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
        printf(std::string("Ind length" + std::to_string(_INDICES.size()) + "\n").c_str());
    }
}

const glm::mat4 VAODrawer::GetModelMatrix() {
    return _MODEL_MATRIX;
}

void VAODrawer::Draw(const Camera& camera, Shader* shader) {
    if (_VAO_ID != -1 && _VBO_VERT_ID != -1 && _VBO_COL_ID != -1) {
        glBindVertexArray(_VAO_ID);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
        glEnableVertexAttribArray(1);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
        shader->UniformMatrix4("MVPMatrix", (camera.GetProjectionMatrix() * camera.GetViewMatrix() * GetModelMatrix()));
        glDrawElements(GL_TRIANGLES, _INDICES.size(), GL_UNSIGNED_INT, (void*)0);
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }
    else {
        this->Init();
    }
}

void VAODrawer::Rotate(float x, float y, float z) {
    _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, x, glm::vec3(1, 0, 0));
    _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, y, glm::vec3(0, 1, 0));
    _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, z, glm::vec3(0, 0, 1));
}

void VAODrawer::Dispose() {
    glDeleteBuffers(1, &_VBO_VERT_ID);
    glDeleteBuffers(1, &_VBO_COL_ID);
    glDeleteBuffers(1, &_IND_ID);
    glDeleteVertexArrays(1, &_VAO_ID);
}

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。我认为指数被每个顶点的浮点数偏移,而不是顶点的实际数量。更改偏移量以添加8(实际顶点的数量)使所有立方体显示。