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
17package com.badlogic.gdx.assets.loaders;
18
19import com.badlogic.gdx.assets.AssetDescriptor;
20import com.badlogic.gdx.assets.AssetLoaderParameters;
21import com.badlogic.gdx.assets.AssetManager;
22import com.badlogic.gdx.files.FileHandle;
23import com.badlogic.gdx.graphics.Cubemap;
24import com.badlogic.gdx.graphics.CubemapData;
25import com.badlogic.gdx.graphics.Pixmap;
26import com.badlogic.gdx.graphics.Pixmap.Format;
27import com.badlogic.gdx.graphics.PixmapIO;
28import com.badlogic.gdx.graphics.Texture;
29import com.badlogic.gdx.graphics.Texture.TextureFilter;
30import com.badlogic.gdx.graphics.Texture.TextureWrap;
31import com.badlogic.gdx.graphics.TextureData;
32import com.badlogic.gdx.graphics.glutils.ETC1TextureData;
33import com.badlogic.gdx.graphics.glutils.FileTextureData;
34import com.badlogic.gdx.graphics.glutils.KTXTextureData;
35import com.badlogic.gdx.utils.Array;
36
37/** {@link AssetLoader} for {@link Cubemap} instances. The pixel data is loaded asynchronously. The texture is then created on the
38 * rendering thread, synchronously. Passing a {@link CubemapParameter} to
39 * {@link AssetManager#load(String, Class, AssetLoaderParameters)} allows one to specify parameters as can be passed to the
40 * various Cubemap constructors, e.g. filtering and so on.
41 * @author mzechner, Vincent Bousquet */
42public class CubemapLoader extends AsynchronousAssetLoader<Cubemap, CubemapLoader.CubemapParameter> {
43	static public class CubemapLoaderInfo {
44		String filename;
45		CubemapData data;
46		Cubemap cubemap;
47	};
48
49	CubemapLoaderInfo info = new CubemapLoaderInfo();
50
51	public CubemapLoader (FileHandleResolver resolver) {
52		super(resolver);
53	}
54
55	@Override
56	public void loadAsync (AssetManager manager, String fileName, FileHandle file, CubemapParameter parameter) {
57		info.filename = fileName;
58		if (parameter == null || parameter.cubemapData == null) {
59			Pixmap pixmap = null;
60			Format format = null;
61			boolean genMipMaps = false;
62			info.cubemap = null;
63
64			if (parameter != null) {
65				format = parameter.format;
66				info.cubemap = parameter.cubemap;
67			}
68
69			if (fileName.contains(".ktx") || fileName.contains(".zktx")) {
70				info.data = new KTXTextureData(file, genMipMaps);
71			}
72		} else {
73			info.data = parameter.cubemapData;
74			info.cubemap = parameter.cubemap;
75		}
76		if (!info.data.isPrepared()) info.data.prepare();
77	}
78
79	@Override
80	public Cubemap loadSync (AssetManager manager, String fileName, FileHandle file, CubemapParameter parameter) {
81		if (info == null) return null;
82		Cubemap cubemap = info.cubemap;
83		if (cubemap != null) {
84			cubemap.load(info.data);
85		} else {
86			cubemap = new Cubemap(info.data);
87		}
88		if (parameter != null) {
89			cubemap.setFilter(parameter.minFilter, parameter.magFilter);
90			cubemap.setWrap(parameter.wrapU, parameter.wrapV);
91		}
92		return cubemap;
93	}
94
95	@Override
96	public Array<AssetDescriptor> getDependencies (String fileName, FileHandle file, CubemapParameter parameter) {
97		return null;
98	}
99
100	static public class CubemapParameter extends AssetLoaderParameters<Cubemap> {
101		/** the format of the final Texture. Uses the source images format if null **/
102		public Format format = null;
103		/** The texture to put the {@link TextureData} in, optional. **/
104		public Cubemap cubemap = null;
105		/** CubemapData for textures created on the fly, optional. When set, all format and genMipMaps are ignored */
106		public CubemapData cubemapData = null;
107		public TextureFilter minFilter = TextureFilter.Nearest;
108		public TextureFilter magFilter = TextureFilter.Nearest;
109		public TextureWrap wrapU = TextureWrap.ClampToEdge;
110		public TextureWrap wrapV = TextureWrap.ClampToEdge;
111	}
112}
113