gl_PointSize,gl_Position,gl_FragCoord之间的关系 [英] The relation between gl_PointSize, gl_Position, gl_FragCoord

查看:566
本文介绍了gl_PointSize,gl_Position,gl_FragCoord之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例 vs着色器

void main() {
  gl_Position = vec4(0, 0, 0, 1);
  gl_PointSize = 100.0;
}

画布为1x5像素(宽度,高度)

the canvas is 1x5 pixels (width,height)

片段着色器使用gl_FragCoord

the fragment shader uses gl_FragCoord

这5个像素的gl_FragCoord值是什么?

what will the values of gl_FragCoord be for these 5 pixels?

欢呼

推荐答案

对于每个像素gl_FragCoord.xy将是

4.5, 0.5
3.5, 0.5
2.5, 0.5
1.5, 0.5
0.5, 0.5

gl_FragCoord始终是当前正在绘制的像素.您的片段着色器将被调用5次.对于5个像素中的每个像素,一次覆盖100x100大小的点(当然,它被裁剪为画布的大小,您所说的画布大小仅为1x5像素)

gl_FragCoord is always the pixel being drawn currently. Your fragment shader will be called 5 times. Once for each of the 5 pixels the 100x100 sized point covers (it is of course clipped to the size of the canvas which you said is only 1x5 pixels in size)

当前正在绘制哪个像素取决于调用gl.drawArraysgl.drawElements时要求GPU执行的操作.

Which pixels is currently being drawn depends on what you asked the GPU to do when you called gl.drawArrays or gl.drawElements.

上面的顶点着色器(没有更改的输入).假设您已将gl.POINTS传递给gl.drawArrays,它将始终尝试在当前视口的中心向要绘制的内容绘制100x100像素的点".如果您通过了LINESTRIANGLES或其他任何内容,则将无法绘制任何内容,因为着色器始终将gl_Position设置为vec4(0, 0, 0, 1),这将形成长度为零的线或大小为零的三角形.

The vertex shader above as no inputs that change. It will always try to draw a 100x100 pixel "point" in the center of the current viewport to whatever you're drawing to assuming you passed gl.POINTS to gl.drawArrays. If you passed LINES or TRIANGLES or anything else it's not likely to draw anything since the shader always sets gl_Position to vec4(0, 0, 0, 1) which would make a line of zero length or a triangle of zero size.

对于POINTS,gl_Position从剪辑空间转换为您要绘制的对象(画布或帧缓冲区)的像素空间.转换会根据您将gl.viewport设置为什么而发生.

In the case of POINTS, gl_Position is converted from clip space to the pixel space of the thing you're drawing to (canvas or framebuffer). This conversion happens based on whatever you set gl.viewport to.

通常将gl.viewport设置为画布的大小.在这种情况下

generally you set gl.viewport to the size of the canvas. in this case

const x = 0;
const y = 0;
const width = 1;
const height = 5;
gl.viewport(x, y, width, height);

通过视口设置从剪辑空间到像素空间的转换将出现在0.5、2.5的位置.从该像素位置开始,将根据gl_PointSize

The conversion from clipspace to pixelspace via the viewport setting will come out with a position at 0.5, 2.5. From that pixel position a square will be calculated based on gl_PointSize

 gl_Position = vec4(0, 0, 0, 1);
 gl.viewport(0, 0, 1, 5);

 pixelPosition becomes 0.5, 2.5

 x1 = 0.5 - gl_PointSize / 2;
 y1 = 2.5 - gl_PointSize / 2;
 x2 = 0.5 + gl_PointSize / 2;
 y2 = 2.5 + gl_PointSize / 2;

这意味着您要绘制的要点"来自

Which means the "POINT" you asked to be drawn goes from

 x1 = -49.5
 y1 = -47.5
 x2 = 50.5
 y2 = 52.5

该矩形比1x5画布大得多,但会被裁剪,从而导致画布的5个像素被渲染.对于画布中的每个像素,都会调用片段着色器. gl_FragCoord是当前绘制到的像素的坐标.画布中的第一个像素(左下方)的gl_FragCoord.xy始终为(0.5,0.5).该像素右边的一个像素的gl_FragCoord.xy始终为(1.5,0.5).

That rectangle is much larger than the 1x5 canvas but it will be clipped which results in the 5 pixels of the canvas getting rendered to. For each pixel in the canvas the fragment shader is called. gl_FragCoord is the coordinate of the pixel currently being drawn to. The first pixel in the canvas (bottom left) always has a gl_FragCoord.xy of (0.5, 0.5). The pixel one pixel to right of that always has a gl_FragCoord.xy of (1.5, 0.5).

这篇关于gl_PointSize,gl_Position,gl_FragCoord之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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