Home | History | Annotate | Download | only in model
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.graphics.g3d.model;
     18 
     19 import com.badlogic.gdx.graphics.g3d.Material;
     20 import com.badlogic.gdx.graphics.g3d.Model;
     21 import com.badlogic.gdx.graphics.g3d.ModelInstance;
     22 import com.badlogic.gdx.graphics.g3d.Renderable;
     23 import com.badlogic.gdx.math.Matrix4;
     24 import com.badlogic.gdx.utils.ArrayMap;
     25 
     26 /** A combination of {@link MeshPart} and {@link Material}, used to represent a {@link Node}'s graphical properties. A NodePart is
     27  * the smallest visible part of a {@link Model}, each NodePart implies a render call.
     28  * @author badlogic, Xoppa */
     29 public class NodePart {
     30 	/** The MeshPart (shape) to render. Must not be null. */
     31 	public MeshPart meshPart;
     32 	/** The Material used to render the {@link #meshPart}. Must not be null. */
     33 	public Material material;
     34 	/** Mapping to each bone (node) and the inverse transform of the bind pose. Will be used to fill the {@link #bones} array. May
     35 	 * be null. */
     36 	public ArrayMap<Node, Matrix4> invBoneBindTransforms;
     37 	/** The current transformation (relative to the bind pose) of each bone, may be null. When the part is skinned, this will be
     38 	 * updated by a call to {@link ModelInstance#calculateTransforms()}. Do not set or change this value manually. */
     39 	public Matrix4[] bones;
     40 	/** true by default. If set to false, this part will not participate in rendering and bounding box calculation. */
     41 	public boolean enabled = true;
     42 
     43 	/** Construct a new NodePart with null values. At least the {@link #meshPart} and {@link #material} member must be set before
     44 	 * the newly created part can be used. */
     45 	public NodePart () {
     46 	}
     47 
     48 	/** Construct a new NodePart referencing the provided {@link MeshPart} and {@link Material}.
     49 	 * @param meshPart The MeshPart to reference.
     50 	 * @param material The Material to reference. */
     51 	public NodePart (final MeshPart meshPart, final Material material) {
     52 		this.meshPart = meshPart;
     53 		this.material = material;
     54 	}
     55 
     56 	// FIXME add copy constructor and override #equals.
     57 
     58 	/** Convenience method to set the material, mesh, meshPartOffset, meshPartSize, primitiveType and bones members of the specified
     59 	 * Renderable. The other member of the provided {@link Renderable} remain untouched. Note that the material, mesh and bones
     60 	 * members are referenced, not copied. Any changes made to those objects will be reflected in both the NodePart and Renderable
     61 	 * object.
     62 	 * @param out The Renderable of which to set the members to the values of this NodePart. */
     63 	public Renderable setRenderable (final Renderable out) {
     64 		out.material = material;
     65 		out.meshPart.set(meshPart);
     66 		out.bones = bones;
     67 		return out;
     68 	}
     69 
     70 	public NodePart copy () {
     71 		return new NodePart().set(this);
     72 	}
     73 
     74 	protected NodePart set (NodePart other) {
     75 		meshPart = new MeshPart(other.meshPart);
     76 		material = other.material;
     77 		enabled = other.enabled;
     78 		if (other.invBoneBindTransforms == null) {
     79 			invBoneBindTransforms = null;
     80 			bones = null;
     81 		} else {
     82 			if (invBoneBindTransforms == null)
     83 				invBoneBindTransforms = new ArrayMap<Node, Matrix4>(true, other.invBoneBindTransforms.size, Node.class, Matrix4.class);
     84 			else
     85 				invBoneBindTransforms.clear();
     86 			invBoneBindTransforms.putAll(other.invBoneBindTransforms);
     87 
     88 			if (bones == null || bones.length != invBoneBindTransforms.size)
     89 				bones = new Matrix4[invBoneBindTransforms.size];
     90 
     91 			for (int i = 0; i < bones.length; i++) {
     92 				if (bones[i] == null)
     93 					bones[i] = new Matrix4();
     94 			}
     95 		}
     96 		return this;
     97 	}
     98 }
     99