More juicy delicious 3D bits.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3514 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-04-20 23:31:14 +00:00
parent 3b36c61046
commit b1e8d60d18
2 changed files with 163 additions and 5 deletions
+80 -5
View File
@@ -27,16 +27,21 @@ import com.samskivert.util.Queue;
import com.samskivert.util.RunQueue;
import com.samskivert.util.StringUtil;
import com.jme.app.AbstractGame;
import com.jme.input.InputHandler;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.system.PropertiesIO;
import com.jme.system.lwjgl.LWJGLPropertiesDialog;
import com.jme.app.AbstractGame;
import com.jme.input.InputHandler;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.util.Timer;
import com.threerings.presents.client.Client;
@@ -117,6 +122,23 @@ public class JmeApp
// initialize our main camera controls and user input handling
initInput();
// initialize the root node
initRoot();
// initialize the lighting
initLighting();
// update everything for the zeroth tick
_root.updateGeometricState(0f, true);
_root.updateRenderState();
// create and add our statistics display
if (displayStatistics()) {
_stats = new StatsDisplay(_display.getRenderer());
_stats.updateGeometricState(0f, true);
_stats.updateRenderState();
}
} catch (Throwable t) {
Log.logStackTrace(t);
exit();
@@ -219,6 +241,37 @@ public class JmeApp
// input.setMouseSpeed(1f);
}
/**
* Creates our root node and sets up the basic rendering system.
*/
protected void initRoot ()
{
_root = new Node("Root");
// set up a zbuffer
ZBufferState zbuf = _display.getRenderer().createZBufferState();
zbuf.setEnabled(true);
zbuf.setFunction(ZBufferState.CF_LEQUAL);
_root.setRenderState(zbuf);
}
/**
* Sets up some default lighting.
*/
protected void initLighting ()
{
PointLight light = new PointLight();
light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
light.setLocation(new Vector3f(100, 100, 100));
light.setEnabled(true);
_lights = _display.getRenderer().createLightState();
_lights.setEnabled(true);
_lights.attach(light);
_root.setRenderState(_lights);
}
/**
* Processes a single frame.
*/
@@ -263,6 +316,11 @@ public class JmeApp
// run all of the controllers attached to nodes
_root.updateGeometricState(timePerFrame, true);
// update our stats display if we have one
if (_stats != null) {
_stats.update(_timer, _display.getRenderer());
}
}
/**
@@ -279,6 +337,11 @@ public class JmeApp
// this would render bounding boxes
// _display.getRenderer().drawBounds(_root);
// draw our stats atop everything
if (_stats != null) {
_display.getRenderer().draw(_stats);
}
}
/**
@@ -301,6 +364,15 @@ public class JmeApp
System.exit(0);
}
/**
* If true we'll display some renderer statistics at the bottom of the
* screen.
*/
protected boolean displayStatistics ()
{
return true;
}
/**
* Prepends the necessary bits onto the supplied path to properly
* locate it in our configuration directory.
@@ -329,7 +401,10 @@ public class JmeApp
protected DisplaySystem _display;
protected Camera _camera;
protected InputHandler _input;
protected Node _root;
protected LightState _lights;
protected StatsDisplay _stats;
protected int _failures;
@@ -0,0 +1,83 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.jme;
import com.jme.image.Texture;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Text;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
/**
* Tracks and displays render statistics.
*/
public class StatsDisplay extends Node
{
public StatsDisplay (Renderer renderer)
{
super("StatsNode");
// create an alpha state that will allow us to blend the text on
// top of whatever else is below
AlphaState astate = renderer.createAlphaState();
astate.setBlendEnabled(true);
astate.setSrcFunction(AlphaState.SB_SRC_ALPHA);
astate.setDstFunction(AlphaState.DB_ONE);
astate.setTestEnabled(true);
astate.setTestFunction(AlphaState.TF_GREATER);
astate.setEnabled(true);
// create a font texture
TextureState font = renderer.createTextureState();
font.setTexture(
TextureManager.loadTexture(
getClass().getClassLoader().getResource(DEFAULT_JME_FONT),
Texture.MM_LINEAR, Texture.FM_LINEAR));
font.setEnabled(true);
_text = new Text("StatsLabel", "");
_text.setForceView(true);
_text.setTextureCombineMode(TextureState.REPLACE);
attachChild(_text);
setRenderState(font);
setRenderState(astate);
}
public void update (Timer timer, Renderer renderer)
{
_stats.setLength(0);
_stats.append("FPS: ").append((int)timer.getFrameRate());
_stats.append(" - ").append(renderer.getStatistics(_temp));
_text.print(_stats);
}
protected Text _text;
protected StringBuffer _stats = new StringBuffer();
protected StringBuffer _temp = new StringBuffer();
protected static final String DEFAULT_JME_FONT =
"com/jme/app/defaultfont.tga";
}