1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 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. 15 * 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. 19 * 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. 31 */ 32 package com.jme3.scene.debug; 33 34 import com.jme3.bounding.BoundingSphere; 35 import com.jme3.math.FastMath; 36 import com.jme3.scene.Mesh; 37 import com.jme3.scene.Mesh.Mode; 38 import com.jme3.scene.VertexBuffer; 39 import com.jme3.scene.VertexBuffer.Format; 40 import com.jme3.scene.VertexBuffer.Type; 41 import com.jme3.scene.VertexBuffer.Usage; 42 import com.jme3.util.BufferUtils; 43 import java.nio.FloatBuffer; 44 import java.nio.ShortBuffer; 45 46 public class WireSphere extends Mesh { 47 48 private static final int samples = 30; 49 private static final int zSamples = 10; 50 51 public WireSphere() { 52 this(1); 53 } 54 55 public WireSphere(float radius) { 56 updatePositions(radius); 57 ShortBuffer ib = BufferUtils.createShortBuffer(samples * 2 * 2 + zSamples * samples * 2 /*+ 3 * 2*/); 58 setBuffer(Type.Index, 2, ib); 59 60 // ib.put(new byte[]{ 61 // (byte) 0, (byte) 1, 62 // (byte) 2, (byte) 3, 63 // (byte) 4, (byte) 5, 64 // }); 65 66 // int curNum = 3 * 2; 67 int curNum = 0; 68 for (int j = 0; j < 2 + zSamples; j++) { 69 for (int i = curNum; i < curNum + samples - 1; i++) { 70 ib.put((short) i).put((short) (i + 1)); 71 } 72 ib.put((short) (curNum + samples - 1)).put((short) curNum); 73 curNum += samples; 74 } 75 76 setMode(Mode.Lines); 77 78 updateBound(); 79 updateCounts(); 80 } 81 82 public void updatePositions(float radius) { 83 VertexBuffer pvb = getBuffer(Type.Position); 84 FloatBuffer pb; 85 86 if (pvb == null) { 87 pvb = new VertexBuffer(Type.Position); 88 pb = BufferUtils.createVector3Buffer(samples * 2 + samples * zSamples /*+ 6 * 3*/); 89 pvb.setupData(Usage.Dynamic, 3, Format.Float, pb); 90 setBuffer(pvb); 91 } else { 92 pb = (FloatBuffer) pvb.getData(); 93 } 94 95 pb.rewind(); 96 97 // X axis 98 // pb.put(radius).put(0).put(0); 99 // pb.put(-radius).put(0).put(0); 100 // 101 // // Y axis 102 // pb.put(0).put(radius).put(0); 103 // pb.put(0).put(-radius).put(0); 104 // 105 // // Z axis 106 // pb.put(0).put(0).put(radius); 107 // pb.put(0).put(0).put(-radius); 108 109 float rate = FastMath.TWO_PI / (float) samples; 110 float angle = 0; 111 for (int i = 0; i < samples; i++) { 112 float x = radius * FastMath.cos(angle); 113 float y = radius * FastMath.sin(angle); 114 pb.put(x).put(y).put(0); 115 angle += rate; 116 } 117 118 angle = 0; 119 for (int i = 0; i < samples; i++) { 120 float x = radius * FastMath.cos(angle); 121 float y = radius * FastMath.sin(angle); 122 pb.put(0).put(x).put(y); 123 angle += rate; 124 } 125 126 float zRate = (radius * 2) / (float) (zSamples); 127 float zHeight = -radius + (zRate / 2f); 128 129 130 float rb = 1f / zSamples; 131 float b = rb / 2f; 132 133 for (int k = 0; k < zSamples; k++) { 134 angle = 0; 135 float scale = FastMath.sin(b * FastMath.PI); 136 for (int i = 0; i < samples; i++) { 137 float x = radius * FastMath.cos(angle); 138 float y = radius * FastMath.sin(angle); 139 140 pb.put(x * scale).put(zHeight).put(y * scale); 141 142 angle += rate; 143 } 144 zHeight += zRate; 145 b += rb; 146 } 147 } 148 149 /** 150 * Create a WireSphere from a BoundingSphere 151 * 152 * @param bsph 153 * BoundingSphere used to create the WireSphere 154 * 155 */ 156 public void fromBoundingSphere(BoundingSphere bsph) { 157 updatePositions(bsph.getRadius()); 158 } 159 } 160