如何在opengl es 2中的顶点着色器程序中访问其他顶点?

发布于 2025-01-01 04:33:52 字数 735 浏览 2 评论 0原文

我是一个android应用程序,我想计算依赖于该表面的其他顶点的表面的法线。我不想在“主”程序中完成它,因为它需要很多时间。实际上,对于每个顶点,我为每个 vextex 传递 4 个浮点数组:

attribute vec3 a_bottom;
attribute vec3 a_left;
attribute vec3 a_right;
attribute vec3 a_top;

vec3 calculNormal( ) {
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

我知道这是非常非常非常脏的代码,所以我不想传递 4 个数组,而是想这样做:

vec3 calculNormal( ) {
    vec3 a_left = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - 1 ];
    vec3 a_bottom = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - X ];
    ...
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

那么在顶点着色器程序中是否可以访问当前的浮动缓冲区?是否有像 currentFloat 这样的特殊关键字?还是我错过了另一种可能性?

I a android application , I want to calcul the normal of a surface depanding on other vertex of this surface. I don't want to do it in "master" programm because it take to much time. Actually for each vertex i pass 4 float array for each vextex :

attribute vec3 a_bottom;
attribute vec3 a_left;
attribute vec3 a_right;
attribute vec3 a_top;

vec3 calculNormal( ) {
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

it is very very very dirty code i know, so instead of passing 4 arrays , i want to do that :

vec3 calculNormal( ) {
    vec3 a_left = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - 1 ];
    vec3 a_bottom = CURRENT_FLOATBUFFER[ CURRENT_FLOAT_BUFFER_POSITION - X ];
    ...
    return normalize( cross( (a_left - a_right) , ( a_bottom - a_top ) ) );
}

So is it possible in a vertex shader programm to acess to the current float buffer ? Is there a special keywords like currentFloat ? Or is there another possibility that i miss ?

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

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

发布评论

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

评论(2

森林散布 2025-01-08 04:33:52

这确实是不可能的。顶点着色器只能访问当前处理的顶点及其属性。由于 OpenGL ES 没有 texture_buffer_object 扩展,因此您无法访问着色器内的 VBO 数据。因此,访问顶点的邻居的唯一方法是显式地将它们作为顶点属性放入,就像第一个示例中一样。

但由于您的几何图形看起来像是一个矩形图案,因此您也可以将其存储在纹理中(或将其复制到纹理中,如果 ES 支持,pixel_buffer_object 扩展可能会对此有所帮助)。在这种情况下,您可以只使用经典的 GPGPU 片段着色器,它根据以下值计算输出图像(在本例中为矩形几何体的法线数据)的每个“像素”(在本例中为顶点)的法线它的邻居(通过简单的纹理访问来访问)。

但我想,考虑到额外的编程开销和/或内存复制操作,第一个和第二个都不会真正被你所接受,因为计算顶点法线已经相当快了。无论如何,您不必每帧都执行此操作,如果您确实这样做,那是因为您也在更新顶点位置,在这种情况下,您可以对普通数据使用相同的更新例程(无论是 CPU 还是GPGPU)。

This is indeed not possible. A vertex shader only has access to the currently processed vertex and its attributes. And since OpenGL ES doesn't have a texture_buffer_object extension, you cannot access the data of a VBO inside a shader. So the only way to access a vertex' neighbours is by explicitly putting them in as vertex attributes, like in your first example.

But since it looks like your geometry is a rectangular pattern, you might also store it in a texture (or copy it to one, a pixel_buffer_object extension might help with that if supported in ES). In this case you could just use a classic GPGPU fragment shader, that computes the normals for each "pixel" (in this case a vertex) of the output image (in this case the normal data of the rectangular geometry) based on the values of its neighbours (accessed by simple texture accesses).

But I guess neither the first one nor the second one would really by you anything, considering the additional prgramming overhead and/or memory copy operations, since computing vertex normals is already pretty fast. You don't have to do it every frame anyway and if you really do, then it is because you're updating your vertex positions, too, in which case you can use the same update routine for the normal data (be it CPU or GPGPU).

风和你 2025-01-08 04:33:52

我想根据该曲面的其他顶点计算该曲面的法线。

错误的做法。计算法线并将其与顶点存储在一起。

我不想在“主”程序中执行此操作,因为它需要很多时间。

您不应该重新计算每个渲染通道的法线。只需将它们计算一并存储即可。顶点着色器中的计算不是免费的。而在VS中计算法线只是浪费处理能力。

计算它们,存储它们。

I want to calcul the normal of a surface depanding on other vertex of this surface.

Wrong approach. Calculate the normals and store them together with the vertices.

I don't want to do it in "master" programm because it take to much time.

You should not recalculate the normals for each rendering pass. Just calculate them one and store them. Calculations in the vertex shader are not for free. And calculating normals in the VS is just waste of processing power.

Calculate them, store them.

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