<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Parse Error &#187; Programming</title>
	<atom:link href="https://vertostudio.com/gamedev/?cat=4&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://vertostudio.com/gamedev</link>
	<description>Michael L. Farrell&#039;s Game Dev Blog</description>
	<lastBuildDate>Tue, 08 Dec 2020 18:13:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>DirectX 12 Demos</title>
		<link>https://vertostudio.com/gamedev/?p=446</link>
		<comments>https://vertostudio.com/gamedev/?p=446#comments</comments>
		<pubDate>Mon, 17 Aug 2015 22:11:51 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Downloads & Demos]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=446</guid>
		<description><![CDATA[This last month, DirectX 12 launched alongside windows 10 so I decided to see what would happen if I ported my engine backend to run on something besides OpenGL. Because I&#8217;m crazy enough to attempt something like this, I dove into what limited documentation there was on D3D12 and went at it. I wanted to [...]]]></description>
			<content:encoded><![CDATA[<p>This last month, DirectX 12 launched alongside windows 10 so I decided to see what would happen if I ported my engine backend to run on something besides OpenGL. Because I&#8217;m crazy enough to attempt something like this, I dove into what limited documentation there was on D3D12 and went at it. I wanted to get a head start on some of the concepts that I know are coming when Vulkan launches later this year, and to get myself off of a strict dependency of OpenGL for my engine graphics. One crucial decision that I made early was to continue using just one shader language (GLSL) for even the D3D12 engine backend. I did this because Verto Studio files often contain embedded shader code that is written against the Verto Studio shader standard, and I wasn&#8217;t about to break that part of the platform. Luckily, the Angle project&#8217;s shader translator proved to be a perfect solution to auto-translate my GLSL code to HLSL and interoperate with the rest of my D3D12 layer quite nicely.</p>
<p>I had worked at this low of a level before and I was amazed at how hard I had to bust my ass to get performance that would first match, and eventually beat my windows NVIDIA OpenGL drivers in some cases. Whats more interesting, is that I had managed to double the framerate performances that I was getting on OpenGL on OSX, which really speaks to how bad the Apple OpenGL drivers really are in comparison to windows. Mechanisms such as pipeline state caching, frame latency planning, and large ahead-of-time heap allocations were all necessary to bring my performance up to par. There still are some hiccups but for the most part, I&#8217;m really pleased with what amounted from about a week and a half of messing around with D3D12.</p>
<p>Windows 10 demos are below. Keyboard shortcuts aren&#8217;t that great but namely arrow keys revolve the camera, and shift+alt combinations allow you to move forward, and vertically. These demos don&#8217;t push the bar graphically and they certainly come nowhere close to pushing D3D12 to what it can do, but if you just want something to run that uses DirectX 12, well here you go.</p>
<p><a title="http://vertostudio.com/pub/demos/VGLPP_DX12_Demos.zip" href="http://vertostudio.com/pub/demos/VGLPP_DX12_Demos.zip">Download DirectX 12 Demos</a></p>
<div align="center"><a href="http://vertostudio.com/gamedev/wp-content/uploads/2015/08/ss.png"><img class="size-large wp-image-447 aligncenter" title="ss" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/08/ss-1024x640.png" alt="" width="625" height="390" /></a></div>
<p>OpenGL equivalent demos are provided in the zip archive as well for comparison..</p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=446</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ambient Occlusion</title>
		<link>https://vertostudio.com/gamedev/?p=362</link>
		<comments>https://vertostudio.com/gamedev/?p=362#comments</comments>
		<pubDate>Fri, 30 Jan 2015 21:51:55 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=362</guid>
		<description><![CDATA[So I got the ambient occlusion effect implemented in the actual game shader code.  I followed the technique outlined in this article.  I must say, it&#8217;s very subtle, but it looks nice.  The real benefit here is that when I turn off the &#8220;check depth range&#8221; mechanism, I get a very awesome surreal dark outline [...]]]></description>
			<content:encoded><![CDATA[<p><span style="line-height: 1.714285714; font-size: 1rem;">So I got the ambient occlusion effect implemented in the actual game shader code.  I followed the technique outlined in <a title="this article" href="http://t.co/xleU0YlQbY">this article</a>.  I must say, it&#8217;s very subtle, but it looks nice.  The real benefit here is that when I turn off the &#8220;check depth range&#8221; mechanism, I get a very awesome surreal dark outline around all of the models in the scene.  This will be an excellent effect to use when I work on the second half of the game (which will use an artistic black and white noire post processing style signifying the protagonists slowly weakening grip on reality).</span></p>
<p>I didn&#8217;t understand how ambient occlusion worked at all before implementing the effect, but now I must say, I have a pretty reasonable grasp on it.  Basically in one brief description, it takes random samples at every point on the surface to determine how much ambient light from the rest of the scene can reach that point.  In other words, how much of the ambient light is occluded by other nearby surfaces.  This is done by randomly sampling points within a small hemisphere emanating from the surface.  What makes SSAO so nice, is that this technique is approximated entirely in screen space, making it reasonable for real-time rendering in a game.  The downside is it seems to be a little noisy and I see artifacts from time to time on the screen, but hopefully I&#8217;ll be able to clean some of that up in time.</p>
<p>Below are some more progress shots showing the kinds of subtle shadowing I can achieve using SSAO in the VGLPP/DrivebyGangster engine.</p>

<a href='https://vertostudio.com/gamedev/?attachment_id=363' title='Screen Shot 2015-01-28 at 2.22.25 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/01/Screen-Shot-2015-01-28-at-2.22.25-PM-150x150.png" class="attachment-thumbnail" alt="Screen Shot 2015-01-28 at 2.22.25 PM" title="Screen Shot 2015-01-28 at 2.22.25 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=364' title='ssao_comparison'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/01/ssao_comparison-150x150.png" class="attachment-thumbnail" alt="ssao_comparison" title="ssao_comparison" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=365' title='ssaoshot'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/01/ssaoshot-150x150.png" class="attachment-thumbnail" alt="ssaoshot" title="ssaoshot" /></a>

]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=362</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ambient Occlusion!</title>
		<link>https://vertostudio.com/gamedev/?p=357</link>
		<comments>https://vertostudio.com/gamedev/?p=357#comments</comments>
		<pubDate>Tue, 27 Jan 2015 02:49:00 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=357</guid>
		<description><![CDATA[Today was huge!  I am exhausted.  I busted my ass today to get an affect known as screen-space ambient occlusion (SSAO) working in a prototype scene in verto studio.  Right when I was about to give up, I got it working!  I&#8217;m too groggy to explain anything about it, so I&#8217;ll just post pictures! This [...]]]></description>
			<content:encoded><![CDATA[<p>Today was huge!  I am exhausted.  I busted my ass today to get an affect known as screen-space ambient occlusion (SSAO) working in a prototype scene in verto studio.  Right when I was about to give up, I got it working!  I&#8217;m too groggy to explain anything about it, so I&#8217;ll just post pictures!</p>
<p>This is going to add a huge boost to the visual look-and-feel of the game without a severe performance penalty.  Needless to say, I&#8217;m pretty stoked!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<a href='https://vertostudio.com/gamedev/?attachment_id=359' title='Screen Shot 2015-01-26 at 6.24.37 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/01/Screen-Shot-2015-01-26-at-6.24.37-PM-150x150.png" class="attachment-thumbnail" alt="Screen Shot 2015-01-26 at 6.24.37 PM" title="Screen Shot 2015-01-26 at 6.24.37 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=360' title='Screen Shot 2015-01-26 at 6.44.02 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2015/01/Screen-Shot-2015-01-26-at-6.44.02-PM-150x150.png" class="attachment-thumbnail" alt="Screen Shot 2015-01-26 at 6.44.02 PM" title="Screen Shot 2015-01-26 at 6.44.02 PM" /></a>

]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=357</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL point light shadows &amp; Atomspheric FX hacks</title>
		<link>https://vertostudio.com/gamedev/?p=319</link>
		<comments>https://vertostudio.com/gamedev/?p=319#comments</comments>
		<pubDate>Sat, 27 Dec 2014 08:44:56 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=319</guid>
		<description><![CDATA[So asset creation in the form of modeling the final environment that&#8217;ll be used in the game took the past few days.  It&#8217;s amazed how much faster it went this time compared the first scene (the city street).  I chalk this up to experience that I&#8217;ve picked up now that I&#8217;ve been doing this awhile, [...]]]></description>
			<content:encoded><![CDATA[<p>So asset creation in the form of modeling the final environment that&#8217;ll be used in the game took the past few days.  It&#8217;s amazed how much faster it went this time compared the first scene (the city street).  I chalk this up to experience that I&#8217;ve picked up now that I&#8217;ve been doing this awhile, and my understanding of things that are very time consuming to model vs things I can quickly grab, import, texture, and place from turbosquid.  The perfect balance of both techniques has led to me being done with the dark night alley street scene after just a few days.</p>
<p>Where I spent the most time this time was in the shaders.  I really wanted to have quality rendering and go out with a bang since this&#8217;ll likely be the last environment that I do for the game.  I want the dark alleyway to appear foggy and dark, like its just about to rain.  That includes moist shiny cobblestone rocks, and a damp fog effect that feels very palpable to the player.</p>
<p>This idea of mine posed two major problems that I haven&#8217;t had to tackle until now.</p>
<h2>Point Light Shadows</h2>
<p>The first major problem, is that I could no longer &#8220;cheat&#8221; using a single directional light source representing the &#8220;sun&#8221; in the scene.  Instead, I had to place a street lamp every building or so and have these be the only sources of light in the scene.  This means, in order to keep my shadows working, I now had to implement shadows from a point light source.  In some ways this was easier than point lighting (no longer necessary to compute an optimal shadow bounding box to be used for the light&#8217;s ortho matrix projection).  However, now I needed render to and utilize cube maps to store the shadow depth information.  Surprisingly, there was very little comprehensive information on the web about how to properly do PCF shadows using cubemaps.</p>
<p>What I found that works is the following.</p>
<ul>
<li>Create a cubemap renderer that is positioned at the exact same position as one of the point lights - <span style="line-height: 1.714285714; font-size: 1rem;">this special vgl engine object renders the scene into 6 faces of a cubemap with 90-degree fov angles to properly capture the entire scene from &#8220;all angles&#8221;.</span></li>
<li><span style="line-height: 1.714285714; font-size: 1rem;">Format the cubemap shader to have each face hold floating point depth-component values and keep the size small (256&#215;256) since there will be alot of these.</span></li>
<li><span style="line-height: 1.714285714; font-size: 1rem;">Define an &#8220;override&#8221; shader to be used by the above cubemap renderer to ensure a simple specialized &#8220;light render&#8221; shader was used when rendering the scene into the cubemap faces.  </span></li>
<li>Set the cubemap compare mode (<span style="line-height: 1.714285714; font-size: 1rem;"><em>GL_TEXTURE_COMPARE_MODE</em>) </span>for the rendered cubemap texture to <span style="line-height: 1.714285714; font-size: 1rem;"><em>GL_COMPARE_REF_TO_TEXTURE</em> which helps with percentage-closest-filtering (PCF) mode to allow smoother linear-interpolated shadow edges.  </span></li>
<li><span style="line-height: 1.714285714; font-size: 1rem;">Lastly, pass a rendered shadow cubemap for each light to the scene shaders in the form of <em>samplerCubeShadow</em></span></li>
</ul>
<p>The &#8220;light render&#8221; shader mentioned above which is used when rendering the depth information to the cubemaps looks like so:</p><pre class="crayon-plain-tag">precision mediump float;
in mediump vec4 w_pos;
out vec4 fragColor;

struct Light
{
  vec4 worldPosition;
};
uniform Light lights[8];

#define LIGHT      0
uniform vec2 nearFarPlane;

void main()
{
  // distance to light
  float distanceToLight = distance(lights[LIGHT].worldPosition.xyz, w_pos.xyz);

  float resultingColor = (distanceToLight - nearFarPlane.x) /
           (nearFarPlane.y - nearFarPlane.x);

  gl_FragDepth = resultingColor;
  fragColor = vec4(1.0);
}</pre><p>Basically this encodes the distance from the surface point to the light in the form normalized depth information.  This is done by manually overriding the depth value stored in <em>gl_FragDepth</em>.  Each light has its own cubemap renderer with a custom-tailored shader like this (using the correct light index to compute distance from).</p>
<p>The per-fragment lighting shader code that utilizes the cube shadow maps looks like:</p><pre class="crayon-plain-tag">/*....*/

#define LIGHT 0
#define ATTENUATION

uniform samplerCubeShadow shadowMap;

/*.......*/

void pointLight0(in mediump vec3 normal, in mediump vec3 eye, in mediump vec3 ecPosition3)
{
  mediump float nDotVP;       // normal . light direction
  mediump float nDotHV;       // normal . light half vector
  //mediump float pf = 0.0;           // power factor
  mediump float attenuation = 1.0;  // computed attenuation factor
  mediump float d;            // distance from surface to light source
  mediump vec3  VP;           // direction from surface to light position
  mediump vec3  halfVector;   // direction of maximum highlights

  // Compute vector from surface to light position
  VP = vec3(lights[LIGHT].position) - ecPosition3;

#ifdef ATTENUATION
  // Compute distance between surface and light position
  d = length(VP);
#endif

  // Normalize the vector from surface to light position
  VP = normalize(VP);

  // Compute attenuation
#ifdef ATTENUATION
  {
    attenuation = 1.0 / (lights[LIGHT].constantAttenuation +
                         lights[LIGHT].linearAttenuation * d +
                         lights[LIGHT].quadraticAttenuation * d * d);
  }
#endif

  nDotVP = dot(normal, VP);
  mediump vec2 frontAndBack = vec2(nDotVP, -nDotVP);
  frontAndBack = max(vec2(0.0), frontAndBack);

  float visibility = 1.0;

  // difference between position of the light source and position of the fragment
  vec3 fromLightToFragment = lights[LIGHT].worldPosition.xyz - va_position.xyz;

  // normalized distance to the point light source
  float distanceToLight = length(fromLightToFragment);
  float currentDistanceToLight = (distanceToLight - nearFarPlane.x) / (nearFarPlane.y - nearFarPlane.x);

  // normalized direction from light source for sampling
  fromLightToFragment = normalize(fromLightToFragment);      
  visibility *= max(texture(shadowMap, vec4(-fromLightToFragment, currentDistanceToLight-shadowBias), 0.0), 0.0);

  //  if(nDotVP &gt; 0.0)
  {
    diffuse  += visibility*material.diffuse*lights[LIGHT].diffuse * frontAndBack.x * attenuation;
    diffuseBack += visibility*material.diffuse*lights[LIGHT].diffuse * frontAndBack.y * attenuation;
  }

  //if(lights[LIGHT].doSpec)
  {
    mediump vec2 cutOff = step(frontAndBack, vec2(0.0));

    halfVector = normalize(VP + eye);
    nDotHV = dot(normal, halfVector);
    frontAndBack = vec2(nDotHV, -nDotHV);
    frontAndBack = max(vec2(0.0), frontAndBack);

    lowp vec2 pf = pow(frontAndBack, vec2(material.shininess, material.shininess));
    specular += visibility*material.specular*lights[LIGHT].specular * pf.x * attenuation * cutOff.y;
    specularBack += lights[LIGHT].specular * pf.y * attenuation * cutOff.x;
  }

  ambient += lights[LIGHT].ambient * attenuation;
}</pre><p>Essentially the highlighted code above compares the distance between the surface point to the light to the stored depth value in the cube texture using the fragment-light vector as a lookup value into the cubemap.  Because we&#8217;re sampling using a special &#8220;shadow&#8221; version of the cubemap sampler, the result will be properly interpolated between shadow texels to avoid ugly edges between shadowed and non-shadowed areas.</p>
<p>Luckily, I was able to build this into the Verto Studio editor and associated graphics system and test this out with relatively little trouble.  Even though I have 4 or 5 statically rendered cubemap shadow textures in the entire scene.  I was able to keep performance high by building versions of the shaders for each side of the street so that each individual shader only has to shade with at most 3 lights at a time.  This worked out better than I had expected.</p>
<h2>Light-Fog Interaction (Volume FX)</h2>
<p>This part was tricky..  I had an idea in my head of how I wanted this to look.  So I did something that usually leads to trouble;  I tried to come up with a volume rendering technique on my own from scratch and implement it and just kinda see how it goes.</p>
<p>The basic idea stems from what I&#8217;ve observed in real life from foggy, dark, damp nights and lighting.  Essentially, fog at night is black&#8230; IF there is no light around to interact with the fog.  Naturally, if the water droplets don&#8217;t have any light to interact with them, you won&#8217;t see them and the fog will appear black.  However, if any light interacts with the water vapor in the air, it&#8217;ll create the illusion of a whiter colored and denser fog.  So this is what I set out to emulate with my shader.</p>
<p>Now atmospheric shader effects can often lead to the necessity of raymarching and heavy iteration in the fragment shader to simulate the accumulation of light-atomosphere interaction.  To this I said &#8220;hell no&#8221; since ray marching of any kind in a shader terribly robs performance.  I quickly realized that I could avoid raymarching entirely if I used a simple model to represent the light-fog interaction that I was going for.</p>
<p>In my case, it turned out I could do the whole effect using something as simple as a sphere intersection test.  Basically, when I&#8217;m shading a pixel (a point on the surface), I&#8217;m interested in what happens to the light on its way back from the surface to the viewer, the surface-to-viewer vector.  If the atmosphere affects the light at any point along this vector, I&#8217;ll need to compute that.  In other words, if the ray from the surface to the viewer intersects a sphere centered at the light, then the fog affects the light on the way back to the viewer.  How much?  Well if I calculate the length of the segment between the entry and exit points of the ray intersection (how much of the sphere the ray pierces), I find that length is proportional to both the perceived increase in density of the fog and the brightening of the fog color.</p>
<p>This algorithm is given below in fragment shader code:</p><pre class="crayon-plain-tag">/*.....*/

uniform float fogDensity;
uniform vec3 fogColor;

//sphere intersection
bool intersect(vec3 raydir, vec3 rayorig, vec3 pos, float radiusSquared, 
               out float innerSegmentLength)
{
   float t0, t1; // solutions for t if the ray intersects

   // geometric solution
   vec3 L = pos - rayorig;
   float tca = dot(L, raydir);

   //seems to be true if ray o is inside the sphere
   //we want this to be a positive..
   //if(tca &lt; 0) 
   //  return false;

   float d2 = dot(L, L) - tca * tca;
   if(d2 &gt; radiusSquared)
     return false;
   float thc = sqrt(radiusSquared - d2);
   t0 = tca - thc;
   t1 = tca + thc;

   innerSegmentLength = abs(t0-t1);

   return true;
}

vec4 computeFog(vec4 color)
{
  vec3 viewDirection = normalize(cameraPosition - vec3(va_position));

  vec3 surfacePos = ec_pos.xyz/va_position.w;
  const float LOG2 = 1.442695;
  float fogFactor = exp2(-fogDensity * length(surfacePos) * LOG2);  

  vec3 fogCol = fogColor;
  vec3 rayO = vec3(va_position);
  vec3 rayD = viewDirection;
  float len = 0.0;
  const float r = 80.0;

  //for each light we interact with...
  if(intersect(rayD, rayO, lights[LIGHT].worldPosition.xyz, r*r, len))
  {
    float d = len/r;
    float p = clamp(log(d)*d, 0.0, 1.0);
    fogCol = mix(fogColor, vec3(0.4), p);

    len = 0.0;
    const float innerR = 25.0f;
    if(intersect(rayD, rayO, lights[LIGHT].worldPosition.xyz, innerR*innerR, len))
    {
      float len10 = len/innerR;
      float nd = min(len10*0.25, 1.0);
      fogFactor *= mix(1.0, 0.0, nd);

      fogCol += mix(vec3(0.0), vec3(0.2), len10);
    }
  }

  if(intersect(rayD, rayO, lights[LIGHT1].worldPosition.xyz, r*r, len))
  {
    float d = len/r;
    float p = clamp(log(d)*d, 0.0, 1.0);
    fogCol = mix(fogColor, vec3(0.4), p);

    len = 0.0;
    const float innerR = 25.0f;
    if(intersect(rayD, rayO, lights[LIGHT1].worldPosition.xyz, innerR*innerR, len))
    {
      float len10 = len/innerR;
      float nd = min(len10*0.25, 1.0);
      fogFactor *= mix(1.0, 0.0, nd);

      fogCol += mix(vec3(0.0), vec3(0.2), len10);
    }
  }

  return mix(vec4(fogCol, 1.0), color, fogFactor);
}</pre><p>&nbsp;</p>
<p>I&#8217;m sure I&#8217;m not the first one to come up with this idea before, but it still did feel pretty cool to reason my way through a shading problem like this.  The visual result looks amazing.</p>
<p><span style="line-height: 1.714285714; font-size: 1rem;">The progress of this all is below in a gallery.  </span></p>

<a href='https://vertostudio.com/gamedev/?attachment_id=322' title='Screen Shot 2014-12-22 at 12.36.54 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-22-at-12.36.54-PM-150x150.png" class="attachment-thumbnail" alt="Screen Shot 2014-12-22 at 12.36.54 PM" title="Screen Shot 2014-12-22 at 12.36.54 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=323' title='Screen Shot 2014-12-22 at 2.21.58 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-22-at-2.21.58-PM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-12-22 at 2.21.58 PM" title="Screen Shot 2014-12-22 at 2.21.58 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=324' title='Screen Shot 2014-12-22 at 7.33.04 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-22-at-7.33.04-PM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-12-22 at 7.33.04 PM" title="Screen Shot 2014-12-22 at 7.33.04 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=325' title='Screen Shot 2014-12-24 at 11.33.34 AM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-24-at-11.33.34-AM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-12-24 at 11.33.34 AM" title="Screen Shot 2014-12-24 at 11.33.34 AM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=326' title='Screen Shot 2014-12-24 at 3.12.39 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-24-at-3.12.39-PM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-12-24 at 3.12.39 PM" title="Screen Shot 2014-12-24 at 3.12.39 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=327' title='rendering'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/rendering-150x150.png" class="attachment-thumbnail" alt="rendering" title="rendering" /></a>

]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=319</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to 3D sound in windows (hint:  it&#8217;s not OpenAL)</title>
		<link>https://vertostudio.com/gamedev/?p=316</link>
		<comments>https://vertostudio.com/gamedev/?p=316#comments</comments>
		<pubDate>Fri, 19 Dec 2014 02:36:42 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=316</guid>
		<description><![CDATA[So yesterday and today, I bit the bullet and started working on compiling the game for windows.  I wanted to do this before the project got too much further along so I didn&#8217;t write any code that would have serious implications on windows.  I must say, I&#8217;m amazed at how much trouble I didn&#8217;t have [...]]]></description>
			<content:encoded><![CDATA[<p>So yesterday and today, I bit the bullet and started working on compiling the game for windows.  I wanted to do this before the project got too much further along so I didn&#8217;t write any code that would have serious implications on windows.  I must say, I&#8217;m amazed at how much trouble I didn&#8217;t have with the rendering system.  The levels/screens load up and look great!  I did have some issues with visual studio that I chalk up to lack of experience in windows dev on my part.  Namely getting the builds to be 64-bit instead of the default 32, SSE3 operator overload stuff, and getting debug builds to forego some of the extremely slow settings that make the engine run at a crawl.  Once I took care of all that, my little game was running on windows like a champ, complete with same stellar OpenGL 3.3 performance I get on mac.  All was well&#8230; that is except for one little tiny issue.  The sound.</p>
<p>One thing that became apparent almost right away was that OpenAL support on windows is in a terrible state.  Counting on it to just be there like OpenGL is a no no.. and installing your own via something like OpenAL soft gives pretty poor support.  I realized that I had to do something I&#8217;ve never done before, and that&#8217;s directly use a DirectX subsystem.  After some quick googling I discovered what I need can be done using a combination of xAudio2 and X3DAudio.  Both of these systems come with DirectX and the DX SDK so I was already set up to use em.</p>
<p>So basically, the plan on windows was pretty simple:  since I already have all my 3D sound calls abstracted out in my SoundManager class, I all I had to do is re-implement the stuff that OpenAL handled using this new xAudio2 stuff.. and pray that it played nice along side SDL_Mixer which I use for mp3 music.  More or less, things went relatively painless and I was able to find an xAudio2/X3DAudio analog for just about everything I was doing in OpenAL EXCEPT for a little issue I had with source playback.</p>
<p>On mac, I&#8217;m able to play a source overtop of itself as many times as I want to.  On windows, this seems to be a no-go.  So for win, I had to manually pre-allocate pools of sources for the sounds that I would be playing rapidly such as bullet ricochet sounds and typewriter keystrokes and ensure that I return an available source whenever I ask to play that sound.  That process more or less worked out just fine.</p>
<p>After all that, we have my reworked soundmanager class for windows.  The code is below for anyone who wants to learn from it.</p><pre class="crayon-plain-tag">//
//  SoundManager.h
//  Gangster Driveby
//
//

#ifndef __Gangster_Driveby__SoundManager__
#define __Gangster_Driveby__SoundManager__

#include &lt;windows.h&gt;
#include &lt;xaudio2.h&gt;
#include &lt;x3daudio.h&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include "VecTypes.h"
#include "Wave.h"
#include "SDL_mixer.h"

class SoundManager
{
public:
  enum Effect
  {
    Engine = 0,
    Typewriter1,
    Typewriter2,
    Typewriter3,
    Typewriter4,
    Typewriter5,
    TypewriterSpace,
    TypewriterDing,
    TypewriterFeed,
    TommyGun,
    TommyGunLoop,
    GlassBreak,
    BulbBreak,
    BulletHoleGlass,
    DramaticBoom,
    PaperIn,
    PaperOut,
    OceanClose,
    WavesAmbience,

    //MUST be last
    LongMusic
  };

  enum Music
  {
    DarkFlashes = 0,
    NightDocksSax, 
    DontGoWayNobody
  };

  class MultiSource
  {
    friend class SoundManager;
  public:
    MultiSource(IXAudio2 *engine, Wave *wave, int maxSources = 1)
    {
      for(int i = 0; i &lt; maxSources; i++)
      {
        IXAudio2SourceVoice *source = nullptr;

        if(FAILED(engine-&gt;CreateSourceVoice(&amp;source, wave-&gt;wf())))
        {
          std::cerr &lt;&lt; "source creation error!" &lt;&lt; std::endl;
        }
        sources.push_back(source);
      }

      ZeroMemory(&amp;emitter, sizeof(emitter));
      emitter.ChannelCount = 1; //not 100% on this
      emitter.CurveDistanceScaler = FLT_MIN;

      ZeroMemory(&amp;dspSettings, sizeof(dspSettings));
      FLOAT32 *dspMatrix = nullptr;

      XAUDIO2_DEVICE_DETAILS details;
      engine-&gt;GetDeviceDetails(0, &amp;details);

      dspMatrix = new FLOAT32[details.OutputFormat.Format.nChannels];
      dspSettings.SrcChannelCount = 1;
      dspSettings.DstChannelCount = details.OutputFormat.Format.nChannels;
      dspSettings.pMatrixCoefficients = dspMatrix;
    }

    ///Returns an available source
    IXAudio2SourceVoice *getSource();

    ///Updates and calculates 3D sound stuff
    void update3D(SoundManager *sm);

    //One or many sources per sound
    std::vector&lt;IXAudio2SourceVoice *&gt; sources;

    //3D sound emitter (if needed for this source)
    X3DAUDIO_EMITTER emitter;
    X3DAUDIO_DSP_SETTINGS dspSettings;
    bool is3D = false, is3DUpdated = false;

    //Useful to keep track of this since 3D audio calculates its own
    float basePitch = 1.0f;
  };

  SoundManager();
  ~SoundManager();

  static SoundManager *manager(); 

  inline bool isActive() { return (engine != nullptr); }

  void setEffectBasicProperties(Effect effect, float gain, float pitch=1.0f, bool looping=false);
  void setEffectGain(Effect effect, float gain);
  void setEffectPitch(Effect effect, float pitch);

  void setEffectSpatialProperties(Effect effect, float referenceDistance, float maxDistance, float rollOffFactor=0.0f,
                                  float3 velocity = float3::zero, float3 direction = float3::zero);

  void setEffectPosition(Effect effect, float3 pos);

  void setListenerPosition(float3 pos, float3 orientation[]);

  void playEffect(Effect effect);
  void stopEffect(Effect effect);

  void playSong(Music song);
  void stopSong();
  void setSongVolume(float volume);
  void setSongPosition(double pos);
  float getSongVolume();

  void loadLongMusicEffect(const char *fn);
  void unloadLongMusicEffect();

  inline void setSoundAvailable(bool b) { soundAvailable = b; }

  void update3D();

private:
  void loadSoundEffects();

  IXAudio2 *engine = nullptr;
  IXAudio2MasteringVoice *master = nullptr;
  XAUDIO2_DEVICE_DETAILS deviceDetails;

  X3DAUDIO_HANDLE engine3d;
  X3DAUDIO_LISTENER listener;

  std::vector&lt;MultiSource *&gt; sources;
  std::vector&lt;Wave *&gt; buffers;
  Wave *longMusicBuffer = nullptr;
  MultiSource *longMusicSource = nullptr;

  Mix_Music *currentSong = nullptr;

  bool soundAvailable = true;  
  float currentSongVolume = 1.0f;
};

#endif /* defined(__Gangster_Driveby__SoundManager__) */</pre><p></p><pre class="crayon-plain-tag">//
//  SoundManager.cpp
//  Gangster Driveby
//
//  Created by Mike Farrell on 11/14/14.
//  Copyright (c) 2014 Mike Farrell. All rights reserved.
//

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include "PortableSoundInfo.h"
#include "SoundManager.h"
#include "SDL_mixer.h"

static SoundManager *currentSoundManager = nullptr;

static const char *sounds[] = {
  "eng_1_mid_loop.wav",
  "type1.wav",
  "type2.wav",
  "type3.wav",
  "type4.wav",
  "type5.wav",
  "type_space.wav",
  "type_ding.wav",
  "type_feed.wav",
  "tommy.wav",
  "tommy_loop.wav",
  "glassbreak.wav",
  "bulb_break.wav",
  "bullethole_glass.wav",
  "dramatic_boom.wav",
  "paper_in.wav",
  "paper_out.wav",
  "ocean_close.wav",
  "waves_ambience.wav"
};

static const int soundSourceCounts[] = {
  1, 
  8, 8, 8, 8, 8, 8, 
  1, 1, 
  8, 1, 
  8, 8, 8, 
  1, 1, 1, 1, 1 
};

static const char *songs[] = {
/*....*/
};

static const int numSounds = sizeof(sounds)/sizeof(char *);
//static const int numSongs = sizeof(songs)/sizeof(char *);

using namespace std;

SoundManager::SoundManager()
{
  //create the engine
  if(!FAILED(XAudio2Create(&amp;engine)))
  {
    //create the mastering voice
    if(!FAILED(engine-&gt;CreateMasteringVoice(&amp;master)))
    {
      //create 3D sound engine
      DWORD dwChannelMask;
      if(!FAILED(engine-&gt;GetDeviceDetails(0, &amp;deviceDetails)))
      {
        dwChannelMask = deviceDetails.OutputFormat.dwChannelMask;
        X3DAudioInitialize(dwChannelMask, X3DAUDIO_SPEED_OF_SOUND, engine3d);
        ZeroMemory(&amp;listener, sizeof(listener));    
      }
      else
      {
        cerr &lt;&lt; "3D sound init failed!" &lt;&lt; endl;
      }

      //and load sound fx
      loadSoundEffects();
    }
  }
  else
  {
    cerr &lt;&lt; "Could not initialize DirectSound for sound!" &lt;&lt; endl;
  }

  currentSoundManager = this;
}

SoundManager::~SoundManager()
{
  if(engine)
    engine-&gt;Release();
  for(Wave *wave : buffers) if(wave)
    delete wave;
  for(MultiSource *ms : sources) if(ms)
    delete ms;
  if(currentSong)
    Mix_FreeMusic(currentSong);
}

SoundManager *SoundManager::manager()
{
  return currentSoundManager;
}

void SoundManager::loadSoundEffects()
{
  auto sz = numSounds;

  for(int i = 0; i &lt; numSounds; i++)
  {
    Wave *buffer = new Wave();
    if(!buffer-&gt;load(sounds[i]))
    {
      cerr &lt;&lt; "shilt" &lt;&lt; endl;
    }

    int maxSources = soundSourceCounts[i];
    MultiSource *source = new MultiSource(engine, buffer, maxSources);

    buffers.push_back(buffer);
    sources.push_back(source);
  }

  //long music placeholder
  sources.push_back(nullptr);
  buffers.push_back(nullptr);
}

void SoundManager::update3D()
{
  for(auto s : sources) if(s)
  {
    s-&gt;update3D(this);
  }
}

void SoundManager::loadLongMusicEffect(const char *fn)
{
  if(longMusicBuffer)
    unloadLongMusicEffect();

  PortableSoundInfo sound(fn);
  longMusicBuffer = new Wave();
  if(!longMusicBuffer-&gt;load(sound))
  {
    cerr &lt;&lt; "long music load failed!" &lt;&lt; endl;
  }
  longMusicSource = new MultiSource(engine, longMusicBuffer, 1);

  sources[(int)LongMusic] = longMusicSource;
  buffers[(int)LongMusic] = longMusicBuffer;
}

void SoundManager::unloadLongMusicEffect()
{
  delete longMusicBuffer;
  longMusicBuffer = nullptr;
  delete longMusicSource;
  longMusicSource = nullptr;
}

void SoundManager::setEffectGain(SoundManager::Effect effect, float gain)
{
  const float vol = gain;

  auto source = sources[(int)effect];
  for(auto s : source-&gt;sources)
  {
    s-&gt;SetVolume(vol);
  }
}

void SoundManager::setEffectBasicProperties(SoundManager::Effect effect, float gain, float pitch, bool looping)
{
  setEffectGain(effect, gain);
  if(pitch != 0.0f)
  {
    setEffectPitch(effect, pitch);
  }

  if(!looping)
  {
    buffers[(int)effect]-&gt;xaBuffer()-&gt;LoopCount = 0;
  }
  else
  {
    buffers[(int)effect]-&gt;xaBuffer()-&gt;LoopCount = XAUDIO2_MAX_LOOP_COUNT;
  }
}

void SoundManager::setEffectPitch(SoundManager::Effect effect, float pitch)
{
  auto source = sources[(int)effect];
  for(auto s : source-&gt;sources)
  {
    const float ratio = pitch;
    s-&gt;SetFrequencyRatio(ratio);
  }

  //mm.. mmmm, pitch!
  source-&gt;basePitch = pitch;
}

void SoundManager::setEffectSpatialProperties(SoundManager::Effect effect, float referenceDistance, float maxDistance, float rollOffFactor,
  float3 velocity, float3 direction)
{
  //stereo tracks get a horrible sound blasting result when you try to do this
  //lets just do what openAL does, which is silently fail
  if(buffers[(int)effect]-&gt;wf()-&gt;nChannels &gt; 1)
  {
    return;
  }

  //not reversible for now
  auto source = sources[(int)effect];
  source-&gt;is3D = true;

  source-&gt;emitter.Velocity = { velocity.x, velocity.y, velocity.z };
  if(direction != float3::zero)
  {
    source-&gt;emitter.OrientFront = { direction.x, direction.y, direction.z };
    source-&gt;emitter.OrientTop = listener.OrientTop;
  }

  //a "max distance" isn't available in x3d audio it seems..

  float distanceScalar = referenceDistance - rollOffFactor*2.5;
  source-&gt;emitter.CurveDistanceScaler = max(0.0f, distanceScalar);
}

void SoundManager::setEffectPosition(Effect effect, float3 pos)
{
  auto source = sources[(int)effect];
  source-&gt;emitter.Position = { pos.x, pos.y, pos.z };
}

void SoundManager::setListenerPosition(float3 pos, float3 orientation[])
{
  listener.Position = { pos.x, pos.y, pos.z };
  listener.OrientFront = { orientation[0].x, orientation[0].y, orientation[0].z };
  listener.OrientTop = { orientation[1].x, orientation[1].y, orientation[1].z };
}

void SoundManager::playEffect(SoundManager::Effect effect)
{
  auto ms = sources[(int)effect];
  auto source = ms-&gt;getSource();

  //ensure initial 3D calculations done if requested
  if(ms-&gt;is3D &amp;&amp; !ms-&gt;is3DUpdated)
  {
    ms-&gt;update3D(this);
  }

  source-&gt;Start();
  source-&gt;SubmitSourceBuffer(buffers[(int)effect]-&gt;xaBuffer());
}

void SoundManager::stopEffect(SoundManager::Effect effect)
{
  auto source = sources[(int)effect];
  for(auto s : source-&gt;sources)
  {
    s-&gt;Stop();
  }
}

void SoundManager::playSong(SoundManager::Music song)
{
  if(soundAvailable)
  {
    if(currentSong)
      Mix_FreeMusic(currentSong);
    currentSong = Mix_LoadMUS(songs[(int)song]);
    Mix_PlayMusic(currentSong, -1);
  }
}

void SoundManager::stopSong()
{
  if(soundAvailable)
  {
    Mix_HaltMusic();
  }
}

void SoundManager::setSongVolume(float volume)
{
  currentSongVolume = volume;
  if(soundAvailable)
  {
    Mix_VolumeMusic((int)roundf(volume*MIX_MAX_VOLUME));
  }
}

void SoundManager::setSongPosition(double pos)
{
  if(soundAvailable)
  {
    Mix_SetMusicPosition(pos);
  }
}

float SoundManager::getSongVolume()
{
  return currentSongVolume;
}

IXAudio2SourceVoice *SoundManager::MultiSource::getSource()
{
  auto isPlaying = [](IXAudio2SourceVoice *source)-&gt;bool {
    XAUDIO2_VOICE_STATE state;
    ZeroMemory(&amp;state, sizeof(XAUDIO2_VOICE_STATE));

    source-&gt;GetState(&amp;state);
    return (state.BuffersQueued != 0);
  };

  int i = 0;
  while(i &lt; sources.size() &amp;&amp; isPlaying(sources[i]))
  {
    i++;
  }

  if(i == sources.size())
  {
    if(sources.size() &gt; 1)
    {
#ifdef DEBUG
      cout &lt;&lt; "Warning:  sound queue will occur (this will suck)" &lt;&lt; endl;
#endif
    }
    return sources[0];
  }

  return sources[i];
}

void SoundManager::MultiSource::update3D(SoundManager *sm)
{
  if(is3D)
  {
    //X3DAUDIO_CALCULATE_LPF_DIRECT ?
    X3DAudioCalculate(sm-&gt;engine3d, &amp;sm-&gt;listener, &amp;emitter, X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER, &amp;dspSettings);
    for(auto s : sources)
    {
      s-&gt;SetOutputMatrix(sm-&gt;master, 1, sm-&gt;deviceDetails.OutputFormat.Format.nChannels, dspSettings.pMatrixCoefficients);
      s-&gt;SetFrequencyRatio(basePitch * dspSettings.DopplerFactor);
    }

    is3DUpdated = true;
  }
}</pre><p><span style="line-height: 1.714285714; font-size: 1rem;">All of the multiple source stuff is essentially handled by my inner MultiSource class which handles the souce pools for sounds that need em.  I found that I never needed more than 8 sources per sound.  I also had to add a new update3D method to update the 3D sound calculations and call it once every other frame or so (which wasn&#8217;t needed in OpenAL since it was done automatically).</span></p>
<p><span style="line-height: 1.714285714; font-size: 1rem;">Not shown here is a Wave class which essentially is a slightly modified version of the one from this <a title="tutorial" href="http://www.win32developer.com/tutorial/xaudio/xaudio_tutorial_1.shtm" target="_blank">tutorial</a>.  That class basically handles wav file loading for me and manages the sound buffer data internally.  </span></p>
<p>That&#8217;s just about it.  Everything else was pretty straightforward.</p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=316</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++11&#8242;s take on retain cycles</title>
		<link>https://vertostudio.com/gamedev/?p=298</link>
		<comments>https://vertostudio.com/gamedev/?p=298#comments</comments>
		<pubDate>Fri, 12 Dec 2014 19:31:56 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=298</guid>
		<description><![CDATA[I&#8217;d like to take a brief moment to discuss something rather programmy today.  Namely, the topic of retain cycles.  If you&#8217;ve ever programmed in Objective-C before (since the addition of ARC) or Swift, this concept will be very familiar to you.  But if you&#8217;re coming from other programming languages such as the older (pre-11) C++, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to take a brief moment to discuss something rather programmy today.  Namely, the topic of retain cycles.  If you&#8217;ve ever programmed in Objective-C before (since the addition of ARC) or Swift, this concept will be very familiar to you.  But if you&#8217;re coming from other programming languages such as the older (pre-11) C++, This concept will be new and you might want to read on.</p>
<p>Now before I get started, let me say that this example may not be the most correct way to handle delegation via lambda (block) functions in C++11.  Many will tell you that shared ownership of objects in C++ can get you into trouble, and this is certainly a case of how that can happen.  However it&#8217;s the most conceptually compatible with other languages, which I value over &#8220;c++ correctness&#8221; due to the fact that I&#8217;m porting code from another language to start with, and shared ownership allows me to get work done without reasoning around the lifespan of my objects.</p>
<p>Now as many newcomers to C++11 know, concepts such as smart pointers and lambda callback delegation can really save you alot of time when developing new systems.  However, there&#8217;s a<span style="line-height: 1.714285714; font-size: 1rem;">hidden evil in using these two concepts together, that must be uncovered.</span></p>
<h2>Example 1 :: All is well</h2>
<p>Consider this example:</p><pre class="crayon-plain-tag">#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;memory&gt;

using namespace std;

class A
{
public:
  ~A()
  {
    cout &lt;&lt; "object freed!" &lt;&lt; endl;
  }

  std::function&lt;void()&gt; someCallback;

  void f()
  {
    if(someCallback)
    {
      someCallback();
    }
  }  

  int someProperty = 100;
};

int main()
{
  shared_ptr&lt;A&gt; obj1;  
  obj1 = make_shared&lt;A&gt;();

  obj1-&gt;someCallback = [] {
    cout &lt;&lt; "obj1 callback called!" &lt;&lt; endl;
  };

  obj1-&gt;f();
  return 0;
}</pre><p>and its output:</p><pre class="crayon-plain-tag">obj1 callback called!
object freed!

Exited with status code: 0</pre><p>What we&#8217;ve essentially done here is created an object (via shared pointer), set a callback lambda, and issued a method call which at some point calls our callback.  We see that everything is working as desired.  Most importantly, we see that our object is properly deleted when we are finished our program (when the shared pointer goes out of scope).</p>
<h2>Example 2 :: Not so much</h2>
<p>Now lets look what happens when I add a slightly more complicated, second case.</p><pre class="crayon-plain-tag">#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;memory&gt;

using namespace std;

class A
{
public:
  ~A()
  {
    cout &lt;&lt; "object freed!" &lt;&lt; endl;
  }

  std::function&lt;void()&gt; someCallback;

  void f()
  {
    if(someCallback)
    {
      someCallback();
    }
  }  

  int someProperty = 100;
};

int main()
{
  shared_ptr&lt;A&gt; obj1, obj2;

  obj1 = make_shared&lt;A&gt;();
  obj2 = make_shared&lt;A&gt;();

  obj1-&gt;someCallback = [] {
    cout &lt;&lt; "obj1 callback called!" &lt;&lt; endl;
  };

  obj1-&gt;f();

  obj2-&gt;someCallback = [obj2] {
    cout &lt;&lt; "obj2 callback called: " &lt;&lt; obj2-&gt;someProperty &lt;&lt; endl;
  };

  obj2-&gt;f();

  return 0;
}</pre><p>Obj2 essentially is the same as obj1 right?  All we do is add a little printout of obj2&#8242;s someProperty value from within the lambda&#8230; and of course, C++11 dictates that we must explicitly declare all external objects that we access within our lambda (a capture list).</p>
<p>So what is the output then?</p><pre class="crayon-plain-tag">obj1 callback called!
obj2 callback called: 100
object freed!

Exited with status code: 0</pre><p>Note the alarming lack of &#8220;object freed&#8221; being displayed for obj2.  That&#8217;s right, the above code memory-leaks obj2.  You see, obj2 owns someCallback, and someCallback owns obj2 via a capture. This is a retain cycle and it permanently causes obj2 to be &#8220;forever retained&#8221; and therefore leaks.  Once a retain cycle is made, you can consider things to be more-or-less, too late.</p>
<p><span style="line-height: 1.714285714; font-size: 1rem;">This type of bug is a dangerous silent killer of the night.  Why?  Because it can be so easy to miss when developing.  Objective-C and swift have evolved over time to add compiler and IDE warnings about this kind of &#8220;retain cycle&#8221; problem.  C++11 assumes that you just wouldn&#8217;t write this type of code.  But when porting code over from Objective-C, this kind of code can pop up ALL the time.</span></p>
<p>So what do we do?</p>
<h2>Solution 1 :: The ObjC way</h2>
<p>Well Objective-C handles this problem by introducing the concept of &#8220;weak pointers&#8221;.  These are special pointers that ensure that they do not own their reference, they only keep track of whether or not it is allocated (if any shared pointers to it exist).  Luckily, C++11 brings the weak pointer to us as well.  A solution that uses them looks like so:</p><pre class="crayon-plain-tag">auto weakObj2 = weak_ptr&lt;A&gt;(obj2);
  obj2-&gt;someCallback = [weakObj2] {
    cout &lt;&lt; "obj2 callback called: " &lt;&lt; weakObj2.lock()-&gt;someProperty &lt;&lt; endl;
  };

  obj2-&gt;f();</pre><p>You&#8217;ll note now that running this code will solve the leak.  <span style="line-height: 1.714285714; font-size: 1rem;">It&#8217;s also important to note that, while this is an ugly solution, this is/was the only way to solve a retain cycle of this nature in Objective-C.</span></p>
<p>However, for this explicitly unique case, in C++, there&#8217;s another way.</p>
<h2>Solution 2 :: The tempting C++ way</h2>
<p>What if I told you there was a way to solve the original problem by adding one character to the original code?  Here it is:</p><pre class="crayon-plain-tag">obj2-&gt;someCallback = [&amp;obj2] {
    cout &lt;&lt; "obj2 callback called: " &lt;&lt; obj2-&gt;someProperty &lt;&lt; endl;
  };

  obj2-&gt;f();</pre><p>See that little &#8216;&amp;&#8217; added before obj2 in the capture?  This allows us to capture a <em>reference</em> to the smart pointer and not a <em>value</em>.  Why does this matter?  Well when a smart_pointer value is copied, it essentially increments its retain count of the object in question, and when it finally is destroyed by going out of scope, it&#8217;ll decrement the retain count, restoring the natural balance to the universe.  However, as I mentioned before, a retain cycle prevents the retain count from ever reaching zero in this case.  A reference to obj2&#8242;s smart pointer value allows us to forego the copy-increment mechanism and essentially stop obj2 from being over-retained.  Its essentially like taking a pointer to a pointer&#8230;.</p>
<p><strong>However, this solution can be dangerous</strong>, as it assumes the coder understands the rammifcations of doing things like capturing a reference to a stack variable, an argument, or anything that may be &#8220;dead&#8221; by the time the callback is ran.  Almost always, this &#8220;solution&#8221; may give unintended consequences.</p>
<h2>The &#8220;correct&#8221; way</h2>
<p>Here&#8217;s where things get somewhat opinionated. The &#8220;correct&#8221; way to handle this really depends on the situation at hand. I suggest that if you a working with a team, you might want to use the safer and more verbose weak pointer method since it is more explicit in its intentions and helps explain why strong ownership could be a problem.  Furthermore, if you are working in a smaller system where shared pointership is not necessary, you might want to avoid using shared pointers altogether (assuming you fully understand the ownership semantics of the objects being used in the captures).</p>
<p>Either way, the golden rule is to understand the side effects of the code you are writing and when using shared pointers, keep that little retain cycle demon in the back of your mind&#8230; always</p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=298</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallax Occlusion Mapping &#8211; Advances in shader land</title>
		<link>https://vertostudio.com/gamedev/?p=290</link>
		<comments>https://vertostudio.com/gamedev/?p=290#comments</comments>
		<pubDate>Thu, 04 Dec 2014 05:16:02 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=290</guid>
		<description><![CDATA[Today was cool&#8230; at least the first part of it was, before I spend 5 hours tracking down a bug in some of the game code. I stumbled onto a pretty cool little article geared towards a very awesome technique called &#8220;parallax occlusion mapping&#8221;. This is essentially bump mapping with some extra samples taken at [...]]]></description>
			<content:encoded><![CDATA[<p>Today was cool&#8230; at least the first part of it was, before I spend 5 hours tracking down a bug in some of the game code.</p>
<p>I stumbled onto a pretty cool little article geared towards a very awesome technique called &#8220;parallax occlusion mapping&#8221;. This is essentially bump mapping with some extra samples taken at each pixel to provide a realistic depth (or parallax) effect to the surface. This provides the illusion of realistic surface detail all while shading a single polygon. The article was somewhat dated and targeted for Direct3D instead of OpenGL, but I was able to port it over to GLSL 150 for OpenGL 3.2.</p>
<p>Here&#8217;s the original article:<br />
<a title="Article" href="http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262" target="_blank"><span style="line-height: 1.714285714; font-size: 1rem;">http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262</span></a></p>
<div></div>
<p><span style="line-height: 1.714285714; font-size: 1rem;">And here&#8217;s my port of the shader code for anyone who is interested.</span></p><pre class="crayon-plain-tag">in highp vec4 position;
in mediump vec3 normal;
in mediump vec2 texcoord0;
in mediump vec3 tangent;

out mediump vec2 va_texcoord;

out mediump vec3 va_eye;
out mediump vec3 va_normal2;
out mediump vec3 va_light;

uniform mat4 modelViewProjectionMatrix;
uniform mediump mat4 modelViewMatrix;
uniform mat4 modelMatrix;
uniform mat4 modelMatrixInverse;
uniform mediump vec2 textureScale;
uniform mediump vec2 bumpScale;
uniform mediump mat3 normalMatrix;
uniform float shadowBiasFactor;
uniform vec3 cameraPosition;

uniform bool doNormalize;

#define LIGHT    4

struct Light
{
  mediump vec4 worldPosition;
};

#ifdef GL_ES
highp mat3 transpose(in highp mat3 inMatrix) 
{
  highp vec3 i0 = inMatrix[0];
  highp vec3 i1 = inMatrix[1];
  highp vec3 i2 = inMatrix[2];
  //highp vec4 i3 = inMatrix[3];

  highp mat3 outMatrix = mat3(
                 vec3(i0.x, i1.x, i2.x),
                 vec3(i0.y, i1.y, i2.y),
                 vec3(i0.z, i1.z, i2.z)
                 );
  return outMatrix;
}
#endif

uniform Light lights[8];

void main()
{
  vec3 P = (modelMatrix * position).xyz;
  vec3 N = normal;
  vec3 E = P - cameraPosition;
  vec3 L = -lights[LIGHT].worldPosition.xyz - P;

  //Compute transformed normal
  vec3 eyeNormal = normalize(normalMatrix * normal);

  //Pass transformed texcoord.
  va_texcoord = texcoord0*textureScale;

  vec4 nNormal = vec4(normalize(normal), 0.0);
  vec4 nTangent = vec4(normalize(tangent), 0.0);
  vec4 nBinormal = vec4(cross(nNormal.xyz, nTangent.xyz), 0.0);
  mat3 tangentToWorldSpace;
  tangentToWorldSpace[0] = (modelMatrix * nTangent).xyz;
  tangentToWorldSpace[1] = (modelMatrix * nBinormal).xyz;
  tangentToWorldSpace[2] = (modelMatrix * nNormal).xyz;

  mat3 worldToTangentSpace = transpose(tangentToWorldSpace);

  va_eye = E * worldToTangentSpace;
  va_normal2 = N * worldToTangentSpace;
  va_light = L * worldToTangentSpace;

  //Pass GL-transformed to vertex down pipeline
  gl_Position = modelViewProjectionMatrix * position;
}</pre><p>&nbsp;</p><pre class="crayon-plain-tag">#ifdef GL_ES
#extension GL_OES_standard_derivatives : require
#extension GL_EXT_shader_texture_lod : require
#endif

precision mediump float; 

in mediump vec2 va_texcoord;
in mediump vec4 ec_pos;

in mediump vec3 va_eye;
in mediump vec3 va_normal2;
in mediump vec3 va_light;

out vec4 fragColor;

#define LIGHT    4

//Prototypes
vec4 computeLight(in mediump vec3 normal, in mediump vec4 ecPosition, in lowp float alphaFade, out lowp vec4 otherSideColor, out lowp vec4 secondaryHighlight);

//Lights
struct Light
{
  mediump vec4 worldPosition;
};

uniform vec3 cameraPosition;
uniform Light lights[8];
uniform vec4 lightModelProductSceneColor;
uniform lowp sampler2D texture0;
uniform sampler2D bumpTexture;
uniform sampler2D heightTexture;

//uniform bool lightingEnabled;

const float fHeightMapScale = 0.02;
const int nMaxSamples = 32;
const int nMinSamples = 8;

void main()
{
  // Calculate the geometric surface normal vector, the vector from
  // the viewer to the fragment, and the vector from the fragment
  // to the light.
  vec3 N = normalize(va_normal2);
  vec3 E = normalize(va_eye);
  vec3 L = normalize(va_light);

  float fParallaxLimit = -length( va_eye.xy ) / va_eye.z;
  fParallaxLimit *= -fHeightMapScale;

  vec2 vOffsetDir = normalize(va_eye.xy);
  vec2 vMaxOffset = vOffsetDir * fParallaxLimit;

  int nNumSamples = int(mix(float(nMaxSamples), float(nMinSamples), dot(E, N)));
  float fStepSize = 1.0 / float(nNumSamples);

  vec2 dx = dFdx(va_texcoord);
  vec2 dy = dFdy(va_texcoord);

  float fCurrRayHeight = 1.0;
  vec2 vCurrOffset = vec2(0.0);
  vec2 vLastOffset = vec2(0.0);

  float fLastSampledHeight = 1.0;
  float fCurrSampledHeight = 1.0;

  int nCurrSample = 0;

  while(nCurrSample &lt; nNumSamples)
  {
    fCurrSampledHeight = textureGrad(heightTexture, va_texcoord+vCurrOffset, dx, dy).r;
    if(fCurrSampledHeight &gt; fCurrRayHeight)
    {
      float delta1 = fCurrSampledHeight - fCurrRayHeight;
      float delta2 = ( fCurrRayHeight + fStepSize ) - fLastSampledHeight;

      float ratio = delta1/(delta1+delta2);

      vCurrOffset = (ratio) * vLastOffset + (1.0-ratio) * vCurrOffset;

      nCurrSample = nNumSamples + 1;
    }
    else
    {
      nCurrSample++;

      fCurrRayHeight -= fStepSize;

      vLastOffset = vCurrOffset;
      vCurrOffset += fStepSize * vMaxOffset;

      fLastSampledHeight = fCurrSampledHeight;
    }
  }

  vec2 vFinalCoords = va_texcoord + vCurrOffset;
  vec4 vFinalNormal = texture(bumpTexture, va_texcoord + vCurrOffset);
  lowp vec4 vFinalColor = texture(texture0, vFinalCoords); //vec4(1.0);

  vFinalNormal = vFinalNormal * 2.0 - 1.0;

  vec3 vAmbient = vFinalColor.rgb * 0.1;
  vec3 vDiffuse = vFinalColor.rgb * max( 0.0, dot( L, vFinalNormal.xyz ) ) * 0.5;

  vFinalColor.rgb = vAmbient + vDiffuse;

  fragColor = vFinalColor;
}</pre><p>Some of this code is &#8220;massaged&#8221; by Verto Studio&#8217;s code converter when ran on mobile.  It&#8217;s definitely a pretty cool effect!  It requires 3 texture maps to work currently, a standard diffuse (color texture) map, a normal map, and a displacement or height map.  Lucky for me, there&#8217;s a sick little program called &#8220;crazy bump&#8221; for mac that can generate both normal maps and displacement maps from any standard diffuse texture map!</p>

<a href='https://vertostudio.com/gamedev/?attachment_id=291' title='crazy bump in action'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-03-at-9.12.16-PM-150x150.png" class="attachment-thumbnail" alt="crazy bump in action" title="crazy bump in action" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=292' title='Screen Shot 2014-12-03 at 9.05.20 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/12/Screen-Shot-2014-12-03-at-9.05.20-PM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-12-03 at 9.05.20 PM" title="Screen Shot 2014-12-03 at 9.05.20 PM" /></a>

<h2> WebGL demo</h2>
<p>For those who want to see the shader effect in action, I got a <a title="WebGL demo" href="http://vertostudio.com/pub/webgl/parallax/" target="_blank">WebGL demo</a> which runs on chrome and safari (firefox and IE don&#8217;t work).</p>
<p>It&#8217;s an expensive effect however, so I&#8217;m not sure yet if I can work it in for Driveby Gangster.  Either way, it&#8217;ll definitely be a nice little addition to the shader arsenal.  If I do actually put it into use, I hope to eventually add self-shadowing effects as well.</p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=290</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swift is fired</title>
		<link>https://vertostudio.com/gamedev/?p=254</link>
		<comments>https://vertostudio.com/gamedev/?p=254#comments</comments>
		<pubDate>Sat, 08 Nov 2014 23:03:15 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=254</guid>
		<description><![CDATA[So it had to happen&#8230; The more I work on this game the more I realize that I kind of like it&#8230; and that means there&#8217;s certain things need to change.  For starters, it&#8217;s definitely running longer than I expected it to (the original two-week estimate now sounds pretty &#8220;out there&#8221;).  For this reason, I [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 1rem; line-height: 1.714285714;">So it had to happen&#8230;</span></p>
<p>The more I work on this game the more I realize that I kind of like it&#8230; and that means there&#8217;s certain things need to change.  For starters, it&#8217;s definitely running longer than I expected it to (the original two-week estimate now sounds pretty &#8220;out there&#8221;).  For this reason, I need to shave down on the programming time. It has become pretty obvious to me that for every 1/2 hour that I spend writing code in swift for this game, 10 minutes of it is spent screwing around with the language and that is friction then I can no longer accept.  So I&#8217;m dropping swift for this game.</p>
<p>Since I&#8217;m switching languages and I&#8217;m going to be rewriting some code, I decided to open things up quite a bit and go back to C++.  It&#8217;s going to cost me roughly a week or two to rewrite things, but the benefit is going to be huge.  For starters, since this game depended on my very large and very complicated Objective-C graphics engine (Verto Studio), I now have the &#8220;opportunity&#8221; to continue rewriting all of this code in C++ (this was originally started on my github as VGLPP).  This means that this game and any other game that I want to use with the ported engine will be able to run on ANY platform including windows.  This is a major win for me since making a game for OSX/iOS only isn&#8217;t really that smart.  I&#8217;ve been wanting to do this for awhile and my frustrations with swift finally pushed me to do it.</p>
<p>So that&#8217;s what I&#8217;m going to be up to for awhile.  As I&#8217;ve mentioned on twitter, I&#8217;ll be streaming roughly twice a week on my twitch channel for anyone who is curiuos about the process and wants to follow along.  http://twitch.tv/vertostudio3d</p>
<p>I&#8217;ve already gotten quite a bit done and have surpassed my biggest barrier for doing this which was finding a reasonable way to parse verto studio files (spat out by NSKeyedArchiver/NSCoder objective-c mechanisms) in a portable way in C++.  TinyXML and a little of reverse engineering did the trick.</p>
<p>With respect to all of the &#8220;benefits&#8221; swift and Objective C were providing over a lower level language such as C++, I&#8217;ve essentially found a reasonable replacement for each one thanks to C++11&#8242;s new features.</p>
<h2>Basic Outline of Porting to C++<span style="line-height: 1.714285714; font-size: 1rem;"> </span></h2>
<table>
<tbody>
<tr>
<td>Swift/Objc</td>
<td>C++11</td>
</tr>
<tr>
<td>Strong Pointer (Reference Counting)</td>
<td>std::shared_ptr</td>
</tr>
<tr>
<td>Weak Pointer (Reference Counting)</td>
<td>std::weak_ptr</td>
</tr>
<tr>
<td>Blocks &amp; Closures</td>
<td>Lambda Functions</td>
</tr>
<tr>
<td>Protocols</td>
<td>Multiple Inheritence</td>
</tr>
<tr>
<td>Dynamic Typechecking via &#8220;isKindOfClass&#8221;</td>
<td>C++ typeid / RTTI</td>
</tr>
</tbody>
</table>
<p>So you get the idea, nothings really out of reach with C++. It&#8217;s still more annoying and I&#8217;m not 100% thrilled with dealing with things like templates, header files and verbose syntax again, but I&#8217;ll take it over an unstable language any day.</p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=254</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lots of code&#8230; lots</title>
		<link>https://vertostudio.com/gamedev/?p=247</link>
		<comments>https://vertostudio.com/gamedev/?p=247#comments</comments>
		<pubDate>Sat, 01 Nov 2014 23:34:56 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Dev logs]]></category>
		<category><![CDATA[Driveby Gangster]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=247</guid>
		<description><![CDATA[I&#8217;ve been less active on here because I&#8217;ve been writing quite a bit of code.  Like I said previously, I&#8217;m in the main swing of developing this game.  This is where things start to get crazy.  Namely, I&#8217;ve added quite a bit of classes to the project to handle everything from basic collisions and 3D [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been less active on here because I&#8217;ve been writing quite a bit of code.  Like I said previously, I&#8217;m in the main swing of developing this game.  This is where things start to get crazy.  Namely, I&#8217;ve added quite a bit of classes to the project to handle everything from basic collisions and 3D math extensions to generating and displaying 3D text on the screen.  I feel like (despite my previous post regarding Swift) things are keeping organized quite well and I haven&#8217;t strayed <em>too </em>far from my original architecture plan.  I&#8217;ve spent most of my time working on the <span style="line-height: 1.714285714; font-size: 1rem;">PracticeGameStateRunner class which runs the &#8220;Target Practice&#8221; initial level in the game.  This mini level serves as a point for the player to learn the very simple controls and game mechanics of the game&#8230; and it&#8217;s serving excellently as a sandbox for me to test these all during development as well.  </span></p>
<p>I&#8217;ll cover just a few pieces of code today to show the changes that I&#8217;ve made regarding the game State Runner protocol, and some cool stuff that I&#8217;ve been able to do with &#8220;smart&#8221; enums in swift.  There&#8217;s a lot more I can talk about that I don&#8217;t want this post to go on forever.</p>
<h2>Some Code</h2>
<p>State runner protocol.  Now I have some stuff in there to quickly respond to &#8220;game controller&#8221; and mouse events, all stemming from the game loop class.</p><pre class="crayon-plain-tag">//
//  GameState.swift
//  Gangster Driveby
//
//  Created by Mike Farrell on 10/15/14.
//  Copyright (c) 2014 Mike Farrell. All rights reserved.
//

import Foundation

protocol GameStateRunner
{
  func processLogic(#delta: Float)
  func processEvents()
  func render()

  func keyDown(keysym: SDL_Keycode)
  func keyUp(keysym: SDL_Keycode)
  func mouseMoved(event: SDL_MouseMotionEvent)
  func mouseDown(event: SDL_MouseButtonEvent)
  func mouseUp(event: SDL_MouseButtonEvent)

  func joystickDown(controllerButton button: GangsterGameLoop.ControllerButton)
  func joystickUp(controllerButton button: GangsterGameLoop.ControllerButton)
}</pre><p>Mesh Line Collider &#8211; a badly needed construct to determine whether or not a bullet-trajectory would intersect a polygonal mesh in the scene or not (and if so where).  I tried porting this over to ObjC before swift and it was a nitemare.  Swift&#8217;s version is definitely simpler thanks to operator overloading and multiple return values.  Note the unsafe pointer craziness in swift which is considerably easier to deal with in C.  (ported from http://geomalgorithms.com/a06-_intersect-2.html)</p><pre class="crayon-plain-tag">//
//  MeshLineCollider.swift
//  Gangster Driveby
//
//  Created by Mike Farrell on 10/30/14.
//  Copyright (c) 2014 Mike Farrell. All rights reserved.
//

import Foundation

class MeshLineCollider
{
  weak var entity: Entity!
  var boundingBoxCollider: MeshLineCollider?
  let verts: GrowableArray
  let faceIndices: GrowableArray

  init(meshEntity: Entity)
  {
    entity = meshEntity

    verts = entity.editVerts.mutableCopy() as GrowableArray
    entity.applyTransformToVerts(verts, andNorms: nil)

    let s: SubMesh = entity.submeshes[0] as SubMesh
    faceIndices = s.editInds.mutableCopy() as GrowableArray

    generateBoundingBoxCollider()
  }

  init(verts: GrowableArray, faces: GrowableArray)
  {
    self.verts = verts
    faceIndices = faces
    boundingBoxCollider = nil
  }

  convenience init(var verts: [float3], var faces: [uint3])
  {
    let vBuffer = UnsafeMutableBufferPointer(start: &amp;verts, count: verts.count)
    let vgArray = GrowableArray(data: vBuffer.baseAddress, ofSize: Int32(verts.count), andElementSize:Int32(sizeof(float3)), andExtraCapacity: 0)

    let fBuffer =  UnsafeMutableBufferPointer&lt;uint3&gt;(start: &amp;faces, count: faces.count)
    let fArray = GrowableArray(data: fBuffer.baseAddress, ofSize: Int32(faces.count*3), andElementSize:Int32(sizeof(uint)), andExtraCapacity: 0)

    self.init(verts: vgArray, faces: fArray)
  }

  private final func split(quad: uint4) -&gt; (uint3, uint3)
  {
    return (uint3(x: quad.x, y: quad.y, z: quad.w), uint3(x: quad.w, y: quad.y, z: quad.z))
  }

  private func generateBoundingBoxCollider()
  {
    var bbSize = float3.zero
    entity.getBBox(&amp;bbSize)

    let box = Box3D(dimensions: bbSize, andPosition: float3.zero)
    let boxVerts = box.generateVertices()
    entity.applyTransformToVerts(boxVerts, andNorms: nil)

    var boxInds: [uint4] = [ ]

    boxInds.append(uint4(x: 0, y: 1, z: 2, w: 3))
    boxInds.append(uint4(x: 0, y: 4, z: 5, w: 1))
    boxInds.append(uint4(x: 2, y: 6, z: 7, w: 3))
    boxInds.append(uint4(x: 3, y: 7, z: 4, w: 0))
    boxInds.append(uint4(x: 1, y: 5, z: 6, w: 2))
    boxInds.append(uint4(x: 4, y: 5, z: 6, w: 7))

    var triangles: [uint3] = [ ]
    for quad in boxInds
    {
      let spl = split(quad)
      triangles.append(spl.0)
      triangles.append(spl.1)
    }

    let fBuffer = UnsafeMutableBufferPointer&lt;uint3&gt;(start: &amp;triangles, count: triangles.count)
    let fArray = GrowableArray(data: fBuffer.baseAddress, ofSize: Int32(triangles.count*3), andElementSize:Int32(sizeof(uint)), andExtraCapacity: 0)

    boundingBoxCollider = MeshLineCollider(verts: boxVerts, faces: fArray)
  }

  func debugRender()
  {
    if boundingBoxCollider != nil
    {
      boundingBoxCollider?.debugRender()
      return
    }

    glDisable(GL_CULL_FACE.e)
    glDepthRange(0.0, 0.9999);

    let vgl = VertoGLStateMachine.currentMachine()
    vgl.vertexPointerOfSize(3, type: GL_FLOAT.e, stride: 0, pointer: verts.data, bufferLength: GLsizeiptr(Int32(sizeof(float3))*verts.count))

    vgl.setPrimaryColor(makeFloat4(0.2, 0.2, 0.2, 1))
    vgl.prepareToDraw()

    vgl.drawElementsInMode(GL_TRIANGLES.e, andCount: GLsizei(faceIndices.count), type: GL_UNSIGNED_INT.e, indexPointer: faceIndices.data,
      bufferLength: GLsizeiptr(Int32(sizeof(uint))*faceIndices.count))

    glDepthRange(0.0, 1.0);
  }

  //MARK: - Collision with line

  func rayIntersection(rayOrigin ro: float3, rayDirection rd: float3, inout intersectionPoint: float3) -&gt; IntersectionPoint?
  {
    if let bbox = boundingBoxCollider
    {
      if bbox.rayIntersection(rayOrigin: ro, rayDirection: rd, intersectionPoint: &amp;intersectionPoint) == nil
      {
        return nil
      }
    }

    let pointSet = IntersectionPointSet()

    for i in 0..&lt;faceIndices.count/3
    {
      let result = triangleIntersection(triangleIndex: Int(i), rayOrigin: ro, rayDirection: rd, intersectionPoint: &amp;intersectionPoint)
      if result.0
      {
        pointSet.add(IntersectionPoint(distance: result.1, point: intersectionPoint))
      }
    }

    if !pointSet.empty()
    {
      return pointSet.nearest()
    }

    return nil
  }

  private func triangleIntersection(triangleIndex i: Int, rayOrigin ro: float3, rayDirection rd: float3, inout intersectionPoint ip: float3) -&gt; (Bool, Float)
  {
    let smallNum: Float = 0.00000001
    var u: float3, v: float3, n: float3       // triangle vectors
    var dir: float3, w0: float3, w: float3    // ray vectors
    var r: Float, a: Float, b: Float          // params to calc ray-plane intersect

    let vData = UnsafeMutableBufferPointer&lt;float3&gt;(start: UnsafeMutablePointer&lt;float3&gt;(verts.data), count: Int(verts.count))
    let iData = UnsafeMutableBufferPointer&lt;uint3&gt;(start: UnsafeMutablePointer&lt;uint3&gt;(faceIndices.data), count: Int(faceIndices.count/3))

    let v0 = vData[Int(iData[i].x)], v1 = vData[Int(iData[i].y)], v2 = vData[Int(iData[i].z)]

    // get triangle edge vectors and plane normal
    u = v1 - v0
    v = v2 - v0
    n = u.cross(v)              // cross product
    if n == float3.zero          // triangle is degenerate
    {
      // do not deal with this case
      return (false, 0)
    }

    dir = rd
    w0 = ro - v0
    a = -n.dot(w0)
    b = n.dot(dir)
    if fabsf(b) &lt; smallNum
    {
      return (false, 0)
    }

    // get intersect point of ray with triangle plane
    r = a / b
    if r &lt; 0.0                    // ray goes away from triangle
    {
      return (false, 0)                   // =&gt; no intersect
    }
    // for a segment, also test if (r &gt; 1.0) =&gt; no intersect

    // intersect point of ray and plane
    ip = ro + dir*r

    // is I inside T?
    var uu: Float, uv: Float, vv: Float, wu: Float, wv: Float, D: Float
    uu = u.dot(u)
    uv = u.dot(v)
    vv = v.dot(v)
    w = ip - v0
    wu = w.dot(u)
    wv = w.dot(v)
    D = uv * uv - uu * vv

    // get and test parametric coords
    var s: Float, t: Float
    s = (uv * wv - vv * wu) / D
    if s &lt; 0.0 || s &gt; 1.0         // I is outside T
    {
      return (false, 0)
    }
    t = (uv * wu - uu * wv) / D
    if t &lt; 0.0 || (s + t) &gt; 1.0  // I is outside T
    {
      return (false, 0)
    }

    return (true, r)                       // I is in T
  }

}</pre><p>Cool little WIP controller button enum nested in the game loop class</p><pre class="crayon-plain-tag">enum ControllerButton: Int
  {
    case Shoot = 9
    case Aim = 8

    var name: String
    {
      let buttonNames: [ControllerButton: String] = [ .Shoot: "Shoot", .Aim: "Aim" ]

      return buttonNames[self]!
    }

    var obj: NSNumber
    {
      return NSNumber(integer: self.rawValue)
    }
  }</pre><p>&nbsp;</p>
<h2>Some Screens</h2>

<a href='https://vertostudio.com/gamedev/?attachment_id=248' title='Screen Shot 2014-10-30 at 9.26.06 PM'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/11/Screen-Shot-2014-10-30-at-9.26.06-PM-150x150.jpg" class="attachment-thumbnail" alt="Screen Shot 2014-10-30 at 9.26.06 PM" title="Screen Shot 2014-10-30 at 9.26.06 PM" /></a>
<a href='https://vertostudio.com/gamedev/?attachment_id=249' title='pew'><img width="150" height="150" src="http://vertostudio.com/gamedev/wp-content/uploads/2014/11/pew-150x150.gif" class="attachment-thumbnail" alt="pew" title="pew" /></a>

]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=247</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More about swift&#8230;</title>
		<link>https://vertostudio.com/gamedev/?p=244</link>
		<comments>https://vertostudio.com/gamedev/?p=244#comments</comments>
		<pubDate>Thu, 30 Oct 2014 05:44:51 +0000</pubDate>
		<dc:creator>Michael Farrell</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://vertostudio.com/gamedev/?p=244</guid>
		<description><![CDATA[Okay so when you use a language daily, you begin to really pick up some opinions and feelings about it that tend to really stick with you.  With swift, I definitely have some of those. Let me focus on just two&#8230; of the things that really irk me about the freaking swift language. Number one &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Okay so when you use a language daily, you begin to really pick up some opinions and feelings about it that tend to really stick with you.  With swift, I definitely have some of those.</p>
<p>Let me focus on just two&#8230; of the things that really <strong>irk</strong> me about the freaking swift language.</p>
<h1>Number one &#8211; Casting</h1>
<p>This is my number one gripe with the swift language.  The compiler just wont shut the hell up and do what I was intending.  Now I know that apple has designed the language to absolutely not allow any implicit casting, and I understand their reasons behind this, but man, you&#8217;ve taken one benefit, and added 10-fold the costs.  Requiring explicit casting takes code in swift that <em>should</em> look extremely simple and adds about 3-4 explicit constructor-casts to it causing the code to display as bloated, confusing ill-intended crap.  What really bothers me is that this is the case even for <strong>upgrading </strong>types such as assigning a float into a double.  Another problem is that in the C world (all of the APIs that we work with on a daily basis) there might be 4 or 5 different integer types (int, Sint32, GLuint, etc) and multiple float types (GLfloat, float, CGFloat, etc).  Casting between these in C/C++/Objc is completely harmless 99% of the time and therefore done implicitly.  In swift, I have to pepper this code with constructor casts every, single, freaking time.</p>
<p>In C++, you could allow implicit types by overloading the constructor so that an expression such as int x = someFloat would be the same as a constructor for int that takes a float as an argument.  Swift badly needs this.</p>
<p>For now I&#8217;ve gotten to the point where I&#8217;ve added very simple computed properties to the various types in swift to at least make some of this casting more painless.  Just a few of those extensions are below.</p><pre class="crayon-plain-tag">//
//  CastingHelpers.swift
//  Gangster Driveby
//
//  Created by Mike Farrell on 10/27/14.
//  Copyright (c) 2014 Mike Farrell. All rights reserved.
//

import Foundation

//swift is driving me crazy
extension Int
{
  var f: Float
  {
    return Float(self)
  }
}

extension Int32
{
  var f: Float
  {
    return Float(self)
  }

  var e: GLenum
  {
    return GLenum(self)
  }

  /*func c&lt;T: IntegerLiteralConvertible&gt;() -&gt; T
  {
    return T(integerLiteral: i)
  }*/
}

extension Sint16
{
  var f: Float
  {
    return Float(self)
  }
}

extension UInt
{
  var f: Float
  {
    return Float(self)
  }
}

extension Double
{
  var f: Float
  {
    return Float(self)
  }
}

extension Float
{
  var cg: CGFloat
  {
    return CGFloat(self)
  }
}</pre><p>This makes it so that code that would have to look like</p><pre class="crayon-plain-tag">glDisable(GLEnum(GL_DEPTH_TEST))

let f: Float = 3.0
let f2 = f*Float(sin(0.5))</pre><p>Can be&#8230;</p><pre class="crayon-plain-tag">glDisable(GL_DEPTH_TEST.e)

let f = (3.0).f
let f2 = f*sin(0.5).f</pre><p>&nbsp;</p>
<h1>Number two &#8211; Stability (source kit crashes)</h1>
<p>Google swift and &#8220;sourcekit&#8221; and you&#8217;ll see, this is nowhere NEAR an actual finished language.  Apple has rushed this out the door and I&#8217;m honestly sorry that I&#8217;ve decided to use it to make a game.  Imagine typing code and then every half hour you get 30 seconds of a frozen spinner before &#8221;sourcekit crashed&#8221; appears in your IDE.  If you&#8217;re lucky, it&#8217;ll stop for another 5 minutes, but if you aren&#8217;<span style="line-height: 1.714285714; font-size: 1rem;">t it&#8217;ll happen in a loop and you have to go delete your &#8220;ModuleCache&#8221; dir and quit and restart xcode and start working again&#8230;. until about an hour later.  This is NOT how I prefer to work.  I like the &#8220;idea&#8221; of swift, but its just way too unstable to use.  I&#8217;m going to finish this game with it, and then probably stay away from swift for any real projects for another year or so until apple fixes this extremely annoying stability problem.</span></p>
<p>&nbsp;</p>
<p>These two things may seem small, but they&#8217;re enough to waste 10 m<span style="line-height: 1.714285714; font-size: 1rem;">inutes of time for every hour I work on this game, and for me, that&#8217;s just completely unacceptable.  </span></p>
]]></content:encoded>
			<wfw:commentRss>https://vertostudio.com/gamedev/?feed=rss2&#038;p=244</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
