c++ - 如何将逐顶点属性传递给 GLSL 着色器

标签 c++ opengl shader

如果可能,如何将自定义顶点属性发送到着色器并访问它?请注意,模型将使用 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/

相关文章:

c++ - Netbeans C++ : Execute a source script before building

C++ 错误;我应该如何解释它的含义?

opengl - 具有数组输入的顶点着色器

c++ - 旋转 4x4 矩阵会导致随时间缩放

ios - 为什么我的场景相机在使用 SCNTechnique 时只显示黑屏?

c++ - 使用作业对象限制 Windows 进程内存/执行时间 - 我的代码有什么问题?

c++ - 我在哪里可以学习有关C++编译器的 “what I need to know”?

Opengl:根据 Z 的值,将四边形拟合到屏幕上

opengl - 确定内存中已编译着色器的大小

shader - .hlsl 和 .hlsli 有什么区别?