![]() ![]() This condition can take the following values: If(gl_FragCoord.x ) out float gl_FragDepth An example fragment shader that outputs a different color based on the fragment's screen coordinates is given below: We could for example split the screen in two by rendering one output to the left side of the window and another output to the right side of the window. A common usage for the gl_FragCoord variable is for comparing visual output of different fragment calculations, as usually seen in tech demos. Using the fragment shader we could calculate a different color value based on the screen coordinate of the fragment. We specified a render window of 800圆00 with glViewport so the screen-space coordinates of the fragment will have x values between 0 and 800, and y values between 0 and 600. The gl_FragCoord's x and y component are the window- or screen-space coordinates of the fragment, originating from the bottom-left of the window. However, we can also use the x and y component of that vector for some interesting effects. We've seen the gl_FragCoord a couple of times before during the discussion of depth testing, because the z component of the gl_FragCoord vector is equal to the depth value of that particular fragment. GLSL gives us two interesting input variables called gl_FragCoord and gl_FrontFacing. Within the fragment shader we also have access to some interesting variables. When drawing without indices (via glDrawArrays) this variable holds the number of the currently processed vertex since the start of the render call. ![]() When doing indexed rendering (with glDrawElements) this variable holds the current index of the vertex we're drawing. ![]() The integer variable gl_VertexID holds the current ID of the vertex we're drawing. The vertex shader also gives us an interesting input variable, that we can only read from, called gl_VertexID. The gl_Position and gl_PointSize are output variables since their value is read as output from the vertex shader we can influence the result by writing to them. You can imagine that varying the point size per vertex is interesting for techniques like particle generation. The result is that the points we've drawn are rendered larger the more we move away from them: Gl_Position = projection * view * model * vec4(aPos, 1.0) The point size should then increase the further we are from the vertices as the viewer. Influencing the point sizes in the vertex shader is disabled by default, but if you want to enable this you'll have to enable OpenGL's GL_PROGRAM_POINT_SIZE:Ī simple example of influencing point sizes is by setting the point size equal to the clip-space position's z value which is equal to the vertex's distance to the viewer. By setting the point's size in the vertex shader we get per-vertex control over this point's dimensions. One output variable defined by GLSL is called gl_PointSize that is a float variable where you can set the point's width and height in pixels. It is possible to set the size of the points being rendered via OpenGL's glPointSize function, but we can also influence this value in the vertex shader. One of the render primitives we're able to choose from is GL_POINTS in which case each single vertex is a primitive and rendered as a point. Setting gl_Position in the vertex shader is a strict requirement if you want to render anything on the screen. We've already seen gl_Position which is the clip-space output position vector of the vertex shader. Note that we won't discuss all built-in variables that exist in GLSL so if you want to see all built-in variables you can check OpenGL's wiki. We'll discuss a few interesting built-in input and output variables that are built-in in GLSL and explain how they may benefit us. We've already seen two of them in the chapters so far: gl_Position that is the output vector of the vertex shader, and the fragment shader's gl_FragCoord. There are however a few extra variables defined by GLSL prefixed with gl_ that give us an extra means to gather and/or write data. We learned to do this via vertex attributes, uniforms, and samplers. Shaders are extremely pipelined, if we need data from any other source outside of the current shader we'll have to pass data around. We'll discuss some interesting built-in variables, new ways to organize shader input and output, and a very useful tool called uniform buffer objects. Basically some good to knows and features that may make your life easier when creating OpenGL applications in combination with GLSL. This chapter goes more or less into some interesting aspects of GLSL and some nice tricks that may help you in your future endeavors. This chapter won't really show you super advanced cool new features that give an enormous boost to your scene's visual quality. Advanced GLSL Advanced-OpenGL/Advanced-GLSL ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |