Home | History | Annotate | Download | only in loaders
      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.assets.loaders;
     18 
     19 import java.util.Iterator;
     20 
     21 import com.badlogic.gdx.assets.AssetDescriptor;
     22 import com.badlogic.gdx.assets.AssetLoaderParameters;
     23 import com.badlogic.gdx.assets.AssetManager;
     24 import com.badlogic.gdx.files.FileHandle;
     25 import com.badlogic.gdx.graphics.Texture;
     26 import com.badlogic.gdx.graphics.g3d.Model;
     27 import com.badlogic.gdx.graphics.g3d.model.data.ModelData;
     28 import com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial;
     29 import com.badlogic.gdx.graphics.g3d.model.data.ModelTexture;
     30 import com.badlogic.gdx.graphics.g3d.utils.TextureProvider;
     31 import com.badlogic.gdx.utils.Array;
     32 import com.badlogic.gdx.utils.Disposable;
     33 import com.badlogic.gdx.utils.ObjectMap;
     34 
     35 public abstract class ModelLoader<P extends ModelLoader.ModelParameters> extends AsynchronousAssetLoader<Model, P> {
     36 	public ModelLoader (FileHandleResolver resolver) {
     37 		super(resolver);
     38 	}
     39 
     40 	protected Array<ObjectMap.Entry<String, ModelData>> items = new Array<ObjectMap.Entry<String, ModelData>>();
     41 	protected ModelParameters defaultParameters = new ModelParameters();
     42 
     43 	/** Directly load the raw model data on the calling thread. */
     44 	public abstract ModelData loadModelData (final FileHandle fileHandle, P parameters);
     45 
     46 	/** Directly load the raw model data on the calling thread. */
     47 	public ModelData loadModelData (final FileHandle fileHandle) {
     48 		return loadModelData(fileHandle, null);
     49 	}
     50 
     51 	/** Directly load the model on the calling thread. The model with not be managed by an {@link AssetManager}. */
     52 	public Model loadModel (final FileHandle fileHandle, TextureProvider textureProvider, P parameters) {
     53 		final ModelData data = loadModelData(fileHandle, parameters);
     54 		return data == null ? null : new Model(data, textureProvider);
     55 	}
     56 
     57 	/** Directly load the model on the calling thread. The model with not be managed by an {@link AssetManager}. */
     58 	public Model loadModel (final FileHandle fileHandle, P parameters) {
     59 		return loadModel(fileHandle, new TextureProvider.FileTextureProvider(), parameters);
     60 	}
     61 
     62 	/** Directly load the model on the calling thread. The model with not be managed by an {@link AssetManager}. */
     63 	public Model loadModel (final FileHandle fileHandle, TextureProvider textureProvider) {
     64 		return loadModel(fileHandle, textureProvider, null);
     65 	}
     66 
     67 	/** Directly load the model on the calling thread. The model with not be managed by an {@link AssetManager}. */
     68 	public Model loadModel (final FileHandle fileHandle) {
     69 		return loadModel(fileHandle, new TextureProvider.FileTextureProvider(), null);
     70 	}
     71 
     72 	@Override
     73 	public Array<AssetDescriptor> getDependencies (String fileName, FileHandle file, P parameters) {
     74 		final Array<AssetDescriptor> deps = new Array();
     75 		ModelData data = loadModelData(file, parameters);
     76 		if (data == null) return deps;
     77 
     78 		ObjectMap.Entry<String, ModelData> item = new ObjectMap.Entry<String, ModelData>();
     79 		item.key = fileName;
     80 		item.value = data;
     81 		synchronized (items) {
     82 			items.add(item);
     83 		}
     84 
     85 		TextureLoader.TextureParameter textureParameter = (parameters != null)
     86 				? parameters.textureParameter
     87 				: defaultParameters.textureParameter;
     88 
     89 		for (final ModelMaterial modelMaterial : data.materials) {
     90 			if (modelMaterial.textures != null) {
     91 				for (final ModelTexture modelTexture : modelMaterial.textures)
     92 					deps.add(new AssetDescriptor(modelTexture.fileName, Texture.class, textureParameter));
     93 			}
     94 		}
     95 		return deps;
     96 	}
     97 
     98 	@Override
     99 	public void loadAsync (AssetManager manager, String fileName, FileHandle file, P parameters) {
    100 	}
    101 
    102 	@Override
    103 	public Model loadSync (AssetManager manager, String fileName, FileHandle file, P parameters) {
    104 		ModelData data = null;
    105 		synchronized (items) {
    106 			for (int i = 0; i < items.size; i++) {
    107 				if (items.get(i).key.equals(fileName)) {
    108 					data = items.get(i).value;
    109 					items.removeIndex(i);
    110 				}
    111 			}
    112 		}
    113 		if (data == null) return null;
    114 		final Model result = new Model(data, new TextureProvider.AssetTextureProvider(manager));
    115 		// need to remove the textures from the managed disposables, or else ref counting
    116 		// doesn't work!
    117 		Iterator<Disposable> disposables = result.getManagedDisposables().iterator();
    118 		while (disposables.hasNext()) {
    119 			Disposable disposable = disposables.next();
    120 			if (disposable instanceof Texture) {
    121 				disposables.remove();
    122 			}
    123 		}
    124 		data = null;
    125 		return result;
    126 	}
    127 
    128 	static public class ModelParameters extends AssetLoaderParameters<Model> {
    129 		public TextureLoader.TextureParameter textureParameter;
    130 
    131 		public ModelParameters() {
    132 			textureParameter = new TextureLoader.TextureParameter();
    133 			textureParameter.minFilter = textureParameter.magFilter = Texture.TextureFilter.Linear;
    134 			textureParameter.wrapU = textureParameter.wrapV = Texture.TextureWrap.Repeat;
    135 		}
    136 	}
    137 }
    138