glDrawElement,为什么它会导致程序停止工作?

时间:2014-07-26 05:43:16

标签: c++ opengl

我对此感到沮丧。我正在尝试使用顶点缓冲对象渲染一个立方体,我正在学习投影,所以我试图制作一个立方体的框架。但是,此代码不起作用。当我在Code :: Blocks上运行程序时,程序停止工作。

Program stops working

我试图通过注释render()方法的内容找出原因,然后程序不会停止工作。所以这行代码

glDrawElements(GL_TRIANGLES, m_index->size(), GL_UNSIGNED_INT, 0); // render the cube

可能是问题的根源。但我不知道如何解决这个问题,因为这正是我通常做的事情(我已经制作了类似的程序并且有效)

我非常感谢你的帮助!

ProjectionCube::ProjectionCube()
{
    rotationAngle = 0.0;
}

bool ProjectionCube::initialize()
{
#ifdef _WIN32
    glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers");
    glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
    glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
#else
    glGenBuffers = (PFNGLGENBUFFERSARBPROC)glXGetProcAddress((const GLubyte*)"glGenBuffers");
    glBindBuffer = (PFNGLBINDBUFFERPROC)glXGetProcAddress((const GLubyte*)"glBindBuffer");
    glBufferData = (PFNGLBUFFERDATAPROC)glXGetProcAddress((const GLubyte*)"glBufferData");
#endif

    if (!glGenBuffers || !glBindBuffer || !glBufferData)
    {
        std::cerr << "VBOs are not supported by your graphics card" << std::endl;
        return false;
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    initializeVertexBuffers();

    // bind vertex buffer and index buffer
    // set vertex pointer to the buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]);
    glVertexPointer(3, GL_FLOAT, 0, 0); 

    // no color buffer to bind
    return true;
}

void ProjectionCube::initializeVertexBuffers()
{
    const float size = 0.5f;
    m_vertex = getCubeVertices(size);

    for (int i = 0; i < m_vertex->size(); i++) {
        std::cout << m_vertex->at(i) << ", ";
    }
    std::cout << std::endl;

    static const unsigned int index[] = {//using triangles to render
                                  0, 1, 2, 0, 2, 3, //bottom
                                  0, 4, 5, 0, 1, 5, //back
                                  0, 4, 7, 0, 3, 7, //left
                                  1, 5, 6, 1, 2, 6, //right
                                  4, 5, 6, 4, 7, 6, //top
                                  2, 6, 7, 2, 3, 7}; // front

    m_index = new vector<unsigned int>(index, index + sizeof(index) / sizeof(index[0]));

    for (int i = 0; i < m_index->size(); i++) {
        std::cout << m_index->at(i) << ", ";
    }
    std::cout << std::endl;

    glGenBuffers(1, &m_vbos[VERTEX_BUFFER]);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_vertex->size(),
                 &m_vertex->at(0), GL_STATIC_DRAW);

    glGenBuffers(1, &m_vbos[INDEX_BUFFER]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * m_index->size(),
                 &m_index->at(0), GL_STATIC_DRAW);
}

void ProjectionCube::render(float m_x, float m_y, float m_z)
{
   glTranslatef(m_x, m_y, m_z); // move to where the cube is located
    //glRotatef(rotationAngle, 0.5f, 0.0f, 0.0f);
   glDrawElements(GL_TRIANGLES, m_index->size(), GL_UNSIGNED_INT, 0); // render the cube
}

void ProjectionCube::animate(float dt)
{
    const float SPEED = 15.0f;
    rotationAngle += SPEED * dt;

    if (rotationAngle >= 360 || rotationAngle <= 0) {
        rotationAngle = -rotationAngle;
    }
}

vector<GLfloat>* ProjectionCube::getCubeVertices(float r)
{
    static const GLfloat vertices[] = {//bottom square
                                       -r, -r, -r,
                                       r, -r, -r,
                                       r, -r, r,
                                       -r, -r, r,
                                       //top square
                                       -r, r, -r,
                                       r, r, -r,
                                       r, r, r,
                                       -r, r, r,};
    vector<GLfloat>* result = new vector<GLfloat>(vertices, vertices + sizeof(vertices) / sizeof(vertices[0]));
    return result;
}

1 个答案:

答案 0 :(得分:2)

由于您在调用render时没有发帖,我所能做的就是建议您不要使用new vector。就我所见,没有必要。

由于在m_index函数中使用render时发生错误,并且假设m_index是指向向量的指针,因此不需要它作为指针(假设它是ProjectionCube)的成员变量。

new vector存在两个问题。为什么您的程序在getCubeVertices函数中动态分配?以下删除了动态分配:

vector<GLfloat> ProjectionCube::getCubeVertices(float r)
{
    static const GLfloat vertices[] = {//bottom square
                                       -r, -r, -r,
                                       r, -r, -r,
                                       r, -r, r,
                                       -r, -r, r,
                                       //top square
                                       -r, r, -r,
                                       r, r, -r,
                                       r, r, r,
                                       -r, r, r,};
   return vector<GLfloat>(vertices, vertices + sizeof(vertices) / sizeof(vertices[0]));
}

然后在InitializeVertexBuffers()中,m_vertex成员变量不再是指针,而是一个对象:

std::vector<GLfloat> m_vertex;  // assuming these are member variables in your class
std::vector<unsigned int> m_index;
//...
void ProjectionCube::initializeVertexBuffers()
{
    const float size = 0.5f;
    m_vertex = getCubeVertices(size);
    //...
    m_index = vector<unsigned int>(index, index + sizeof(index) / sizeof(index[0]));

同样,不需要new vector。您现在使用m_index.m_vertex.

现在这会给你带来什么,而不是你以前做过什么?好吧,现在您确保在调用m_indexrender有效。由于m_index不再是指针,因此m_index无法解除分配,或指针已损坏等。

使用上述方法,您也可以摆脱潜在的内存泄漏。