glDrawElements没有绘制任何东西到屏幕上

时间:2014-08-18 03:47:55

标签: c++ opengl

每当我尝试使用glDrawElements绘制网格物体时,我的程序都没有错误,但它并没有绘制任何东西。我很确定问题出在glDrawElements上,因为如果我将glDrawElements注释掉并用glDrawArrays替换它,它就能很好地工作。所以,我的问题是,任何人都可以帮我弄清楚为什么会这样吗?

glGetError也没有返回错误

#include "Mesh.h"

Mesh::Mesh(glm::vec2* vertices, glm::vec2* texCoords, GLushort* elements, int size)
{
    m_size = size;
    m_vertices = vertices;
    m_elements = elements;
    movementVector = glm::vec2(0.0f, 0.0f);

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), vertices, GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    glGenBuffers(1, &tex_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, tex_vbo);
    glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), texCoords, GL_STATIC_DRAW);

    // note: I assume that vertex positions are location 0 
    int dimensions = 2; // 2d data for texture coords 
    glVertexAttribPointer(1, dimensions, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray (1); // don't forget this!

    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

}


Mesh::~Mesh()
{
    glDeleteVertexArrays(1, &vbo);
}

void Mesh::Draw()
{
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);

    //glDrawArrays(GL_TRIANGLES, 0, 8);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);

    glDisableVertexAttribArray(0);
}

这里是指向处理绘图对象到屏幕的代码的链接,同样,我已经测试了我的着色器,它们功能齐全,除非通过glDrawElements绘图也需要更改着色器,所以没有什么是与他们一起造成这种情况。

0 个答案:

没有答案