Generating Shadow Volume + zFail + MD2

Started by
0 comments, last by x452Alba 17 years, 2 months ago
Hi all, I'm using the MD2 model format for animation support (and 'cos there are a lot of free models 'out there') and I'm tackling trying to generate and render a shadow volume using the popular z Fail technique. I'm also using a shader (GLSL) to extrude the volume (this part is correct and successfully working). After experimenting for 2 solid days I cannot produce acceptable results as you can clearly see. I'm not sure what code snippets to paste here that would be the most helpful (and I don't want to just dump the entire source and say "what's wrong"). So here is my GenerateShadowVolume function:

enum {x, y, z, w};

struct vertex_t
{
   float m[3];

   float& operator[](int i)
   { 
      return m;
   }

   float operator[](int i) const
   { 
      return m;
   }
};

struct normal_vertex_t
{
// GL_N3F_V3F
   vertex_t n;
   vertex_t v;
};

...

bool operator == (const vertex_t& a, const vertex_t& b)
{
   return fabs(a[x] - b[x]) < EPSILON && fabs(a[y] - b[y]) < EPSILON && fabs(a[z] - b[z]) < EPSILON;
}

...

struct triangle_t
{
   unsigned short vertex[3];
   unsigned short st[3];
};

...

void Model_md2::GenerateShadowVolume()
{
   bool* bEdgeUsed = NULL;

   int i = 0;

   bEdgeUsed = new bool[3 * nTriangles];
   for (int j = 0; j < 3 * nTriangles; bEdgeUsed[j++] = false);
   volume = new triangle_t[4 * nTriangles];

   for (int t1 = 0; t1 < nTriangles; ++t1)
   {
      volume.vertex[<span class="cpp-number">0</span>] = triangles[t1].vertex[<span class="cpp-number">0</span>];
      volume.vertex[<span class="cpp-number">1</span>] = triangles[t1].vertex[<span class="cpp-number">1</span>];
      volume[i++].vertex[<span class="cpp-number">2</span>] = triangles[t1].vertex[<span class="cpp-number">2</span>];

      <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> t2 = (t1 + <span class="cpp-number">1</span>); t2 &lt; nTriangles; ++t2)
      {
         <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> v1 = <span class="cpp-number">0</span>; v1 &lt; <span class="cpp-number">3</span>; ++v1)
         {
            <span class="cpp-keyword">if</span> (bEdgeUsed[t1 * <span class="cpp-number">3</span> + v1] == <span class="cpp-keyword">false</span>)
            {
               <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> v2 = <span class="cpp-number">0</span>; v2 &lt; <span class="cpp-number">3</span>; ++v2)
               {
                  <span class="cpp-keyword">if</span> (bEdgeUsed[t2 * <span class="cpp-number">3</span> + v2] == <span class="cpp-keyword">false</span>)
                  {
                     <span class="cpp-keyword">int</span> v1_next = (v1 + <span class="cpp-number">1</span>) % <span class="cpp-number">3</span>; <span class="cpp-comment">/* Wrap around. */</span>
                     <span class="cpp-keyword">int</span> v2_next = (v2 + <span class="cpp-number">1</span>) % <span class="cpp-number">3</span>; <span class="cpp-comment">/* Wrap around. */</span>

                     normal_vertex_t pEdge1[<span class="cpp-number">2</span>], pEdge2[<span class="cpp-number">2</span>];
                     pEdge1[<span class="cpp-number">0</span>] = vertices[triangles[t1].vertex[v1]];
                     pEdge1[<span class="cpp-number">1</span>] = vertices[triangles[t1].vertex[v1_next]];
                     pEdge2[<span class="cpp-number">0</span>] = vertices[triangles[t2].vertex[v2]];
                     pEdge2[<span class="cpp-number">1</span>] = vertices[triangles[t2].vertex[v2_next]];

                     <span class="cpp-keyword">if</span> (pEdge1[<span class="cpp-number">1</span>].v == pEdge2[<span class="cpp-number">0</span>].v &amp;&amp; pEdge1[<span class="cpp-number">0</span>].v == pEdge2[<span class="cpp-number">1</span>].v)
                     {
                     <span class="cpp-comment">/* Build a quad. */</span>
                        volume.vertex[<span class="cpp-number">0</span>] = triangles[t1].vertex[v1]; <span class="cpp-comment">/* Triangle 1. */</span>
                        volume.vertex[<span class="cpp-number">1</span>] = triangles[t2].vertex[v2_next];
                        volume[i++].vertex[<span class="cpp-number">2</span>] = triangles[t2].vertex[v2];
                        volume.vertex[<span class="cpp-number">0</span>] = triangles[t2].vertex[v2]; <span class="cpp-comment">/* Triangle 2. */</span>
                        volume.vertex[<span class="cpp-number">1</span>] = triangles[t1].vertex[v1_next];
                        volume[i++].vertex[<span class="cpp-number">2</span>] = triangles[t1].vertex[v1];

                        bEdgeUsed[t1 * <span class="cpp-number">3</span> + v1] = <span class="cpp-keyword">true</span>;
                        bEdgeUsed[t2 * <span class="cpp-number">3</span> + v2] = <span class="cpp-keyword">true</span>;
                        <span class="cpp-keyword">break</span>;
                     }
                  }
               }
            }
         }
      }                 
   }

   <span class="cpp-keyword">delete</span> [] bEdgeUsed;

   nVolume = i;

   <span class="cpp-keyword">return</span>;
}


</pre></div><!–ENDSCRIPT–>

And here is how I am rendering the shadow volume:

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">void</span> Model_md2::RenderShadowVolume()
{
   normal_vertex_t vertex_list[MAX_MD2_VERTS];
   Interpolate(vertex_list);

   glBegin(GL_TRIANGLES);
   <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; nVolume; ++i)
   {
      <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; <span class="cpp-number">3</span>; ++j)
      {
         glNormal3f(vertex_list[volume<span style="font-weight:bold;">.vertex[j]].n[x], vertex_list[volume<span style="font-weight:bold;">.vertex[j]].n[y], vertex_list[volume<span style="font-weight:bold;">.vertex[j]].n[z]);
         glVertex3f(vertex_list[volume<span style="font-weight:bold;">.vertex[j]].v[x], vertex_list[volume<span style="font-weight:bold;">.vertex[j]].v[y], vertex_list[volume<span style="font-weight:bold;">.vertex[j]].v[z]);
      }
   }
   glEnd();

   <span class="cpp-keyword">return</span>;
}


</pre></div><!–ENDSCRIPT–>

I also have stencil buffer code if it helps?

As you can tell, I'm stuck in 1997 and using the MD2 format for testing purposes. To be honest, I'm thinking the normal support in the MD2 format is poor and this lack of information (each vertex normal is &#111;nly &#111;ne of 162 possible values which are hardcoded) is causing the trouble!

Any hints as to what my issue is, and where I've f'ed up? Help!?

If more code will help, feel free to ask.

Thanks in advance.
Advertisement
Oh the joy, it's working like a gem. I looked at the normals again. I was using the smooth'd vertex normal and not the facelet normals.

Thanks anyway.

This topic is closed to new replies.

Advertisement