REAL-TIME SHADOWS IN OPENGL CAUSED BY THE PRESENCE OF MULTIPLE LIGHT SOURCES

Preliminary communication In modern computer graphics, the emphasis is on the details of the scene, and taking into account the improvements in hardware performances, it is not allowed to make compromises when it comes to the reality of scenes. Each reflection, shadow, rounded corner and transparency must be brought to perfection and presented in order to make a depicted scene more realistic. In one of the most widely used API for rendering 3D objects, OpenGL, there is nothing similar to a library for adding those phenomena that exist in reality. If the possibility of existence of multiple light sources is considered, rendering all these details becomes a real challenge. The aim of this paper is to provide a method for generating shadows in an efficient way, for the objects commonly used as components of complex 3D objects, in conditions of the presence of moving light sources.


Introduction
Bearing in mind its ability of creating virtual reality, computer graphics is widely used in designing and programming computer games, CAT software, the software dedicated to various types of process simulations, in a modeling of scientific phenomena and for many other purposes.The existence of shadows and reflections in a 3D scene is crucial in enhancing the scene realism and in providing important visual cues.
In recent years, many important contributions have been made in modeling shadows, as phenomena caused by the presence of light sources and reflections, which are the consequences of presence of objects made of special material, with the mirror effect.Shadows are created by a light source which is placed above the shadowed object.Those shadows are placed on the surface where the shadowed object is placed.But, if a point light source is placed below a shadowed object, made of the material which reflects light, the phenomenon thus created on such surface is a reflection.Their presence on the scene improves a visual perception.Shadows enhance the 3D impression in order to make better immersive 3D feeling.Also, they depict relations between objects on a scene, so the distance between two virtual objects can be sensed more clearly [1].
A shadow is an area where direct light from a light source cannot reach due to obstruction by an object.It occupies the whole surface behind an opaque object with light in front of it.The cross section of a shadow is a twodimensional silhouette or reversed projection of the object blocking the light.
Physically, a shadow consists of the umbra and the penumbra.The umbra is a result of the existence of an area created by a shadowed object that is not visible from any part of the light source.This is a dark part of the shadow.With their own area, light sources can make a penumbra, which is less dark than umbra.It happens when the area of a shadowed object can receive some, but not all light from the light source.As it cannot be the case with point light source (it does not have significant area), the shadows made in this way have only an umbra [2,3].
When a light source is added to the 3D scene drawn in OpenGL workspace, some changes on the objects in the scene and their material can be noticeable, but new creations caused by this introduction of lighting, such as shadows, are not made, which is not the case in reality.The current methods are faced with problems of high demands for rendering scenes; otherwise, if they do not require a lot of resources for implementation and rendering, they do not make realistic shadows.The methods vary in difficulty of their implementation, their performance, and the quality of their results.These qualities depend on two parameters: the complexity of the observed object and the complexity of the scene that is being shadowed [4÷8].
The approach described in this paper promotes facilitation of the formation of shadows, taking into account the events that occur when objects are exposed to light in reality.In this example, the light sources are point sources of light, and the surface, where the light rays create the shadows, is flat.These terms are used in complex scenes, where rendering speed is important, but we use them to demonstrate our shading method and to simplify its understanding.
This paper presents a new method for the formation of shadows in the case of two 2D primitives (circle and triangle) and 3D object (a ball and loads).The aim is to show that the shadow can be drawn if we know the necessary number of important points of primitives.Also, apparently different forms have their common features that facilitate calculation of these characteristic points.Also, the dependence of the color of the shadow from the distance of the light source is displayed.In the end, it is shown that making shadow of a complex 3D object is related with 2D objects which are its basis.
This paper is organized as follows.The first section provides an overview of the state of art in this area, with the emphasis on their advantages and disadvantages of existing methods.The next chapter presents the mathematical basis and the pseudo code for the derivation of the formula used for finding characteristic points for drawing shadows, and their color.This is followed by the description of experiments, with an application created using derived mathematical basis, and the display of results.Finally, conclusions and trends in future work are given.

Related work
When it comes to displaying real phenomena using computer graphics high standards are set, since there are several techniques for creating shadows, but they are constantly improving.When OpenGL is concerned, the most important shading techniques are: projected shadows, shadow maps and shadow volumes.
The projected shadows are facile for implementation, but they are approximate to the shadows made in real circumstances.The idea is to project the shadowed object onto one of the axes.By moving the light source or the shadowed object, the form of a shadow will not change [5].
The shadow volume is built by the rays from the light source that cut off the corners in the item which casts a shadow, going ahead out of the scene.In this way, a polygonal surface which contains shaded or partially shaded items is created.This strategy uses the stencil buffer to calculate which portion of the object is within the shadow volume.Depending on whether the test depth passes, the stencil value for each pixel in the scene will increase if the limits for the shadow volume intersect on the way in, but reduce if they cut on the way out.
This method is struggling when shadowing complex objects, and its implementation is greatly onerous [1,6,7].
In order to create a shadow in a scene, the shadow map strategy uses depth buffer and texture projection.The scene is outlined from the light's point of view.As the objects in the scene are rendered, the depth buffer is updated.It produces the shadow map which is used for placing textures on the area in the shadow.The disadvantages of this strategy are aliased shadow edges and self-shadowing effects [8÷13].
The problem of existence of multiple light sources is considered in the literature [14,15,16], but there was not any solution that could be applied independently of number of light sources, or could be efficient and cheap at the same time.Also, the possibility of moving the light sources was not considered.
All the methods for implementing shadows have certain problems with rendering the scene.They all have their advantages and disadvantages.Usually, if they are facile to implement, then the reality of shadows is questionable.If shadows look like in reality, then their computing and drawing requires a lot of resources and time.Also, some of them have a problem when it comes to creating a complex object shadows.The aim of our method is to decrease these problems and to establish a single library which will make feasible the addition of a shadow after drawing primitives, independently of the number or the position of light sources.Also, complex objects consisting of 2D and 3D basic objects will have their shadow formed from the shadows of their components.

Calculating the shadows of the primitive objects
Our approach is based on the advent which occurs in reality, in case when the scene is illuminated by point source of light, which has the capability to move along the axis of the coordinate system [17].The benefit of rendering shadows in this manner is in the fact that it is enough to calculate the lowest number of points which are relevant for drawing shadows.Another advantage is the presence of similarities between shading primitive shapes.The sphere is made by rotation of a circle around one of the axes; the cone is made by rotation of a triangle.So, the invention of the method for forming the shadow for 2D objects makes simple producing the shadow for the 3D object.
The paper considers producing shadows for four objects (circle, sphere, triangle and cone), located away from the surface for a certain distance.The shadow is placed on the flat surface and is created by the point light source with the ability of movement (it can change its X and Y coordinates).
If the light source is near to the Y axis, the shadow area is enhanced, and vice versa.When the light source shifts away from the object in X axis, the shadows are also going in the reverse direction to the motion of the light source.
Some of computation parameters are taken as constants, due to a great number of limitation of 3D system exposure on 2D paper, and they are: The light source is always above shadowed object (y i > y p + h p ).
X and Z are constant coordinates of the object, with the value 0.
x i , y i , z i are the coordinates of the position of the light source (z i = 0).h p is the height of the object (height of the isosceles triangle and cone, distance between the diameter of circle and sphere and the light source).
r is the radius of the circle and the sphere.d is the distance between the object and the surface.
x p , y p , z p are coordinates of center point of object (for an isosceles triangle that is the center of the base, for circle it is the circle's center, for the sphere, and for the cone it is the center of its base).

Calculating the color of the shadow
In order to make the scene where the shadowed objects are placed as realistically as possible, it is not enough to draw the shadow in a certain shade of gray, but is necessary to calculate that shade.
It is known that the shadow is stronger if the light source is, with respect to the shadowed object, closer.If the source of light is further, the created shadow is fainter.The question is how to calculate the distance between the object and the light source, and how to include the position of the shadow in this calculation.
The distance of the object and the light source can be calculated as the distance between their centers.
The shadow has some shade of gray, and it is known that the shades of gray in RGB model are made if all the components of the color, red and green and blue, are set to the same value.If the value is closer to one, the shade of gray is darker, closer to black RGB (0, 0, 0).If the value of the components is closer to 255, the shade of gray color is closer to white RGB (255, 255, 255).
It is necessary to define the coefficient which would multiply 255, and which would be 0 ≤ k ≤ 1 , to give a realistic shade of the shadow; in accordance with the conditions set above, the shade is darker if the distance of the light source is smaller, and vice versa.
In [17] it is explained that the requirement to form a shadow on the surface is that the light source should be above the observed object, and if it is below, then it forms a reflection.This means that the distance between the light source and the surface (center of the shadow) is always greater than the distance of the object from the surface.Thus the ratio of these two distances will always be less than 1, if we take the object distance as the numerator, and the distance of the light source as the denominator.In order to meet the requirement to produce a darker shade of shadow if the object and the light source are closer, then the resulting ratio should be deducted from the Eq.(1) (given in the Eq. ( 1) and following pseudo code).

Calculating the shadow for the circle
In the case when the circuit placed parallel to the surface is illuminated by the light source, only the umbra is present.There are two considered situations regarding the position of light source: when it is just above the circle (x = x p = 0) (Fig. 1a) and when it is to the left or the right (Fig. 1b).
Two important points for rendering shadows are the center of the shadow and the size of radius of the shadow.The shadow in this case is also a circuit, and coordinates of its center could be found using the equation of a line which passes through the two points: the center of the light source and the center of the shadowed object.
In the first case, the coordinates of the center of the shadow are the coordinates of beginning point (0, 0, 0).In the second situation, the formula for obtaining the center of the shadow is given in Eq. ( 2). , d y .
When d = 0, x s equals zero, which means that no shadow appears if the object is on the surface and the light source is above it.
Calculating the length of the radius of the shadow is done using the theorems on similarity of triangles.Fig. 1 shows the relevant triangles for calculating the increasing factor for original radius.The equation for calculating the radius of the shadow is given in Eq. (3).From this formula it can be seen that the length of the radius depends on r, y i , and d.As it can be seen from last two lines of the code, we can draw as many shadows as light sources are present, by calling a method e.g.drawCircleShadow (object, lightSourceN).

Calculating the shadow for the triangle
Like in the previous section, we can compute the points for the shadow of the isosceles triangle in the case when its base is parallel to the X axis.Then the characteristic points will be collinear, hence its shadow will be a line.
It is important to notice the similarity when computing the shadows for the seemingly different forms.Significant points to draw the triangle shadow in this case are those where the beam of light that passes through the vertices of the triangle cuts the surface.Here we can use mitigating circumstance that the equation of the shadow of the circle can facilitate obtaining of these points.
A connection between the base of a triangle and its center, and diameter of a circle and its center is that in both cases the shadow is a line.
We can consider the half of the basis as a circle radius to find the center of the triangle shadow and the length of the shadow basis Eq. ( 4) and (5).Now the problem is simplified to discovering the triangle peak, and as a common point of two equal edges will be calculated according to Eq. ( 6), (7) and (8).
In the case where the base of the triangle is parallel to the Z axis, relevant points are not collinear, and the shadow is not a line but a triangle.Three essential points are: P 1 (0, 0, z s1 ), P 2 (0, 0, z s2 ), P 3 (x s3 , 0, 0), where z s1 and z s2 are calculated similar to x s1 and x s2 .There are two situations considered in regard of the position of light source: when it is just above the triangle (x = x p = 0) and when it is the left or right in Fig. 2. If a is base length, where r = a/2, the coordinates of important points for drawing shadow of a triangle are: P 1 (x s1 , 0, 0), P 2 (x s2 , 0, 0), P 3 (x s3 , 0, 0).

Calculating the shadow for the sphere
When creating a shadow of the sphere, we can use the fact that it is generated by rotating a circle around the x axis.As shown in Fig. 3, the shadow of the sphere is also a circle, and for its calculation in this case, we can use the previous equations, given above in the circle section (shown in Fig. 3).

Calculating the shadow for the cone
When creating a shadow of the cone, we can use the fact that it is generated by rotating a triangle, whose base is, in this case, parallel to the Z axis.Three characteristic pieces of information are necessary for shadowing: projection of top of cone, base center projection and the radius of the shadow.The shadow is a union of the bias shadow (circle) and the cross section triangle shadow (triangle).If the light source is just above the cone, the shadow consists of the circle only (Fig. 4).Calculations for the shadow of a circle are already given above, and the triangle part of the shadow is determined by P 1 (0, 0, z s1 ), P 2 (0, 0, z s2 ), P 3 (x s3 , 0, 0), where z s1 and z s2 are calculated similar to x s1 and x s2 .
The following pseudo code shows how the circle shadowing method could be improved and reused for drawing the shadow of the sphere.The results are given with following screenshots (Fig. 5. and Fig. 6): Also, we made a comparison (observing the rendering time) of our method for generating shadows with two most often used methods which are described in second chapter (projected shadows and ray tracing).The results are given in Fig. 7. What can be deduced by observing the graph is that our method is faster than the ray tracing method, but a little slower compared to projected shadows.Since the shadows generated by our method are more realistic, this advantage in speed of method of projected shadows is negligible.
When it comes to more complex structures, our method shows its advantages, because the shadows are generated much faster compared to the other two methods.This is because, in our calculations of shadows, we used the fact that complex objects are made by rotation of primitives, as described above.

Conclusions and future work
The fact is that techniques for forming shadows for various objects need not be independently perceived.That should be used to facilitate the calculation of the position of a shadow for objects on the scene.
When forming the shadow of a compound object, it can be considered as a union of the shadows of its components.Rendering the scene would be tough and delayed if we projected all points of the objects on it, for the purpose of shading.
For that reason, it is sufficient to find the lowest number of specific points of a shadow, thus the shadow looks real, and its sketching will be easy and cheap.
If there is a method for drawing a shadow on the position of light source, then a number of light sources can be incremented; all of them could change their position, without making the scene more complex for shading.
In further research, the attention will be focused on discovering methods for shading the residual primitives that are frequently used.Other cases will be studied, such as the occurrence when a shaded item can move, when there is more than one shaded object in the scene at the same time, or when the surface is not flat.
After analyzing all the circumstances in which the shaded object can occur, the result could be a united and effective method of constructing real OpenGL shadows.

Figure 2
Figure 2 Shadow of triangle on flat surface using point light source: left xi = xp= 0; right xi ≠ xp

Figure 3
Figure 3 Shadow of the sphere on flat surface using point light source Following pseudo code shows how the circle shadowing method could be improved and reused for drawing the shadow of the sphere.function drawSphereShadow(object, lightSource) begin objectC = findCrossSectionCircleByTangents(); drawCircleShadow(objectC, lightSource); end

Figure 4
Figure 4 Shadow of the cone on flat surface using point light source: left xi = xp = 0; right xi ≠ xp shadowing with multiple lights with the capability of moving In order to confirm the validity of the given formula and pseudo codes and to check the speed of drawing real time shadows, a simple C++ application that uses OpenGL is created.The application takes into account the above limitations.The application has the following features: -changing of shadowed object (keys K(circle), L(sphere), T(triangle), U(cone)); -changing the position of camera (keys Y (increasing y coordinate), X (increasing x coordinate), C (increasing z coordinate), A (decreasing y coordinate), S (decreasing x coordinate), D (decreasing z coordinate)); -changing the position of the chosen point light source (keys V (increasing y coordinate), B (decreasing y coordinate), N (increasing x coordinate), M (decreasing x coordinate)); -changing the chosen point light source (G -light source 1, H -light source 2).

Figure 5 Figure 6
Figure 5 Shading the sphere in OpenGL by two point light sources

Figure 7
Figure 7 The comparison of methods This comparison was implemented in the environment of VS 2010 and on a PC with Intel Core2Duo 2.27 GHz CPU and 4 GB RAM.What can be deduced by observing the graph is that our method is faster than the ray tracing method, but a little slower compared to projected shadows.Since the shadows generated by our method are more realistic, this advantage in speed of method of projected shadows is negligible.When it comes to more complex structures, our method shows its advantages, because the shadows are generated much faster compared to the other two methods.This is because, in our calculations of shadows, we used the fact that complex objects are made by rotation of primitives, as described above.