2013-02-27 237 views
0

如果可能的話,如何將自定義頂點屬性發送給着色器並訪問它?請注意,該模型將與VBO一起呈現。例如,假設我有一個頂點結構如下:如何將每個頂點屬性傳遞給GLSL着色器

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; 

這將如何自定義屬性來實現?

+4

第1步:*停止使用雙打*。第2步:以與其他屬性相同的方式執行此操作。 '正常'沒什麼特別的。另外,'vec3 normal'是一個頂點着色器*輸出*,而不是一個屬性。所以你的問題很困惑。 – 2013-02-27 01:52:40

+0

您正在使用哪種OpenGL版本? OpenGL 2和OpenGL 3+的代碼略有不同。另外,正如@NicolBolas已經提到的,「變化」不是頂點着色器輸入的選項。 – 2013-02-27 08:09:57

回答

1

首先,你不能使用雙精度浮點數,因爲這些不被幾乎所有的GPU.So支持,只是改變你的結構的成員飄起然後......

定義在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,分配的內存,然後將其綁定到着色器:當綁定屬性

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

然後:

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

...然後將您的vbo綁定到您使用上面使用的glsl段創建的着色器程序。

+1

你確定這可以嗎? 'myStructName'不能使用'in'限定符,因爲它是一個'struct'。因此,您不能使用它將值傳遞給着色器。 – 2013-11-23 04:15:21

相關問題