<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Field of view - primitives glitch</title>
		<link>http://www.allegro.cc/forums/view/613860</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 06 Feb 2014 14:58:07 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="remote-thumbnail"><span class="json">{"name":"608294","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/1\/f19249459fdda3ab6ef0eb92cadd3376.png","w":288,"h":385,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/1\/f19249459fdda3ab6ef0eb92cadd3376"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/1/f19249459fdda3ab6ef0eb92cadd3376-240.jpg" alt="608294" width="240" height="320" /></span></p><p>Ok, I&#39;ve tried to implement this 2d visibility / field of view algorithm here:</p><p><a href="http://www.redblobgames.com/articles/visibility/">http://www.redblobgames.com/articles/visibility/</a></p><p>It basically involves &quot;sweeping&quot; through given endpoints / vertices of line segments in the order of the angle to the center point / the &quot;viewer&quot;, producing triangles along the way if the encountered segments are closest to the &quot;viewer&quot;.</p><p>It&#39;s a very nice algorithm, however, for me, it produces &quot;glitches&quot; between triangles in certain positions (see the visible line in the image). Of course they can only be seen because the triangles are drawn with alpha &lt; 1.0. Unfortunately, this is a requirement. </p><p>I first thought the endpoints were not in the correct order or there was a problem with the angles, however, when I output the coordinates of the triangle fan, I can see that it seems to be alright.</p><p>The vertices are drawn with al_draw_prim as a triangle fan.</p><p>The line appears at the angle of almost 135°, vertex 30 and 31 are on that angle (bold in spoiler).<br />It&#39;s just a coincidence that it&#39;s 135° though, it also happens at other angles.</p><div class="spoiler"><p>
</p><pre> 
cx: 499, cy: 238
vertex  1: 453.000000, 240.999939, angle: 176.268677
vertex  2: 453.000000, 205.000076, angle: -144.344742
vertex  3: 433.484680, 191.000000, angle: -144.344742
vertex  4: 476.439484, 191.000000, angle: -115.641518
vertex  5: 486.999725, 213.000000, angle: -115.641518
vertex  6: 562.000061, 213.000000, angle: -21.644417
vertex  7: 576.000000, 207.444458, angle: -21.644426
vertex  8: 576.000000, 282.999451, angle: 30.302376
vertex  9: 576.000000, 282.999451, angle: 30.302376
vertex 10: 576.000000, 510.149841, angle: 74.202072
vertex 11: 557.001160, 443.000000, angle: 74.202072
vertex 12: 548.804688, 443.000000, angle: 76.344574
vertex 13: 542.002075, 415.000000, angle: 76.344582
vertex 14: 510.997437, 415.000000, angle: 86.122299
vertex 15: 511.000000, 415.037903, angle: 86.122307
vertex 16: 511.000000, 434.045959, angle: 86.497284
vertex 17: 511.548096, 443.000000, angle: 86.497284
vertex 18: 484.997650, 443.000000, angle: 93.907471
vertex 19: 476.732849, 564.000000, angle: 93.907471
vertex 20: 402.936768, 564.000000, angle: 106.418793
vertex 21: 438.000000, 445.009460, angle: 106.418800
vertex 22: 438.000000, 437.261169, angle: 107.020966
vertex 23: 469.000000, 335.997284, angle: 107.020973
vertex 24: 469.000000, 320.003021, angle: 110.094551
vertex 25: 469.001099, 320.000000, angle: 110.094551
vertex 26: 449.000427, 320.000000, angle: 121.372787
vertex 27: 442.000000, 331.480804, angle: 121.372787
vertex 28: 442.000000, 321.000305, angle: 124.479118
vertex 29: 442.000214, 321.000000, angle: 124.479118
<b>vertex 30: 416.001801, 321.000000, angle: 134.999374
vertex 31: 299.000000, 438.004333, angle: 134.999374</b>
vertex 32: 299.000000, 286.597717, angle: 166.342468
vertex 33: 392.000000, 263.999786, angle: 166.342468
vertex 34: 392.000000, 244.978119, angle: 176.268677
</pre><p>
</p></div><p>
There is something wrong with primitives coordinates I guess. <br />What am I missing here?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sat, 01 Feb 2014 16:36:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How do you calculate the angle? It doesn&#39;t seem to me that 30 and 31 are the same gradient if you output a few more places behind the comma...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 01 Feb 2014 17:38:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m atan2-ing it from the coordinates to check. Yes, it&#39;s exactly the same. <br /><tt>134.99937438964843750000...</tt></p><p>But the angle is what the &quot;input&quot; is to generating the triangles anyway, so it should be the same.</p><p>My implementation is essentially a copy/port of the Haxe2 implementation the guy gave here: <a href="http://www.redblobgames.com/articles/visibility/Visibility.hx">http://www.redblobgames.com/articles/visibility/Visibility.hx</a></p><p>The interesting stuff happens in the sweep function at lines 260-308. </p><p>The vertices are generated by intersecting the current angles with the line segments. </p><p>I&#39;ll post the &quot;output&quot; function here:</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"> 319</span> <span class="k1">private</span> function addTriangle<span class="k2">(</span>angle1:Float, angle2:Float, segment:Segment<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 320</span>        var p1:Point <span class="k3">=</span> center<span class="k2">;</span>
<span class="number"> 321</span>        var p2:Point <span class="k3">=</span> <span class="k2">{</span>x:center.x <span class="k3">+</span> Math.cos<span class="k2">(</span>angle1<span class="k2">)</span>, y:center.y <span class="k3">+</span> Math.sin<span class="k2">(</span>angle1<span class="k2">)</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 322</span>        var p3:Point <span class="k3">=</span> <span class="k2">{</span>x:0.0, y:0.0<span class="k2">}</span><span class="k2">;</span>
<span class="number"> 323</span>        var p4:Point <span class="k3">=</span> <span class="k2">{</span>x:0.0, y:0.0<span class="k2">}</span><span class="k2">;</span>
<span class="number"> 324</span>
<span class="number"> 325</span>        <span class="k1">if</span> <span class="k2">(</span>segment <span class="k3">!</span><span class="k3">=</span> null<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 326</span>            <span class="c">// Stop the triangle at the intersecting segment</span>
<span class="number"> 327</span>            p3.x <span class="k3">=</span> segment.p1.x<span class="k2">;</span>
<span class="number"> 328</span>            p3.y <span class="k3">=</span> segment.p1.y<span class="k2">;</span>
<span class="number"> 329</span>            p4.x <span class="k3">=</span> segment.p2.x<span class="k2">;</span>
<span class="number"> 330</span>            p4.y <span class="k3">=</span> segment.p2.y<span class="k2">;</span>
<span class="number"> 331</span>        <span class="k2">}</span> <span class="k1">else</span> <span class="k2">{</span>
<span class="number"> 332</span>            <span class="c">// never happens</span>
<span class="number"> 333</span>            <span class="k1">return</span><span class="k2">;</span> 
<span class="number"> 334</span>        <span class="k2">}</span>
<span class="number"> 335</span>    
<span class="number"> 336</span>        var pBegin <span class="k3">=</span> lineIntersection<span class="k2">(</span>p3, p4, p1, p2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 337</span>
<span class="number"> 338</span>        p2.x <span class="k3">=</span> center.x <span class="k3">+</span> Math.cos<span class="k2">(</span>angle2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 339</span>        p2.y <span class="k3">=</span> center.y <span class="k3">+</span> Math.sin<span class="k2">(</span>angle2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 340</span>        var pEnd <span class="k3">=</span> lineIntersection<span class="k2">(</span>p3, p4, p1, p2<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 341</span>
<span class="number"> 342</span>        output.push<span class="k2">(</span>pBegin<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 343</span>        output.push<span class="k2">(</span>pEnd<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 344</span>    <span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sat, 01 Feb 2014 18:28:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>cx: 499, cy: 238</p><p>vertex 30: 416.001801, 321.000000, angle: 134.999374<br />vertex 31: 299.000000, 438.004333, angle: 134.999374</p><p>They don&#39;t are the same, which I suspect is the problem.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 01 Feb 2014 18:51:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think I don&#39;t understand what you mean.<br />The coordinates aren&#39;t the same, but they shouldn&#39;t be.<br />#30 is the little yellow cross where the glitch-line ends, #31 is farther along this line on the segment to the left.<br />Both are needed to draw this thing properly, no? <br />Or do you mean I am maybe too stupid to understand how the vertices for a proper triangle fan have to look like? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sat, 01 Feb 2014 19:04:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, I mean, some rounding error may have caused it... so maybe you can use double instead of float or something like that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 01 Feb 2014 19:32:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve tried to replace every &quot;float&quot; with &quot;double&quot;. To no avail: The glitch-line visible above disappears then, but there are others appearing instead. <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p><p>I&#39;ve just realized my last post could come across as aggressive. Sorry, it wasn&#39;t meant like that at all. I&#39;m merely totally clueless about what is causing this.</p><p>So how to solve this? I guess it&#39;s just not possible to draw this with a simple triangle fan then, because there <b>will</b> be glitches with vertices on the same gradient? What do you think?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sat, 01 Feb 2014 20:09:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Two triangles touching at an edge are guaranteed not to overdraw only if they share the two vertices of that edge. I think what&#39;s happening in your case is that you are violating that rule and creating something called a t-junction, where you have an overlapping edge, but one or two vertices that are not shared. There&#39;s a little discussion on this here (for an unrelated tool, but the idea is the same): <a href="http://wiki.ldraw.org/index.php?title=Avoiding_T-Junctions_in_LDraw_parts">http://wiki.ldraw.org/index.php?title=Avoiding_T-Junctions_in_LDraw_parts</a> . The solution is to subdivide your triangles. I believe your field of view is a simple polygon, so you can use the <span class="source-code">al_triangulate_polygon</span> to triangulate that field and then use <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_indexed_prim"><span class="a">al_draw_indexed_prim</span></a></span> with <span class="source-code">ALLEGRO_PRIM_TRIANGLES_LIST</span> to draw it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sat, 01 Feb 2014 21:51:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you for your words of wisdom.</p><p>Edit: Is triangulating it expensive performance-wise?</p><p>Edit2: I think more vertices aren&#39;t necessary. I just need a clever way to draw it with the given vertices. Somehow recursively deferring the triangles which result from the 2nd, 3rd, ... vertex on the same gradient.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sat, 01 Feb 2014 22:07:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I understand correctly you&#39;re drawing it semi-transparently and it overlaps with itself creating lines? If that is the case you could draw it opaquely to another bitmap, then draw that bitmap to the screen with semi-transparency.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sun, 02 Feb 2014 00:08:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, that&#39;s a possible solution. Which I&#39;m trying to avoid nonetheless because I don&#39;t want a separate buffer to be involved when not strictly necessary.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Sun, 02 Feb 2014 01:54:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613860/995841#target">Polybios</a> said:</div><div class="quote"><p> Is triangulating it expensive performance-wise?
</p></div></div><p>I don&#39;t think it&#39;s too bad.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> I think more vertices aren&#39;t necessary. I just need a clever way to draw it with the given vertices. Somehow recursively deferring the triangles which result from the 2nd, 3rd, ... vertex on the same gradient. </p></div></div><p>That&#39;s right, no new vertices are necessary, but you need more edges.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sun, 02 Feb 2014 05:12:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>... I managed to do it without calling an extra triangulation function. Instead of drawing a triangle fan with 34 vertices and center, I now draw an indexed triangle list with 30 vertices and 90 indices. No glitches so far...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Polybios)</author>
		<pubDate>Thu, 06 Feb 2014 14:58:07 +0000</pubDate>
	</item>
</rss>
