Jump to content
  • Advertisement
Sign in to follow this  
TravisGesslein

OpenGL Problems with drawing shadows (2D)

This topic is 2619 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.

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.

Share this post


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

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!