Java Reference
In-Depth Information
The rotation and orientation methods take an angle and an axis of revolution as parameters.
preRotate()
is applied before the translation, and
postRotate()
is applied after the translation.
Using these methods, you can manipulate the coefficients in the
Transform
matrix without
knowing the math involved.
In the Listing 15-1 code, shown earlier in this section, the camera is moved to
(0,0, 10)
. This
is 10 units towards the viewer along the z-axis. When the camera is first created, it is located at
(0,0,0)
and pointing towards the z-axis (into the display).
The single light is also placed in the same position as the camera, giving the camera a
“headlight” pointing at the direction of the view.
The final code in the rendering loop actually rotates the triangle submesh. The same
Transform
is reused here to save some memory. Calling
setIdentity()
on the
Transform
basically
resets it to a
Transform
that performs no translation, scaling, or rotation. Then the
postRotate()
method is used to rotate the triangle one degree per frame around the axis of choice (z-axis in
this case).
mAngle += 1.0f;
mTransform.setIdentity();
mTransform.postRotate(mAngle, // Rotate 1 degree per frame
0, 0, 1.0f );
mGraphics3D.render(mVertexBuffer, mIndexBuffer,
mAppearance, mTransform);
The call to
render()
will actually draw a 3D frame into the bound 2D
Graphics
instance.
The
Light
and
Camera
have already been set up earlier using the
Graphics3D
instance. Notice
how
Graphic3D
's
render()
method takes the
VertexBuffer
, the
IndexBuffer
(
TriangleStripArray
),
the
Appearance
, and the
Transform
to perform its work.
Experimentation with Culling
TriangleCanvas
is a great framework for experimenting with the M3G APIs. You can try moving
the light, camera, and triangle around; or try working with different
Material
and
Appearance
attributes, etc. All you need is a copy of the Javadoc of the JSR 184 for reference.
As an example, we'll disable culling. Disabling culling will let you see the “back” of the
triangle rendered. To add code to disable culling, look inside the
init()
method and insert the
following highlighted code:
mAppearance.setMaterial(mMaterial);
PolygonMode tPoly = new PolygonMode();
tPoly.setCulling(PolygonMode.CULL_NONE);
mAppearance.setPolygonMode(tPoly);
mBackground.setColor(0x00ee88);
PolygonMode
is an M3G class representing a rendering attribute. It is grouped by an
Appearance
instance and can be used to control culling. In this code, it is set to
CULL_NONE
, allowing the back
of the triangle to be rendered. The default is
CULL_BACK
.
Assuming that you have modified
TriangleCanvas
to rotate the triangle around the x-axis,
rebuild the MIDlet and run it. You should now see both sides of the triangle rendered during
the rotation. Notice, however, that only the side with the vertex normals is rendered with light
and color.
Search WWH ::
Custom Search