对 VBO 中的特定三角形使用不同的纹理 [英] Use different texture for specific triangles in VBO

查看:119
本文介绍了对 VBO 中的特定三角形使用不同的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 9 个由三角形组成的四边形,如下所示:

I have 9 quads made of triangles, like this:

我使用 VBO 来存储关于它们的数据——它们的位置和纹理坐标.

I'm using VBO to store data about them - their position and textures coordinates.

我的问题是 - 是否可以仅使用一个 VBOshader 使 quad 5 具有与其余四边形不同的纹理?:

My question is - is it possible to make the quad 5 to have a different texture than the rest of quads by using only one VBO and shader? :

绿色代表纹理 1,黄色代表纹理 2.

Green color represents texture 1 and yellow - texture 2.

到目前为止我得到了:

GLfloat vertices[] = { 
    // Positions
    0.0f, 0.0f,
    ...

    // Texture coordinates
    0.0f, 0.0f, 
    ...
};

我正在使用 vertices[] 数组创建 VBO,然后绑定我的第一个纹理 m_texture1(我也可以访问第二个 - m_texture2) 并调用着色器:

I'm creating VBO by using that vertices[] array, and then I'm binding my first texture m_texture1 (I also have access to the second one - m_texture2) and calling shader:

glBindTexture(GL_TEXTURE_2D, m_texture1);

glUseProgram(program);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vboId);          // for vertex coordinates
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);                                      // Vertices
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(GLfloat)*108));           // Texture
glDrawArrays(GL_TRIANGLES, 0, 108);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glUseProgram(0);

我的顶点着色器:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec2 texcoord;

out vec2 Texcoord;

void main()
{
    gl_Position = position;
    Texcoord = texcoord;
}

和片段着色器:

#version 330

in vec2 Texcoord;

out vec4 outputColor;

uniform sampler2D tex;

void main()
{
    outputColor = texture(tex, Texcoord) * vec4(1.0,1.0,1.0,1.0);
}

所以基本上我在这里只使用一种纹理,因为我不知道如何使用第二种.

So basically I'm using here only that one texture, because I have no idea how could I use the second one.

推荐答案

您可以使用不同的 纹理图像单元并将它们的值设置为uniform sampler2D tex.

You can do this using different texture image units and setting their values to uniform sampler2D tex.

您必须调用 glActiveTexture 并设置纹理图像单元 (GL_TEXTURE0),绑定您的绿色纹理,然后将活动纹理设置为 GL_TEXTURE1 并绑定您的黄色纹理.这样,您的绿色纹理将具有纹理图像单元 id 0 和黄色 1.

You would have to call glActiveTexture and set texture image unit (GL_TEXTURE0), bind your green texture, then set active texture to GL_TEXTURE1 and bind your yellow texture. This way your green texture will have texture image unit id 0 and yellow 1.

您必须将绘图调用分成两个:一个用于绿色四边形,另一个用于黄色.您必须通过使用 uniform sampler2D tex 来设置要使用的纹理rel="nofollow">glUniform1i 每次调用.最后为 VBO 中的三角形子集调用 glDrawArrays,指示您在当前调用中绘制的绿色或黄色三角形.

You would have to split your drawing calls into two: one for green quads and other for yellow. You will have to set which texture to use by setting your uniform sampler2D tex using glUniform1i for each call. And finally call glDrawArrays for a subset of triangles in your VBO indicating green or yellow ones you are drawing in a current call.

如果您只需要绘制一张图片,您也可以考虑使用 纹理图集你的例子.在这种情况下,您将在一次调用中映射单个纹理.

Also you might consider using a texture atlas if all you need to draw is a picture in your example. In that case you would have a single texture mapped in a single call.

这篇关于对 VBO 中的特定三角形使用不同的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆