View Javadoc
1   /*
2    * Created on 2005-01-13
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package org.sourceforge.jvb3d.Loader;
8   
9   import java.awt.image.BufferedImage;
10  import java.io.File;
11  import java.io.IOException;
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.NoSuchElementException;
15  import java.util.logging.Logger;
16  
17  import javax.imageio.ImageIO;
18  import javax.media.j3d.ImageComponent;
19  import javax.media.j3d.ImageComponent2D;
20  import javax.media.j3d.Texture;
21  import javax.media.j3d.Texture2D;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.parsers.ParserConfigurationException;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.NamedNodeMap;
28  import org.w3c.dom.NodeList;
29  import org.xml.sax.SAXException;
30  
31  /***
32   * @author Łukasz Krzyżak
33   * 
34   * klasa zarządza teksturami - odpowiada za ich wczytywanie, tworzenie i
35   * przekazywanie dalej
36   */
37  public class TextureManager {
38  	protected static final Logger logger = Logger
39  			.getLogger("SceneLoader.TextureManager");
40  
41  	class TextureInfo {
42  		protected String imageFile = null;
43  
44  		protected BufferedImage textureImage = null;
45  
46  		protected Texture2D texture = null;
47  
48  		protected int imageFormat = ImageComponent.FORMAT_RGBA;
49  
50  		protected int textureFormat = Texture.RGBA;
51  
52  		/***
53  		 * funkcja zwraca teksturę zdefiniowaną przez ten obiekt
54  		 * @return tekstura
55  		 * @throws IOException jesli nie powiodło się otwarcie pliku tekstury
56  		 */
57  		public Texture2D getTexture() throws IOException {
58  			if (texture != null)
59  				return texture;
60  
61  			if (textureImage == null) {
62  				logger.fine("Loading image " + imageFile);
63  				textureImage = ImageIO.read(new File(imageFile));
64  			}
65  			ImageComponent2D imageComponent = new ImageComponent2D(imageFormat,
66  					textureImage);
67  
68  			texture = new Texture2D(Texture.BASE_LEVEL, textureFormat,
69  					textureImage.getWidth(), textureImage.getHeight());
70  
71  			texture.setImage(0, imageComponent);
72  			return texture;
73  		}
74  	}
75  
76  	protected Map<String, TextureInfo> textures = new HashMap<String, TextureInfo>();
77  
78  	protected final static String texturesFile = "data/textures.xml";
79  
80  	protected static TextureManager instance = new TextureManager();
81  
82  	/***
83  	 * powoduje wczytanie konfiguracji z pliku
84  	 *  
85  	 */
86  	protected TextureManager() {
87  		try {
88  			parseXmlData();
89  		} catch (Exception e) {
90  			e.printStackTrace();
91  		}
92  	}
93  
94  	/***
95  	 * singleton - zwraca obiekt managera
96  	 * 
97  	 * @return obiekt managera
98  	 */
99  	public static TextureManager getInstance() {
100 		return instance;
101 	}
102 
103 	/***
104 	 * zwraca teksturę o podanym w parametrze ID, jeśli jeszcze jej nie było -
105 	 * zostaje utworzona nowa
106 	 * 
107 	 * @param textureId
108 	 *            ID tekstury
109 	 * @return tekstura
110 	 * @throws NoSuchElementException
111 	 *             jeśli tekstura / plik obrazu nie istnieją
112 	 */
113 	public Texture getTextureById(String textureId) {
114 		TextureInfo texInfo = textures.get(textureId);
115 		if (texInfo == null) {
116 			logger.severe("Texture " + textureId + " not found");
117 			throw new NoSuchElementException("Texture " + textureId
118 					+ " not found in data file");
119 		}
120 		Texture texture = null;
121 		try {
122 			texture = texInfo.getTexture();
123 		} catch (IOException e) {
124 			logger.severe("Image " + texInfo.imageFile + " not found");
125 			throw new NoSuchElementException("Image file for texture "
126 					+ textureId + " not found");
127 		}
128 		return texture;
129 	}
130 
131 	protected void parseXmlData() throws SAXException, IOException,
132 			ParserConfigurationException {
133 		Document dataDocument = DocumentBuilderFactory.newInstance()
134 				.newDocumentBuilder().parse(new File(texturesFile));
135 
136 		Element root = (Element) dataDocument.getElementsByTagName("Textures")
137 				.item(0);
138 		NodeList textureList = root.getElementsByTagName("Texture");
139 
140 		for (int i = 0; i < textureList.getLength(); i++) {
141 			Element texture = (Element) textureList.item(i);
142 
143 			TextureInfo textureInfo = new TextureInfo();
144 			textureInfo.imageFile = texture.getElementsByTagName("Image").item(
145 					0).getAttributes().getNamedItem("source").getNodeValue();
146 			String textureID = texture.getAttribute("id");
147 
148 			Element textureAttributes = (Element) texture.getElementsByTagName(
149 					"TextureFormat").item(0);
150 
151 			if (textureAttributes != null) {
152 				NamedNodeMap nodeMap = textureAttributes.getAttributes();
153 
154 				if (nodeMap.getNamedItem("imageFormat") != null)
155 					textureInfo.imageFormat = parseImageFormat(nodeMap
156 							.getNamedItem("imageFormat").getNodeValue());
157 
158 				if (nodeMap.getNamedItem("textureFormat") != null)
159 					textureInfo.textureFormat = parseTextureFormat(nodeMap
160 							.getNamedItem("textureFormat").getNodeValue());
161 			}
162 
163 			logger.finer("Texture " + textureID + " definition loaded");
164 			textures.put(textureID, textureInfo);
165 		}
166 	}
167 
168 	private int parseImageFormat(String type) {
169 		if (type.equals("channel8"))
170 			return ImageComponent.FORMAT_CHANNEL8;
171 		if (type.equals("lum4_alpha4"))
172 			return ImageComponent.FORMAT_LUM4_ALPHA4;
173 		if (type.equals("lum8_alpha8"))
174 			return ImageComponent.FORMAT_LUM8_ALPHA8;
175 		if (type.equals("r3g3b2"))
176 			return ImageComponent.FORMAT_R3_G3_B2;
177 		if (type.equals("rgb"))
178 			return ImageComponent.FORMAT_RGB;
179 		if (type.equals("rgb4"))
180 			return ImageComponent.FORMAT_RGB4;
181 		if (type.equals("rgb5"))
182 			return ImageComponent.FORMAT_RGB5;
183 		if (type.equals("rgb5_a1"))
184 			return ImageComponent.FORMAT_RGB5_A1;
185 		if (type.equals("rgb8"))
186 			return ImageComponent.FORMAT_RGB8;
187 		if (type.equals("rgba"))
188 			return ImageComponent.FORMAT_RGBA;
189 		if (type.equals("rgba4"))
190 			return ImageComponent.FORMAT_RGBA4;
191 		if (type.equals("rgba8"))
192 			return ImageComponent.FORMAT_RGBA8;
193 		return 0;
194 	}
195 
196 	private int parseTextureFormat(String type) {
197 		if (type.equals("intensity"))
198 			return Texture.INTENSITY;
199 		if (type.equals("luminance"))
200 			return Texture.LUMINANCE;
201 		if (type.equals("luminance_alpha"))
202 			return Texture.LUMINANCE_ALPHA;
203 		if (type.equals("alpha"))
204 			return Texture.ALPHA;
205 		if (type.equals("rgb"))
206 			return Texture.RGB;
207 		if (type.equals("rgba"))
208 			return Texture.RGBA;
209 		return 0;
210 	}
211 }