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;          // 頂點霧座標

用法示例:

//OpenGL將光源的方向保存在視點空間座標系內,因此需要把法線也變換到視點空間。
vec4 normal = gl_NormalMatrix * gl_Normal;
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;   //求取頂點變換到相機空間的位置
vec2 Texcoord = gl_MultiTexCoord0.xy;     //用於向片元着色器傳遞紋理座標
uniform sampler2D decal;                  //片元着色器中的紋理採樣器
vec4 color = texture2D(decal, Texcoord);  //獲取紋理顏色

一致變量

內置的一致變量的值最終是通過OpenGL的函數傳遞過去的。當用戶沒有使用着色器(即可編程管線)編寫程序的時候,程序走固定管線完成繪製,但,一旦用戶使用了着色器編寫GLSL程序,就必須將固定管線的那部分功能用GLSL程序模仿出來!!!
e.g. 當使用着色器實現其他功能的時候,燈光效果、霧效等也都必須有相應的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;   //示例:normal = noraml * gl_NormalScale;

/*窗口座標中深度範圍*/
struct gl_DepthRangeParameters
{
    float near;  //近剪切平面
    float far;   //遠剪切平面
    float diff;  //進剪切平面到遠剪切平面的距離差
};
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;

/*材質,對應於OSG中的Material類,下述參數由該類的函數傳入*/
struct gl_MaterialParameters
{
    vec4 emission;      //自身發出的光
    vec4 ambient;       //對環境光的反射能力
    vec4 diffuse;       //對散射光的反射能力
    vec4 specular;      //對鏡面光的反射能力
    float shininess;    //鏡面指數,示例:float spec = pow(max(dot(eyeVec, reflectVec), 0.0), shininess); 
                        //鏡面指數越大,鏡面反射光越強,散射程度越小,高光點就越集中。
};
uniform gl_MaterialParameters gl_FrontMaterial;       // 正面材質
uniform gl_MaterialParameters gl_BackMaterial;        // 反面材質

/*光源性質,對應於OSG中的Light類,下述參數由該類的函數傳入*/
struct gl_LightSourceParameters
{
    vec4 ambient;                //環境光的顏色
    vec4 diffuse;                //散射光的顏色,反映了在場景中光源對RGBA各成分的散射能力
    vec4 specular;               //鏡面反射光的顏色,它直接影響着材質上高光的顏色,通常爲白色或灰色
    vec4 position;               //光源在場景中的放置位置
    vec4 halfVector;             //半角向量,示例:halfVector = normalize(單位入射向量 + 單位觀察者向量);
    vec3 spotDirection;          //聚光燈方向,表示圓錐體的軸
    float spotExponent;          //聚光指數,表示從圓錐的中心軸向外表面變化時光強度的衰減
    float spotCutoff;            //聚光燈的切角,取值範圍爲[0.0, 90.0]和180,光錐的角度爲爲此角的2倍
    float spotCosCutoff;         //爲上個量的cos值,取值範圍爲[1.0, 0.0]和-1.0
    float constantAttenuation;   //常量衰減因子
    float linearAttenuation;     //線性衰減因子
    float quadraticAttenuation;  //二次衰減因子
    //示例:attenuation = 1.0 / (gl_LightSource[0].constantAttenuation + 
    //gl_LightSource[0].linearAttenuation * d + gl_LightSource[0].quadraticAttenuation * d * d);
};
uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

struct gl_LightModelParameters
{
    vec4 ambient;    //整個場景的環境光的RGBA強度
};
uniform gl_LightModelParameters gl_LightModel;

/*光照和材質的派生狀態*/
struct gl_LightModelProducts
{
    vec4 sceneColor; //等於gl_FrontMaterial.emission + gl_FrontMaterial.ambient * gl_LightModel.ambient;
};
uniform gl_LightModelProducts gl_FrontLightModelProduct; 
uniform gl_LightModelProducts gl_BackLightModelProduct;

struct gl_LightProducts
{
    vec4 ambient;    //等於gl_FrontMaterial.ambient  * gl_LightSource[0].ambient
    vec4 diffuse;    //等於gl_FrontMaterial.diffuse  * gl_LightSource[0].diffuse
    vec4 specular;   //等於gl_FrontMaterial.specular * gl_LightSource[0].specular
};
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];

/*霧參數,對應於OSG的Fog類,下述參數由該類的函數傳入*/
struct gl_FogParameters
{
    vec4  color;
    float density;
    float start;
    float end;
    float scale;  // 1/(end-start)
};
uniform gl_FogParameters gl_Fog;

易變變量

varying vec4 gl_Color;
varying vec4 gl_SecondaryColor;
varying vec4 gl_TexCoord[gl_MaxTextureCoords];
varying float gl_FogFragCoord;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章