2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package jmetest.shape;
34 import java.util.logging.Logger;
36 import com.jme.app.SimpleGame;
37 import com.jme.image.Texture;
38 import com.jme.math.Quaternion;
39 import com.jme.math.Vector3f;
40 import com.jme.renderer.ColorRGBA;
41 import com.jme.renderer.Renderer;
42 import com.jme.scene.Controller;
43 import com.jme.scene.Spatial;
44 import com.jme.scene.TriMesh;
45 import com.jme.scene.shape.GeoSphere;
46 import com.jme.scene.shape.Sphere;
47 import com.jme.scene.state.BlendState;
48 import com.jme.scene.state.MaterialState;
49 import com.jme.scene.state.TextureState;
50 import com.jme.util.TextureManager;
55 public class TestGeoSphere extends SimpleGame {
56 private static final Logger logger = Logger.getLogger(TestGeoSphere.class
59 private TextureState textureState;
62 * Called near end of initGame(). Must be defined by derived classes.
64 protected void simpleInitGame() {
67 createGeoSpheres( true, -3, -1 );
68 createGeoSpheres( false, 0, -2 );
70 final TriMesh sphere1 = new Sphere( "sphere1", 3, 4, 1 );
71 logger.info( "Sphere triangles: " + sphere1.getTriangleCount() );
72 sphere1.getLocalTranslation().set( -2.5f, 3, 0 );
75 final TriMesh sphere2 = new Sphere( "sphere2", 4, 8, 1 );
76 logger.info( "Sphere triangles: " + sphere2.getTriangleCount() );
77 sphere2.getLocalTranslation().set( 0, 3, 0 );
80 final TriMesh sphere3 = new Sphere( "sphere3", 8, 11, 1 );
81 logger.info( "Sphere triangles: " + sphere3.getTriangleCount() );
82 sphere3.getLocalTranslation().set( 2.5f, 3, 0 );
85 final TriMesh sphere4 = new Sphere( "sphere4", 16, 18, 1 );
86 logger.info( "Sphere triangles: " + sphere4.getTriangleCount() );
87 sphere4.getLocalTranslation().set( 5f, 3, 0 );
91 private void createTextureState() {
92 textureState = display.getRenderer().createTextureState();
93 textureState.setEnabled(true);
94 Texture t1 = TextureManager.loadTexture(
95 TestGeoSphere.class.getClassLoader().getResource(
96 "jmetest/data/texture/clouds.png"), Texture.MinificationFilter.BilinearNearestMipMap,
97 Texture.MagnificationFilter.Bilinear);
98 textureState.setTexture(t1);
101 private void createGeoSpheres( boolean ikosa, float y, int offset ) {
102 for ( int level = 1; level <= 4; level++ ) {
103 final TriMesh geosphere = new GeoSphere( "geosphere", ikosa, level );
104 logger.info( "Geosphere (" + ( ikosa ? "ikosa" : "octa" ) + ") triangles: " + geosphere.getTriangleCount() );
105 geosphere.getLocalTranslation().set( ( level + offset ) * 2.5f, y, 0 );
110 private void init( TriMesh spatial ) {
111 BlendState alphaState = display.getRenderer().createBlendState();
112 alphaState.setEnabled( true );
113 alphaState.setBlendEnabled( true );
114 alphaState.setSourceFunction( BlendState.SourceFunction.SourceAlpha );
115 alphaState.setDestinationFunction( BlendState.DestinationFunction.OneMinusSourceAlpha );
117 spatial.addController( new RotatingController( spatial ) );
118 rootNode.attachChild( spatial );
119 spatial.setRenderQueueMode( Renderer.QUEUE_TRANSPARENT );
120 spatial.setRenderState( alphaState );
122 MaterialState material = display.getRenderer().createMaterialState();
123 material.setShininess( 128 );
124 ColorRGBA color = new ColorRGBA( 0.7f, 0.7f, 0.7f, 1f );
125 material.setDiffuse( color );
126 material.setAmbient( color.mult( new ColorRGBA( 0.1f, 0.1f, 0.1f, 1 ) ) );
127 spatial.setRenderState( material );
129 spatial.setRenderState( textureState );
130 // spatial.setRenderState( display.getRenderer().createWireframeState() );
133 public static void main( String[] args ) {
134 new TestGeoSphere().start();
137 private static class RotatingController extends Controller {
138 private static final long serialVersionUID = 1L;
139 private Quaternion rot;
140 private Vector3f axis;
141 private final Spatial spatial;
143 public RotatingController( Spatial spatial ) {
144 this.spatial = spatial;
145 rot = new Quaternion();
146 axis = new Vector3f( 1, 0, 0 ).normalizeLocal();
149 public void update( float time ) {
150 rot.fromAngleNormalAxis( 0.5f * time, axis );
151 spatial.getLocalRotation().multLocal( rot );