Making 3D Shapes with Polygons and the RGB Color Scale


Making 3D Shapes with Polygons and the RGB Color Scale

In the last section, you saw how we could make more complicated shapes by adding triangles together, but we did not delve into the third dimension. We are now going to add a third triangle at (100, 0, 100) to the two before, as shown by Figure 3.14.

click to expand
Figure 3.14: Adding a third triangle to our basic rectangle.

Here are the lines responsible for adding the new triangle:

 // Create several points for polygons (with UV). Point1 = New PointArray(3,100,100,0); Point2 = New PointArray(3,100,100,0); Point3 = New PointArray(3,100,100,0); Point4 = New PointArray(3,100,100,0); Point5 = New PointArray(3,100,0,100); // Create 3 polygons with these points oobject.AddPolygon(Point1,Point2,Point3); oobject.AddPolygon(Point1,Point3,Point4); oobject.AddPolygon(Point2,Point5,Point3); 

Note a couple of things about this code: we have added Point5 with a New PointArray call, and we have added one more AddPolygon line, which adds the new triangle to our previous polygon.

We will make a few other changes to our code to illustrate how you can control colors and add them to polygons without having to add a texture. The colors we will add will be flat colors ”that is, totally uniform. Textures that you create with nonuniform colorings are much more realistic, but you should also be familiar with flat colors and the RGB scale.

Let s say that you had three buckets of paint ”one red, one green, and one blue ”and you wanted to mix them to create different colors. By controlling the amount of each color paint you add, you could come up with a very wide range of colors. The RGB (you guessed it; it stands for red, green, blue) scale for color control works in the same way, specifying the amounts of red, green, or blue that are added together. In this scale, 255 is the most that you can have of a color, and 0 is the least. The color RGB (0, 0, 255) specifies blue, while RGB (0, 255, 0) gives green and RGB (30, 100, 200) gives some mixture of all three colors. Below is a list of some common colors and their RGB equivalents:

Black

255, 255, 255

Blue

0, 0, 255

Cyan

0, 255, 255

Green

0, 255, 0

Yellow

255, 255, 0

White

0, 0, 0

Red

255, 0, 0

Magenta

255, 0, 255

We add the following statements to our code:

 omaterial = New Material(oworld,GetRGB(50,150,200),"lightblue");// all on one line 

This line tells Jamagic to make a new material called omaterial and that it is to be applied in oworld . In addition, the RGB code for the material is given, and the name you are giving the color is light blue. The name of the color is for your own benefit ”the program does nothing with this at this time. You could have called it almost anything you wanted.

 omaterial.setflat(ON); 

This statement tells Jamagic to use the flat coloring mode, as opposed to Gouraud (the default setting) or Lambert shading, which are much more realistic shading techniques than those that can be had with flat colors. Gouraud shading, which we will use soon, gives a 3D effect to a surface by altering the shading of the vertices of the polygons used to make up a figure. Lambert shading is similar to Gouraud shading, but it is more burdensome to the computer s memory. As far as the author can tell, the differences between Gouraud and Lambert shading are so small as to make them indistinguishable.

And finally we add the following:

 oobject.ReplaceMaterial(omaterial); 

This statement, which you ve seen before, applies the material to our strange -shaped oobject .

You may now type in the entire code presented below:

 //Program for making a complicated shape using triangles // Create the world. oworld = New World(); ocamera = New Camera(oworld); // Create an empty object. oobject = New Object(oworld); // Create several points for polygons (with UV). Point1 = New PointArray(3,100,100,0); Point2 = New PointArray(3,100,100,0); Point3 = New PointArray(3,100,100,0); Point4 = New PointArray(3,100,100,0); Point5 = New PointArray(3,100,0,100); // Create three polygons with these points. oobject.AddPolygon(Point1,Point2,Point3); oobject.AddPolygon(Point1,Point3,Point4); oobject.AddPolygon(Point2,Point5,Point3); omaterial = New Material(oworld,GetRGB(50,150,200),"lightblue"); omaterial.SetFlat(ON); // Add the new material to our object. oobject.ReplaceMaterial(omaterial); // Add the movement of the camera. ocamera.SetPosition(0,0,700); oobject.Walk(); // Wait for the user to close the window. While(1) { } 



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