<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Bitmap region vs Transforms for a camera</title>
		<link>http://www.allegro.cc/forums/view/615643</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 10 Aug 2015 07:39:04 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So I was looking at the wiki for examples of a camera implementation for Allegro 5.0.10 (yes, I know 5.1 has now a camera somewhere around) and found two different options, drawing a bitmap region to the screen and calculate myself the camera position, or using an ALLEGRO_TRANSFORM to move the display to the position where the camera must be.</p><p>But would like to have advice on which method could be better in the long term
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rodolfo Lam)</author>
		<pubDate>Mon, 10 Aug 2015 00:44:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I think for a camera, you&#39;d keep track of the camera position and only draw the bits of your level/game that fit in the current screen area. You can use transforms to store your camera position, but its probably easier to just use some regular old variables<span class="ref"><sup>[<a href="#">1</a>]</sup></span> to track the position.</p><p>I&#39;m imagining you have a 2d map, maybe a side scroller or a tile map. In these cases you don&#39;t really want to do any bitmap region magic or transforms to move the drawing around because you can just take the camera position and figure out the current bits of the map to draw from that (from the camera origin minus half the screen, to camera origin plus half the screen, or something similar).
</p><div class="ref-block"><h2>References</h2><ol><li>not HTML! kekekeke. old joke</li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Aug 2015 00:49:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Seems pretty reasonable... The solution I right now have for the tilemap is drawing everything (all the layers, the entire map) to the screen directly, so the amount of offscreen objects is huge and will definitely not scale with even bigger maps. Wanted to get away with not translating the world coords to screen coords with the transform <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> lazy me... Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rodolfo Lam)</author>
		<pubDate>Mon, 10 Aug 2015 01:00:04 +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/615643/1015835#target">Rodolfo Lam</a> said:</div><div class="quote"><p>tilemap is drawing everything (all the layers, the entire map) to the screen directly</p></div></div><p>Yikes.  Yea, that&#39;ll cost ya.</p><p>I much prefer to use the transforms.</p><p>It can be kinda tricky, but once your camera is setup it&#39;s not difficult to translate both to and from world and camera coordinates.  You get the advantage of having zoom and rotation which is nice to have in your pocket, and you won&#39;t have <span class="source-code">pos-camera_x, pos-camer_y</span> cluttering all your drawing code.  Overall, you get more control, simplicity, and power if you choose to use transforms, but there is a learning curve.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 10 Aug 2015 02:01:00 +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/615643/1015838#target">Mark Oates</a> said:</div><div class="quote"><p>Yikes. Yea, that&#39;ll cost ya.</p></div></div><p>Depends. It should be fine if you use tile sheets and bitmap holding.</p><p>I don&#39;t really see how a transform is any better than just clipping like I suggested. Using transforms for translation implies you&#39;re drawing <i>everything</i> unless you also handle not drawing around where the transform is (which needs you to keep track of the translation separately...)</p><p>But yeah, if you want things like simple zoom and rotation, a transform is the way to go, but i think you still want to store the camera coordinates separately and then just &quot;translate&quot; to that in the transform.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Aug 2015 02:09:10 +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/615643/1015839#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>Depends. It should be fine if you use tile sheets and bitmap holding.</p></div></div><p>Which I didn&#39;t <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />, and it was naaaysty <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p>But even with bitmap holding and sprite sheets, I&#39;ve run into (extremer) cases where performance took a hit, (probably because you&#39;re still sending a lot of drawing calls, <i>and</i> allegro is building transforms for each al_draw_bitmap call on the backend).  I think the ultimate optimization is making the tilemap one large <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_VERTEX"><span class="a">ALLEGRO_VERTEX</span></a> tile_map_prim<span class="k2">[</span><span class="k2">]</span></span>, every tile being 2 triangles, and the texture is the tile atlas.  That&#39;s my next pet project, really looking forward to trying that out.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I don&#39;t really see how a transform is any better than just clipping like I suggested. Using transforms for translation implies you&#39;re drawing everything unless you also handle not drawing around where the transform is (which needs you to keep track of the translation separately...)</p></div></div><p>Not separately, you can extract them from the transform.  If your camera has a <span class="source-code">camera_width</span> and <span class="source-code">camera_height</span>, you can <span class="source-code"><a href="http://www.allegro.cc/manual/al_transform_coordinates"><span class="a">al_transform_coordinates</span></a><span class="k2">(</span><span class="k2">)</span></span> on <span class="source-code">camera_x</span>, <span class="source-code">camera_y</span>, <span class="source-code">camera_x<span class="k3">+</span>camera_width</span>, and <span class="source-code">camera_y<span class="k3">+</span>camera_hight</span>.  That&#39;ll give you your clipping rectangle in world coordinates.  If you then divide each coordinate by <span class="source-code"><span class="n">16</span></span> (or whatever your tile size is), then you get the row/column of the tilemap.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 10 Aug 2015 02:46:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m reading <a href="http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/">this tutorial</a> as we speak.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Mon, 10 Aug 2015 03:08:17 +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/615643/1015840#target">Mark Oates</a> said:</div><div class="quote"><p>But even with bitmap holding and sprite sheets, I&#39;ve run into (extremer) cases where performance took a hit, (probably because you&#39;re still sending a lot of drawing calls, and allegro is building transforms for each al_draw_bitmap call on the backend). I think the ultimate optimization is making the tilemap one large ALLEGRO_VERTEX tile_map_prim[], every tile being 2 triangles, and the texture is the tile atlas. That&#39;s my next pet project, really looking forward to trying that out.</p></div></div><p>Yeah, your best bet is to draw the geometry in one go. Bitmap holding is supposed to do that, as long as you only use the same actual texture for ALL calls. Of course using a single batch for all of the map is going to need to use the same texture regardless. </p><p>Though I&#39;ve been experimenting with setting up multiple texture atlases, and having the geometry data send in the texture id to pick the correct one. If more things supported Texture Arrays, this would be so much simpler, but its not a 100% supported thing yet.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Not separately, you can extract them from the transform. If your camera has a camera_width and camera_height, you can al_transform_coordinates() on camera_x, camera_y, camera_x+camera_width, and camera_y+camera_hight. That&#39;ll give you your clipping rectangle in world coordinates. If you then divide each coordinate by 16 (or whatever your tile size is), then you get the row/column of the tilemap.</p></div></div><p>You could but I&#39;m not sure how that&#39;s simpler or better <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Aug 2015 03:46:38 +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/615643/1015842#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>Though I&#39;ve been experimenting with setting up multiple texture atlases, and having the geometry data send in the texture id to pick the correct one. If more things supported Texture Arrays, this would be so much simpler, but its not a 100% supported thing yet.</p></div></div><p>That&#39;s cool.  I&#39;ve ended up just using a custom ALLEGRO_VERTEX that includes the 3D normal for, well, everything 3D.  It&#39;s just use it by default now.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 10 Aug 2015 03:58:15 +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/615643/1015843#target">Mark Oates</a> said:</div><div class="quote"><p> I&#39;ve ended up just using a custom ALLEGRO_VERTEX that includes the 3D normal for, well, everything 3D. It&#39;s just use it by default now.</p></div></div><p>What are you using it for? Isn&#39;t it normally for doing lighting and stuff like that?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Aug 2015 04:46:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yea, lighting.  And anything for shader materials like <a href="https://www.allegro.cc/files/attachment/609697">reflectance</a>, skybox, specular shading, normal maps, etc. use a normal.</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">typedef</span> <span class="k1">struct</span>
  <span class="k2">{</span>
    <span class="k1">float</span> x, y, z<span class="k2">;</span>
    <span class="k1">float</span> u, v<span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/ALLEGRO_COLOR"><span class="a">ALLEGRO_COLOR</span></a> color<span class="k2">;</span>
    <span class="k1">float</span> nx, ny, nz<span class="k2">;</span>
  <span class="k2">}</span> ALLEGRO_VERTEX_WITH_NORMAL<span class="k2">;</span>
</pre></div></div><p>

I also meant to say I&#39;m using <span class="source-code">ALLEGRO_PRIM_TEX_COORD</span> instead of allegro&#39;s default <span class="source-code">ALLEGRO_PRIM_TEX_COORD_PIXEL</span>.  So UV coordinates are in <span class="source-code"><span class="n">0</span>.<span class="n">0</span></span> to <span class="source-code"><span class="n">1</span>.<span class="n">0</span></span> rather than <span class="source-code">texture_width</span> or <span class="source-code">texture_height</span>.</p><p>Here&#39;s what my vertex declaration looks like.</p><div class="source-code snippet"><div class="inner"><pre>  <a href="http://www.allegro.cc/manual/ALLEGRO_VERTEX_ELEMENT"><span class="a">ALLEGRO_VERTEX_ELEMENT</span></a> elems<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>
    <span class="k2">{</span>ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_3, offsetof<span class="k2">(</span>ALLEGRO_VERTEX_WITH_NORMAL, x<span class="k2">)</span><span class="k2">}</span>,
    <span class="k2">{</span>ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof<span class="k2">(</span>ALLEGRO_VERTEX_WITH_NORMAL, u<span class="k2">)</span><span class="k2">}</span>,
    <span class="k2">{</span>ALLEGRO_PRIM_COLOR_ATTR, <span class="n">0</span>, offsetof<span class="k2">(</span>ALLEGRO_VERTEX_WITH_NORMAL, color<span class="k2">)</span><span class="k2">}</span>,
    <span class="k2">{</span>ALLEGRO_PRIM_USER_ATTR, ALLEGRO_PRIM_FLOAT_3, offsetof<span class="k2">(</span>ALLEGRO_VERTEX_WITH_NORMAL, nx<span class="k2">)</span><span class="k2">}</span>,
    <span class="k2">{</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">}</span>
  <span class="k2">}</span><span class="k2">;</span>

  vertex_declaration <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_vertex_decl"><span class="a">al_create_vertex_decl</span></a><span class="k2">(</span>elems, <span class="k1">sizeof</span><span class="k2">(</span>ALLEGRO_VERTEX_WITH_NORMAL<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Mon, 10 Aug 2015 05:23:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, thats more or less what I have.</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_VERTEX_ELEMENT"><span class="a">ALLEGRO_VERTEX_ELEMENT</span></a> elements<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>
    <span class="k2">{</span> ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_3, offsetof<span class="k2">(</span>CustomVertex, pos<span class="k2">)</span> <span class="k2">}</span>,
    <span class="k2">{</span> ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof<span class="k2">(</span>CustomVertex, txcoord<span class="k2">)</span> <span class="k2">}</span>,
    <span class="k2">{</span> ALLEGRO_PRIM_USER_ATTR, ALLEGRO_PRIM_FLOAT_1, offsetof<span class="k2">(</span>CustomVertex, tx_page<span class="k2">)</span> <span class="k2">}</span>,
    <span class="k2">{</span> ALLEGRO_PRIM_COLOR_ATTR, ALLEGRO_PRIM_FLOAT_4, offsetof<span class="k2">(</span>CustomVertex, color<span class="k2">)</span> <span class="k2">}</span>,
    <span class="k2">{</span> <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span> <span class="k2">}</span>
  <span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 10 Aug 2015 07:39:04 +0000</pubDate>
	</item>
</rss>
