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 */
32package com.jme3.scene.debug;
33
34import com.jme3.math.Quaternion;
35import com.jme3.math.Vector3f;
36import com.jme3.scene.Mesh;
37import com.jme3.scene.VertexBuffer;
38import com.jme3.scene.VertexBuffer.Type;
39import java.nio.FloatBuffer;
40
41/**
42 * The <code>Arrow</code> debug shape represents an arrow.
43 * An arrow is simply a line going from the original toward an extent
44 * and at the tip there will be triangle-like shape.
45 *
46 * @author Kirill Vainer
47 */
48public class Arrow extends Mesh {
49
50    private Quaternion tempQuat = new Quaternion();
51    private Vector3f tempVec = new Vector3f();
52
53    private static final float[] positions = new float[]{
54        0, 0, 0,
55        0, 0, 1, // tip
56        0.05f, 0, 0.9f, // tip right
57        -0.05f, 0, 0.9f, // tip left
58        0, 0.05f, 0.9f, // tip top
59        0, -0.05f, 0.9f, // tip buttom
60    };
61
62    /**
63     * Serialization only. Do not use.
64     */
65    public Arrow() {
66    }
67
68    /**
69     * Creates an arrow mesh with the given extent.
70     * The arrow will start at the origin (0,0,0) and finish
71     * at the given extent.
72     *
73     * @param extent Extent of the arrow from origin
74     */
75    public Arrow(Vector3f extent) {
76        float len = extent.length();
77        Vector3f dir = extent.normalize();
78
79        tempQuat.lookAt(dir, Vector3f.UNIT_Y);
80        tempQuat.normalizeLocal();
81
82        float[] newPositions = new float[positions.length];
83        for (int i = 0; i < positions.length; i += 3) {
84            Vector3f vec = tempVec.set(positions[i],
85                    positions[i + 1],
86                    positions[i + 2]);
87            vec.multLocal(len);
88            tempQuat.mult(vec, vec);
89
90            newPositions[i] = vec.getX();
91            newPositions[i + 1] = vec.getY();
92            newPositions[i + 2] = vec.getZ();
93        }
94
95        setBuffer(Type.Position, 3, newPositions);
96        setBuffer(Type.Index, 2,
97                new short[]{
98                    0, 1,
99                    1, 2,
100                    1, 3,
101                    1, 4,
102                    1, 5,});
103        setMode(Mode.Lines);
104
105        updateBound();
106        updateCounts();
107    }
108
109    /**
110     * Sets the arrow's extent.
111     * This will modify the buffers on the mesh.
112     *
113     * @param extent the arrow's extent.
114     */
115    public void setArrowExtent(Vector3f extent) {
116        float len = extent.length();
117//        Vector3f dir = extent.normalize();
118
119        tempQuat.lookAt(extent, Vector3f.UNIT_Y);
120        tempQuat.normalizeLocal();
121
122        VertexBuffer pvb = getBuffer(Type.Position);
123        FloatBuffer buffer = (FloatBuffer)pvb.getData();
124        buffer.rewind();
125        for (int i = 0; i < positions.length; i += 3) {
126            Vector3f vec = tempVec.set(positions[i],
127                    positions[i + 1],
128                    positions[i + 2]);
129            vec.multLocal(len);
130            tempQuat.mult(vec, vec);
131
132            buffer.put(vec.x);
133            buffer.put(vec.y);
134            buffer.put(vec.z);
135        }
136
137        pvb.updateData(buffer);
138
139        updateBound();
140        updateCounts();
141    }
142}
143