LucasS
LucasS

Reputation: 729

How To Pass Per-Vertex Attributes to GLSL Shader

How, if possible, can one send a custom vertex property to a shader and access it? Note that the model would be being rendered with a VBO. For example, say I have a vertex structure as follows:

struct myVertStruct{
    double x, y, z; // for spacial coords
    double nx, ny, nz; // for normals
    double u, v; // for texture coords
    double a, b; // just a couple of custom properties

To access, say, the normals, one could call in the shader:

varying vec3 normal;

How would this be done for the custom properties?

Upvotes: 0

Views: 2689

Answers (1)

Sir Digby Chicken Caesar
Sir Digby Chicken Caesar

Reputation: 3133

First off, you can not use double precision floats as these are not supported by just about ANY GPU.So, just change the members of your struct to float then...

Define the same struct in glsl.

struct myVertStruct
{
    float x, y, z; // for spacial coords
    float nx, ny, nz; // for normals
    float u, v; // for texture coords
    float a, b; // just a couple of custom properties
} myStructName;

Then, instantiate the struct array in c/c++, create your vbo, allocate the memory for it, then bind it to your shader:

struct myVertStruct
{
    float x, y, z; // for spacial coords
    float nx, ny, nz; // for normals
    float u, v; // for texture coords
    float a, b; // just a couple of custom properties
};

GLuint vboID;
myVertStruct myStructs[100]; // just using 100 verts for the sake of the example
// .... fill struct array;

glGenBuffers(1,&vboID);

Then when you bind the attribute:

glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, sizeof(myVertStruct) * 100, GL_FLOAT, GL_FALSE, 0, &struct);
glBindBuffer(GL_ARRAY_BUFFER, 0);

... Then bind your vbo to the shader program you created using the glsl segment used above.

Upvotes: 1

Related Questions