From b1e8d60d18c333f2f6677d6937a3c3723b717534 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 20 Apr 2005 23:31:14 +0000 Subject: [PATCH] More juicy delicious 3D bits. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3514 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/jme/JmeApp.java | 85 +++++++++++++++++-- src/java/com/threerings/jme/StatsDisplay.java | 83 ++++++++++++++++++ 2 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 src/java/com/threerings/jme/StatsDisplay.java diff --git a/src/java/com/threerings/jme/JmeApp.java b/src/java/com/threerings/jme/JmeApp.java index ebba7d2e4..456944d8d 100644 --- a/src/java/com/threerings/jme/JmeApp.java +++ b/src/java/com/threerings/jme/JmeApp.java @@ -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; diff --git a/src/java/com/threerings/jme/StatsDisplay.java b/src/java/com/threerings/jme/StatsDisplay.java new file mode 100644 index 000000000..be1d1dca8 --- /dev/null +++ b/src/java/com/threerings/jme/StatsDisplay.java @@ -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"; +}