我一直在尝试显示网格线框,并将每个边缘作为顶点属性数组传递给自己的颜色。为此,我在顶点缓冲区中为网格的每个边使用了两个顶点。我可以正确显示边缘。问题出现在着色器中的属性位置2,3,4,似乎我没有收到我在VBO中加载的值。当我只重新排序顶点属性时,请说我放了' eColor'在位置0的属性,我开始在片段着色器中获得正确的值。我想知道原因,为什么会这样。我在不同的环境中使用glfw / Qt在不同的环境中执行了相同的代码,我遇到了同样的问题。任何人都可以指出我的错误。
以下是顶点属性绑定的代码:
GL_CHECK( glBindVertexArray( mVAO ) );
GL_CHECK( glUseProgram( mDebugProgram ) );
GL_CHECK( glGenBuffers( 2 , mVBO ) );
GL_CHECK( glBindBuffer( GL_ARRAY_BUFFER , mVBO[ 0 ] ) );
GL_CHECK( glBufferData( GL_ARRAY_BUFFER , mDebugVertexData.size() * sizeof( DebugVertexData ) , mDebugVertexData.data() , GL_DYNAMIC_DRAW ) );
GL_CHECK( glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , mVBO[ 1 ] ) );
GL_CHECK( glBufferData( GL_ELEMENT_ARRAY_BUFFER , mDebugWFIndices.size() * sizeof( GLuint ) , mDebugWFIndices.data() , GL_DYNAMIC_DRAW ) );
int offset = 0;
GL_CHECK( glVertexAttribPointer( 0 , 3, GL_FLOAT, GL_FALSE, sizeof( DebugVertexData ) , 0) );
offset = 3 * sizeof( GLfloat );
GL_CHECK( glVertexAttribPointer( 1 , 3 , GL_FLOAT, GL_FALSE, sizeof ( DebugVertexData ) , ( float * )offset ) );
offset = 3 * sizeof( GLfloat );
GL_CHECK( glVertexAttribPointer( 2 , 3 , GL_FLOAT, GL_FALSE, sizeof ( DebugVertexData ) , ( float * )offset ) );
offset = 3 * sizeof( GLfloat );
GL_CHECK( glVertexAttribPointer( 3 , 1 , GL_FLOAT, GL_FALSE, sizeof ( DebugVertexData ) , ( float * )offset ) );
offset = sizeof( GLfloat );
GL_CHECK( glVertexAttribPointer( 4 , 1 , GL_FLOAT, GL_FALSE, sizeof ( DebugVertexData ) , ( float * )offset ) );
GL_CHECK( glEnableVertexAttribArray(0) );
GL_CHECK( glEnableVertexAttribArray(1) );
GL_CHECK( glEnableVertexAttribArray(2) );
GL_CHECK( glEnableVertexAttribArray(3) );
GL_CHECK( glEnableVertexAttribArray(4) );
GL_CHECK( glBindBuffer( GL_ARRAY_BUFFER , 0 ) );
GL_CHECK( glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , 0 ) );
GL_CHECK( glUseProgram( 0) );
GL_CHECK( glBindVertexArray(0) );
以下是我的顶点着色器:
#version 410
layout ( location=0 ) in vec3 position;
layout ( location=1 ) in vec3 normal;
layout ( location=2 ) in vec3 eColor;
layout ( location=3 ) in float underRegion;
layout ( location=4 ) in float isSplittable;
out vec4 vPosition;
out vec3 vNormal;
out vec3 vColor;
out float flag1;
out float flag2;
void main()
{
vPosition = vec4( position , 1 );
vNormal = normal;
flag1 = underRegion;
flag2 = isSplittable;
vColor = eColor;
// gl_Position = mvpMatrix * vec4( position , 1 );
}
这是几何着色器:
#version 410
layout( lines ) in;
layout( line_strip , max_vertices = 2 ) out;
uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
in vec4[ 2 ] vPosition;
in vec3[ 2 ] vNormal;
in vec3[ 2 ] vColor;
in float[ 2 ] flag1;
in float[ 2 ] flag2;
// Output to the fragment shader
out float isEdgeSplittable;
out vec3 edgeColor;
void main()
{
float l = length( vPosition[ 0 ].xyz - vPosition[ 1 ].xyz );
vec4 v1 = vPosition[ 0 ];
vec4 v2 = vPosition[ 1 ];
v1.xyz += vNormal[ 0 ] * l * 0.001;
v2.xyz += vNormal[ 1 ] * l * 0.001;
v1 = mvpMatrix * v1;
v2 = mvpMatrix * v2;
edgeColor = vColor[ 0 ];
gl_Position = v1;
if( flag1[ 0 ] > 0.5 )
{
isEdgeSplittable = 1.0;
}
else
{
isEdgeSplittable = 0.0;
}
EmitVertex();
gl_Position = v2;
if( flag1[ 0 ] > 0.5 )
{
isEdgeSplittable = 1.0;
}
else
{
isEdgeSplittable = 0.0;
}
edgeColor = vColor[ 1 ];
EmitVertex();
EndPrimitive();
}
以下是片段着色器:
#version 410
layout (location = 0) out vec4 color;
in float isEdgeSplittable;
in vec3 edgeColor;
void main()
{
color.xyz = edgeColor;//vec3(0 , 0 , 1) ;//eColor2;
//color.w = 1.0;
if( isEdgeSplittable > 0.5 )
{
color.xyz = vec3( 0 , 0 , 1 );
}
}
此致
Avanindra