日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GLSL学习笔记

發(fā)布時間:2023/12/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GLSL学习笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
GLSL語言內(nèi)置的變量,包括內(nèi)置的頂點(diǎn)屬性(attribute)、一致變量(uniform)、易變變量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以為以后的編程做查詢之用。頂點(diǎn)屬性——指頂點(diǎn)的信息,OpenGL據(jù)此繪制各個圖元,對于傳統(tǒng)的頂點(diǎn)屬性包括坐標(biāo)、紋理坐標(biāo)、顏色等GLSL都會設(shè)置一個內(nèi)置變量與之對應(yīng),以便在需要時可以在頂點(diǎn)或片元著色器中直接引用,這也體現(xiàn)了GLSL與HLSL的一個最大的不同,HLSL里頂點(diǎn)的屬性是通過語義來定義的,而GLSL充分考慮了OpenGL是個狀態(tài)機(jī)這一事實(shí),將頂點(diǎn)屬性設(shè)為一個狀態(tài)變量。GLSL中內(nèi)置的頂點(diǎn)屬性包括以下幾個:// 頂點(diǎn)屬性attribute?vec4 gl_Color;??????????????// 頂點(diǎn)顏色attribute?vec4 gl_SecondaryColor;?????// 輔助頂點(diǎn)顏色attribute?vec3 gl_Normal;?????????????// 頂點(diǎn)法線attribute?vec4 gl_Vertex;?????????????// 頂點(diǎn)物體空間坐標(biāo)(未變換)attribute?vec4 gl_MultiTexCoord[0-N];?// 頂點(diǎn)紋理坐標(biāo)(N = gl_MaxTextureCoords)attribute?float gl_FogCoord;??????????// 頂點(diǎn)霧坐標(biāo)值得一提的是用戶可以調(diào)用glVertexAttrib設(shè)置自己的頂點(diǎn)屬性(當(dāng)然個數(shù)是有限制的)一致變量——就是常說的Uniform,這是用戶向GLSL傳遞自己數(shù)據(jù)的最常用方法,比如光源位置等等。之所以稱為一致變量,是為了與易變變量相區(qū)別。不同于頂點(diǎn)屬性在每個頂點(diǎn)有其自己的值,也不同于易變變量由頂點(diǎn)程序向片元程序插值傳遞,一致變量在一個圖元的繪制過程中是不會改變的,而且可以在頂點(diǎn)shader和片元shader間共享。這部分變量主要用來描述OpenGL的狀態(tài),可以看作OpenGL狀態(tài)機(jī)的復(fù)制。GLSL內(nèi)置的一致變量包括:// 矩陣狀態(tài)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;// 窗口坐標(biāo)深度范圍struct gl_DepthRangeParameters{??? float near;???? float far;??? float diff;?// far-near};uniform?gl_DepthRangeParameters gl_DepthRange;// 裁剪平面uniform?vec4 gl_ClipPlane[gl_MaxClipPlanes];// 點(diǎn)屬性struct gl_PointParameters{??? float size;???? float sizeMin;??? float sizeMax;??? float fadeThresholdSize;???? float distanceConstantAttenuation;??? float distanceLinearAttenuation;??? float distanceQuadraticAttenuation;};uniform?gl_PointParameters gl_Point;// 材質(zhì)struct gl_MaterialParameters{??? vec4 emission;???????// 自身光照Ecm??? vec4 ambient;????????// 環(huán)境光吸收系數(shù)Acm??? vec4 diffuse;????????// 漫反射吸收系數(shù)Dcm??? vec4 specular;???????// 鏡面反射吸收系數(shù)Scm??? float shininess;?????// Srm};uniform?gl_MaterialParameters gl_FrontMaterial;???????// 正面材質(zhì)uniform?gl_MaterialParameters gl_BackMaterial;????????// 反面材質(zhì)// 光源性質(zhì),參數(shù)性質(zhì)就不解釋了,和OpenGL的三種光源性質(zhì)是一樣的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;// 光照和材質(zhì)的派生狀態(tài)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];// 紋理環(huán)境和生成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];// 霧參數(shù)struct gl_FogParameters{??? vec4 color;??? float density;??? float start;??? float end;??? float scale;?// 1/(end-start)};uniform?gl_FogParameters gl_Fog;易變變量——易變變量只能在頂點(diǎn)shader和片元shader間傳遞,這期間實(shí)際上經(jīng)過了一個光柵化的過程。內(nèi)置的易變變量比較少,如下:varying?vec4 gl_Color;varying?vec4 gl_SecondaryColor;varying?vec4 gl_TexCoord[gl_MaxTextureCoords];varying?float gl_FogFragCoord;熟悉圖形管線的話可以自己描繪出這些易變變量是如何在頂點(diǎn)和片元程序間進(jìn)行傳遞的。內(nèi)置常量——內(nèi)置常量描述了顯卡的渲染能力,依據(jù)各個顯卡而定,這里就不一一列舉了,如果想要查詢的話可以用OpenGL的glGet函數(shù)獲取MAX/MIN一族的常量值,這些值和內(nèi)置變量的值是一致的。1.texture2D//texture2D對紋理進(jìn)行采樣(得到一個紋素(texel),這是一個紋理圖片中的像素。函數(shù)參數(shù)分別為simpler2D以及紋理坐標(biāo))? ? ?gl_FragColor = texture2D(inputImageTexture, textureCoordinate);2.dot? 它實(shí)質(zhì)是計算光的強(qiáng)度,由于dot實(shí)質(zhì)是計算兩向量的夾角余弦,所以當(dāng)角度〉90度時為負(fù)值。0-90度時為正值3.pow(x,y)? ?計算x的y次方。兩個值都為float。

轉(zhuǎn)載于:https://blog.51cto.com/2119784/962118

總結(jié)

以上是生活随笔為你收集整理的GLSL学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。