1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.jme.shape;
\r
14 import javax.vecmath.Vector3d;
\r
16 import org.simantics.g3d.shape.Color4d;
\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
25 public class MeshNode extends Geometry {
\r
29 public MeshNode(Application app) {
\r
33 public void setMesh(org.simantics.g3d.shape.Mesh mesh) {
\r
35 Mesh shape = new Mesh();
\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
44 shape.setBuffer(Type.Position, 3, points);
\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
55 shape.setBuffer(Type.Normal, 3, normals);
\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
61 shape.setBuffer(Type.Index, 3, index);
\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
72 shape.setBuffer(Type.Color, 4, colors);
\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
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
89 if (mesh.getColors() != null)
\r
90 mat.setBoolean("UseVertexColor", true);
\r