【问题标题】:OpenGL not drawing my textured spriteOpenGL没有绘制我的纹理精灵
【发布时间】:2016-02-24 18:37:48
【问题描述】:

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

精灵初始化:

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();

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:

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

    【讨论】:

    • 只有传统的固定管道才需要。它对于 OP 似乎正在使用的基于着色器的渲染无效。
    • @RetoKoradi 感谢您的更正。我对 OpenGL 的经验还不是很丰富。一个问题;我认为glEnable(GL_TEXTURE_2D); 需要使用任何纹理功能,例如glBindTexture()。这只是一个不再需要的遗留功能,还是着色器自动启用它?
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多