1

我将 suzanne 模型从 blender(Monkey head)导出为 .obj 文件,只有在片段着色器中使用 RGB 值时才能看到它。例如frag_color = vec4( 1.0, 0.0, 0.0, 1.0 );使模型变红。但除非我旋转它,否则它看起来就像一个变形的纹理

正视图

我想将法线用作颜色,以便我可以看到面部的特定细节等。我将法线绑定到顶点位置 1。

    if ( mesh -> HasNormals() )
    {

        normals = ( GLfloat * ) malloc( * pointCount * 3 * sizeof( GLfloat ) );

        for ( int i = 0; i < * pointCount; i++ )
        {

            const aiVector3D * vn = &( mesh -> mNormals[ i ] );

            normals[ i * 3 ] = ( GLfloat ) vn -> x;
            normals[ i * 3 + 1 ] = ( GLfloat ) vn -> y;
            normals[ i * 3 + 2 ] = ( GLfloat ) vn -> z;

        }

        GLuint vbo;

        glGenBuffers( 1, &vbo );
        glBindBuffer( GL_ARRAY_BUFFER, vbo );
        glBufferData( GL_ARRAY_BUFFER, 3 * * pointCount * sizeof( GLfloat ), normals, GL_STATIC_DRAW );
        glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, NULL );
        glEnableVertexAttribArray( 1 );

        free( normals );

    }

而且我在附加着色器之后但在链接之前就绑定1了。vertex_normal

glAttachShader( program, vertShader );
glAttachShader( program, fragShader );

glBindAttribLocation( program, 0, "vertex_position" );
glBindAttribLocation( program, 1, "vertex_normal" );

glLinkProgram( program );

这些是我的着色器

vertshader.shader

#version 330

in vec3 vertex_position;
in vec3 vertex_normal;

uniform mat4 proj, view, model;

out vec3 normals;

void main()
{

    normals = vertex_normal;

    gl_Position = proj * vec4( vec3( view * model * vec4( vertex_position, 1.0 ) ), 1.0 );

}

fragshader.shader

#version 330

in vec3 normals;

out vec4 fragment_color;

void main()
{       

    fragment_color = vec4( normals, 1.0 );

}

但这只会输出黑屏。我知道模型正在加载,因为我可以像上面那样将它涂成红色。我尝试vertex_normal直接导入到碎片着色器中,但没有用,我也尝试过规范化normals,但这也没有改变效果。

那么如何在片段着色器中使用模型法线作为颜色呢?

4

1 回答 1

0

好的,我找到了解决方法。显然这是搅拌机的错。我想用我的网格导出的内容有一个侧面板,Write normals但没有被选中。感谢 Reto Koradi,我认为没有法线就不可能编写网格。

在此处输入图像描述

于 2014-06-17T05:20:56.080 回答