]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.jme/src/org/simantics/g3d/jme/shape/MeshNode.java
Remove dependencies on log4j
[simantics/3d.git] / org.simantics.g3d.jme / src / org / simantics / g3d / jme / shape / MeshNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.jme.shape;\r
13 \r
14 import javax.vecmath.Vector3d;\r
15 \r
16 import org.simantics.g3d.shape.Color4d;\r
17 \r
18 import com.jme3.app.Application;\r
19 import com.jme3.material.Material;\r
20 import com.jme3.math.ColorRGBA;\r
21 import com.jme3.scene.Geometry;\r
22 import com.jme3.scene.Mesh;\r
23 import com.jme3.scene.VertexBuffer.Type;\r
24 \r
25 public class MeshNode extends Geometry {\r
26         Application app;\r
27         \r
28         \r
29         public MeshNode(Application app) {\r
30                 this.app = app;\r
31         }\r
32         \r
33         public void setMesh(org.simantics.g3d.shape.Mesh mesh) {\r
34                 \r
35                 Mesh shape = new Mesh();\r
36                 \r
37                 float points[] = new float[mesh.getVertices().size()*3];\r
38                 for (int i = 0; i < mesh.getVertices().size(); i++) {\r
39                         Vector3d v = mesh.getVertices().get(i);\r
40                         points[i * 3] = (float)v.x;\r
41                         points[i * 3+1] = (float)v.y;\r
42                         points[i * 3+2] = (float)v.z;\r
43                 }\r
44                 shape.setBuffer(Type.Position, 3, points);\r
45                 \r
46                 if (mesh.getNormals() == null)\r
47                         mesh.createNormals();\r
48                 float normals[] = new float[mesh.getNormals().size()*3];\r
49                 for (int i = 0; i < mesh.getNormals().size(); i++) {\r
50                         Vector3d v = mesh.getNormals().get(i);\r
51                         normals[i * 3] = (float)v.x;\r
52                         normals[i * 3+1] = (float)v.y;\r
53                         normals[i * 3+2] = (float)v.z;\r
54                 }\r
55                 shape.setBuffer(Type.Normal, 3, normals);\r
56                 \r
57                 int index[] = new int[mesh.getIndices().size()];\r
58                 for (int i = 0; i < index.length; i++) {\r
59                         index[i] = mesh.getIndices().get(i);\r
60                 }\r
61                 shape.setBuffer(Type.Index, 3, index);\r
62                 \r
63                 if (mesh.getColors() != null) {\r
64                         float colors[] = new float[mesh.getColors().size()*4];\r
65                         for (int i = 0; i < mesh.getColors().size(); i++) {\r
66                                 Color4d v = mesh.getColors().get(i);\r
67                                 colors[i * 4] = (float)v.x;\r
68                                 colors[i * 4+1] = (float)v.y;\r
69                                 colors[i * 4+2] = (float)v.z;\r
70                                 colors[i * 4+3] = (float)v.w;\r
71                         }\r
72                         shape.setBuffer(Type.Color, 4, colors);\r
73                 }\r
74                 \r
75                 setMesh(shape);\r
76                 updateModelBound();\r
77                 \r
78                 //Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");\r
79                 Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");\r
80                 \r
81                 \r
82                 if (true) {\r
83                         ColorRGBA color = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);\r
84                         mat.setColor("Diffuse", color);\r
85                         mat.setColor("Ambient", color);\r
86                         mat.setColor("Specular", new ColorRGBA(1, 1, 1, 1));\r
87                         mat.setBoolean("UseMaterialColors", true);\r
88                 }\r
89                 if (mesh.getColors() != null)\r
90                         mat.setBoolean("UseVertexColor", true);\r
91                 setMaterial(mat);\r
92             \r
93         }\r
94 \r
95 }\r