简体   繁体   English

OpenGL-绘制带有纹理的正方形的最简单方法

[英]OpenGL - Easiest way to draw a square with a texture

Usually I draw a square with a texture like this: 通常我会绘制一个具有如下纹理的正方形:

  • Create a VBO with 4 coordinates (A,B,C,D for the square) 创建一个具有4个坐标的VBO(正方形为A,B,C,D)
  • 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. 创建一个包含4个索引(A,C,D和B,C,D)的EBO,告诉我要从2个三角形中绘制一个正方形。
  • Draw this elements with a texture 用纹理绘制此元素

Isn't there an easiest way without having a EBO array? 没有EBO阵列不是最简单的方法吗?

Because it is not very handy to use... If I want to use like this: 因为使用起来不是很方便...如果我想这样使用:

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

EBO = [0, 1, 3, 1, 2, 3, ...] 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. 然后,我需要从我的VAO中删除一个正方形...然后,我还需要从我的EBO数组中删除索引,然后重新排列它。 Is there a better way to do this? 有一个更好的方法吗?

If you really only want to draw a square with a texture on it, you should consider make a new empty VAO, and just call glDrawArrays(GL_TRIANGLE_STRIP, 0,3); 如果您真的只想绘制一个带有纹理的正方形,则应考虑制作一个新的空VAO,然后调用glDrawArrays(GL_TRIANGLE_STRIP, 0,3);

The vertex shader then looks like this: 顶点着色器如下所示:

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

The mapping variable tells the fragmentshader what the texture coordinates are. 映射变量告诉fragmenthader纹理坐标是什么。

Isn't there an easiest way without having a EBO array? 没有EBO阵列不是最简单的方法吗?

Duplicate your vertices & use glDrawArrays() . 复制您的顶点并使用glDrawArrays()

You can use DrawArray to plot the indices. 您可以使用DrawArray绘制索引。

Something like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM