Jump to content
  • Advertisement
Sign in to follow this  
x452Alba

Generating Shadow Volume + zFail + MD2

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i  ].vertex[0] = triangles[t1].vertex[0];
      volume[i  ].vertex[1] = triangles[t1].vertex[1];
      volume[i++].vertex[2] = triangles[t1].vertex[2];

      for (int t2 = (t1 + 1); t2 < nTriangles; ++t2)
      {
         for (int v1 = 0; v1 < 3; ++v1)
         {
            if (bEdgeUsed[t1 * 3 + v1] == false)
            {
               for (int v2 = 0; v2 < 3; ++v2)
               {
                  if (bEdgeUsed[t2 * 3 + v2] == false)
                  {
                     int v1_next = (v1 + 1) % 3; /* Wrap around. */
                     int v2_next = (v2 + 1) % 3; /* Wrap around. */

                     normal_vertex_t pEdge1[2], pEdge2[2];
                     pEdge1[0] = vertices[triangles[t1].vertex[v1]];
                     pEdge1[1] = vertices[triangles[t1].vertex[v1_next]];
                     pEdge2[0] = vertices[triangles[t2].vertex[v2]];
                     pEdge2[1] = vertices[triangles[t2].vertex[v2_next]];

                     if (pEdge1[1].v == pEdge2[0].v && pEdge1[0].v == pEdge2[1].v)
                     {
                     /* Build a quad. */
                        volume[i  ].vertex[0] = triangles[t1].vertex[v1]; /* Triangle 1. */
                        volume[i  ].vertex[1] = triangles[t2].vertex[v2_next];
                        volume[i++].vertex[2] = triangles[t2].vertex[v2];
                        volume[i  ].vertex[0] = triangles[t2].vertex[v2]; /* Triangle 2. */
                        volume[i  ].vertex[1] = triangles[t1].vertex[v1_next];
                        volume[i++].vertex[2] = triangles[t1].vertex[v1];

                        bEdgeUsed[t1 * 3 + v1] = true;
                        bEdgeUsed[t2 * 3 + v2] = true;
                        break;
                     }
                  }
               }
            }
         }
      }                 
   }

   delete [] bEdgeUsed;

   nVolume = i;

   return;
}


And here is how I am rendering the shadow volume:
void Model_md2::RenderShadowVolume()
{
   normal_vertex_t vertex_list[MAX_MD2_VERTS];
   Interpolate(vertex_list);

   glBegin(GL_TRIANGLES);
   for (int i = 0; i < nVolume; ++i)
   {
      for (int j = 0; j < 3; ++j)
      {
         glNormal3f(vertex_list[volume.vertex[j]].n[x], vertex_list[volume.vertex[j]].n[y], vertex_list[volume.vertex[j]].n[z]);
         glVertex3f(vertex_list[volume.vertex[j]].v[x], vertex_list[volume.vertex[j]].v[y], vertex_list[volume.vertex[j]].v[z]);
      }
   }
   glEnd();

   return;
}


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 only one 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!