英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论