What can I do with the 4th component of gl_Position? - Game ...

文章推薦指數: 80 %
投票人數:10人

gl_Position is a Homogeneous coordinates. ... are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, ... 2022DeveloperSurveyisopen!Takesurvey. GameDevelopmentStackExchangeisaquestionandanswersiteforprofessionalandindependentgamedevelopers.Itonlytakesaminutetosignup. Signuptojointhiscommunity Anybodycanaskaquestion Anybodycananswer Thebestanswersarevotedupandrisetothetop Home Public Questions Tags Users Companies Unanswered Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore WhatcanIdowiththe4thcomponentofgl_Position? AskQuestion Asked 4years,4monthsago Modified 4years,4monthsago Viewed 15ktimes 16 6 \$\begingroup\$ WhenIsetgl_PositionIusuallyassignitsuchasgl_Position=vec4(in_position,1.0)wherein_positionasavectorofthreecomponentsrepresentingavertexofmymodel. ButlookinguptutorialsandsuchIcannotfindanythingexplainingwhatthe4thcomponentofthegl_Positionvec4isdoingasidefrommakingthevectorbigenoughsomatrixtransformationscanbeappliedtoit. Q:WhatcanIdowiththe4thcomponentofgl_Position/whatdoesitinfluenceintherenderingprocess? shadersglslvertex-shader Share Improvethisquestion Follow editedJan11,2018at14:35 dot_Sp0T askedJan11,2018at13:01 dot_Sp0Tdot_Sp0T 1,96222goldbadges1313silverbadges2424bronzebadges \$\endgroup\$ 5 1 \$\begingroup\$ JustacommentsinceI'mnotentirelysure.ButifIremembercorrectlyavectorcaneitherbeapositionoradirection.Whenthe4thparameteris1itistreatedaspositionandwhenit's0asdirection.Butofcourseitdependsoncontexthowit'sused.Thisismoreaconvention. \$\endgroup\$ – Skalli Jan11,2018at13:52 1 \$\begingroup\$ gl_Position=vec4(in_position,1.0)Doyouactuallydoitintheshader?Nomatrixtransformations?Doyoureallymeangl_Position=u_matrix*vec4(in_position,1.0);? \$\endgroup\$ – HolyBlackCat Jan11,2018at14:23 \$\begingroup\$ @HolyBlackCatnaturallytherearesomematrixmultiplicationsforviewandprojectiontransformations,butIdidnotseeanypointinbloatingtheexamplewiththem. \$\endgroup\$ – dot_Sp0T Jan11,2018at14:24 1 \$\begingroup\$ @dot_Sp0TIfyouhaveusedaperspectiveprojectionmatrix,afterthatmultiplicationgl_Position.wwillnolongerbeequalto1,andtheperspectivedivisionwillhappen.Notsureifithelpsoryouaskaboutacompletelydifferentthing. \$\endgroup\$ – HolyBlackCat Jan11,2018at14:30 \$\begingroup\$ @HolyBlackCatthatisagoodpoint.Theideabehindthequestionisprobablybestdescribedas'findingoutwhatthatvaluedoesintheend'-Iwillamendthequestion \$\endgroup\$ – dot_Sp0T Jan11,2018at14:34 Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 12 \$\begingroup\$ gl_PositionisaHomogeneouscoordinates.Homogeneouscoordinatesareneededforperspectiveprojection. Note,ifavectorvec4(x,y,z,1.0)ismultipliedbyaperspectiveprojectionmatrix,thisresultsinaHomogeneouscoordinates. Theprojectionmatrixdescribesthemappingfrom3Dpointsofascene,to2Dpointsoftheviewport.Theprojectionmatrixtransformsfromviewspacetotheclipspace.Thecoordinatesintheclipspacearetransformedtothenormalizeddevicecoordinates(NDC)intherange(-1,-1,-1)to(1,1,1)bydividingwiththewcomponentoftheclipcoordinates. AtPerspectiveProjectiontheprojectionmatrixdescribesthemappingfrom3Dpointsintheworldastheyareseenfromofapinholecamera,to2Dpointsoftheviewport.Theeyespacecoordinatesinthecamerafrustum(atruncatedpyramid)aremappedtoacube(thenormalizeddevicecoordinates). Thismeansthepositioninnormalizeddevicespaceiscalculatedlikethis: vec3ndc=gl_Position.xyz/gl_Position.w; Ifyoumanuallysetthewcomponentofgl_Position,thiscausesareciprocalscalingofthepositioninnormalizeddevicespace. Share Improvethisanswer Follow editedJan13,2018at10:50 answeredJan13,2018at9:07 Rabbid76Rabbid76 27111silverbadge99bronzebadges \$\endgroup\$ 4 1 \$\begingroup\$ so,whatwouldmodifyingthewcomponentdoifanythingatall? \$\endgroup\$ – dot_Sp0T Jan13,2018at10:42 \$\begingroup\$ @dot_Sp0TAreciprocalscalingofthenormalizeddevicecoordinates.Seetheanswer. \$\endgroup\$ – Rabbid76 Jan13,2018at10:51 4 \$\begingroup\$ sweet,nowIonlyneedtofindoutwhat'reciprocal'means.+1 \$\endgroup\$ – dot_Sp0T Jan13,2018at10:53 2 \$\begingroup\$ @dot_Sp0TInmathreciprocalmeans*1.0/xincompareto*x.Thereciprocalofxis1.0/x. \$\endgroup\$ – Rabbid76 Jan13,2018at10:55 Addacomment  |  11 \$\begingroup\$ It'snotparticularlyusefulforyoubutyoushoulddefinitelyleaveitat1.It'susedwhenapplyingtransformationstothemodel. Matrixmultiplicationcanonlymultiplynumbers,itcan'tadd.Thatmakesmovingobjectsimpossibleunlessthe4thcomponentexists.Let'slookatanexample:Ifwewanttomovethevector $$\vecv=\begin{bmatrix}a\\b\\c\end{bmatrix}$$ by $$\vecd=\begin{bmatrix}x\\y\\z\end{bmatrix}$$ Thenweusethe $$T=\begin{bmatrix} 1&0&0&x\\ 0&1&0&y\\ 0&0&1&z\\ 0&0&0&1 \end{bmatrix}$$ matrixandgive\$\vecv\$afourthcomponent.Whenwemultiplythemtogether,thentheresultisgoingtobe $$ \vecr=T\space\cdot\space\vecv=\begin{bmatrix} 1\cdota+0\cdotb+0\cdotc+x\cdot1\\ 0\cdota+1\cdotb+0\cdotc+y\cdot1\\ 0\cdota+0\cdotb+1\cdotc+z\cdot1\\ 0\cdota+0\cdotb+0\cdotc+1\cdot1\\ \end{bmatrix}= \begin{bmatrix} a+x\\ b+y\\ c+z\\ 1 \end{bmatrix} $$ Theseconduseforthe4thcomponentisperspectivedivision.Aprojectionmatrixbasicallynormalizesandcopiesthezcomponenttothew.OpenGLthenautomaticallydividesthex,yandzcomponentsbyit. Share Improvethisanswer Follow editedJan13,2018at14:27 answeredJan11,2018at14:04 BálintBálint 14.5k22goldbadges3030silverbadges5353bronzebadges \$\endgroup\$ 1 \$\begingroup\$ A(simplified)real-worldexampleofuseofthisknowledgecanbeseeninmyanswerhere,whereIconvertclipcoordinatesdowntoscreencoordinates,roundthemandconvertback. \$\endgroup\$ – Ruslan Jan1,2020at14:46 Addacomment  |  Youmustlogintoanswerthisquestion. Nottheansweryou'relookingfor?Browseotherquestionstaggedshadersglslvertex-shader. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication Related 0 FormulaforreplicatingglTexGeninOpenGLES2.0GLSL 10 Whatdothecomponentsofgl_TexCoord[0]mean? 1 Lightfollowingmearoundtheroom.Somethingiswrongwithmyshader! 1 OpenGLVBOAdditionalAttributes 2 DistortaquadwithaWorldMatrix 8 Rotatebillboardtowardscamera 2 Shaderonlyrendering1/4thofthescreen 4 GLSL"varying"interpolation:component-wise?Referenceinthespec? 2 Howtorotatedirectionalbillboardparticlespritestowardthedirectiontheparticleismoving HotNetworkQuestions GapsincardinalitiesofMADfamilies Partitioningalistbasedonacriterionforsublists WhyisMIDIgainbasedonafactorof40? SignalprocessinginPythonvsC++(Bandpassfilter) Longestgeometricprogressionofprimes Wouldn'tMiller'splanetbefriedbyblueshiftedradiation? Whatdisadvantagesaretheretoencryptinganentireharddriveorahomedirectory? Can'tmultiplywidthin\newcommandforincludegraphics HowdoIprepareaslidetalkpresentationforconferenceinmathematics? GN:Howtopassaninstancer'slocalizedtexturecolortoitsinstances? Wouldn'tMiller'splanetbefriedbyblueshiftedradiation? Can"LaCorrida"mean"TheBullfight"? MostcommoncommandtocompileaLaTeXdocument? Doesgravitationreallyexistattheparticlelevel? HowtopreventthewhiteflashwhenItrytogobeyondtheend/beginningofline? 'Mapping'thevaluesofalisttovariable Alphaparticlemovingfasterthanthespeedoflight? Canarunascendinglistbemade? OutputofaHubDynamomeasuredwithanOscilloscope HowdoesHebrews11:27sayMoseswasnotafraid? CanWgatebewrittenonlyusingH,T? WhywouldIuseatrainerinsteadofridingoutside? ShouldIrevealmyslidesbit-by-bitwhengivingapresentation/lecture? Googlespreadsheets morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. default Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?