• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 package jme3test.model.shape;
34 
35 import com.jme3.app.SimpleApplication;
36 import com.jme3.material.Material;
37 import com.jme3.math.ColorRGBA;
38 import com.jme3.math.Vector2f;
39 import com.jme3.math.Vector3f;
40 import com.jme3.scene.Geometry;
41 import com.jme3.scene.Mesh;
42 import com.jme3.scene.VertexBuffer.Type;
43 import com.jme3.util.BufferUtils;
44 
45 /**
46  * How to create custom meshes by specifying vertices
47  * We render the mesh in three different ways, once with a solid blue color,
48  * once with vertex colors, and once with a wireframe material.
49  * @author KayTrance
50  */
51 public class TestCustomMesh extends SimpleApplication {
52 
main(String[] args)53     public static void main(String[] args){
54         TestCustomMesh app = new TestCustomMesh();
55         app.start();
56     }
57 
58     @Override
simpleInitApp()59     public void simpleInitApp() {
60 
61         Mesh m = new Mesh();
62 
63         // Vertex positions in space
64         Vector3f [] vertices = new Vector3f[4];
65         vertices[0] = new Vector3f(0,0,0);
66         vertices[1] = new Vector3f(3,0,0);
67         vertices[2] = new Vector3f(0,3,0);
68         vertices[3] = new Vector3f(3,3,0);
69 
70         // Texture coordinates
71         Vector2f [] texCoord = new Vector2f[4];
72         texCoord[0] = new Vector2f(0,0);
73         texCoord[1] = new Vector2f(1,0);
74         texCoord[2] = new Vector2f(0,1);
75         texCoord[3] = new Vector2f(1,1);
76 
77         // Indexes. We define the order in which mesh should be constructed
78         int [] indexes = {2,0,1,1,3,2};
79 
80         // Setting buffers
81         m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
82         m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
83         m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
84         m.updateBound();
85 
86         // *************************************************************************
87         // First mesh uses one solid color
88         // *************************************************************************
89 
90         // Creating a geometry, and apply a single color material to it
91         Geometry geom = new Geometry("OurMesh", m);
92         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
93         mat.setColor("Color", ColorRGBA.Blue);
94         geom.setMaterial(mat);
95 
96         // Attaching our geometry to the root node.
97         rootNode.attachChild(geom);
98 
99         // *************************************************************************
100         // Second mesh uses vertex colors to color each vertex
101         // *************************************************************************
102         Mesh cMesh = m.clone();
103         Geometry coloredMesh = new Geometry ("ColoredMesh", cMesh);
104         Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
105         matVC.setBoolean("VertexColor", true);
106 
107         //We have 4 vertices and 4 color values for each of them.
108         //If you have more vertices, you need 'new float[yourVertexCount * 4]' here!
109         float[] colorArray = new float[4*4];
110         int colorIndex = 0;
111 
112         //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f
113         for(int i = 0; i < 4; i++){
114            // Red value (is increased by .2 on each next vertex here)
115            colorArray[colorIndex++]= 0.1f+(.2f*i);
116            // Green value (is reduced by .2 on each next vertex)
117            colorArray[colorIndex++]= 0.9f-(0.2f*i);
118            // Blue value (remains the same in our case)
119            colorArray[colorIndex++]= 0.5f;
120            // Alpha value (no transparency set here)
121            colorArray[colorIndex++]= 1.0f;
122         }
123         // Set the color buffer
124         cMesh.setBuffer(Type.Color, 4, colorArray);
125         coloredMesh.setMaterial(matVC);
126         // move mesh a bit so that it doesn't intersect with the first one
127         coloredMesh.setLocalTranslation(4, 0, 0);
128         rootNode.attachChild(coloredMesh);
129 
130 //        /** Alternatively, you can show the mesh vertixes as points
131 //          * instead of coloring the faces. */
132 //        cMesh.setMode(Mesh.Mode.Points);
133 //        cMesh.setPointSize(10f);
134 //        cMesh.updateBound();
135 //        cMesh.setStatic();
136 //        Geometry points = new Geometry("Points", m);
137 //        points.setMaterial(mat);
138 //        rootNode.attachChild(points);
139 
140         // *************************************************************************
141         // Third mesh will use a wireframe shader to show wireframe
142         // *************************************************************************
143         Mesh wfMesh = m.clone();
144         Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh);
145         Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
146         matWireframe.setColor("Color", ColorRGBA.Green);
147         matWireframe.getAdditionalRenderState().setWireframe(true);
148         wfGeom.setMaterial(matWireframe);
149         wfGeom.setLocalTranslation(4, 4, 0);
150         rootNode.attachChild(wfGeom);
151 
152     }
153 }
154