Monday, April 27, 2009

Sprites - Poor man's 3D rendering

Modeling stuff like Trees, explosions in 3D is excessively complex. The number of polygons used to render them make it prohibitively expensive even for high end cards. So most games take shortcuts to create approximations, which are nearly as good, but extremely cheap. One such concept is sprites...

The basic idea behind sprite is that, no matter how much u model, the final image appears on the 2D screen. So technically you can get away with just rendering the 2D view of it. So how do you do this? simple, just draw one polygon with the 2D view of the object, and then make sure that the Polygon is *ALWAYS* facing the user. so no matter which place in the 3D world the user looks at from, he sees the same side of the polygon facing him. This gives a reasonable illusion of 3D for a very very cheap CPU and RAM cost! Obviously this works best only for objects which do not require much interaction with the user, and are mere eye candy.

Adrian now implements sprites, and litters the map with some trees here and there. Another thing is that although a tree looks complex, we just render a single rectangle. We take an image of a tree, in which parts of it not inside are made transparent (instead of some color), and saved as a TGA file (JPEGs etc do NOT support transparency). Now we map the image to the rectangle, and it gives the illusion of a tree.

For the OpenGL coders, to do this you have to enable transparency use the following code

    glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


However if 2 or more transparent objects overlap, then the results will result in the one drawn first getting hidden by the later drawn one (even IF the first object is in front of the other!). To overcome this order the objects need to be distance sorted.

If distance sorting is not an option, then my friend Vamsi suggested that you can enable Alpha Test to make sure that before overwriting the buffer, OpenGL compares the existing alpha value and if lesser does not overwrite the existing one.

    glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5);

1 comment:

  1. I got good information about 3D Rendering on your blog and choose your suitable services about 3D Rendering Company.

    ReplyDelete