如果可能,如何将自定义顶点属性发送到着色器并访问它?请注意,模型将使用 VBO 进行渲染。例如,假设我有一个顶点结构如下:
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
要访问法线,可以在着色器中调用:
varying vec3 normal;
如何为自定义属性完成此操作?
最佳答案
首先,您不能使用 double float ,因为几乎任何 GPU 都不支持它们。因此,只需将结构的成员更改为 float ...
在 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;
然后,在 C/C++ 中实例化结构数组,创建您的 vbo,为其分配内存,然后将其绑定(bind)到您的着色器:
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);
然后当你绑定(bind)属性时:
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, sizeof(myVertStruct) * 100, GL_FLOAT, GL_FALSE, 0, &struct);
glBindBuffer(GL_ARRAY_BUFFER, 0);
... 然后将您的 vbo 绑定(bind)到您使用上面使用的 glsl 段创建的着色器程序。
关于c++ - 如何将逐顶点属性传递给 GLSL 着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102593/