GLSL语言内置的变量,包括内置的顶点属性(attribute)、一致变量(uniform)、易变变量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以为以后的编程做查询之用。 顶点属性—— 指顶点的信息,OpenGL据此绘制各个图元,对于传统的顶点属性包括坐标、纹理坐标、颜色等GLSL都会设置一个内置变量与之对应,以便在需要时可以在 顶点或片元着色器中直接引用,这也体现了GLSL与HLSL的一个最大的不同,HLSL里顶点的属性是通过语义来定义的,而GLSL充分考虑了 OpenGL是个状态机这一事实,将顶点属性设为一个状态变量。GLSL中内置的顶点属性包括以下几个: // 顶点属性 attribute vec4 gl_Color; // 顶点颜色 attribute vec4 gl_SecondaryColor; // 辅助顶点颜色 attribute vec3 gl_Normal; // 顶点法线 attribute vec4 gl_Vertex; // 顶点物体空间坐标(未变换) attribute vec4 gl_MultiTexCoord[0-N]; // 顶点纹理坐标(N = gl_MaxTextureCoords) attribute float gl_FogCoord; // 顶点雾坐标 值得一提的是用户可以调用glVertexAttrib设置自己的顶点属性(当然个数是有限制的) 一致变量—— 就是常说的Uniform,这是用户向GLSL传递自己数据的最常用方法,比如光源位置等等。之所以称为一致变量,是为了与易变变量相区别。不同于顶点属 性在每个顶点有其自己的值,也不同于易变变量由顶点程序向片元程序插值传递,一致变量在一个图元的绘制过程中是不会改变的,而且可以在顶点shader和 片元shader间共享。这部分变量主要用来描述OpenGL的状态,可以看作OpenGL状态机的复制。GLSL内置的一致变量包括: // 矩阵状态 uniform mat4 gl_ModelViewMatrix; // 模型视图变换矩阵 uniform mat4 gl_ProjectMatrix; // 投影矩阵 uniform mat4 gl_ModelViewProjectMatrix; // 模型视图投影变换矩阵(ftransform()) uniform mat3 gl_NormalMatrix; // 法向量变换到视空间矩阵 uniform mat4 gl_TextureMatrix[gl_MatTextureCoords]; // 各纹理变换矩阵
// 普通缩放因子 uniform float gl_NormalScale;
// 窗口坐标深度范围 struct gl_DepthRangeParameters { float near; float far; float diff; // far-near }; uniform gl_DepthRangeParameters gl_DepthRange;
// 裁剪平面 uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];
// 点属性 struct gl_PointParameters { float size; float sizeMin; float sizeMax; float fadeThresholdSize; float distanceConstantAttenuation; float distanceLinearAttenuation; float distanceQuadraticAttenuation; }; uniform gl_PointParameters gl_Point;
// 材质 struct gl_MaterialParameters { vec4 emission; // 自身光照Ecm vec4 ambient; // 环境光吸收系数Acm vec4 diffuse; // 漫反射吸收系数Dcm vec4 specular; // 镜面反射吸收系数Scm float shininess; // Srm }; uniform gl_MaterialParameters gl_FrontMaterial; // 正面材质 uniform gl_MaterialParameters gl_BackMaterial; // 反面材质
// 光源性质,参数性质就不解释了,和OpenGL的三种光源性质是一样的 struct gl_LightSourceParameters { vec4 ambient; // Acii vec4 diffuse; // Dcii vec4 specular; // Scii vec4 position; // Ppii vec4 halfVector; // Hi vec3 spotDirection; // Sdli float spotExponent; // Srli float spotCutoff; // Crli float spotCosCutoff; // cos(Crli) float constantAttenuation; // K0 float linearAttenuation; // K1 float quadraticAttenuation; // K2 }; uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights]; struct gl_LightModelParameters { vec4 ambient; // Acs }; uniform gl_LightModelParameters gl_LightModel;
// 光照和材质的派生状态 struct gl_LightModelProducts { vec4 sceneColor; // Ecm+Acm*Acs }; uniform gl_LightModelProducts gl_FrontLightModelProduct; uniform gl_LightModelProducts gl_BackLightModelProduct; struct gl_LightProducts { vec4 ambient; // Acm * Acli vec4 diffuse; // Dcm * Dcli vec4 specular; // Scm * Scli }; uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights]; uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];
// 纹理环境和生成 unifrom vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits]; unifrom vec4 gl_EyePlaneS[gl_MaxTextureCoords]; unifrom vec4 gl_EyePlaneT[gl_MaxTextureCoords]; unifrom vec4 gl_EyePlaneR[gl_MaxTextureCoords]; unifrom vec4 gl_EyePlaneQ[gl_MaxTextureCoords]; unifrom vec4 gl_ObjectPlaneS[gl_MaxTextureCoords]; unifrom vec4 gl_ObjectPlaneT[gl_MaxTextureCoords]; unifrom vec4 gl_ObjectPlaneR[gl_MaxTextureCoords]; unifrom vec4 gl_ObjectPlaneQ[gl_MaxTextureCoords];
// 雾参数 struct gl_FogParameters { vec4 color; float density; float start; float end; float scale; // 1/(end-start) }; uniform gl_FogParameters gl_Fog;
易变变量——易变变量只能在顶点shader和片元shader间传递,这期间实际上经过了一个光栅化的过程。内置的易变变量比较少,如下: varying vec4 gl_Color; varying vec4 gl_SecondaryColor; varying vec4 gl_TexCoord[gl_MaxTextureCoords]; varying float gl_FogFragCoord; 熟悉图形管线的话可以自己描绘出这些易变变量是如何在顶点和片元程序间进行传递的。
内置常量——内置常量描述了显卡的渲染能力,依据各个显卡而定,这里就不一一列举了,如果想要查询的话可以用OpenGL的glGet函数获取MAX/MIN一族的常量值,这些值和内置变量的值是一致的。 |
|
来自: 昵称12278894 > 《OSG》