Problems with drawing shadows (2D)

Started by
2 comments, last by raigan 12 years, 11 months ago
Hi.

The following function (java) calculates the shadows which a shape casts depending on the position of the lights. It isn't required to understand my problem, but I'll post it anyways just for completeness:



public Shape calculateShadow(Drawable drawable)
{
Shape shadow = new Shape();
shadow.setColor(new Color(0,0,0));

HashMap<Integer, Vector2f> required_vertices = new HashMap<Integer, Vector2f>();

Vector<Vector2f> edge_normals = drawable.getEdgeNormals();
Vector<Vector2f> vertices = drawable.getVertices();

int size = vertices.size();

Vector2f to_light = new Vector2f(0.0f,0.0f);
Vector2f vertex;
for(int i=0; i<size; ++i)
{
vertex = vertices.get(i);
to_light.x = position.x - vertex.x;
to_light.y = position.y - vertex.y;

if(edge_normals.get(i).dotProduct(to_light) <= 0.0f)
{
required_vertices.put(i, vertex);
if(i+1 <size)
{
required_vertices.put(i+1, vertices.get(i+1));
}
else
{
required_vertices.put(0, vertices.get(0));
}
}
}

Collection<Vector2f> c = required_vertices.values();
Iterator<Vector2f> it = c.iterator();
while(it.hasNext())
{
vertex = it.next();
shadow.addPoint(vertex);
shadow.addPoint(Vector2f.multiply(Vector2f.subtract(vertex, this.position), 1000.0f));
}
for(int i=0; i<shadow.getVertices().size();++i)
{
System.out.println(i + " " + shadow.getVertices().get(i));
}

return shadow;
}


This has nothing to do with my problem, but to sum it up, the function goes through each edge of a given shape and determines if it's facing away from the light or towards it. If it is indeed facing away from the light, the algorithm adds both vertices attached to the edge to the shadow's shape, as well as two additional vertices which are just projections from the light to the corner vertices over a long distance (so that they're certainly offscreen). I'm using a HashMap in there to guarantee that all necessary vertices only appear once in the final shadow shape (simply makes drawing faster).

Now my problem: The algorithm adds the vertices to the shape simply in the order of their appearance of the Vector<Vector2f> that is returned by the shape's function getEdgeVertices. Which means that if it detects that the corner vertices 0, 2 and 3 (as well as their projections) are necessary to draw the shadow, they will be saved in this order: vertex_0, projectionfromvertex_0, vertex_2, projectionfromvertex_2, vertex_3, projectionfromvertex_3

Now I'm using GL_TRIANGLE_STRIP to draw the shadow, but because of the way it's drawn this way, it sometimes (when the light is to the right or below the shape) draws the shadow over my shape which is obviously not supposed to happen.

To give you a better understanding of what I mean, here's a super fancy drawing:

u7keX.jpg


The yellow dot is the light source, the red rectangle my shape that is casting the shadow, and the green dots are the vertices that are added to the shadow's shape. The numbers next to them represent the order in which they are saved inside the Vector<Vector2f> of the shape.

Now you all know how GL_TRIANGLE_STRIP works: It takes the array and then draws a triangle from vertices 0,1,2 - then from vertices 1,2,3 - then 2,3,4 etc.

In the above constellation this will result in a perfectly fine shadow, since drawing 0,1,2 then 1,2,3 then 2,3,4 etc. will create a shape by opengl which is drawn outside of the red rectangle (sorry for crappy quality, MS Paint :D )

XC6gv.jpg:

But now in this setup, where the light is to the right of the rectangle:
qKQce.jpg


It will put the vertices into the shadow's shape in the same order. Now, when OpenGL renders the thing, it draws triangle 0,1,2 and 1,2,3 (which are fine), but drawing triangles 2,3,4 and a couple of others will result in drawing the shape over the rectangle, which is bad:

Y04jk.jpg


How can I fix this problem? One thing you have to take for granted, which I can't explain to you right now: I can't simply draw the shadows first, for various reasons.
Advertisement
don't use a triangle strip

don't use a triangle strip



what else then? none of the ones that i tried produced different results

A simpler solution would just be to draw the whole shadow (i.e including the part that covers the object) first, THEN draw the object over top of the shadow. As a bonus this will ensure that the shadow shape is convex so you can just use a trifan to draw it.

This topic is closed to new replies.

Advertisement