Convert gl_FragCoord range to fragment range - Game ...
文章推薦指數: 80 %
I'm playing with ranges and division based on resolution and fragment size/position, but couldn't figure it out. The second image was compiled ... 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 Convertgl_FragCoordrangetofragmentrange AskQuestion Asked 4years,5monthsago Modified 4years,5monthsago Viewed 2ktimes 0 \$\begingroup\$ I'mworkingwithshaderssofar,butI'mnotsurehowIcanmakemyshadergl_FragCoordrelativetothewindow.Tobemoreclear,thisiswhatIhave: andthisiswhatI'mlookingtoachievenomatterwhereIplacethefragmentonthescreen: Asyoucansee,theshadertakethewholewindowastherefferencecanvasinstedofthefragment.I'mplayingwithrangesanddivisionbasedonresolutionandfragmentsize/position,butcouldn'tfigureitout. Thesecondimagewascompiledbasedonhardcodeddata,needtofindageneralsolution.. EDIT:Thisismyvert&fragglslcode: #version330core layout(location=0)invec2position; voidmain(){ vec2fp=vec2(position.x,position.y); gl_Position=vec4(position.x,position.y,0.0,1.0); }; $ #version330core layout(location=0)outvec4color; voidmain(){ vec2pos=gl_FragCoord.xy/vec2(800.,600.); floatr=0.4/2.0; floatc=smoothstep(r,r-0.01,distance(pos,vec2(0.5))); color=vec4(vec3(c)*vec3(0.0,0.5,1.0),1.0); }; mathematicsglsl Share Improvethisquestion Follow askedDec21,2017at14:59 alex1997alex1997 12066bronzebadges \$\endgroup\$ Addacomment | 2Answers 2 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 0 \$\begingroup\$ Yourresolutionisundoubtedlynotactually800by600,asthefollowingshadertoyprogramgivestheoutputyoudesire,withtheonlychangebeingtheproperresolutionisusedtodivide.Youalsodon'tneedtopassincoordinatesatall,lookatthisexplanationofgl_FragCoord voidmainImage(outvec4fragColor,invec2fragCoord) { vec2pos=gl_FragCoord.xy/iResolution.xy; floatr=0.4/2.0; floatc=smoothstep(r,r-0.01,distance(pos,vec2(0.5))); fragColor=vec4(vec3(c)*vec3(0.0,0.5,1.0),1.0); } Testitouthere EDIT: Youappeartobehintingthatyouareactuallyusing400x300resolution,ifthatisthecase,myansweris100%correct,tofixyourproblemdivideby400and300insteadof800and600! soinsteadof vec2pos=gl_FragCoord.xy/vec2(800.,600.); dothis vec2pos=gl_FragCoord.xy/vec2(400.,300.); Orifyourresolutionisdifferentdothiswhereresolution_xandresolution_yarethevaluesforyouractualresolution... vec2pos=gl_FragCoord.xy/vec2([resolution_x].,[resolution_y].); Ifyouwantthistoworkwithallresolutionsyouwillstillneedtopasstheresolutiontotheshader. Irecommendchangingyourshadertothisandpassingaresolutionuniformin. #version330core layout(location=0)outvec4color; uniformvec2screenResolution voidmain(){ vec2pos=gl_FragCoord.xy/screenResolution.xy; floatr=0.4/2.0; floatc=smoothstep(r,r-0.01,distance(pos,vec2(0.5))); color=vec4(vec3(c)*vec3(0.0,0.5,1.0),1.0); }; Share Improvethisanswer Follow editedDec21,2017at16:36 answeredDec21,2017at16:10 KrupipKrupip 1,67399silverbadges2020bronzebadges \$\endgroup\$ 13 \$\begingroup\$ Shadertoytakesthewholewindowastherefference,notafragmentobeject.800x600isthewindowresolution,theobjectis400x300,inthiscase.. \$\endgroup\$ – alex1997 Dec21,2017at16:17 \$\begingroup\$ @alex1997whatareyoutalkingabout?Thisiscomputedperfragment,I'mnotsurewhatyouaretryingtosay.Clearlyyourresolutioniswrong,shadertoyusesactualglsltodisplay.Yesyoudon'thaveaccesstotheuniformiResolution,howeveraslongasyouactuallyhavethecorrectresolutionyourshaderwillwork.clearlywhateveryouaredividingyourfragcoordsbyisn'tgoingtowork. \$\endgroup\$ – Krupip Dec21,2017at16:20 \$\begingroup\$ NowIseewhatyoudidthere,youdividedrwith2whichmeansmultiplicationoffragmenWidth/windowWidth(thisiswhatIdidtogetthecircleinthefragment),butifIwanttorenderacircleinarectaglee.q400x300atposition(30,200),itwon'tworkatallsincetheoffestshouldbetakkeninconsiderationuntillthere'ssomethingbetterlikegl_TextCoord[].xy..seeherestackoverflow.com/questions/47702694/… \$\endgroup\$ – alex1997 Dec21,2017at16:26 \$\begingroup\$ @alex1997whatareyoutalkingaboutIlitterallycopiedyourcodeandchangedonlytheiResolutionpart...youALSOdidthatexactsamethingwithther.Itseemslikeyourresolutionis400x300,inwhichcasemyansweris100%correct,youdidn'tactuallydividegl_fragCoord.xybytherightnumbers(youuse800by600) \$\endgroup\$ – Krupip Dec21,2017at16:30 \$\begingroup\$ IfI'mapplyingyourcodetothefirstimagecase(wheretherectangleispositionedatoff1xoff2itwon'twork,thecircleitwon'tbeintherectangle..thisiswhatI'mreallytryingtofixandgottheideatogetthesizeof1pxin[0,1]rangethanmultiplyitbymyoffsetandaddittotheposvalue.Thatshoulddothetrick,butseemsthatI'mdoingsomethingwrong \$\endgroup\$ – alex1997 Dec21,2017at16:36 | Show8morecomments 0 \$\begingroup\$ Weusethebuilt-ingl_FragCoordvalue,whichgivesusthe(x,y)coordinates(inpixels)ofthecurrentfragmentintheframebuffer.Wedividethisbytheresolutionwidthandheight(e.g.800,600)togetvaluesbetween0.0and1.0. gl_FragCoord.xy/resolution.xy Notethatgl_FragCoordvalueusesalowerleftorigin,so(32,10)wouldmean32pixelstotheright,10pixelsupfromthebottom.However,sinceourcircleissymmetricalandlocatedatcenter,wedonotneedtoworryaboutthisdifferenceinourspecificdemo. Wesubtract(0.5,0.5)sothatwecandeterminethelengthfromcenter,ratherthanlower-left(theorigin). //determinecenter vec2position=(gl_FragCoord.xy/resolution.xy)-vec2(0.5); WewilluseGLSL'sbuilt-inlength()methodtodeterminethelengthfromthecenterpoint.Fragmentsatthecenterwillhavealengthofzero(i.e.black),anditwillincreaseaswemoveoutward(i.e.intowhite/gray).Wecantesttheshadersofar: //ourscreenresolution,setfromJavawheneverthedisplayisresized uniformvec2resolution; //"in"attributesfromourvertexshader varyingvec4vColor; voidmain(){ //determineorigin vec2position=(gl_FragCoord.xy/resolution.xy)-vec2(0.5); //determinethevectorlengthofthecenterpositionandusingstepfunctionfordrawingcircle floatcircle=step(length(position),0.5); //showourlengthfordebugging gl_FragColor=vec4(vec3(length),1.0); } https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson3 https://thebookofshaders.com/07/ Share Improvethisanswer Follow editedDec21,2017at16:17 answeredDec21,2017at16:09 SeyedMortezaKamaliSeyedMortezaKamali 8,0851111goldbadges4141silverbadges8080bronzebadges \$\endgroup\$ 2 \$\begingroup\$ Yeah,Iwhatyoudidthere,butIdon'tseethefragmentoffsettakkeninconsideration.Yourcodewillrenderancircleinthemiddleofthescreen(ThewayI'mdoingitwithdistanceisbetterbecauseitwon'trequiresubstractingvec2(0.5)tomodifyorigin),butifIwanttorenderacircleinarectaglee.q400x300atposition(30,200),itwon'tworkatall.. \$\endgroup\$ – alex1997 Dec21,2017at16:23 \$\begingroup\$ Thisseemstodothetrick,butituses#version120stuffsstackoverflow.com/questions/47702694/… \$\endgroup\$ – alex1997 Dec21,2017at16:27 Addacomment | Youmustlogintoanswerthisquestion. Nottheansweryou'relookingfor?Browseotherquestionstaggedmathematicsglsl. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication Related 0 FormulaforreplicatingglTexGeninOpenGLES2.0GLSL 3 Anglerangedetection 4 Convertlargerangeofnumberstoasmallerscale 0 GLSLFragmentshaderlightinglambertterm 1 Strangeartifactswhenusinggl_FragCoordtoaccesstexels 3 Mergeanglerange 0 convertgl_Positionintoscreencoordinates 0 GLSLConvertYUVtoRGB HotNetworkQuestions ShouldIrevealmyslidesbit-by-bitwhengivingapresentation/lecture? Whatdoestheidiomaticphrase"erronthesideof"mean? Audiosplitterwithoutbufferpossible? Iworkfromhomeasasoftwareengineerandmyjobishappywithmyperformance.ButI'mputtinginlittleeffort.AmIabadpersonoremployee? UnabletomountaLinuxRAIDarray Movingplayerinsideofmovingspaceship? Whatisthemostecologicallysustainablewaytohandleragscoveredinchaingrease? Whyweremarijuanarestrictionsimplementedinthe1970smoresuccessfulinSouthKoreathanintheUnitedStates? sObjecttypesfromstring Softwareforcreatinghand-writtenslidesthatarerevealedgradually Isitdangeroustoconsume30mgofZinc(asZincPicolinate)perday? Whyishavingbloatedinterfacesanantipattern? GenerateAll8Knight'sMoves Pleasehelpmeclarifythissentenceabout'microtones'onGroveMusic IsitallowedtocopycodeundertheUnityReference-OnlyLicense? Isthissinkfaucetremovable? Ihaveanarmadaofteleportingskyfortresses.DoIstillneedanavy? Using\textsc{}manuallyinsection StarWars:R2D2connectedtomainframe Currentlyself-studyingQFTandTheStandardModelbySchwartzandI'mstuckatequation1.5inPart1regardingblackbodyradiation Colourina5by5grid,withevolutionrules,sothatthestartinggridisthesameastheoutputgrid Howtodetectchains(sequenceofdegree2vertices)inagraph? WhywouldIuseatrainerinsteadofridingoutside? Can'tmultiplywidthin\newcommandforincludegraphics morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. default Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1What is the range of gl_FragCoord - Stack Overflow
- 2Cardboard VR Projects for Android - 第 90 頁 - Google 圖書結果
float distance = length(u_LightPos - modelViewVertex); vec3 lightVector ... depth = gl_FragCoord....
- 3Coordinates & Transforms in 2D
gl_FragCoord.xy / resolution.xy produces a new vec2 by dividing ... above) contains X and Y coord...
- 4Fragment Shader Introduction - FreeDesktop.Org
The Z component contains fragment's depth value on the range [0, 1]. ... The fragment shader in F...
- 5WebGL Programming Guide: Interactive 3D Graphics Programming ...
screen, and gl_FragCoord.z contains the normalized z value in the range of [0, 1]. This is calcul...