Opengl 顶点着色器为每个顶点设置布尔值 (glVertexAttribPointer)
Opengl Vertex Shader set boolean per vertex (glVertexAttribPointer)
问题: 我有一个包含结构数组 (MyStruct) 的 VBO,它包含一个布尔属性。我想将此布尔值传递给我的顶点着色器,但根据 glVertexAttribPointer(...);
.
的文档,不允许 GL_BOOL
Specifies the data type of each component in the array. The symbolic
constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT,
GL_INT, and GL_UNSIGNED_INT are accepted by glVertexAttribPointer and
glVertexAttribIPointer. Additionally GL_HALF_FLOAT, GL_FLOAT,
GL_DOUBLE, GL_FIXED, GL_INT_2_10_10_10_REV,
GL_UNSIGNED_INT_2_10_10_10_REV and GL_UNSIGNED_INT_10F_11F_11F_REV are
accepted by glVertexAttribPointer. GL_DOUBLE is also accepted by
glVertexAttribLPointer and is the only token accepted by the type
parameter for that function. The initial value is GL_FLOAT.
Source: https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
我尝试了什么: 如模型所示,我尝试使用 in int myBoolean;
代替,但结构的布尔值(设置正确,我检查过)没有正确映射到顶点着色器中的整数(不过这可能是我的问题?)。
问题的模拟代码:
我的结构
struct MyStruct{
...
bool myBoolean;
...
}
顶点着色器
#version 400 compatibility // Can be set higher if needed
in vec3 position;
//in bool myBoolean;
//in int myBoolean; -> doesn't work either
out vec4 color;
void main( void )
{
//if (myBoolean){ // (myBool == 1) for the int
// color = vec4(1,0,0,1);
//} else {
color = vec4(position, 1);
//}
...
};
我如何设置缓冲区:
// Build OpenGL VBO
glGenBuffers( 1, &m_glVBO );
glBindBuffer( GL_ARRAY_BUFFER, m_glVBO );
glBufferData( GL_ARRAY_BUFFER, numOfMyStructs*sizeof(MyStruct), myStructs, GL_DYNAMIC_DRAW );
// Build OpenGL VAO
glGenVertexArrays( 1, &m_glVAO );
glBindVertexArray( m_glVAO );
// Position attribute
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof(myStruct), (GLvoid*)offsetof(MyStruct, position) );
// Does not work, all booleans in the vertex buffer are false
// and GL_BOOL is not allowed.
//glEnableVertexAttribArray( 1 );
//glVertexAttribPointer( 1, 1, GL_INT, GL_FALSE, sizeof(MyStruct), (GLvoid*)offsetof(MyStruct, myBoolean) );
glBindVertexArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
绑定着色器:
myShader = new QGLShaderProgram( QGLContext::currentContext() );
...
myShader->bindAttributeLocation( "position", 0 );
//myShader->bindAttributeLocation( "myBoolean", 1);
glBindFragDataLocation( myShader->programId(), 0, "color" );
...
渲染:(我以后想用实例渲染(一个球体)不过没关系)
...
glEnable( GL_POINT_SMOOTH );
glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );
glBindVertexArray( m_glVAO );
glDrawArrays( GL_POINTS, 0, numMyStructs );
glBindVertexArray( 0 );
...
附加信息:我使用 QtCreator (3.6.1)、C++11、OpenGL (4.5)、GLM (0.9.7.4)、GLEW(1.13)、 Nvidia 卡(364.16 驱动程序 + CUDA 7.5),Linux 64 位。 我是 OpenGL 新手。 如果可能,我宁愿不使用额外的库。
它在没有 myBoolean 的情况下工作正常(因此在模型代码中设置了注释部分)。模型代码是为 Whosebug 快速键入的,对于可能的语法错误,我深表歉意,请随时指出它们(真实世界的代码在没有 myBoolean 的情况下也能正常工作)。如果需要,我可以提供更多信息。
提前致谢。
你可能想要 glVertexAttribIPointer
(注意字母 'I' 位于那里)——这是为了传递整数值。而且由于结构中的所有内容至少是字节对齐的,所以单个 bool 可以'小于 GL_UNSIGNED_BYTE
https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
问题: 我有一个包含结构数组 (MyStruct) 的 VBO,它包含一个布尔属性。我想将此布尔值传递给我的顶点着色器,但根据 glVertexAttribPointer(...);
.
GL_BOOL
Specifies the data type of each component in the array. The symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, and GL_UNSIGNED_INT are accepted by glVertexAttribPointer and glVertexAttribIPointer. Additionally GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE, GL_FIXED, GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV and GL_UNSIGNED_INT_10F_11F_11F_REV are accepted by glVertexAttribPointer. GL_DOUBLE is also accepted by glVertexAttribLPointer and is the only token accepted by the type parameter for that function. The initial value is GL_FLOAT. Source: https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
我尝试了什么: 如模型所示,我尝试使用 in int myBoolean;
代替,但结构的布尔值(设置正确,我检查过)没有正确映射到顶点着色器中的整数(不过这可能是我的问题?)。
问题的模拟代码:
我的结构
struct MyStruct{
...
bool myBoolean;
...
}
顶点着色器
#version 400 compatibility // Can be set higher if needed
in vec3 position;
//in bool myBoolean;
//in int myBoolean; -> doesn't work either
out vec4 color;
void main( void )
{
//if (myBoolean){ // (myBool == 1) for the int
// color = vec4(1,0,0,1);
//} else {
color = vec4(position, 1);
//}
...
};
我如何设置缓冲区:
// Build OpenGL VBO
glGenBuffers( 1, &m_glVBO );
glBindBuffer( GL_ARRAY_BUFFER, m_glVBO );
glBufferData( GL_ARRAY_BUFFER, numOfMyStructs*sizeof(MyStruct), myStructs, GL_DYNAMIC_DRAW );
// Build OpenGL VAO
glGenVertexArrays( 1, &m_glVAO );
glBindVertexArray( m_glVAO );
// Position attribute
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof(myStruct), (GLvoid*)offsetof(MyStruct, position) );
// Does not work, all booleans in the vertex buffer are false
// and GL_BOOL is not allowed.
//glEnableVertexAttribArray( 1 );
//glVertexAttribPointer( 1, 1, GL_INT, GL_FALSE, sizeof(MyStruct), (GLvoid*)offsetof(MyStruct, myBoolean) );
glBindVertexArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
绑定着色器:
myShader = new QGLShaderProgram( QGLContext::currentContext() );
...
myShader->bindAttributeLocation( "position", 0 );
//myShader->bindAttributeLocation( "myBoolean", 1);
glBindFragDataLocation( myShader->programId(), 0, "color" );
...
渲染:(我以后想用实例渲染(一个球体)不过没关系)
...
glEnable( GL_POINT_SMOOTH );
glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );
glBindVertexArray( m_glVAO );
glDrawArrays( GL_POINTS, 0, numMyStructs );
glBindVertexArray( 0 );
...
附加信息:我使用 QtCreator (3.6.1)、C++11、OpenGL (4.5)、GLM (0.9.7.4)、GLEW(1.13)、 Nvidia 卡(364.16 驱动程序 + CUDA 7.5),Linux 64 位。 我是 OpenGL 新手。 如果可能,我宁愿不使用额外的库。 它在没有 myBoolean 的情况下工作正常(因此在模型代码中设置了注释部分)。模型代码是为 Whosebug 快速键入的,对于可能的语法错误,我深表歉意,请随时指出它们(真实世界的代码在没有 myBoolean 的情况下也能正常工作)。如果需要,我可以提供更多信息。
提前致谢。
你可能想要 glVertexAttribIPointer
(注意字母 'I' 位于那里)——这是为了传递整数值。而且由于结构中的所有内容至少是字节对齐的,所以单个 bool 可以'小于 GL_UNSIGNED_BYTE
https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml