片段着色器如何精确地用于纹理化? [英] How exactly does fragment shader work for texturing?

查看:242
本文介绍了片段着色器如何精确地用于纹理化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习opengl,我以为我很了解片段着色器.我的直觉是,片段着色器仅对每个像素应用一次,但是最近在处理纹理时,我对它们的工作方式感到困惑.

I am learning opengl and I thought I pretty much understand fragment shader. My intuition is that fragment shader gets applied once to every pixel but recently when working with texture, I became confused on how they exactly work.

首先,片段着色器通常采用一系列纹理坐标,因此,如果我有一个四边形,则片段着色器将采用四边形的4个角的纹理坐标.现在,我不了解的是采样过程,即获取纹理坐标并在该纹理坐标处获取适当的颜色值的过程.具体来说,由于我只提供4个纹理坐标,因此opengl如何知道如何为颜色值采样之间的坐标.

First of all, fragment shader typically takes in a series of texture coordinate so if I have a quad, the fragment shader would takes in the texture coordinates for the 4 corners of the quads. Now what I don't understand is the sampling process which is the process of taking the texture coordinates and getting the appropriate color value at that texture coordinates. Specifically, since I only supply 4 texture coordinates, how does opengl knows to samples the coordinates in between for color value.

考虑到顶点着色器直接进入片段着色器并且每个顶点都应用了顶点着色器的事实,此任务变得更加令人困惑.这意味着在任何给定时间,片段着色器仅知道与单个顶点相对应的纹理坐标,而不是构成四边形的整个4个坐标.因此,当一次仅具有一个纹理坐标时,如何准确地采样适合屏幕形状的值呢?

This task is made even more confusing when you consider the fact that vertex shader goes straight to fragment shader and vertex shader gets applied per vertex. This means that at any given time, the fragment shader only knows about the texture coordinate corresponding to a single vertex rather than the whole 4 coordinates that make up the quads. Thus how exactly it knows to samples the values that fit the shapes on the screen when it only have one texture coordinates available at a time?

推荐答案

所有varying变量都是自动插值的.

All varying variables are interpolated automatically.

因此,如果将每个顶点的纹理坐标放入varying,则此后无需对它们进行任何特殊处理.

Thus if you put texture coordinates for each vertex into a varying, you don't need to do anything special with them after that.

它可能像这样简单:

// Vertex
#version 330 compatibility
attribute vec2 a_texcoord;
varying vec2 v_texcoord;

void main()
{
    v_texcoord = a_texcoord;
}

// Fragment
uniform vec2 u_texture;
varying vec2 v_texcoord;

void main()
{
    gl_FragColor = texture2D(u_texture, v_texcoord);
}

免责声明:我使用了旧的GLSL语法.在较新的GLSL版本中,attribute将替换为in. varying将在顶点着色器中替换为out,在片段着色器中替换为in. gl_FragColor将替换为自定义的out vec4变量. texture2D()将替换为texture()

Disclaimer: I used the old GLSL syntax. In newer GLSL versions, attribute would be replaced with in. varying would replaced with out in the vertex shader and with in in the fragment shader. gl_FragColor would be replaced with a custom out vec4 variable. texture2D() would be replaced with texture()

请注意,此片段着色器如何不执行任何手动插值.它仅接收一个vec2 v_texcoord,该vec2 v_texcoord是从包含原始图元 1 所属片段的顶点的v_texcoord内插在引擎盖下的.

Notice how this fragment shader doesn't do any manual interpolation. It receives just a single vec2 v_texcoord, which was interpolated under the hood from v_texcoords of vertices comprising a primitive1 current fragment belongs to.

1. primitive 表示点,线,三角形或四边形.

1. A primitive means a point, a line, a triangle or a quad.

这篇关于片段着色器如何精确地用于纹理化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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