openGL之API学习(一六七)默认着色器 顶点属性索引 别名索引

Posted hankern

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了openGL之API学习(一六七)默认着色器 顶点属性索引 别名索引相关的知识,希望对你有一定的参考价值。

opengl在调用glDrawElements或glDrawArrays进行绘制时,如果没有指定用户自定义的顶点、片段等着色器,opengl会为用户设置一个默认的着色器,类似于

顶点着色器
#version 330

void main()
{
    
}
片段着色器
#version 330

void main()
{
    
}

在顶点着色器中默认使用gl_Vertex绘制顶点。

在片段着色器中默认使用gl_Color绘制颜色。

gl_Vertex、gl_Color一般通过glVertexAttribPointer、glVertexAttrib*等传递数据。

gl_Vertex在着色器中的默认索引值为0,对应于layout(location=0)。

gl_Color的默认索引值为2。

此外还有:

gl_Normal的默认索引值为1。

顶点属性索引值的数量和GPU硬件有关系,所有 OpenGL ES 3.0 的实现必须至少支持16个vertex attribute,实际支持的个数可以通过GL_MAX_VERTEX_ATTRIBS查询
 

GLint maxVertexAttribs; // n will be >= 16
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);

osg中是这样设置顶点属性索引默认值的

osg/State.cpp
void State::resetVertexAttributeAlias(bool compactAliasing, unsigned int numTextureUnits)
{
    _texCoordAliasList.clear();
    _attributeBindingList.clear();

    if (compactAliasing)
    {
        unsigned int slot = 0;
        setUpVertexAttribAlias(_vertexAlias, slot++, "gl_Vertex","osg_Vertex","vec4 ");
        setUpVertexAttribAlias(_normalAlias, slot++, "gl_Normal","osg_Normal","vec3 ");
        setUpVertexAttribAlias(_colorAlias, slot++, "gl_Color","osg_Color","vec4 ");

        _texCoordAliasList.resize(numTextureUnits);
        for(unsigned int i=0; i<_texCoordAliasList.size(); i++)
        {
            std::stringstream gl_MultiTexCoord;
            std::stringstream osg_MultiTexCoord;
            gl_MultiTexCoord<<"gl_MultiTexCoord"<<i;
            osg_MultiTexCoord<<"osg_MultiTexCoord"<<i;

            setUpVertexAttribAlias(_texCoordAliasList[i], slot++, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "vec4 ");
        }

        setUpVertexAttribAlias(_secondaryColorAlias, slot++, "gl_SecondaryColor","osg_SecondaryColor","vec4 ");
        setUpVertexAttribAlias(_fogCoordAlias, slot++, "gl_FogCoord","osg_FogCoord","float ");

    }
    else
    {
        setUpVertexAttribAlias(_vertexAlias,0, "gl_Vertex","osg_Vertex","vec4 ");
        setUpVertexAttribAlias(_normalAlias, 2, "gl_Normal","osg_Normal","vec3 ");
        setUpVertexAttribAlias(_colorAlias, 3, "gl_Color","osg_Color","vec4 ");
        setUpVertexAttribAlias(_secondaryColorAlias, 4, "gl_SecondaryColor","osg_SecondaryColor","vec4 ");
        setUpVertexAttribAlias(_fogCoordAlias, 5, "gl_FogCoord","osg_FogCoord","float ");

        unsigned int base = 8;
        _texCoordAliasList.resize(numTextureUnits);
        for(unsigned int i=0; i<_texCoordAliasList.size(); i++)
        {
            std::stringstream gl_MultiTexCoord;
            std::stringstream osg_MultiTexCoord;
            gl_MultiTexCoord<<"gl_MultiTexCoord"<<i;
            osg_MultiTexCoord<<"osg_MultiTexCoord"<<i;

            setUpVertexAttribAlias(_texCoordAliasList[i], base+i, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "vec4 ");
        }
    }
}

以上是关于openGL之API学习(一六七)默认着色器 顶点属性索引 别名索引的主要内容,如果未能解决你的问题,请参考以下文章

openGL之API学习(一六九)glVertexPointer

openGL之API学习(一九八)默认着色器

openGL之API学习(一八九)gl_Position gl_Vertex

OpenGL学习随笔-- 顶点着色器(VertexShader)

openGL之API学习(一六八)gl_SecondaryColor

python下学习opengl之着色器