片段着色器为什么根据片段坐标将图像着色为饱和颜色?

huangapple go评论54阅读模式
英文:

Why do fragment shader colors an image based on the fragment coordinate as a saturated color?

问题

以下是您要翻译的内容:

以下的代码根据其片段位置着色图像:

顶点着色器:

varying vec2 vXY; 	
void main(void) {
	vXY = position.xy;
	gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

片段着色器:

precision mediump float;
varying vec2 vXY; 		
void main(void) {	
	vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
	color.x = vXY.x;
	color.y = vXY.y;			
	gl_FragColor = color;
}

它将图像分成4个颜色方块,如下图所示:

片段着色器为什么根据片段坐标将图像着色为饱和颜色?

从中,我可以清晰地看到笛卡尔坐标系,这很好,但我不明白的是:为什么颜色的亮度就像是恒定的1.0?

根据我的理解,由于(x, y)坐标的范围在实数域中从-1到1,所以每个颜色方块的亮度应该随着位置的变化而逐渐变亮,但这并不会发生。为什么?

英文:

The following code colors a image based on its fragment positions:

Vertex shader:

varying vec2 vXY; 	
void main(void) {
	vXY = position.xy;
	gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

Fragment shader:

precision mediump float;
varying vec2 vXY; 		
void main(void) {	
	vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
	color.x = vXY.x;
	color.y = vXY.y;			
	gl_FragColor = color;
}

It separates the image into 4 squares of colors, as the picture bellow:

片段着色器为什么根据片段坐标将图像着色为饱和颜色?

From this, I can clearly see the cartesian coordinate system, which is fine, but what I don't understand is: why is the brightness of the colors as if it were constant 1.0?

From my understanding, since the range of the (x, y) coordinates is from -1 to 1 in the real numbers domain, then each square of color should get gradually brighter as the position changes, but this does not happen. Why?

答案1

得分: 2

"The 'position' attribute contains the original coordinates of the vertex specification. The components of that attribute are only in the range [-1, 1] if you have specified them in the range [-1, 1]. The normalized device coordinates are in the range [-1, 1]. You get the normalized device coordinate by dividing the 'xyz' components of the clip space coordinate ('gl_Position') by its 'w' component (透视除法):

varying vec2 vXY;   
void main(void) {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    vXY = gl_Position.xy / gl_Position.w;
}
英文:

The "position" attribute contains the original coordinates of the vertex specification. The components of that attribute are only in the range [-1, 1] if you have specified them in the range [-1, 1]. The normalized device coordinates are in the range [-1, 1]. You get the normalized device coordinate by dividing the xyz components of the clip space coordinate (gl_Position) by its w component (Perspective divide):

varying vec2 vXY;   
void main(void) {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    vXY = gl_Position.xy / gl_Position.w;
}

huangapple
  • 本文由 发表于 2023年4月10日 20:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定