0

Usually I draw a square with a texture like this:

  • Create a VBO with 4 coordinates (A,B,C,D for the square)
  • Create a EBO with 4 indices (A,C,D and B,C,D) telling that I want to draw a square out of 2 triangles.
  • Draw this elements with a texture

Isn't there an easiest way without having a EBO array?

Because it is not very handy to use... If I want to use like this:

VAO = [-0.8f, 0.5f, 0.0f, ...]

EBO = [0, 1, 3, 1, 2, 3, ...]

Then I need to remove a square from my VAO... then I also need to remove the indices from my EBO array and re-arrange it. Is there a better way to do this?

4

3 回答 3

2

如果你真的只想画一个有纹理的正方形,你应该考虑新建一个空的 VAO,然后调用glDrawArrays(GL_TRIANGLE_STRIP, 0,3);

顶点着色器看起来像这样:

out vec2 mapping;

void main()
{
    float size = 1.0f;

    vec2 offset;
    switch(gl_VertexID)
    {
    case 0:
        //Bottom-left
        mapping = vec2(0.0f, 0.0f);
        offset = vec2(-size, -size);
        break;
    case 1:
        //Top-left
        mapping = vec2(0.0f, 1.0f);
        offset = vec2(-size, size);
        break;
    case 2:
        //Bottom-right
        mapping = vec2(1.0, 0.0);
        offset = vec2(size, -size);
        break;
    case 3:
        //Top-right
        mapping = vec2(1.0, 1.0);
        offset = vec2(size, size);
        break;
    }

     gl_Position = vec4(offset, 0.0f, 1.0f);
}

映射变量告诉片段着色器纹理坐标是什么。

于 2015-07-10T19:51:31.773 回答
1

没有 EBO 阵列就没有最简单的方法吗?

复制您的顶点并使用glDrawArrays().

于 2015-07-10T19:07:48.793 回答
1

您可以使用 DrawArray 绘制索引。

像这样的东西:

Vertex2D* vertex = (Vertex2D*) vbo->lock();
        vertex[0].x = x[0]; vertex[0].y = y[0]; vertex[0].u = u[0]; vertex[0].v = v[0]; vertex[0].color = color;
        vertex[1].x = x[0]; vertex[1].y = y[1]; vertex[1].u = u[0]; vertex[1].v = v[1]; vertex[1].color = color;
        vertex[2].x = x[1]; vertex[2].y = y[1]; vertex[2].u = u[1]; vertex[2].v = v[1]; vertex[2].color = color;
        vertex[3].x = x[1]; vertex[3].y = y[0]; vertex[3].u = u[1]; vertex[3].v = v[0]; vertex[3].color = color;
vbo->unlock();

shader->bind();
vbo->bind();
vao->bind();
tex->bind();
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
tex->unbind();
vao->unbind();
vbo->unbind();
shader->unbind();
于 2015-07-10T19:09:55.637 回答