简体   繁体   中英

Passing data to a GLSL Vertex Shader

I'm trying to convert a program written in C using legacy OpenGL Fixed Pipeline commands.

I'm stuck trying to pass some data into a Vertex Shader. I'm trying to use the latest 4.5 commands and I have managed to pass in my array of vertex coordinates "vertices[]" to the Vertex Shader using

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);

This all works fine and I can render the vertices as points.

As well as passing the vertices I also need to pass an additional block of 4 values for each vertex which I then want to pass from the Vertex Shader to a Geometry Shader. The values I need to pass are in an array of structures (1 per vertex) where the structure is defined as

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;

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

Inside the vertex shader I need the 4 values as integers in the ranges

 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.

Well, you're going to have to because you can't use struct s as interface variables between shader stages. You can pass the data along as a single ivec4 , with each component storing the value you want. Though really, you should just pass the floating-point values you compute in the shader; it's going to be 128-bits per-vertex either way, so no point in taking the time to quantize the data.

If the size of this data is shown through profiling to be an actual problem (and using a GS at all is far more likely to be the performance problem), you can encode the data in a single uint for passing to the GS, then unpacking it on the other end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM