159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.model.shape; 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector2f; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Geometry; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Mesh; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer.Type; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.BufferUtils; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * How to create custom meshes by specifying vertices 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * We render the mesh in three different ways, once with a solid blue color, 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * once with vertex colors, and once with a wireframe material. 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author KayTrance 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestCustomMesh extends SimpleApplication { 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static void main(String[] args){ 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta TestCustomMesh app = new TestCustomMesh(); 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta app.start(); 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleInitApp() { 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Mesh m = new Mesh(); 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Vertex positions in space 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f [] vertices = new Vector3f[4]; 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta vertices[0] = new Vector3f(0,0,0); 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta vertices[1] = new Vector3f(3,0,0); 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta vertices[2] = new Vector3f(0,3,0); 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta vertices[3] = new Vector3f(3,3,0); 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Texture coordinates 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector2f [] texCoord = new Vector2f[4]; 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texCoord[0] = new Vector2f(0,0); 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texCoord[1] = new Vector2f(1,0); 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texCoord[2] = new Vector2f(0,1); 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texCoord[3] = new Vector2f(1,1); 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Indexes. We define the order in which mesh should be constructed 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int [] indexes = {2,0,1,1,3,2}; 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Setting buffers 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord)); 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes)); 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta m.updateBound(); 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // First mesh uses one solid color 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Creating a geometry, and apply a single color material to it 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry geom = new Geometry("OurMesh", m); 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta mat.setColor("Color", ColorRGBA.Blue); 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta geom.setMaterial(mat); 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Attaching our geometry to the root node. 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(geom); 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Second mesh uses vertex colors to color each vertex 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Mesh cMesh = m.clone(); 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry coloredMesh = new Geometry ("ColoredMesh", cMesh); 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta matVC.setBoolean("VertexColor", true); 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //We have 4 vertices and 4 color values for each of them. 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //If you have more vertices, you need 'new float[yourVertexCount * 4]' here! 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] colorArray = new float[4*4]; 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int colorIndex = 0; 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for(int i = 0; i < 4; i++){ 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Red value (is increased by .2 on each next vertex here) 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta colorArray[colorIndex++]= 0.1f+(.2f*i); 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Green value (is reduced by .2 on each next vertex) 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta colorArray[colorIndex++]= 0.9f-(0.2f*i); 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Blue value (remains the same in our case) 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta colorArray[colorIndex++]= 0.5f; 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Alpha value (no transparency set here) 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta colorArray[colorIndex++]= 1.0f; 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Set the color buffer 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cMesh.setBuffer(Type.Color, 4, colorArray); 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta coloredMesh.setMaterial(matVC); 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // move mesh a bit so that it doesn't intersect with the first one 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta coloredMesh.setLocalTranslation(4, 0, 0); 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(coloredMesh); 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// /** Alternatively, you can show the mesh vertixes as points 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// * instead of coloring the faces. */ 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// cMesh.setMode(Mesh.Mode.Points); 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// cMesh.setPointSize(10f); 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// cMesh.updateBound(); 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// cMesh.setStatic(); 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// Geometry points = new Geometry("Points", m); 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// points.setMaterial(mat); 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// rootNode.attachChild(points); 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Third mesh will use a wireframe shader to show wireframe 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // ************************************************************************* 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Mesh wfMesh = m.clone(); 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh); 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta matWireframe.setColor("Color", ColorRGBA.Green); 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta matWireframe.getAdditionalRenderState().setWireframe(true); 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta wfGeom.setMaterial(matWireframe); 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta wfGeom.setLocalTranslation(4, 4, 0); 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(wfGeom); 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 154