当我将 gl_Position 的 x 坐标设置为零时,WebGL/GLSL 顶点着色器将 9 个点折叠为单个点

发布于 2024-09-19 18:27:31 字数 1169 浏览 2 评论 0原文

我不明白为什么当我渲染 x 坐标为零的顶点缓冲区时,以下两个着色器会产生不同的结果:

第一:

attribute vec3 position;

void main() {
  gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}

第二:

attribute vec3 position;

void main() {
  gl_Position = vec4(0.0, position.y, 0.0, 1.0);
}

第一个的结果是一行九点。第二个结果是一个点。

我将以下顶点数组绘制为 GL_POINTS:

0.0, -1.00, 0.0,
0.0, -0.75, 0.0,
0.0, -0.50, 0.0,
0.0, -0.25, 0.0,
0.0,  0.00, 0.0,
0.0,  0.25, 0.0,
0.0,  0.50, 0.0,
0.0,  0.75, 0.0,
0.0,  1.00, 0.0

这是 VBO 准备调用:

var a = new Float32Array([
    0.0, -1.00, 0.0,
    0.0, -0.75, 0.0,
    0.0, -0.50, 0.0,
    0.0, -0.25, 0.0,
    0.0,  0.00, 0.0,
    0.0,  0.25, 0.0,
    0.0,  0.50, 0.0,
    0.0,  0.75, 0.0,
    0.0,  1.00, 0.0
]);
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, a.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, a);

这是绘制调用:

gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.vertexAttribPointer(p.position, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(p.position);
gl.drawArrays(gl.POINTS, 0, 9);

I do not understand why the following two shaders produce different results when I render a vertex buffer with x coordinates equal to zero:

First:

attribute vec3 position;

void main() {
  gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}

Second:

attribute vec3 position;

void main() {
  gl_Position = vec4(0.0, position.y, 0.0, 1.0);
}

The result of the first is a line of nine dots. The result of the second is a single dot.

I'm drawing the following vertex array as GL_POINTS:

0.0, -1.00, 0.0,
0.0, -0.75, 0.0,
0.0, -0.50, 0.0,
0.0, -0.25, 0.0,
0.0,  0.00, 0.0,
0.0,  0.25, 0.0,
0.0,  0.50, 0.0,
0.0,  0.75, 0.0,
0.0,  1.00, 0.0

Here's the VBO preparation calls:

var a = new Float32Array([
    0.0, -1.00, 0.0,
    0.0, -0.75, 0.0,
    0.0, -0.50, 0.0,
    0.0, -0.25, 0.0,
    0.0,  0.00, 0.0,
    0.0,  0.25, 0.0,
    0.0,  0.50, 0.0,
    0.0,  0.75, 0.0,
    0.0,  1.00, 0.0
]);
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, a.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, a);

Here's the draw calls:

gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.vertexAttribPointer(p.position, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(p.position);
gl.drawArrays(gl.POINTS, 0, 9);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-09-26 18:27:31

这是一个已知问题(抱歉,目前没有链接)。

基本上,GLSL 编译器认为属性“position”没有被使用,因为它的第一个组件没有被使用。要检查这一点,请尝试以下测试:

gl_Position = vec4(0.0, position.y, 0.0, 1.0) + position.x*0.001;

确认错误后,您可以更新视频驱动程序或使用明显的解决方法。

That is a known problem (sorry, no link for now).

Basically, GLSL compiler thinks that attribute 'position' is not used, because the first component of it is not used. To check this try the following test:

gl_Position = vec4(0.0, position.y, 0.0, 1.0) + position.x*0.001;

Once you confirm the bug, you can either update you video driver or use an obvious workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文