GLSL - Passing coordinates to fragment shader? - Stack ...

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

gl_Position is the clip-space position of a particular vertex. gl_FragCoord.xyz contains the window-space position of the fragment. These are ... 2022DeveloperSurveyisopen!Takesurvey. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore GLSL-Passingcoordinatestofragmentshader? AskQuestion Asked 8years,8monthsago Modified 8years,8monthsago Viewed 20ktimes 9 Ihavethefollowingshaders VertexShader #version330 layout(location=0)invec3inPosition; layout(location=1)invec4inColour; layout(location=2)invec2inTex; layout(location=3)invec3inNorm; uniformmat4transformationMatrix; uniformmat4projectionMatrix; outvec4ShadowCoord; voidmain() { gl_Position=projectionMatrix*transformationMatrix*vec4(inPosition,1.0); ShadowCoord=gl_Position; } FragmentShader #version330 invec4ShadowCoord; outvec4FragColor; uniformsampler2DheightmapTexture; voidmain() { FragColor=vec4(vec3(clamp(ShadowCoord.x/500,0.0,1.0),clamp(gl_FragCoord.y/500,0.0,1.0),0.0),1.0); } WhatIwouldexpectthistodoistodrawthescene,withgreeninthetopleft,yellowinthetopright,redinthebottomrightandblackinthebottomleft.IndeedthisiswhathappenswhenIreplaceShadowCoord.xwithgl_FragCoord.xinmyfragmentshader.Whythen,doIonlygetaverticalgradientofgreen?EvidentlysomethingscrewyishappeningtomyShadowCoord.Anyoneknowwhatitis? openglglslshader Share Improvethisquestion Follow askedSep3,2013at10:35 rspencerrspencer 2,42122goldbadges1818silverbadges2828bronzebadges 2 1 "Anyoneknowwhatitis?"No.BecausethecontentsofShadowCoorddependentirelyonthecontentsoftwomatricesthatwehavenoideawhattheyare.Wedon'tevenknowifyou'reusingaperspectiveororthographicproject. – NicolBolas Sep3,2013at10:49 Ok,butthepointisthatifIreplaceShadowCoordwithgl_FragCoordinthefragshaderthenitworks.Isgl_FragCoordnotderivedfromgl_Position?IfsowhydoesShadowCoordnotundergothesameinterpolation?FYI,itsaperspectiveprojectionofacameratransformation. – rspencer Sep3,2013at10:51 Addacomment  |  1Answer 1 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 15 Yes,gl_FragCoordis"derivedfromgl_Position".Thatdoesnotmeanthatitisequaltoit. gl_Positionistheclip-spacepositionofaparticularvertex.gl_FragCoord.xyzcontainsthewindow-spacepositionofthefragment.Thesearenotthesamespaces.Togetfromonetotheother,thegraphicshardwareperformsanumberoftransformations. Transformationsthatdonothappentoothervalues.That'swhythefragmentshaderdoesn'tcallitgl_Position;becauseit'snotthatvalueanymore.It'sanewvaluecomputedfromtheoriginalbasedonvariousstate(theviewport&depthrange)thattheuserhasset. User-definedvertexparametersareinterpolatedastheyare.Nothing"screwy"ishappeningwithyouruser-definedinterpolatedparameter.Thesystemisdoingexactlywhatitshould:interpolatingthevalueyoupassedacrossthetriangle'ssurface. Share Improvethisanswer Follow answeredSep3,2013at10:59 NicolBolasNicolBolas 412k6161goldbadges709709silverbadges902902bronzebadges 2 Rightthatmakessense.Howwouldonetellthesystemtodothoseprocessesonadifferentvariablethen?Say,eventually,ShadowCoordisgivenbysomeothertransformationmatrix,andIwanttothengetthecoordinatesofits'fragment'.WouldIhavetomanuallyimplementwhatisdescribedinthelink? – rspencer Sep3,2013at11:05 1 "Howwouldonetellthesystemtodothoseprocessesonadifferentvariablethen?"Youdon't;you'llhavetocomputeityourself.It'sjustanothermatrixmultiply;youcanjustfolditintothebackendofthematrixyou'realreadyusing. – NicolBolas Sep3,2013at11:14 Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedopenglglslshaderoraskyourownquestion. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication RetiringOurCommunity-SpecificClosureReasonsforServerFaultandSuperUser Temporarilypausingthesitesatisfactionsurvey StagingGround:ReviewerMotivation,Scaling,andOpenQuestions Related 222 HowtodebugaGLSLshader? 1 CreatingatrianglewithOpenGL&GLSL 7 ProblemsportingaGLSLshadertoyshadertounity 0 Texturemappingresultsinsolidcolorof1pixelfromtexture HotNetworkQuestions Non-committingauthenticatedencryptionschemesvscommittingauthenticatedencryptionschemes ESTAform:Mentionthecitydistrictinthecontactinformationsection? Provingarecursiveformulaisindeedafunction CanWgatebewrittenonlyusingH,T? Whatmakesaluminumaerospacegrade? DifferencebetweenfirewallandACL ArederivativesfrominterpolatingfunctionscreatedbyNDSolveusingderivativesofthesplineornumericsfromsolvingDE WhatwasthereasonforextendingNATOinthe90s? WhatdoesGandalftheWhitemeanbyhisstrangespeechaboutBoromir? Audiosplitterwithoutbufferpossible? WhattapeformatdidtheNintendoFamicomDataRecorderuse? Whatdoyoucalladesperateattemptunlikelytosucceed? Whatdoestheidiomaticphrase"erronthesideof"mean? Doesgravitationreallyexistattheparticlelevel? SignalprocessinginPythonvsC++(Bandpassfilter) Ihaveanarmadaofteleportingskyfortresses.DoIstillneedanavy? Planeticketincludestrainticket(Europe),whatifIdonotshowupattrainstation? Isitdangeroustoconsume30mgofZinc(asZincPicolinate)perday? GameinJamesBondfilm"FromRussiawithlove" Wouldn'tMiller'splanetbefriedbyblueshiftedradiation? Whyaren'tnegativefrequenciesfoldedinreconstructionofthealiasedsignal? OutputofaHubDynamomeasuredwithanOscilloscope Isthisaninsectnest? Meaningofclappingattheendofmass morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-c Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?