C ++ OpenGL三角形似乎是错误的顶点

时间:2015-07-18 13:22:43

标签: c++ opengl buffer vertex

我尝试使用OpenGL创建一个多维数据集。当只调用其中一个函数来构建一个立方体时,它可以工作。但是当绘制多边形时,三角形似乎可以解决错误的顶点。你知道我做错了吗?

CubeCreator.cpp

#include "../headers/CubeCreator.h"

CubeCreator::CubeCreator(float voxelSize) {
    this->voxelSize = voxelSize;
}

void CubeCreator::addVertex(glm::vec3 position, glm::vec3 color)
{
    Vertex vertex;
    vertex.position = position;
    vertex.color = color;
    vertices.push_back(vertex);
}

void CubeCreator::addVertices(float verts[8], glm::vec3 color)
{
    for (int i = 0; i < 12; i+=3)
    {
        addVertex(glm::vec3(verts[i], verts[i+1], verts[i+2]), color);
    }
}

void CubeCreator::addTriangles(GLuint tris[6])
{
    for (int i = 0; i < 6; i++)
    {
        triangles.push_back(triangleCount+tris[i]);
    }
    triangleCount += 6;
}

void CubeCreator::left(glm::vec3 position, glm::vec3 color)
{
    float verts[12] =
    {
        position.x,             position.y,             position.z - voxelSize,
        position.x,             position.y,             position.z,
        position.x,             position.y + voxelSize, position.z,
        position.x,             position.y + voxelSize, position.z - voxelSize
    }; addVertices(verts, color);
    GLuint tris[6] =
    {
        0, 1, 3,
        1, 2, 3
    }; addTriangles(tris);
}

void CubeCreator::right(glm::vec3 position, glm::vec3 color)
{
    float verts[12] =
    {
        position.x + voxelSize,             position.y,             position.z,
        position.x + voxelSize,             position.y,             position.z - voxelSize,
        position.x + voxelSize,             position.y + voxelSize, position.z - voxelSize,
        position.x + voxelSize,             position.y + voxelSize
    }; addVertices(verts, color);
    GLuint tris[6] =
    {
        0, 1, 3,
        1, 2, 3
    }; addTriangles(tris);
}

void CubeCreator::back(glm::vec3 position, glm::vec3 color)
{
    float verts[12] =
    {
        position.x,             position.y,             position.z,
        position.x + voxelSize, position.y,             position.z,
        position.x + voxelSize, position.y + voxelSize, position.z,
        position.x,             position.y + voxelSize, position.z
    }; addVertices(verts, color);
    GLuint tris[6] =
    {
        0, 1, 3,
        1, 2, 3
    }; addTriangles(tris);
}

Mesh CubeCreator::getMesh() {
    Mesh mesh(vertices, triangles);
    return mesh;
}

Mesh.cpp

#include "../headers/Mesh.h"

Mesh::Mesh(vector<Vertex> vertices, vector<GLuint> triangles)
{
    this->vertices  = vertices;
    this->triangles = triangles;

    this->init();
}

void Mesh::init() {
    glGenVertexArrays(1, &this->VAO);
    glGenBuffers(1, &this->VBO);
    glGenBuffers(1, &this->EBO);

    glBindVertexArray(this->VAO);
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);

    glBufferData(GL_ARRAY_BUFFER,
                 this->vertices.size() * sizeof(Vertex),
                 &this->vertices[0], GL_STATIC_DRAW);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 this->triangles.size() * sizeof(GLuint),
                 &this->triangles[0], GL_STATIC_DRAW);

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

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,
                          3,
                          GL_FLOAT,
                          GL_FALSE,
                          sizeof(Vertex),
                          (GLvoid*)offsetof(Vertex, color));
}

void Mesh::draw() {
    glBindVertexArray(this->VAO);
    glDrawElements(GL_TRIANGLES, this->triangles.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

的main.cpp

CubeCreator cubeCreator(1.0f);
    cubeCreator.left(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
    cubeCreator.right(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 1.0f));

我知道很多代码,但我不知道错误在哪个部分! 提前谢谢!

0 个答案:

没有答案