]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.jme/src/org/simantics/g3d/jme/shape/MeshNode.java
b6baa7982186c29739e35bc6f73988d84e191b6a
[simantics/3d.git] / org.simantics.g3d.jme / src / org / simantics / g3d / jme / shape / MeshNode.java
1 package org.simantics.g3d.jme.shape;\r
2 \r
3 import javax.vecmath.Vector3d;\r
4 \r
5 import org.simantics.g3d.shape.Color4d;\r
6 \r
7 import com.jme3.app.Application;\r
8 import com.jme3.material.Material;\r
9 import com.jme3.math.ColorRGBA;\r
10 import com.jme3.scene.Geometry;\r
11 import com.jme3.scene.Mesh;\r
12 import com.jme3.scene.VertexBuffer.Type;\r
13 \r
14 public class MeshNode extends Geometry {\r
15         Application app;\r
16         \r
17         \r
18         public MeshNode(Application app) {\r
19                 this.app = app;\r
20         }\r
21         \r
22         public void setMesh(org.simantics.g3d.shape.Mesh mesh) {\r
23                 \r
24                 Mesh shape = new Mesh();\r
25                 \r
26                 float points[] = new float[mesh.getVertices().size()*3];\r
27                 for (int i = 0; i < mesh.getVertices().size(); i++) {\r
28                         Vector3d v = mesh.getVertices().get(i);\r
29                         points[i * 3] = (float)v.x;\r
30                         points[i * 3+1] = (float)v.y;\r
31                         points[i * 3+2] = (float)v.z;\r
32                 }\r
33                 shape.setBuffer(Type.Position, 3, points);\r
34                 \r
35                 if (mesh.getNormals() == null)\r
36                         mesh.createNormals();\r
37                 float normals[] = new float[mesh.getNormals().size()*3];\r
38                 for (int i = 0; i < mesh.getNormals().size(); i++) {\r
39                         Vector3d v = mesh.getNormals().get(i);\r
40                         normals[i * 3] = (float)v.x;\r
41                         normals[i * 3+1] = (float)v.y;\r
42                         normals[i * 3+2] = (float)v.z;\r
43                 }\r
44                 shape.setBuffer(Type.Normal, 3, normals);\r
45                 \r
46                 int index[] = new int[mesh.getIndices().size()];\r
47                 for (int i = 0; i < index.length; i++) {\r
48                         index[i] = mesh.getIndices().get(i);\r
49                 }\r
50                 shape.setBuffer(Type.Index, 3, index);\r
51                 \r
52                 if (mesh.getColors() != null) {\r
53                         float colors[] = new float[mesh.getColors().size()*4];\r
54                         for (int i = 0; i < mesh.getColors().size(); i++) {\r
55                                 Color4d v = mesh.getColors().get(i);\r
56                                 colors[i * 4] = (float)v.x;\r
57                                 colors[i * 4+1] = (float)v.y;\r
58                                 colors[i * 4+2] = (float)v.z;\r
59                                 colors[i * 4+3] = (float)v.w;\r
60                         }\r
61                         shape.setBuffer(Type.Color, 4, colors);\r
62                 }\r
63                 \r
64                 setMesh(shape);\r
65                 updateModelBound();\r
66                 \r
67                 //Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");\r
68                 Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");\r
69                 \r
70                 \r
71                 if (true) {\r
72                         ColorRGBA color = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);\r
73                         mat.setColor("Diffuse", color);\r
74                         mat.setColor("Ambient", color);\r
75                         mat.setColor("Specular", new ColorRGBA(1, 1, 1, 1));\r
76                         mat.setBoolean("UseMaterialColors", true);\r
77                 }\r
78                 if (mesh.getColors() != null)\r
79                         mat.setBoolean("UseVertexColor", true);\r
80                 setMaterial(mat);\r
81             \r
82         }\r
83 \r
84 }\r