OpenGL使用ve4动态创建纹理坐标

yv5phkfx  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在跟随这个site学习OpenGL。在核心配置文件模式中,为了渲染四边形和纹理坐标,我定义了如下数据:

float vertices[] = {
    // positions          // texture coords
     0.5f,  0.5f, 0.0f,   s0, t1,   // top right
     0.5f, -0.5f, 0.0f,   s1, t1,   // bottom right
    -0.5f, -0.5f, 0.0f,   s1, t0,   // bottom left
    -0.5f,  0.5f, 0.0f,   s0, t0    // top left 
};

片段着色器


# version 330 core

// FRAGMENT SHADER

out vec4 frag_color;
in vec2 tex_coord;
uniform sampler2D texture;

void main()
{
    frag_color = texture(texture, tex_coord);
}

我要找的是只定义职位数据:

float vertices[] = {
    // positions      
     0.5f,  0.5f, 0.0f,   // top right
     0.5f, -0.5f, 0.0f,   // bottom right
    -0.5f, -0.5f, 0.0f,   // bottom left
    -0.5f,  0.5f, 0.0f    // top left 
};

然后在外部以一致的方式传递一个ve4

glUniform4f( glGetUniformLocation( shader, "texcoord" ),
                 s0, t0, s1, t1);

然后在碎片着色器或顶点着色器中(不知道计算将在哪里进行),从(s0,t0,s1,t1)创建纹理坐标数据,并传递它以计算最终颜色。我的问题是,如何以及在哪里创建文本带?使用这种技术,我可以使用单个顶点缓冲区和图标图集来渲染多个图标。

hof1towb

hof1towb1#

我建议在[0.0,1.0]范围内指定纹理坐标属性。在顶点或碎片着色器中,将纹理坐标从范围[0.0,1.0]Map到范围[s0s1]和[t0t1]。例如:

out vec4 frag_color;
in vec2 tex_coord;     // 0.0 .. 1.0
uniform vec4 texcoord; // s0, t0, s1, t1
uniform sampler2D texture;

void main()
{
    vec2 uv = mix(texcoord.xy, texcoord.zw, tex_coord.xy)
    frag_color = texture(texture, uv);
}

相关问题