OpenGL没有绘制我的纹理精灵

时间:2016-02-24 18:37:48

标签: c++ opengl

我使用着色器和现代OpenGL。我尝试了glGetError()检查,但没有返回错误,我也尝试使用apitrace进行调试,但我找不到任何东西。我甚至不确定问题是初始化还是绘图代码。

Sprite init:

void Sprite::init(float _x, float _y, float _width, float _height, const char* texture_path) {
    x = _x;
    y = _y;
    width = _width;
    height = _height;

    texture.init(texture_path);

    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ebo);

    // This array will hold our vertex data
    // We need 4 vertices, and each vertex has 2 floats for X and Y
    Vertex vertexData[4];

    // Top right
    vertexData[0].set_position(x + width, y + height);
    vertexData[0].set_uv(1.0f, 1.0f);
    // Bottom right
    vertexData[1].set_position(x + width, y);
    vertexData[1].set_uv(1.0f, 0.0f);
    // Bottom left
    vertexData[2].set_position(x, y);
    vertexData[2].set_uv(0.0f, 0.0f);
    // Top left
    vertexData[3].set_position(x, y + height);
    vertexData[3].set_uv(0.0f, 1.0f);

    for (int i = 0; i < 4; i++) {
    vertexData[i].set_color(255, 255, 255, 255);
    }

    GLuint indices[] = {  // Note that we start from 0!
        0, 1, 3, // First Triangle
        1, 2, 3  // Second Triangle
    };
    // Bind the vertex buffer object (active buffer)
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    // Upload the buffer data to GPU
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    // Unbind the buffer
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

精灵抽奖:

void Sprite::draw() {
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture.id);
    // Bind the buffer object
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

    // Tell OpenGL that we want to use the first attribute array
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    // This is the position attribute pointer
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
    // This is the color attribute pointer
    glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
    // This is the UV attribute pointer
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));

    // Draw the 4 vertices to the screen
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    // Disable the vertex attrib array
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);

    // Unbind the VBO and EBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

渲染代码:

Sprite sprite;
sprite.init(0, 0, 500, 500, "assets/textures/awesomeface.png");

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Enable shader
shader_program.enable();

sprite.draw();

// Disable shader
shader_program.disable();
// Swap buffers
window.swap_window();

1 个答案:

答案 0 :(得分:0)

您需要调用glEnable(GL_TEXTURE_2D);以启用纹理。最好在完成使用该实用程序后立即禁用它,只需在完成绘制后立即放置glDisable(GL_TEXTURE2D);,或者在完成纹理处理后立即禁用它。希望这可以帮助!我也有这个问题,我花了3天的时间盯着一个闪烁的光标来判断。