{"id":38,"date":"2021-10-20T00:02:30","date_gmt":"2021-10-20T00:02:30","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/?p=38"},"modified":"2021-10-20T00:02:30","modified_gmt":"2021-10-20T00:02:30","slug":"vertex-shader-and-fragment-shader","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/2021\/10\/20\/vertex-shader-and-fragment-shader\/","title":{"rendered":"Vertex Shader and Fragment Shader"},"content":{"rendered":"\n<p>1.0  <strong>Introduction<\/strong><\/p>\n\n\n\n<p>As beginners continue to make progress with OpenGL, they should pick up more new tools to advance their learning. One of the recommended tools is Shader. In this blog, I will discuss the advantages of using vertex and fragment Shaders and some basic OpenGL Shading Language.<\/p>\n\n\n\n<p>2.0 <strong>Advantage of Shader over Fixed-Function OpenGL pipeline<\/strong><\/p>\n\n\n\n<p>With Shader, users can put and run codes in the GPU, this can significantly improve performance of the product as GPU can simultaneously excuse multiple data streams and fragments. Display rate can increase to hundreds of frames per second [1]. In addition, Shader provide flexibility and quality for graphic design. Per-fragment lighting and per-fragment animation, for example, could produce effective and attractive visual effects for users.<\/p>\n\n\n\n<p>2.1 <strong>Roles of Vertex shader and fragment shader in the OpenGL pipeline<\/strong><\/p>\n\n\n\n<p>Studying the figure 1 can help users understand the roles of vertex Shader and fragment Shader in the OpenGL pipeline compared to the fixed-functions. Vertex Shader will replace the sequential steps of model transform, view transform, per &#8211; vertex lighting, and projection transformation with some built-in variables such as gl_Vertex, gl_Color, gl_Normal, gl_ModelViewMatrix, gl_ProjectionMatrix, gl_ModelViewProjectionMatrix. Vertex Shader use in variables and uniform variables. The gl_Position and out variable created from vertex Shader undergo the rasterization process to create a fragment for every pixel.\u00a0 Fragment Shader is used in variable, uniform variable, and local variables to perform fragment processing, texturing, and per-fragment lighting. gl_FragColor produced from fragment Shader then get into the framebuffer.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"448\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/1-4.png\" alt=\"\" class=\"wp-image-39\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/1-4.png 780w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/1-4-300x172.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/1-4-768x441.png 768w\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" \/><figcaption>Figure 1: The Shader-style in the OpenGL pileline. <br>Source: http:\/\/web.engr.oregonstate.edu\/~mjb\/cs550\/PDFs\/Shaders.1pp.pdf<\/figcaption><\/figure>\n\n\n\n<p>2.2 <strong>Basic knowledge of OpenGL Shading Language (GLSL)<\/strong> <\/p>\n\n\n\n<p>There is no pointer, string, or enum in GLSL. Also, GLSP does not support mixed data types; thus, users need to cast data. For vectors, vector components include x, y, z, w (for geometric data) or r, g, b, a  (for color data) or s, t, p, q (for texture data).<strong> Swizzle notation<\/strong> can help create new vectors based on other defined vectors. Common vectors are Boolean vectors (<strong>bvec2, bvec3, <\/strong>and<strong> bvec4<\/strong>), Integer vector (<strong>ivec2, ivec3, <\/strong>and<strong> ivec4<\/strong>), and float point vector(<strong>vec2, vec3, <\/strong>and<strong> vec4)<\/strong>. Studying the below examples [3] can be a good way to understand how vectors work in GLSP.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/4-1-1024x560.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/4-1-1024x560.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/4-1-300x164.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/4-1-768x420.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/4-1.png 1228w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Figure 2: Basic variables in GLSL. <br>Source: http:\/\/learnwebgl.brown37.net\/12_shader_language\/glsl_data_types.html<\/figcaption><\/figure>\n\n\n\n<p>In GLSP, users need to declare functions before calling them. If-else statement, while loop, and for loop can also be utilized. For calculation, some command functions are <a href=\"https:\/\/thebookofshaders.com\/glossary\/?search=exp\">exp()<\/a>, <a href=\"https:\/\/thebookofshaders.com\/glossary\/?search=log\">log()<\/a>, <a href=\"https:\/\/thebookofshaders.com\/glossary\/?search=sqrt\">sqrt()<\/a>, mod(), fract(), ceil(), floor(), abs(), min(), max(), fract(), clamp(), sin(), cos(), etc. Math functions can be used to animate, to distort shape, and blend values.&nbsp;&nbsp;<\/p>\n\n\n\n<p>GLSL is used to write vertex and fragment shaders, but there are some similarities and differences between those two shaders. Both vertex and fragment shaders have a main function, built-in functions, variables, and expressions. <strong>Uniform<\/strong> is a read-only variable that both vertex shader and fragment shader can read from.\u00a0<\/p>\n\n\n\n<p>2.2.1 <strong>Vertex Shader<\/strong><\/p>\n\n\n\n<p>However, some specific built-in functions are used for vertex Shader only. The <strong>out <\/strong>variables are\u00a0 from vertex Shaders. <strong>Attribute <\/strong>variables can only be read from the vertex Shader. The <strong>gl_Vertex<\/strong> and <strong>gl_Normal <\/strong>are the two important built-in variables in vertex Shader. In Shader, without vec4 gl_Vertex, one can not draw any geometry; without vec3 gl_Normal, one cannot light the object. The vertex position and normal vector for each vertex can be defined in OpenGL and is accessed in vertex Shader file and fragment Shader file using the built-in gl_Vertex and gl_Normal respectively. Interestingly, users can set the rgb color for an object by using the untransformed gl_Vertex or the transformed gl_Vertex. The final color effect is very different. While the untransformed one has a fixed color position, the transformed one allows color to change based on position.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"482\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/2-2.png\" alt=\"\" class=\"wp-image-40\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/2-2.png 840w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/2-2-300x172.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/2-2-768x441.png 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><figcaption>Figure 3: Untransformed vs. transformed gl_Vertex in setting rgb.<br>Source: http:\/\/web.engr.oregonstate.edu\/~mjb\/cs550\/PDFs\/Shaders.1pp.pdf [2]<\/figcaption><\/figure>\n\n\n\n<p>2.2.2 <strong>Fragment Shader<\/strong><\/p>\n\n\n\n<p>Fragment Shaders help build\u00a0 per-fragment lighting. As users might notice, the fixed-function OpenGL can only allow per-vertex and per-face\u00a0 lighting. In per-vertex lighting might be fast, but the quality seems different from the per-fragment lighting. In per-vertex light, tessellation of the surface impacts the lighting quality [4].\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"924\" height=\"490\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/5-1.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/5-1.png 924w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/5-1-300x159.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4779\/files\/2021\/10\/5-1-768x407.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><figcaption>Figure 4: per-vertex vs. per-fragment lighting effect. <br>Source: <a href=\"https:\/\/www.gamersnexus.net\/guides\/2429-gpu-rendering-and-game-graphics-explained\">https:\/\/www.gamersnexus.net\/guides\/2429-gpu-rendering-and-game-graphics-explained<\/a> [4] <\/figcaption><\/figure>\n\n\n\n<p>3.0 <strong>Conclusion<\/strong>:<\/p>\n\n\n\n<p>We have learned some basic background for vertex Shader and fragment Shader. Hopefully, this could add some more understanding for some beginners who start programming with those Shaders. With Shader, users make a huge difference in the quality and performance of of graphic design. \u00a0<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Reference<\/strong> <\/p>\n\n\n\n<p>[1] L3Harris Geospatial. (2020). <em>How Shaders Enhance Performance<\/em>. [Online]. Available: https:\/\/www.l3harrisgeospatial.com\/docs\/shaderperformance.html. Accessed Oct 19th, 2021.<\/p>\n\n\n\n<p>[2] Mike Bailey.(2021). <em>Introduction to using the OpenGL Shading Language (GLSL)<\/em>. Oregon state university. [Online]. Available: http:\/\/web.engr.oregonstate.edu\/~mjb\/cs550\/PDFs\/Shaders.1pp.pdf<\/p>\n\n\n\n<p>[3] Learn WebGL.(2016). <em>GLSL Data types and Variables<\/em>.[Online]. Available: http:\/\/learnwebgl.brown37.net\/12_shader_language\/glsl_data_types.html. Accessed: Oct 19th, 2021. <\/p>\n\n\n\n<p>[4] Steve Burke. (2016). <em> GPU Rendering &amp; Game Graphics Pipeline Explained with nVidia.<\/em> Gamer Nexus. [Online]. Available: <a href=\"https:\/\/www.gamersnexus.net\/guides\/2429-gpu-rendering-and-game-graphics-explained\">https:\/\/www.gamersnexus.net\/guides\/2429-gpu-rendering-and-game-graphics-explained<\/a>.Accessed: Oct 19th, 2021. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.0 Introduction As beginners continue to make progress with OpenGL, they should pick up more new tools to advance their learning. One of the recommended tools is Shader. In this blog, I will discuss the advantages of using vertex and fragment Shaders and some basic OpenGL Shading Language. 2.0 Advantage of Shader over Fixed-Function OpenGL&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/2021\/10\/20\/vertex-shader-and-fragment-shader\/\">Continue reading <span class=\"screen-reader-text\">Vertex Shader and Fragment Shader<\/span><\/a><\/p>\n","protected":false},"author":11550,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/users\/11550"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/posts\/38\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/learnfromscratch\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}