Game Development Reference
In-Depth Information
Listing 11-10. Excerpt from EulerCameraTest.java; the EulerCameraScreen
class
EulerCameraScreen
extends
GLScreen {
Texture crateTexture;
Vertices3 cube;
PointLight light;
EulerCamera camera;
Texture buttonTexture;
SpriteBatcher batcher;
Camera2D guiCamera;
TextureRegion buttonRegion;
Vector2 touchPos;
float
lastX = -1;
float
lastY = -1;
We start off with some members. The first two store the texture for the crate and the vertices of
the texture cube. We'll generate the vertices with the
createCube()
method from the previous
example. The next member is a
PointLight
instance, which we are already familiar with,
followed by an instance of our new
EulerCamera
class.
Next up are a couple of members we need to render the button. We use a separate 64×64
image called
button.png
for that button. To render it, we also need a
SpriteBatcher
instance
as well as a
Camera2D
instance and a
TextureRegion
instance. This means that we are going to
combine 3D and 2D rendering in this example! The last three members are used to keep track of
the current
touchPos
in the UI coordinate system (which is fixed to 480×320), as well as store the
last known touch positions. We'll use the value -1 for
lastX
and
lastY
to indicate that no valid
last touch position is known yet.
public
EulerCameraScreen(Game game) {
super
(game);
crateTexture =
new
Texture(glGame, "crate.png",
true
);
cube = createCube();
light =
new
PointLight();
light.setPosition(3, 3, -3);
camera =
new
EulerCamera(67, glGraphics.getWidth() / (
float
)glGraphics.getHeight(), 1, 100);
camera.getPosition().set(0, 1, 3);
buttonTexture =
new
Texture(glGame, "button.png");
batcher =
new
SpriteBatcher(glGraphics, 1);
guiCamera =
new
Camera2D(glGraphics, 480, 320);
buttonRegion =
new
TextureRegion(buttonTexture, 0, 0, 64, 64);
touchPos =
new
Vector2();
}
In the constructor, we load the crate texture and create the cube vertices as we did in the
previous example. We also create a
PointLight
and set its position to (3,3,-3). The
EulerCamera
instance is created with the standard parameters, a 67-degree field of view, the aspect ratio
of the current screen resolution, a near clipping plane distance of 1, and a far clipping plane
distance of 100. Finally, we set the camera position to (0,1,3), as shown in Figure
11-10
.