<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Shader normalized coordinates.</title>
		<link>http://www.allegro.cc/forums/view/617777</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 15 Mar 2019 23:13:09 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi Everyone,</p><p>New member, so hello <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Just beginning with shaders but am struggling with this one problem. Everything following is in relation to basic 2D, no 3D required...</p><p>If I draw a basic bitmap to the screen with a shader, how do I get access to the normalized (0.0 to 1.0) coordinates for just the bitmap, not the whole screen?</p><p>I want to do something with the bitmap in relation to the position within it &amp; mapping it&#39;s texture coordinates to range 0.0, 1.0 makes things simpler.<br />I want to map 0.0 to the left side &amp; 1.0 to the right side of the bitmap etc.<br />I believe 0.0 would be bottom &amp; 1.0 would be top also?</p><p>It seems when Allegro gives you the texture coordinates for the bitmap, it is in relation to the screen size - say 1280 x 720.</p><p>For example:<br />I want r &amp; g to be in the range 0.0 to 1.0, but just for the bitmap, no matter where the bitmap is located within the 2D screen.<br />gl_FragColor = vec4(r, g, 0.0, 1.0);</p><p>The following code is how it is recommended to normalize within the fragment shader, but this does not work when trying the normalize a bitmap within a 1280x720 screen space:<br />vec2 xy = gl_FragCoord.xy;    // get coords for the current pixel<br />xy.x = xy.x / 1280;           // divide the coords by the screen size<br />xy.y = xy.y / 720;</p><p>My simple shader just currently renders the texture normally:</p><p>Vertex Shader:</p><p>attribute vec4 al_pos;<br />attribute vec2 al_texcoord;<br />uniform mat4 al_projview_matrix;<br />varying vec2 varying_texcoord;</p><p>void main()<br />{<br />   varying_texcoord = al_texcoord;<br />   gl_Position = al_projview_matrix * al_pos;<br />}</p><p>Fragment Shader:</p><p>uniform sampler2D al_tex;<br />varying vec2 varying_texcoord;</p><p>void main()<br />{<br />   vec4 color = texture2D(al_tex, varying_texcoord);<br />   gl_FragColor = color;<br />}</p><p>Thank You if anybody is able to help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (albert69)</author>
		<pubDate>Thu, 14 Mar 2019 02:00:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>added &lt;code&gt; tags.</p><div class="source-code snippet"><div class="inner"><pre>vec2 xy <span class="k3">=</span> gl_FragCoord.xy<span class="k2">;</span> <span class="c">// get coords for the current pixel</span>
xy.x <span class="k3">=</span> xy.x <span class="k3">/</span> <span class="n">1280</span><span class="k2">;</span> <span class="c">// divide the coords by the screen size</span>
xy.y <span class="k3">=</span> xy.y <span class="k3">/</span> <span class="n">720</span><span class="k2">;</span>
</pre></div></div><p>

</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span> vec4 al_pos<span class="k2">;</span>
<span class="number">  2</span>attribute vec2 al_texcoord<span class="k2">;</span>
<span class="number">  3</span>uniform mat4 al_projview_matrix<span class="k2">;</span>
<span class="number">  4</span>varying vec2 varying_texcoord<span class="k2">;</span>
<span class="number">  5</span>
<span class="number">  6</span><span class="k1">void</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number">  7</span><span class="k2">{</span>
<span class="number">  8</span>varying_texcoord <span class="k3">=</span> al_texcoord<span class="k2">;</span>
<span class="number">  9</span>gl_Position <span class="k3">=</span> al_projview_matrix <span class="k3">*</span> al_pos<span class="k2">;</span>
<span class="number"> 10</span><span class="k2">}</span>
<span class="number"> 11</span>
<span class="number"> 12</span>Fragment Shader:
<span class="number"> 13</span>
<span class="number"> 14</span>uniform sampler2D al_tex<span class="k2">;</span>
<span class="number"> 15</span>varying vec2 varying_texcoord<span class="k2">;</span>
<span class="number"> 16</span>
<span class="number"> 17</span><span class="k1">void</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 18</span><span class="k2">{</span>
<span class="number"> 19</span>vec4 color <span class="k3">=</span> texture2D<span class="k2">(</span>al_tex, varying_texcoord<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>gl_FragColor <span class="k3">=</span> color<span class="k2">;</span>
<span class="number"> 21</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Thu, 14 Mar 2019 04:03:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m confused. don&#39;t you just want al_texcoord?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 14 Mar 2019 04:23:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi</p><p>I read in a different thread the following:</p><p>&quot;Allegro texture coordinates are [0-bitmap_width], [0-bitmap_height]. This is different from the usual [0-1], [0-1] texture coordinates that you get with UV textures.&quot;</p><p>So the following code lines are getting the correct color from the texture at the correct coord, but they are in [0-bitmap_width], [0-bitmap_height] scale.</p><p>varying_texcoord = al_texcoord;<br />vec4 color = texture2D(al_tex, varying_texcoord);</p><p>If I wanted to shade the bitmap a certain way - like mapping the r,g components to a color according to its position on the bitmap, do I need normalized coordinates? Because rgb colors are mapped in the 0.0 to 1.0 range.</p><p>gl_FragColor = vec4(r, g, 0.0, 1.0);</p><p>Sorry if I am confusing things. Just started.</p><p>Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (albert69)</author>
		<pubDate>Thu, 14 Mar 2019 19:43:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can pass the texture width and height as uniforms and divide, which may work if your image is a single texture.</p><p>Another option is to use gl_TexCoord[0].xy</p><p>In your vertex shader :
</p><pre>
    gl_TexCoord[0] = gl_MultiTexCoord0;
</pre><p>


In your fragment shader :
</p><pre>
    gl_FragColor = vec4(gl_TexCoord[0].x , gl_TexCoord[0].y , 0.0 , 1.0);
</pre><p>

I think this will work. It&#39;s using the older fixed pipeline though, which is bad if you&#39;re trying to use modern techniques.</p><p>I attached a simple shader demo that shows how to vary the color of an image based on time. There&#39;s a static exe, source, a CB project, and a vertex and fragment shader.</p><p><a href="https://www.allegro.cc/files/attachment/611947">BasicShader.7z</a></p><p><b>EDIT</b><br />I modified it to use your example coloring, and it produces this :</p><p><span class="remote-thumbnail"><span class="json">{"name":"611948","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/f\/df329f7cd0fe0754574e36794c7f3d23.png","w":802,"h":633,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/f\/df329f7cd0fe0754574e36794c7f3d23"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/d/f/df329f7cd0fe0754574e36794c7f3d23-240.jpg" alt="611948" width="240" height="189" /></span></p><p>with this fragment shader :
</p><div class="source-code snippet"><div class="inner"><pre>    vec2 uv <span class="k3">=</span> gl_TexCoord<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.xy<span class="k2">;</span>
    gl_FragColor <span class="k3">=</span> vec4<span class="k2">(</span>uv.x , uv.y , texel.b , texel.a<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Red increases from left to right with x and green increases from bottom to top with y on the texture.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 14 Mar 2019 21:07:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi Edgar,</p><p>Thank You. That works perfectly.</p><p>gl_TexCoord[0] seems to have done the trick, even though it&#39;s deprecated?</p><p>I really appreciate your help with this.</p><p>Thanks <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (albert69)</author>
		<pubDate>Fri, 15 Mar 2019 03:55:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>albert69, no problem. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>Since you&#39;re new to shaders, you may want to check out some really great OpenGL shader tutorials to get yourself acquainted with them.</p><p><a href="https://learnopengl.com/Getting-started/Shaders">https://learnopengl.com/Getting-started/Shaders</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 15 Mar 2019 23:13:09 +0000</pubDate>
	</item>
</rss>
