Getting Started With Java3D

Getting Started: 3D Objects and Light

Introduction

In this tutorial we will be adding a 3D Sphere object and a light source to illuminate and give color to this 3D object. You will need to have completed the previous tutorial The JFrame in order to complete this tutorial.

What You Will Need to Start

In the previous tutorial we set up a JFrame for the Java3D canvas. We will be continuing on with that exact same code. I recommend taking a copy of the code rather than overwriting it as the blank JFrame code will be used again in the future.

Create a new project (call it whatever you want), import all of the necessary libraries as we did in the very first tutorial and create a class in the default package called “Main”You can then copy the code from the previous tutorial into this new class.

Implementation

Now it’s time to code. To start I would like to give you all the imports that you will need for this tutorial. They have been condensed, you can delete the imports from your current class and replace them with the ones seen below.

929677980bfeb71dcb3f2d9bbf404303

Creating the Sphere

Let’s create the object, in this tutorial we will be working in the constructor method “public Main()”. To create the sphere we use “Sphere sphere = new Sphere(size);”. Where the size parameter is I used the value “0.25f”.

Now we need to add the sphere into the scene, simply use “group.addChild(sphere)”.  This adds the sphere object into the BranchGroup which we have already created. Your constructor method will look similar to the image below.

c6bd90a430c8cac580136162fb7f230b

When you run this program, all you will see is a black screen. This is because there is no lighting within the scene. So lets implement the lighting.

Creating the Light

First of all we need to set a few properties for our light. This is the color of the light along with the direction of the light. To create the color of the light we will create a new Color3f object as done below. Feel free to change the values for the color.

8344d7663799a45f91cd8a2f3a556f46

Now we can set the direction of the light, this has been done below. You can change the values for the direction of the light and experiment with what happens when you do that.

ab2eda9d535d8a4a08d5e8687047dfc2

Now we can set the BoundingSphere for the object, this will define the position and the distance that the light shines within our scene.

4999359a69e9436e396011630839aad7

Finally we can create the actual light within the scene and set the color and direction which we already created. Then set the InfluenciningBounds property of the light to the BoundingSphere which we have already created. Finally, we can add the light into the BranchGroup.

f4855d93405c67f3bbfb9c88497f067c

That is it, when you run your program you should have a sphere of the color you decided. As I small exercise, change the values of the light direction and the color and see what happens!

d61d7b6b14ed9ec30154250734eee350

Here is the code for the entire constructor method.

bfabf94789af7dfa578aa64efae6d2da6db22a191ba7f6235a579226f2c46c92

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s