We will now make a new program called Sphere2, so you ll have to start a new source code. In this program, we will investigate the different types of shading we can use in our projects: flat, mesh, and Gouraud . Please see Figure 4.3 for a screenshot

click to expand
Figure 4.3: Screenshot of the Sphere2 program for experimenting with shading techniques.

Flat, Mesh, and Gouraud Shading

In this project, we make three spheres and apply different shading techniques to each. Here is the first new statement in this code:


This statement applies Gouraud shading to the object mysphere . The result will be a nicely shaded object with a realistic look.


This statement causes a material we create ( mymaterial ) to be applied in a flat mode that has no shading effects.

If you should wish to return to a mesh-type surface (the default until you apply a texture) you can use the following command:


Please type in the following code after you start a new project called Sphere2; this code will create three spheres with different shading. As you will see, you have already learned quite a bit! Some of the things that you have already learned that are in this code include the use of RGB colors, the use of the keyboard to move the spheres, the CreateSphere command, if statements, and much more.

Sphere2 Code

 // // Sphere2: Second Spherelander game. Will add second     // sphere // and 3rd sphere, // gouraud shading, flat color // Create a new world with a new camera. oworld = New World(); ocamera = New Camera(oworld); // Create a sphere with Gouraud shading. mysphere = oworld.CreateSphere(100,100,100,10,10); mysphere.SetPosition(0,0,0); omaterial = New Material(oworld     ,GetRGB(50,150,200),"lightblue");//one line mysphere.SetGouraud(ON); mysphere.ReplaceMaterial(omaterial); //Create a 2nd sphere with flat solid color. sphere2 = oworld.CreateSphere(100,100,100,10,10); sphere2.SetPosition(300,100,1000); mat2 = New Material(oworld,GetRGB(0,200,0),"green"); mat2.SetFlat(ON); sphere2.ReplaceMaterial(mat2); //Create a 3rd sphere with flat mesh. ball3 = oworld.CreateSphere(100,50,50,10,5); ball3.SetPosition(500,500,1000); mat3 = New Material     (oworld,GetRGB(50,150,200),"lightblue");//one line ball3.ReplaceMaterial(mat3); // Camera ocamera.SetPosition(0,0,3000); //Game loop While(1) { if(Keyboard.IsKeyDown(Keyboard.UP))  {    mysphere.Move(200,2);  } if(Keyboard.IsKeyDown(Keyboard.DOWN))  {    mysphere.Move(200,2);  } if(Keyboard.IsKeyDown(Keyboard.RIGHT))  {    mysphere.MoveRight(200,2);  } if(Keyboard.IsKeyDown(Keyboard.LEFT))  {    mysphere.MoveLeft(200,2);  } if(Keyboard.IsKeyDown(Keyboard.SHIFT))  {    mysphere.MoveDown(200,2);  } if(Keyboard.IsKeyDown(Keyboard.CAPSLOCK))  {    mysphere.MoveUp(200,2);  } // The end of the game loop follows. } 

It s very important that you notice several things about this program when you run it. First, note the difference between the blue mesh sphere and the blue mesh ellipse at the upper-right corner of the screen. They both have the same color, but the ellipse s color is totally uniform. The blue sphere, on the other hand, has a very realistic lighting effect on it due to the Gouraud shading.

You should also notice that the green sphere is totally filled in with a flat color ”not very interesting at all (sorry green sphere!).

Now for some fun! Something kind of fascinating happens when you collide the blue sphere (by using the arrows and keys) with the solid green sphere. The two blend together to form one at the spot of contact, as shown in Figure 4.3.

And here is something else that is also very interesting: if you maneuver the blue sphere behind the green sphere, you will see that the blue sphere becomes invisible where the green one covers it up. This is something like what happens when someone with a big head blocks your view of the screen at a movie. This makes our 3D world much more believable doesn t it? Try it now!

Elementary Game Programming and Simulators Using Jamagic
Elementary Game Programming & Simulations Using Jamagic (Charles River Media Game Development)
ISBN: 1584502614
EAN: 2147483647
Year: 2002
Pages: 105
Authors: Sergio Perez

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net