Home > Articles

This chapter is from the book

Primitive Creation

We have now prepared the movie for the generation of 3D content on-the-fly. This section explains how to build an API of custom handlers for the creation of primitives. These custom handlers are designed to approach several of the primitive types discussed in Chapter 2: the plane, sphere, box, and cylinder. The particle primitive is explored in depth in Chapter 17, "Special Effects."

In general, the custom handlers for creating primitives utilize some of the properties of each Modelresource type. For instance, it is not useful to build an extremely specific API with features for every aspect of the cylinder unless you are going to need these features. We will revisit the box and cylinder primitive custom handlers in Chapter 10, "Histograms," and Chapter 11, "Pie Charts," respectively. The custom handlers that we are going to build in this section are quite general, allowing you to modify them as required for your individual projects.

Plane

The plane primitive is versatile, simple, and concise, and it can be used in a variety of situations. The plane primitive does not have an excessive amount of properties that define it. The two properties that are most critical are Length and Width. For this reason, we will focus on the Length and Width properties as the controllable parameters for the createplane() custom handler, shown here:

 1: Global scene
 2: On createplane(planeName, L, W, planeColor)
 3: If check3Dready(scene) then
 4: Res = Scene.newmodelresource(planeName & "res", #plane)
 5: Res.length = L
 6: Res.width = W
 7: Obj = scene.newmodel(planeName, res)
 8: Shd = scene.newshader(planeName & "shd", #standard)
 9: Shd.diffuse = planeColor
10: Shd.texture = void
11: Obj.shaderlist = shd
12: Return scene.model(planename)
13: Else
14: Return false
15: End if
16: End

The createplane() custom handler is called with the syntax createplane("modelname", float, float, rgb(R,G,B)). This custom handler is built to conserve your typing. Although calling it requires typing a long line of code, this is easier than typing the contents of the handler each time you want a plane. Also, you are probably aware that creating planes, boxes, and spheres is a good first step, but there are many other concerns to contend with. Because of this, it is best to simplify the parts of code that are crucial but not demanding. What's more, after you have established an API of custom handlers to deal with frequent tasks, it will become easier to perform these repetitive tasks with ease.

If we were creating an environment that called for two planes, it might be easier to create them in other ways. When you realize that you actually need an environment with 30 planes, this method may seem more approachable. This process is easily encapsulated inside of a repeat loop to accomplish the mass-creation of objects. Here's an example:

1: Repeat with x = 1 to 30
2: Createplane("plane" & x, 10, 20, rgb(200,30,30))
3: End repeat

This example will quickly create 30 planes of a bright red color. If you decide later that you need another plane, you have a very concise way of changing the repeat loop to repeat until 31. Therefore, the time saved in typing accumulates as you develop a project.

NOTE

In this code example, you might expect that a wrong type error might occur, due to the fact that I am concatenating a string and an integer. Although you might disagree, Lingo is a very forgiving programming language when compared to others. Data types are not strongly enforced in Lingo, as they are in other languages. This logic is therefore a good programming trick for Lingo, but it's not a trick to take to other languages.

Finally, there are many occasions when you might be testing certain functions of the 3D environment on an isolated object. If you were to build a test Model to check one or two functions of a project, it would be tiring to have to type up all the commands to create a plane or a sphere. This method allows you to focus your attention on the needs of your project, rather than spending your energy reinventing the wheel (or in this case, the plane).

Sphere

The sphere is perhaps one of the more aesthetically pleasing types of primitives in the 3D environment because it exhibits visually interesting qualities. The smooth surface and the ability to simulate complex interactions with light make the sphere an attractive primitive. Even when you're working with primitives, it is obvious that some are more visually engaging.

One reason for this complexity is the curvature of the surface. When you are designing your 3D worlds and modeling many of the objects, you can use the visual interest that draws the eye to curved surfaces to create focal points in your worlds. Although you may not necessarily use a sphere to accomplish this, you might create complex objects with curved surfaces.

Curved surfaces tend to utilize more geometry and therefore more memory than flat-surfaced objects. For this reason, depending on your intended user, you will most likely need to limit the usage of highly descriptive geometry in your scenes. This can aid in emphasizing your choices for using curvilinear surfaces in areas of visual focus.

Finally, as a compositional element, spheres tend to have well-defined areas of highlight, midtone, and shadow. These three visual demarcations are used in a painting technique known as chiaroscuro. This technique is used to create the illusion of three-dimensional objects on a two-dimensional surface. By this description, the approach toward realistic painting techniques are similar to that of a 3D environment. For this reason, you may want to examine the vocabulary of formal composition as a method of learning how to control what you would like to see in your 3D world.

The custom handler for the creation of a sphere is similar to the createplane() handler. Note that the key difference is that the defining property of the sphere is truly its radius. For this reason, our handler will only deal with this one property. Here's the code:

 1: Global scene
 2: On createsphere(sphereName, R, sphereColor)
 3: If check3Dready(scene) then
 4: Res = Scene.newmodelresource(sphereName & "res", #sphere)
 5: Res.radius = R
 6: Obj = scene.newmodel(sphereName, res)
 7: Shd = scene.newshader(sphereName & "shd", #standard)
 8: Shd.diffuse = sphereColor
 9: Shd.texture = void
10: Obj.shaderlist = shd
11: Return scene.model(spherename)
12: Else
13: Return false
14: End if
15: End

The critical lines of code that create Models in the createplane() and createsphere() custom handlers are encapsulated in an if/then statement. This if/then statement utilizes the check3Dready() custom handler we explored earlier. Note that after the successful creation of the Model, createsphere() returns a reference to the code object for the Model. This is true in createplane() as well. Also, upon failure, these custom functions will return false. The reason for this is twofold. Recall in prior examples, as well as in the createsphere() custom handler, that when we create code objects in the 3D world, we often save a reference to those objects in a variable to reduce our typing requirements. If we were to call the createsphere() function from the initialize() custom handler, the returning information allows us to create a reference to the new sphere's code object as follows:

Mynewsphere = createsphere("ball", 27, rgb(50,100,10))

This syntax allows us to refer to this sphere as mynewsphere throughout the initialize() handler. If we were to declare the mynewsphere variable as global prior to this line of code, we could easily reference the sphere throughout the rest of the Movie through this variable. Even as a local variable, this information affords us much latitude in our coding. One addition that we could make would be to encapsulate our creation of the sphere as follows:

-- create the sphere
1: Mynewsphere = createsphere("ball", 27, rgb(10,40,50))
then --check to make sure that mynewsphere is not false
2: If not mynewsphere
-- take some action if mynewsphere is false
3: Alert "problem!"
4: End if

Line 3 of this example is critical—we have decided to alert that there is a problem. We can do a few useful things here: We can either call an alert or halt the program, or we can redirect the Playback Head to a section of the movie where we can make the process of error handling more transparent to the end users. For example, you might send them back to the "hold" marker to make sure that the 3D Castmember is truly ready. Alternatively, you might have a marker called "error" where you could gracefully let the users know that something is wrong and give them a general idea of the problem. Error checking in this case is quite simple because the only error a user might encounter is that the Castmember somehow does not have a State setting of 4 when you are trying to create the sphere. The createsphere() custom handler could be expanded to include error checking to ensure that when the movie is loaded, the name you intend to use for the sphere is unique. Here's an example:

 1: Global scene
 2: On createsphere sphereName, R, sphereColor
 3: If check3Dready(scene) then
 4: If scene.model(spherename) = void then
 5: Res = Scene.newmodelresource(sphereName & "res", #sphere)
 6: Res.radius = R
 7: Obj = scene.newmodel(sphereName, res)
 8: Shd = scene.newshader(sphereName & "shd", #standard)
 9: Shd.diffuse = sphereColor
10: Shd.texture = void
11: Obj.shaderlist = shd
12: Return scene.model(spherename)
13: Else
14: Return –2
15: End if
16: Else
17: Return -1
18: End if
19: End

Now this custom handler begins to develop some character, and more importantly a system for error checking. In this example, -1 informs us that the Castmember was not ready, and -2 tells us that we are using a duplicate name. It would not be difficult to modify these values to return -2001 and -2002. We could then easily say that error -2002 was generated when we tried to create a sphere, because we used a duplicate name. Perhaps –1002 could be reserved for when we try to create a plane with a duplicate name.

Box

Only the plane primitive rivals the versatility of the box primitive in the creation of basic architectural spaces. The box primitive will often be your first choice for walls, ceilings, and floors when describing interior spaces. Although a special case of the box primitive is the cube, rectangular boxes are viable building blocks. For this reason, our createbox() custom handler will be concerned with the Length, Width, and Height parameters of boxes. If we were more interested in only creating cubes, our custom handler would only need to accept one value for Length, Width, and Height. As it is, the createbox() custom handler is similar in form to createsphere() and createplane(), but it requires several arguments, as shown here:

 1: Global scene
 2: On createbox boxName, L,W,H, boxColor
 3: If check3Dready(scene) then
 4: Res = Scene.newmodelresource(boxName & "res", #box)
 5: Res.length = L
 6: Res.width = W
 7: Res.height = H
 8: Obj = scene.newmodel(boxName, res)
 9: Shd = scene.newshader(boxName & "shd", #standard)
10: Shd.diffuse = boxColor
11: Shd.texture = void
12: Obj.shaderlist = shd
13: Return scene.model(boxname)
14: Else
15: Return false
16: End if
17: End

Lines 9 through 12 occupy themselves with the creation of a Shader dedicated to this Model. Note that the name of the Shader is the name of the box with the letters "shd" appended. Using a concise naming convention like this will aid you in controlling your worlds. Also, understand that the Shader object and Shader property are two separate entities.

This handler is quick and should suffice for the creation of basic box shapes. If we were interested in creating a maze-generation game, we might customize the box handler to build several boxes and position them correctly. We could then rename it the "createroom()" handler.

Cylinder

The cylinder primitive is intriguing because it can be reshaped into so many variations. Because of this, I have created two custom handlers: one to deal with common cylinder creation and another to deal with the creation of cones.

Basic Cylinder Handler

For a basic cylinder, the pertinent parameters we are interested in are its height and radius. Therefore, the custom handler looks like this:

 1: Global scene
 2: On createcylinder cylName, R,H, cylColor
 3: If check3Dready(scene) then
 4: Res = Scene.newmodelresource(cylName & "res", #cylinder)
 5: Res.height = H
 
 6: res.topradius = R
 7: res.bottomradius = R
 8: Obj = scene.newmodel(cylName, res)
 9: Shd = scene.newshader(cylName & "shd", #standard)
10: Shd.diffuse = cylColor
11: Shd.texture = void
12: Obj.shaderlist = shd
13: Return scene.model(cylname)
14: Else
15: Return false
16: End if
17: End

Notice in this example that in order to set the radius of the cylinder, I must set the Topradius and Bottomradius. Keep in mind that radius is not a property of the cylinder primitive. I will elaborate on the basic cylinder-creation handler in Chapter 11, where we will specialize this function to deal with the creation of pie charts.

Basic Cone Handler

You can also use the cylinder primitive to create cones by modifying the Topradius and Bottomradius properties. If our goal is to create a simple cone, we only need to modify one of these properties. In addition, we still need to modify the height. The revised cylinder handler becomes the simple cone handler and looks like this:

 1: Global scene
 2: On createcone coneName, R,H, coneColor
 3: If check3Dready(scene) then
 4: Res = Scene.newmodelresource(coneName & "res", #cylinder)
 5: Res.height = H
 6: Res.bottomradius = R
 7: Res.topradius = 0
 8: Obj = scene.newmodel(coneName, res)
 9: Shd = scene.newshader(coneName & "shd", #standard)
10: Shd.diffuse = coneColor
11: Shd.texture = void
12: Obj.shaderlist = shd
13: Return scene.model(conename)
14: Else
15: Return false
16: End if
17: End

Line 6 now sets Bottomradius as defined when you call the createcone() custom handler, and line 7 is "hard-wired" to set Topradius to 0. Although this will always create cones that point up, it is possible to rotate them into any position we want.

It would not be overly difficult to add a control for the resolution property to this handler in order to create a pyramid-creation custom handler. We could easily have a createpyramid() custom handler if we were to change all references of cone to pyramid and then insert the following line of code at line 7:

Res.resolution = 1

You can see that it is possible to develop an entire suite of frequently used primitive-creation handlers that are meant to ease your workload. Also, depending on the needs of your application, you may want to build error checking into your custom handlers. You will probably need more error checking if you are going to be building projects that generate Models on their own or via user manipulation. If all the Models you are going to create are known, you will most likely not need to have robust error checking for these operations. Aside from error checking, the strategy is clear: encapsulate and reuse. With this strategy, we can begin to focus our attention on how to work with the Models we have created.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020