- [Instructor] Let's talk about 3D shapes in JavaFX. Remember, a point in 3D space is represented by the coordinates x, y and z. A 3D object has three dimensions. Where x represents the width, y represents the height, and z is the depth of the image. The positive direction of the x-axis points to the right from the origin. The positive direction of the y-axis actually points down, which is a little bit different than your Cartesian coordinate system, but that is normal when you're dealing with graphics in any programming language.
The positive direction of the z-axis points into the screen, away from the viewer. The negative directions are on the axes's which are not shown, but they extend in the opposite directions at the origin 0,0,0. To check to see if your computer supports 3D graphics, you can use this statement in Java. System.out.println, Platform.isSupported, open parenthesis, ConditionalFeature.SCENE3D.
This will print out true if it supports it, and false if it does not. Another important thing to understand about 3D objects in JavaFX, is that by default, nodes are rendered in the order that they're added to the scene graph. So, if you add a sphere after you add a box, it's going to put the sphere right on top of the box, if you're not careful. To make sure items appear in the correct order, we must specify that the scene needs to include a depth buffer.
We also need to specify the nodes that have z coordinate values and that they should be used during rendering. For example, we could declare a variable of type boolean called depth buffer equals true and then when we create our Scene, we can add that value. So we have Scene equals new scene root comma 500 by 500 for the window size, comma and our depth buffer is set to true. In order to see shapes with a z value, we need to add a camera.
A camera will give us the perspective we need. There are three basic types of shapes that come by default with JavaFX. They include the box, the sphere, and the cylinder. These shapes are found in the JavaFX dot scene dot shape package. All three shapes have properties in common. They all have a material, a draw mode, and a cull face. The material gives our shapes color, the draw mode indicates whether the shape is rendered as a line drawing or if it should be filled in, cull face or face culling is a technique used to only render the visible portions of your 3D image.
For example, if you're rendering a building, but you're standing in front of the building, you only need to render the front of the building since you won't be able to see the sides or the back. This makes our rendering more efficient. A 3D surface consists of many connected polygons that are made up of triangles. For example, let's think about a box. A box six sides. Each side is split into two triangles. So a box has 12 triangles. Here's an image of a box with a diagonal line representing the different triangles.
In JavaFX, all shapes are considered nodes so we can apply transformations to our shapes. Let's switch over to Netbeans and take a look at adding some shapes. I already have a program here called, "Shapes3D." In this program, I've added a sphere, a cylinder, and a box. Let's take a look at the code. Starting at line 27, I declare a sphere that has a radius of 75 and then I say I want to set the x and y values for the radius position to be at 80.
That way, it'll be inside the window a little bit. Next we have a cylinder. Cylinder has a radius of 50 and a height of 100. I also set the x and y value of the cylinder so that it is down and to the right of a sphere. And finally, I'm creating a box. Box equals new box. When you're creating a box, you have to give it the width, the height, and the depth. So I'm doing 100 by 100 by 100. And then I'm translating the x, y, and z coordinates where to put the box.
On line 44, I'm creating a new group called Root and it's equal to a new group that has the sphere, the cylinder, and the box. I'm creating a new scene that uses that root to display the images. And finally, I'm filling in the background with a light gray to make sure we can see all of our shapes. Right, let's run the program and take a look at the results. There's our sphere, our cylinder, and our box. The first thing that you might notice is the lighting on the sphere and cylinder.
JavaFX automatically gives us some lighting to make these appear as if they're three dimensional. But now the box, again since we're facing the box, it's only showing us the very front view of the box. So let's close this. Let's change the draw mode to a line drawing for each one. So I'm going to uncomment this code that I have here and I'm going to draw each of those in line mode. So instead of filling them, they'll just be lines. Let's take a look at this.
You can see the sphere is made up of numerous little triangles. It's hard to see the cylinder triangles but it is also made up of triangles, and the box again is only showing me the current face and it's showing me that this face of the box is in two triangles. When we add a camera, we'll be able to see more of the box. But for right now, this is all we can see.
- What is JavaFX?
- Creating charts
- Review of Java UI controls
- 3D shapes in JavaFX
- User-defined 3D shapes
- Add a camera
- Add lighting
- Apply shape materials
- Interacting with 3D scenes