{"id":116,"date":"2008-07-09T19:15:50","date_gmt":"2008-07-09T18:15:50","guid":{"rendered":"http:\/\/www.ozone3d.net\/blogs\/lab\/?p=116"},"modified":"2011-09-07T20:44:38","modified_gmt":"2011-09-07T19:44:38","slug":"saturate-function-in-glsl","status":"publish","type":"post","link":"https:\/\/www.ozone3d.net\/blogs\/lab\/20080709\/saturate-function-in-glsl\/","title":{"rendered":"Saturate function in GLSL"},"content":{"rendered":"<p>During the conversion of shaders written in <b>Cg\/HLSL<\/b>, we often find the <b>saturate()<\/b> function. This function is not valid in <b>GLSL<\/b> even though on NVIDIA, the GLSL compiler accepts it (do not forget that NVIDIA&#8217;s GLSL compiler is based on Cg compiler). But ATI&#8217;s GLSL compiler will reject saturate() with a nice error. This function allows to limit the value of a variable to the range [0.0 &#8211; 1.0]. In GLSL, there is a simple manner to do the same thing: <b>clamp()<\/b>.<\/p>\n<p>Cg code:<\/p>\n<pre>\r\nfloat3 result = saturate(texCol0.rgb - Density*(texCol1.rgb));\r\n<\/pre>\n<p>GLSL equivalent:<\/p>\n<pre>\r\nvec3 result = clamp(texCol0.rgb - Density*(texCol1.rgb), 0.0, 1.0);\r\n<\/pre>\n<p>BTW, don&#8217;t forget all float4, float3 and float2 which correct syntax in GLSL is vec4, vec3 and vec2.<\/p>\n<p><\/p>\n<p>Lors de la conversion de shaders \u00e9crits en <b>Cg\/HLSL<\/b>, on trouve souvent la fonction <b>saturate()<\/b>. Cette fonction n&#8217;est pas valide en <b>GLSL<\/b> bien que sur les NVIDIA le compilateur l&#8217;accepte (n&#8217;oublions pas que le compilateur GLSL de NVIDIA repose sur le compilateur Cg). Mais le compilateur GLSL d&#8217;ATI g\u00e9n\u00e9rera une belle erreur \u00e0 la vue de saturate(). Cette fonction sert \u00e0 limit\u00e9 la valeur d&#8217;une variable entre 0.0 et 1.0. En GLSL il y un moyen tout simple de faire la m\u00eame chose: <b>clamp()<\/b>.<\/p>\n<p>Code Cg:<\/p>\n<pre>\r\nfloat3 result = saturate(texCol0.rgb - Density*(texCol1.rgb));\r\n<\/pre>\n<p>Equivalent GLSL:<\/p>\n<pre>\r\nvec3 result = clamp(texCol0.rgb - Density*(texCol1.rgb), 0.0, 1.0);\r\n<\/pre>\n<p>Au passage lors des conversions, n&#8217;oubliez pas les float4, float3 et float2 qui s&#8217;\u00e9crivent en GLSL en vec4, vec3 et vec2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the conversion of shaders written in Cg\/HLSL, we often find the saturate() function. This function is not valid in GLSL even though on NVIDIA, the GLSL compiler accepts it (do not forget that NVIDIA&#8217;s GLSL compiler is based on Cg compiler). But ATI&#8217;s GLSL compiler will reject saturate() with a nice error. This function allows to limit the value of a variable to the &hellip; <a href=\"https:\/\/www.ozone3d.net\/blogs\/lab\/20080709\/saturate-function-in-glsl\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Saturate function in GLSL<\/span> <span class=\"meta-nav\">&raquo;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[172,305,308,43,18,307,303,304,306],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-opengl","category-programming","tag-cg","tag-clamp","tag-game-developpment","tag-gamedev","tag-glsl","tag-graphics-programming","tag-hlsl","tag-saturate","tag-shader-conversion"],"aioseo_notices":[],"views":55684,"_links":{"self":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":0,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}