Original Post
Hi, I''m trying to implement 2D shadows based on Orangy Tang''s 2D shadow article, and I''ve implemented most of things right, except for the actual shadow generation. his article was good, but I''m somewhat of a noob to vector calculations, and I''ve hacked together an implementation that doesn''t work the way it should. Here is my shadow creation algorithm/code:
The part you should focus on is in between the second FOR loop...
Sorry, it may seem like a ton of code, but its what i have.
Thank you so very much in advance,
Cipher3D
void cLightManager::DrawObjectShadows(cLight* light)
{
if (!light)
return;
for (int c=0;c<m_vertexList.size();c++)
{
int numEdges = sizeof(m_vertexList[c]);
vector<typVertex> shadowGeometry;
bool *frontfacing = new bool[numEdges];
for (int g=0;g<numEdges;g++)
frontfacing[g] = false;
for (g=0;g<numEdges;g++)
{
if (light->x == 50)
{
int body;
body++;
}
int prev = (g == 0 ? numEdges-1 : g-1);
//for each edge
int x,y;
x = m_vertexList[c][g].x-m_vertexList[c][prev].x;
y = m_vertexList[c][g].y-m_vertexList[c][prev].y;
if (light->x > m_vertexList[c][g].x)
{
x = -x;
}
if (light->y > m_vertexList[c][g].y)
{
y = -y;
} //end if
float dot = (x*light->x)+(y*light->y);
//check whats the result of dot
if (dot < 0)
{
float vertx,verty,projx,projy;
//boundary = true;
frontfacing[g] = true;
//then we add to shadow geometry
shadowGeometry.push_back(m_vertexList[c][prev]);
projx = m_vertexList[c][prev].x-light->x;
projy = m_vertexList[c][prev].y-light->y;
vertx = m_vertexList[c][prev].x;
verty = m_vertexList[c][prev].y;
projx *= light->size/100.0f; projy *= light->size/100.0f;
vertx+=projx;
verty+=projy;
typVertex s = {vertx,verty};
shadowGeometry.push_back(s);
} //end if
else
{
if (frontfacing[prev])
{
shadowGeometry.push_back(m_vertexList[c][prev]);
} //end if
} //end else
} //done
if (frontfacing)
delete frontfacing;
//NOW draw the frickin shadow
glBlendFunc(GL_ZERO,GL_ZERO);
glBegin(GL_TRIANGLE_STRIP);
glColor4f(0,0,0,1.0f);
for (int ang = 0;ang<shadowGeometry.size();ang++)
{
int x = shadowGeometry[ang].x;
int y = shadowGeometry[ang].y;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x > g_pVideo->GetWidth())
x = g_pVideo->GetWidth();
if (y > g_pVideo->GetHeight())
y = g_pVideo->GetHeight();
glVertex2f(x,y);
}
glEnd();
shadowGeometry.empty();
} //end for
} //end


