I've been getting 3ds objects to appear on my computer screen with my jme code for a couple of weeks now and had mistakenly thought I had how to do that all figured out. So I thought I'd try to make a simple game where the player flies a spaceship around a small defined space from a top down view. I'm using a quad to create a basic star background.
All the code should currently do is create a background and then place a 3ds spaceship above the background. The background works great, but I can't get the spaceship to appear. I commented out the background to see if that was somehow interfering, but all I had then was a black screen. I've tried moving the camera position and the spaceship position to see if I simply have the ship out of the camera's line of sight to no avail. I suspect that the spaceship is not even being rendered and I suspect this is due to me making some newbie error.
I'm hoping that some kind soul will be able to scan through my code and instantly spot my error.
public class SpaceArena extends BaseGame
{
//variable declarations
private int width, height, depth, freq;
private boolean fullscreen;
private Camera cam;
public Node scene;
protected Timer timer;
private Node BackNode;
private Quad BackQuad;
private Rectangle BackRec;
private CameraNode camNode;
private int textureWidth;
private int textureHeight;
protected SpaceInput input;
private ZBufferState buf;
private Node starship;
public static void main(String[] args)
{
SpaceArena app = new SpaceArena();
app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
// Turn the logger off so we can see the XML later on
LoggingSystem.getLogger().setLevel(Level.OFF);
app.start();
}
protected void render(float interpolation) {
// Clear the screen
display.getRenderer().clearBuffers();
display.getRenderer().draw(scene);
}
protected void initSystem()
{
width = properties.getWidth();
height = properties.getHeight();
depth = properties.getDepth();
freq = properties.getFreq();
fullscreen = properties.getFullscreen();
timer = Timer.getTimer();
//cam init
try {
display = DisplaySystem.getDisplaySystem(properties.getRenderer());
display.createWindow(width, height, depth, freq, fullscreen);
cam = display.getRenderer().createCamera(width, height);
}
catch (JmeException e)
{
e.printStackTrace();
System.exit(1);
}
//set the background to black
display.getRenderer().setBackgroundColor(ColorRGBA.black);
display.getRenderer().setCamera(cam);
}
protected void initGame() {
display.setTitle("Space Arena");
scene = new Node("Scene graph node");
/** Create a ZBuffer to display pixels closest to the camera above farther ones. */
buf = display.getRenderer().createZBufferState();
buf.setEnabled(true);
buf.setFunction(ZBufferState.CF_LEQUAL);
scene.setRenderState(buf);
//build camera
buildOverheadCamera();
//build arena
buildArena();
//init input
input = new SpaceInput(properties.getRenderer());
//Create and place player's spaceship
createPlayerShip();
}
private void buildArena ()
{
//Define Quad to cover entire screen
BackQuad = new Quad("BackQuad", display.getWidth(), display.getHeight());
BackQuad.getLocalRotation().set(0, 0, 0, 1);
BackQuad.getLocalTranslation().set(display.getWidth() / 2, display.getHeight() / 2, 0);
BackQuad.getLocalScale().set(1, 1, 1);
BackQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
BackNode = new Node("BackNode");
//load background Texture
Texture bg = TextureManager.loadTexture(
SpaceArena.class.getClassLoader().getResource(
"SpaceArenaTex/nebula1280.jpg" ),
Texture.MM_LINEAR,
Texture.FM_LINEAR );
TextureState bgts = display.getRenderer().createTextureState();
bgts.setTexture( bg );
bgts.setEnabled( true );
//attach texture to quad
BackQuad.setRenderState( bgts );
BackQuad.updateRenderState();
//attach to scene
BackNode.attachChild(BackQuad);
scene.attachChild(BackNode);
}
private void buildOverheadCamera()
//uses a camNode
{
camNode = new CameraNode("Camera Node", cam);
Quaternion q = new Quaternion();
float angle = 20;
angle -= 180;
angle *= -1 * FastMath.DEG_TO_RAD;
q.fromAngleAxis(angle, new Vector3f(0, 1, 0));
camNode.setLocalRotation(q);
camNode.setCamera(cam);
camNode.setLocalTranslation(new Vector3f(0,0, 250));
}
private void createPlayerShip()
{
// Point to a URL of my model
URL model=SpaceArena.class.getClassLoader().getResource("3dm-v-38.3ds");
// Create something to convert .3ds format to .jme
FormatConverter converter=new MaxToJme();
// This byte array will hold my .jme file
ByteArrayOutputStream BO=new ByteArrayOutputStream();
//starship = null;
starship = new Node("starship");
try {
// Use the format converter to convert to .jme
//for textures
TextureKey.setOverridingLocation(new URL("file:SpaceArenaTex\"));
//do conversion
converter.convert(model.openStream(), BO);
starship=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
// shrink this baby down some
starship.setLocalScale(.05f);
starship.setModelBound(new BoundingBox());
starship.updateModelBound();
starship.setLocalTranslation(new Vector3f(1,1,1));
starship.setRenderState(buf);
scene.updateGeometricState(0, true);
}
catch (IOException e)
{ // Just in case anything happens
System.out.println("Damn exceptions!" + e);
e.printStackTrace();
System.exit(0);
}
}
protected void update(float interpolation)
{
// update the time to get the framerate
timer.update();
interpolation = timer.getTimePerFrame();
//check for keyboard input
input.update(interpolation);
if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit"))
{
finished = true;
}
scene.updateGeometricState(interpolation, true);
//update Overhead Camera
camNode.updateWorldData(interpolation);
}
protected void reinit()
{
display.recreateWindow(width, height, depth, freq, fullscreen);
}
protected void quit() {
super.quit();
System.exit(0);
}
protected void cleanup() {
}
}