blob: 1ea48fb7d447d922f3bb6997f77a7f0f70bf165d [file] [log] [blame]
/*
* Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package jme3test.model.shape;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.util.BufferUtils;
/**
* How to create custom meshes by specifying vertices
* We render the mesh in three different ways, once with a solid blue color,
* once with vertex colors, and once with a wireframe material.
* @author KayTrance
*/
public class TestCustomMesh extends SimpleApplication {
public static void main(String[] args){
TestCustomMesh app = new TestCustomMesh();
app.start();
}
@Override
public void simpleInitApp() {
Mesh m = new Mesh();
// Vertex positions in space
Vector3f [] vertices = new Vector3f[4];
vertices[0] = new Vector3f(0,0,0);
vertices[1] = new Vector3f(3,0,0);
vertices[2] = new Vector3f(0,3,0);
vertices[3] = new Vector3f(3,3,0);
// Texture coordinates
Vector2f [] texCoord = new Vector2f[4];
texCoord[0] = new Vector2f(0,0);
texCoord[1] = new Vector2f(1,0);
texCoord[2] = new Vector2f(0,1);
texCoord[3] = new Vector2f(1,1);
// Indexes. We define the order in which mesh should be constructed
int [] indexes = {2,0,1,1,3,2};
// Setting buffers
m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
m.updateBound();
// *************************************************************************
// First mesh uses one solid color
// *************************************************************************
// Creating a geometry, and apply a single color material to it
Geometry geom = new Geometry("OurMesh", m);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
// Attaching our geometry to the root node.
rootNode.attachChild(geom);
// *************************************************************************
// Second mesh uses vertex colors to color each vertex
// *************************************************************************
Mesh cMesh = m.clone();
Geometry coloredMesh = new Geometry ("ColoredMesh", cMesh);
Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matVC.setBoolean("VertexColor", true);
//We have 4 vertices and 4 color values for each of them.
//If you have more vertices, you need 'new float[yourVertexCount * 4]' here!
float[] colorArray = new float[4*4];
int colorIndex = 0;
//Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f
for(int i = 0; i < 4; i++){
// Red value (is increased by .2 on each next vertex here)
colorArray[colorIndex++]= 0.1f+(.2f*i);
// Green value (is reduced by .2 on each next vertex)
colorArray[colorIndex++]= 0.9f-(0.2f*i);
// Blue value (remains the same in our case)
colorArray[colorIndex++]= 0.5f;
// Alpha value (no transparency set here)
colorArray[colorIndex++]= 1.0f;
}
// Set the color buffer
cMesh.setBuffer(Type.Color, 4, colorArray);
coloredMesh.setMaterial(matVC);
// move mesh a bit so that it doesn't intersect with the first one
coloredMesh.setLocalTranslation(4, 0, 0);
rootNode.attachChild(coloredMesh);
// /** Alternatively, you can show the mesh vertixes as points
// * instead of coloring the faces. */
// cMesh.setMode(Mesh.Mode.Points);
// cMesh.setPointSize(10f);
// cMesh.updateBound();
// cMesh.setStatic();
// Geometry points = new Geometry("Points", m);
// points.setMaterial(mat);
// rootNode.attachChild(points);
// *************************************************************************
// Third mesh will use a wireframe shader to show wireframe
// *************************************************************************
Mesh wfMesh = m.clone();
Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh);
Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matWireframe.setColor("Color", ColorRGBA.Green);
matWireframe.getAdditionalRenderState().setWireframe(true);
wfGeom.setMaterial(matWireframe);
wfGeom.setLocalTranslation(4, 4, 0);
rootNode.attachChild(wfGeom);
}
}