将数据传递给 GLSL 顶点着色器

Passing data to a GLSL Vertex Shader

我正在尝试使用旧版 OpenGL 固定管道命令转换用 C 编写的程序。

我无法尝试将一些数据传递到顶点着色器。我正在尝试使用最新的 4.5 命令,并且我已经设法将我的顶点坐标数组 "vertices[]" 传递给顶点着色器使用

glCreateVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);

glCreateBuffers(1, &vertex_buffer);
glNamedBufferStorage(vertex_buffer, sizeof(verticies), verticies, GL_DYNAMIC_STORAGE_BIT);


glVertexArrayVertexBuffer(vertex_array_object, 0, vertex_buffer, 0, sizeof(float)*3);
glVertexArrayAttribFormat(vertex_array_object,0, 3, GL_FLOAT, GL_FALSE, 0);

glVertexArrayAttribBinding(vertex_array_object,0,0);
glEnableVertexArrayAttrib(vertex_array_object,0);

一切正常,我可以将顶点渲染为点。

除了传递顶点外,我还需要为每个顶点传递额外的 4 个值块,然后我想将其从顶点着色器传递到几何着色器。我需要传递的值在结构数组中(每个顶点 1 个),其中结构定义为

typedef struct {        /* Vertex vector data structure */

unsigned char  v;           /* Scaled magnitude value     "    */
char           x;           /* X Component of direction cosine */
char           y;           /* Y     "      "     "       "    */
char           z;           /* Z     "      "     "       "    */

} NVD;

我无法轻易更改此结构,因为它在代码的许多其他地方使用。

在顶点着色器中,我需要 4 个值作为范围内的整数

 v (0->255)
 x,y,z (-127 > 127)

I can't easily change this structure as it's used in lots of other places in the code.

好吧,您将不得不这样做,因为您不能将 struct 用作着色器阶段之间的接口变量。您可以将数据作为单个 ivec4 传递,每个组件存储您想要的值。实际上,您应该只传递在着色器中计算的浮点值;无论哪种方式,每个顶点都是 128 位,因此没有必要花时间量化数据。

如果通过分析显示此数据的大小是一个实际问题(并且完全使用 GS 更有可能是性能问题),您可以将数据编码为单个 uint 以传递给 GS,然后在另一端解包。