GLSL語言內置的變量詳解

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設置自己的頂點屬性(當然個數是有限制的)函數參見:http://blog.csdn.net/yuanjingjiang/article/details/13511657

一致變量—— 就是常說的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一族的常量值,這些值和內置變量的值是一致的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章