Creating common setup for both types of loaders.

Fixing a bug with loading objects from all layers when using BlenderModelLoader.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8043 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Kae..pl 13 years ago
parent daa86cfec7
commit 6c9395c4e9
  1. 141
      engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java
  2. 69
      engine/src/blender/com/jme3/scene/plugins/blender/BlenderModelLoader.java
  3. 48
      engine/src/blender/com/jme3/scene/plugins/blender/JmeConverter.java
  4. 2
      engine/src/test/jme3test/blender/ManualBlenderTester.java

@ -47,6 +47,7 @@ import com.jme3.asset.ModelKey;
import com.jme3.light.Light; import com.jme3.light.Light;
import com.jme3.renderer.Camera; import com.jme3.renderer.Camera;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.blender.animations.ArmatureHelper; import com.jme3.scene.plugins.blender.animations.ArmatureHelper;
import com.jme3.scene.plugins.blender.animations.IpoHelper; import com.jme3.scene.plugins.blender.animations.IpoHelper;
import com.jme3.scene.plugins.blender.cameras.CameraHelper; import com.jme3.scene.plugins.blender.cameras.CameraHelper;
@ -72,63 +73,22 @@ public class BlenderLoader implements AssetLoader {
private static final Logger LOGGER = Logger.getLogger(BlenderLoader.class.getName()); private static final Logger LOGGER = Logger.getLogger(BlenderLoader.class.getName());
/** Converter for blender structures. */
protected JmeConverter converter;
/** The data repository. */
protected DataRepository dataRepository;
/** The blender key to use. */
protected BlenderKey blenderKey;
/** The blocks read from the file. */
protected List<FileBlockHeader> blocks;
/** Blender input stream. */
protected BlenderInputStream inputStream;
@Override @Override
public LoadingResults load(AssetInfo assetInfo) throws IOException { public Spatial load(AssetInfo assetInfo) throws IOException {
try { try {
//registering loaders this.setup(assetInfo);
ModelKey modelKey = (ModelKey) assetInfo.getKey();
BlenderKey blenderKey;
if (modelKey instanceof BlenderKey) {
blenderKey = (BlenderKey) modelKey;
} else {
blenderKey = new BlenderKey(modelKey.getName());
blenderKey.setAssetRootPath(modelKey.getFolder());
}
//opening stream
BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream(), assetInfo.getManager());
//reading blocks
List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
FileBlockHeader fileBlock;
DataRepository dataRepository = new DataRepository();
dataRepository.setAssetManager(assetInfo.getManager());
dataRepository.setInputStream(inputStream);
dataRepository.setBlenderKey(blenderKey);
//creating helpers
dataRepository.putHelper(ArmatureHelper.class, new ArmatureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), dataRepository));
dataRepository.putHelper(IpoHelper.class, new IpoHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber()));
//setting additional data to helpers
if (blenderKey.isFixUpAxis()) {
ObjectHelper objectHelper = dataRepository.getHelper(ObjectHelper.class);
objectHelper.setyIsUpAxis(true);
CurvesHelper curvesHelper = dataRepository.getHelper(CurvesHelper.class);
curvesHelper.setyIsUpAxis(true);
}
MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class);
materialHelper.setFaceCullMode(blenderKey.getFaceCullMode());
//reading the blocks (dna block is automatically saved in the data repository when found)//TODO: zmienić to
do {
fileBlock = new FileBlockHeader(inputStream, dataRepository);
if (!fileBlock.isDnaBlock()) {
blocks.add(fileBlock);
}
} while (!fileBlock.isLastBlock());
JmeConverter converter = new JmeConverter(dataRepository);
LoadingResults loadingResults = blenderKey.prepareLoadingResults(); LoadingResults loadingResults = blenderKey.prepareLoadingResults();
WorldData worldData = null;// a set of data used in different scene aspects WorldData worldData = null;// a set of data used in different scene aspects
for (FileBlockHeader block : blocks) { for (FileBlockHeader block : blocks) {
@ -187,4 +147,77 @@ public class BlenderLoader implements AssetLoader {
} }
return null; return null;
} }
/**
* This method sets up the loader.
* @param assetInfo
* the asset info
* @throws BlenderFileException
* an exception is throw when something wrong happens with blender file
*/
protected void setup(AssetInfo assetInfo) throws BlenderFileException {
// registering loaders
ModelKey modelKey = (ModelKey) assetInfo.getKey();
if (modelKey instanceof BlenderKey) {
blenderKey = (BlenderKey) modelKey;
} else {
blenderKey = new BlenderKey(modelKey.getName());
blenderKey.setAssetRootPath(modelKey.getFolder());
}
// opening stream
inputStream = new BlenderInputStream(assetInfo.openStream(), assetInfo.getManager());
// reading blocks
blocks = new ArrayList<FileBlockHeader>();
FileBlockHeader fileBlock;
dataRepository = new DataRepository();
dataRepository.setAssetManager(assetInfo.getManager());
dataRepository.setInputStream(inputStream);
dataRepository.setBlenderKey(blenderKey);
// creating helpers
dataRepository.putHelper(ArmatureHelper.class, new ArmatureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), dataRepository));
dataRepository.putHelper(IpoHelper.class, new IpoHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber()));
// setting additional data to helpers
if (blenderKey.isFixUpAxis()) {
ObjectHelper objectHelper = dataRepository.getHelper(ObjectHelper.class);
objectHelper.setyIsUpAxis(true);
CurvesHelper curvesHelper = dataRepository.getHelper(CurvesHelper.class);
curvesHelper.setyIsUpAxis(true);
}
MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class);
materialHelper.setFaceCullMode(blenderKey.getFaceCullMode());
// reading the blocks (dna block is automatically saved in the data repository when found)//TODO: zmienić to
FileBlockHeader sceneFileBlock = null;
do {
fileBlock = new FileBlockHeader(inputStream, dataRepository);
if (!fileBlock.isDnaBlock()) {
blocks.add(fileBlock);
// save the scene's file block
if (fileBlock.getCode() == FileBlockHeader.BLOCK_SC00 && blenderKey.getLayersToLoad() < 0) {
sceneFileBlock = fileBlock;
}
}
} while (!fileBlock.isLastBlock());
// VERIFY LAYERS TO BE LOADED BEFORE LOADING FEATURES
if (sceneFileBlock != null) {
int lay = ((Number) sceneFileBlock.getStructure(dataRepository).getFieldValue("lay")).intValue();
dataRepository.getBlenderKey().setLayersToLoad(lay);// load only current layer
}
converter = new JmeConverter(dataRepository);
}
} }

@ -32,95 +32,30 @@
package com.jme3.scene.plugins.blender; package com.jme3.scene.plugins.blender;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.jme3.asset.AssetInfo; import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.BlenderKey;
import com.jme3.asset.BlenderKey.LoadingResults; import com.jme3.asset.BlenderKey.LoadingResults;
import com.jme3.asset.ModelKey;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.blender.animations.ArmatureHelper;
import com.jme3.scene.plugins.blender.animations.IpoHelper;
import com.jme3.scene.plugins.blender.cameras.CameraHelper;
import com.jme3.scene.plugins.blender.constraints.ConstraintHelper;
import com.jme3.scene.plugins.blender.curves.CurvesHelper;
import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
import com.jme3.scene.plugins.blender.file.BlenderInputStream;
import com.jme3.scene.plugins.blender.file.FileBlockHeader; import com.jme3.scene.plugins.blender.file.FileBlockHeader;
import com.jme3.scene.plugins.blender.lights.LightHelper;
import com.jme3.scene.plugins.blender.materials.MaterialHelper;
import com.jme3.scene.plugins.blender.meshes.MeshHelper;
import com.jme3.scene.plugins.blender.modifiers.ModifierHelper;
import com.jme3.scene.plugins.blender.objects.ObjectHelper;
import com.jme3.scene.plugins.blender.particles.ParticlesHelper;
import com.jme3.scene.plugins.blender.textures.TextureHelper;
/** /**
* This is the main loading class. Have in notice that asset manager needs to have loaders for resources like textures. * This is the main loading class. Have in notice that asset manager needs to have loaders for resources like textures.
* @author Marcin Roguski * @author Marcin Roguski
*/ */
public class BlenderModelLoader implements AssetLoader { public class BlenderModelLoader extends BlenderLoader {
private static final Logger LOGGER = Logger.getLogger(BlenderModelLoader.class.getName()); private static final Logger LOGGER = Logger.getLogger(BlenderModelLoader.class.getName());
@Override @Override
public Spatial load(AssetInfo assetInfo) throws IOException { public Spatial load(AssetInfo assetInfo) throws IOException {
try { try {
//registering loaders this.setup(assetInfo);
ModelKey modelKey = (ModelKey) assetInfo.getKey();
BlenderKey blenderKey;
if (modelKey instanceof BlenderKey) {
blenderKey = (BlenderKey) modelKey;
} else {
blenderKey = new BlenderKey(modelKey.getName());
blenderKey.setAssetRootPath(modelKey.getFolder());
}
//opening stream
BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream(), assetInfo.getManager());
List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
FileBlockHeader fileBlock;
DataRepository dataRepository = new DataRepository();
dataRepository.setAssetManager(assetInfo.getManager());
dataRepository.setInputStream(inputStream);
dataRepository.setBlenderKey(blenderKey);
dataRepository.putHelper(ArmatureHelper.class, new ArmatureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), dataRepository));
dataRepository.putHelper(IpoHelper.class, new IpoHelper(inputStream.getVersionNumber()));
dataRepository.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber()));
//setting additional data to helpers
if (blenderKey.isFixUpAxis()) {
ObjectHelper objectHelper = dataRepository.getHelper(ObjectHelper.class);
objectHelper.setyIsUpAxis(true);
CurvesHelper curvesHelper = dataRepository.getHelper(CurvesHelper.class);
curvesHelper.setyIsUpAxis(true);
}
MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class);
materialHelper.setFaceCullMode(blenderKey.getFaceCullMode());
//reading the blocks (dna block is automatically saved in the data repository when found)//TODO: zmienić to
do {
fileBlock = new FileBlockHeader(inputStream, dataRepository);
if (!fileBlock.isDnaBlock()) {
blocks.add(fileBlock);
}
} while (!fileBlock.isLastBlock());
JmeConverter converter = new JmeConverter(dataRepository);
LoadingResults loadingResults = blenderKey.prepareLoadingResults(); LoadingResults loadingResults = blenderKey.prepareLoadingResults();
for (FileBlockHeader block : blocks) { for (FileBlockHeader block : blocks) {
if (block.getCode() == FileBlockHeader.BLOCK_OB00) { if (block.getCode() == FileBlockHeader.BLOCK_OB00) {

@ -84,21 +84,25 @@ import com.jme3.scene.plugins.blender.objects.ObjectHelper;
this.dataRepository = dataRepository; this.dataRepository = dataRepository;
} }
public Node toScene(Structure structure) {//TODO: poprawny import sceny /**
* This method converts the given structure to a scene node.
* @param structure
* structure of a scene
* @return scene's node
*/
public Node toScene(Structure structure) {// TODO: import the scene
if ((dataRepository.getBlenderKey().getFeaturesToLoad() & FeaturesToLoad.SCENES) == 0) { if ((dataRepository.getBlenderKey().getFeaturesToLoad() & FeaturesToLoad.SCENES) == 0) {
return null; return null;
} }
Structure id = (Structure) structure.getFieldValue("id"); return new Node(structure.getName());
String sceneName = id.getFieldValue("name").toString();
//veryfying layers to be loaded
if (dataRepository.getBlenderKey().getLayersToLoad() < 0) {
int lay = ((Number) structure.getFieldValue("lay")).intValue();
dataRepository.getBlenderKey().setLayersToLoad(lay);//load only current layer
}
return new Node(sceneName);
} }
/**
* This method converts the given structure to a camera.
* @param structure
* structure of a camera
* @return camera's node
*/
public Camera toCamera(Structure structure) throws BlenderFileException { public Camera toCamera(Structure structure) throws BlenderFileException {
CameraHelper cameraHelper = dataRepository.getHelper(CameraHelper.class); CameraHelper cameraHelper = dataRepository.getHelper(CameraHelper.class);
if (cameraHelper.shouldBeLoaded(structure, dataRepository)) { if (cameraHelper.shouldBeLoaded(structure, dataRepository)) {
@ -107,6 +111,12 @@ import com.jme3.scene.plugins.blender.objects.ObjectHelper;
return null; return null;
} }
/**
* This method converts the given structure to a light.
* @param structure
* structure of a light
* @return light's node
*/
public Light toLight(Structure structure) throws BlenderFileException { public Light toLight(Structure structure) throws BlenderFileException {
LightHelper lightHelper = dataRepository.getHelper(LightHelper.class); LightHelper lightHelper = dataRepository.getHelper(LightHelper.class);
if (lightHelper.shouldBeLoaded(structure, dataRepository)) { if (lightHelper.shouldBeLoaded(structure, dataRepository)) {
@ -115,6 +125,12 @@ import com.jme3.scene.plugins.blender.objects.ObjectHelper;
return null; return null;
} }
/**
* This method converts the given structure to a node.
* @param structure
* structure of an object
* @return object's node
*/
public Object toObject(Structure structure) throws BlenderFileException { public Object toObject(Structure structure) throws BlenderFileException {
ObjectHelper objectHelper = dataRepository.getHelper(ObjectHelper.class); ObjectHelper objectHelper = dataRepository.getHelper(ObjectHelper.class);
if (objectHelper.shouldBeLoaded(structure, dataRepository)) { if (objectHelper.shouldBeLoaded(structure, dataRepository)) {
@ -123,6 +139,12 @@ import com.jme3.scene.plugins.blender.objects.ObjectHelper;
return null; return null;
} }
/**
* This method converts the given structure to a list of geometries.
* @param structure
* structure of a mesh
* @return list of geometries
*/
public List<Geometry> toMesh(Structure structure) throws BlenderFileException { public List<Geometry> toMesh(Structure structure) throws BlenderFileException {
MeshHelper meshHelper = dataRepository.getHelper(MeshHelper.class); MeshHelper meshHelper = dataRepository.getHelper(MeshHelper.class);
if (meshHelper.shouldBeLoaded(structure, dataRepository)) { if (meshHelper.shouldBeLoaded(structure, dataRepository)) {
@ -131,6 +153,12 @@ import com.jme3.scene.plugins.blender.objects.ObjectHelper;
return null; return null;
} }
/**
* This method converts the given structure to a material.
* @param structure
* structure of a material
* @return material's node
*/
public Material toMaterial(Structure structure) throws BlenderFileException { public Material toMaterial(Structure structure) throws BlenderFileException {
MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class); MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class);
if (materialHelper.shouldBeLoaded(structure, dataRepository)) { if (materialHelper.shouldBeLoaded(structure, dataRepository)) {

@ -187,7 +187,7 @@ public class ManualBlenderTester extends SimpleApplication {
Node blenderModel = null; Node blenderModel = null;
BlenderLoader blenderLoader = new BlenderLoader(); BlenderLoader blenderLoader = new BlenderLoader();
try { try {
LoadingResults loadingResults = blenderLoader.load(assetInfo); LoadingResults loadingResults = (LoadingResults) blenderLoader.load(assetInfo);
for (Node object : loadingResults.getObjects()) { for (Node object : loadingResults.getObjects()) {
this.rootNode.attachChild(object); this.rootNode.attachChild(object);
blenderModel = object; blenderModel = object;

Loading…
Cancel
Save