该函数是OpenSubdiv库中的一个函数,用于返回GLSL着色器程序的源代码。这些着色器程序可以对细分和绘制曲面进行控制。
该函数有以下参数:
shaderType
:着色器类型,可选值为顶点着色器(Vertex),几何着色器(Geometry)和片段着色器(Fragment)。numVertexElements
:顶点元素数,即顶点的属性数量。vertexElements
:顶点元素数组,包含所有属性元素的名称和数据类型的描述。rvalueShaderSource
:用于储存生成源代码的字符串。该函数没有返回值。
该函数可以通过以下步骤进行使用:
const int numVertexElements = 3;
OpenSubdiv::Osd::VertexElement vertexElements[numVertexElements];
vertexElements[0] = OpenSubdiv::Osd::VertexElement(
0, // vertex attribute index
OpenSubdiv::HbrMesh::VERTEX_FACE_VERTEX_OFFSETS, // base element
OpenSubdiv::Osd::kInt, // data type
1, // data dimension
"vertexIndex"); // name
vertexElements[1] = OpenSubdiv::Osd::VertexElement(
0, // vertex attribute index
OpenSubdiv::HbrMesh::VERTEX_FACE_VARYING_OFFSETS, // base element
OpenSubdiv::Osd::kFloat, // data type
2, // data dimension
"stCoords"); // name
vertexElements[2] = OpenSubdiv::Osd::VertexElement(
0, // vertex attribute index
OpenSubdiv::HbrMesh::NORMAL, // base element
OpenSubdiv::Osd::kFloat, // data type
3, // data dimension
"normal"); // name
std::ostringstream shaderSourceStream;
OpenSubdiv::Osd::GLSLPatchShaderSource::getInstance()->generateShader(
OpenSubdiv::Osd::GLSLPatchShaderSource::VERTEX_SHADER, // shader type
numVertexElements, // vertex element count
vertexElements, // vertex elements array
shaderSourceStream); // output stream
std::string shaderSourceString = shaderSourceStream.str();
以下是一个使用GLSLPatchShaderSource生成源代码并编译成可执行程序的示例:
const int numVertexElements = 3;
OpenSubdiv::Osd::VertexElement vertexElements[numVertexElements];
vertexElements[0] = OpenSubdiv::Osd::VertexElement(
0,
OpenSubdiv::HbrMesh::VERTEX_FACE_VERTEX_OFFSETS,
OpenSubdiv::Osd::kInt,
1,
"vertexIndex");
vertexElements[1] = OpenSubdiv::Osd::VertexElement(
0,
OpenSubdiv::HbrMesh::VERTEX_FACE_VARYING_OFFSETS,
OpenSubdiv::Osd::kFloat,
2,
"stCoords");
vertexElements[2] = OpenSubdiv::Osd::VertexElement(
0,
OpenSubdiv::HbrMesh::NORMAL,
OpenSubdiv::Osd::kFloat,
3,
"normal");
std::ostringstream shaderSourceStream;
OpenSubdiv::Osd::GLSLPatchShaderSource::getInstance()->generateShader(
OpenSubdiv::Osd::GLSLPatchShaderSource::VERTEX_SHADER,
numVertexElements,
vertexElements,
shaderSourceStream);
std::string vertShaderSource = shaderSourceStream.str();
GLint vertShader = glCreateShader(GL_VERTEX_SHADER);
const GLchar* src[1] = { vertShaderSource.c_str()};
glShaderSource(vertShader, 1, src, NULL);
glCompileShader(vertShader);