12.5 - GLSL Built-in Functions and Variables — LearnWebGL
文章推薦指數: 80 %
gl_Position : a vec4 position of a vertex in “clip coordinates”. ... The values for x and y are in the range [-1.0,+1.0] and represent the location of a ... LearnWebGL TableofContents BookIndex 12.5-GLSLBuilt-inFunctionsandVariables¶ Built-inFunctions¶ GLSLprovidesasignificantnumberofbuilt-infunctionsandyoushould befamiliarwiththem.Acompletelistofthesefunctionscanbefoundon pagefourofthisQuickReferenceCard.Pleasenotethefollowingaboutthefunctions. Themajorityofthefunctionsonlyworkonfloatingpointscalarsorvectors.They donotworkonintegersorbooleans.Theexceptionisagroupof“Vector RelationalFunctions”thatacceptBooleanandintegerinputsandreturnBooleanvalues. Thefunctionsareoverloadedtoacceptvariousinputsandreturnaresult ofthesamedatatype.Forexample,considerthefunctionprototypeTsin(Tangle), whereTrepresentsthedatatypesfloat,vec2,vec3,or vec4.Thisrepresentsfourversionsofthesinfunction. Eachversionperformsacomponent-wisesinecalculationandreturnsa resultthatisthesamesizeasitsinput. floatfunctionsin(floatangle); vec2functionsin(vec2angles); vec3functionsin(vec3angles); vec4functionsin(vec4angles); Pleasereviewthelistofbuilt-infunctionsonpagefouroftheQuickReferenceCard. Built-inVariables¶ Shaderprogramscommunicatewiththegraphicspipelineusingpre-definedinput andoutputvariables. AVertexShader‘sOutputs¶ Avertexshaderhastwooutputs: gl_Position:avec4positionofavertexin“clipcoordinates”. Clipcoordinatesweredescribedindetailinsection8. Thevaluesforxandyareintherange[-1.0,+1.0]andrepresentthe locationofavertexintheviewingwindow. Thezvalueisintherange[-1.0,+1.0]andrepresents thedistanceofthevertexfromthecamera. Thewvalueis1.0fororthogonalprojections,orthewvalueisthe perspectivedividevalueforperspectiveprojections. Anyvertexoutsidetheclippingcubeisclippedtothecube’sboundaries. gl_PointSize:thenumberofpixelstousetorenderapoint. Itisafloatvaluethatcanhaveafractionalpart.Itonly appliestotherenderingofsinglepoints,nottotheverticesoflinesand triangles.Ifnovalueisspecified,itsdefaultvalueit1.0. Theoutputsofavertexshaderareusedbythegraphicspipelinetodetermine thepixels(i.e.,thefragments)intheviewingwindowthatcomposeagraphicsprimitive. Therasterizerinthegraphicspipelinecreatesafragmentforeachpixel, calculatestheinterpolatedvaluesforanyvaryingvariables,and setsthevaluesforthefollowingfragmentshaderinputvalues. InputstoaFragmentShader¶ gl_FragCoord:avec4valuethatholdsthe(x,y,z,w)value ofthefragment.Thisisthevalueofgl_Positionafterithas beentransformedbytheviewporttransformandtheperspectivedividehas beenperformed.Therefore,the(x,y)valuesarethelocationofthefragment intheimagetoberendered.Thezvalueisthedistancefromthecamera. Notethatthesearefloatingpointvaluesandthatthe(x,y)valuesarethe centerofapixel.Forexample,thebottom-leftcornerpixelhasan(x,y) valueof(0.5,0.5). gl_FrontFacing:aBooleanvaluethatistrueifthisfragmentis partofafront-facingprimitive.Thisonlyappliestotriangles.Atriangle is“frontfacing”ifitsnormalvectorispointingtowardthecamera.The normalvectoriscalculatedfromthetriangle’svertices,anda counter-clockwiseorderingoftheverticeswillproducea“front-facing” normalvector. gl_PointCoord:isavec2valuewhereits(x,y)values areintherange[0.0,1.0]. Thevaluesindicatetherelativelocationofthisfragmentwithinthe renderingofapoint.Thepoint’stotalsize,inpixels,comesfromthe gl_PointSizeoutputvariable.Theoriginofthecoordinatesystem forapointrenderingistheupperleftcornerofthesquarethatcovers thepoint.(Thisonlyappliestorenderedpoints,gl.POINTS. Itisundefinedwhenrenderinggl.LINESandgl.TRIANGLES.) OutputsfromaFragmentShader¶ AWebGLfragmentshaderhasoneoutput–acolorvalueforitsfragment. gl_FragColor:aRGBA(vec4)valuethatisplacedintothe colorbufferforthefragmentitisprocessing. OpenGLsupportsrenderingtomultiplecolorbuffersforthecreationof advancedvisualeffects.Writingcolorstogl_FragData[n]allows afragmentshadertocreatemultipleimagesthatcanbe“composited”into asinglefinalrendering.WebGL1.0doesnotsupportmultiplecolorbuffers withoutusingextensions,sowewillnotdiscussthemhere. NextSection-12.6-GLSLCompilingandLinking
延伸文章資訊
- 1gl_Position - GLSL ES 3 - docs.gl
The variable gl_Position is intended for writing the homogeneous vertex position. It can be writt...
- 2Intro to GLSL - Cornell CS
- 3OpenGL gl_Position z-range and depth buffer range - Game ...
Yes, gl_Position.z should be between [-1, 1] after dividing gl_Position.w , otherwise will be cli...
- 4Advanced GLSL - LearnOpenGL
Setting gl_Position in the vertex shader is a strict requirement if you want ... position's z val...
- 5GLSL - Using custom output attribute instead of gl_Position