Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package min3d.core;

import java.util.HashMap;
import java.util.Set;

import min3d.Shared;
import android.graphics.Bitmap;

/**
 * TextureManager is responsible for managing textures for the whole environment. 
 * It maintains a list of id's that are mapped to the GL texture names (id's).
 * 
 * You add a Bitmap to the TextureManager, which adds a textureId to its list.
 * Then, you assign one or more TextureVo's to your Object3d's using id's that 
 * exist in the TextureManager.
 * 
 * Note that the _idToTextureName and _idToHasMipMap HashMaps used below
 * don't test for exceptions. 
 */
public class TextureManager 
{
	private HashMap<String, Integer> _idToTextureName;
	private HashMap<String, Boolean> _idToHasMipMap;
	private static int _counter = 1000001;
	private static int _atlasId = 0;
	
	
	public TextureManager()
	{
		reset();
	}

	public void reset()
	{
		// Delete any extant textures

		if (_idToTextureName != null) 
		{
			Set<String> s = _idToTextureName.keySet();
			Object[] a = s.toArray(); 
			for (int i = 0; i < a.length; i++) {
				int glId = getGlTextureId((String)a[i]);
				Shared.renderer().deleteTexture(glId);
			}
			// ...pain
		}
		
		_idToTextureName = new HashMap<String, Integer>();
		_idToHasMipMap = new HashMap<String, Boolean>();
	}

	/**
	 * 'Uploads' a texture via OpenGL which is mapped to a textureId to the TextureManager, 
	 * which can subsequently be used to assign textures to Object3d's. 
	 * 
	 * @return The textureId as added to TextureManager, which is identical to $id 
	 */
	public String addTextureId(Bitmap $b, String $id, boolean $generateMipMap)
	{
		if (_idToTextureName.containsKey($id)) throw new Error("Texture id \"" + $id + "\" already exists."); 

		int glId = Shared.renderer().uploadTextureAndReturnId($b, $generateMipMap);

		String s = $id;
		_idToTextureName.put(s, glId);
		_idToHasMipMap.put(s, $generateMipMap);
	
		_counter++;
		
		// For debugging purposes (potentially adds a lot of chatter)
		// logContents();
		
		return s;
	}

	/**
	 * Alternate signature for "addTextureId", with MIP mapping set to false by default.
	 * Kept for API backward-compatibility. 
	 */
	public String addTextureId(Bitmap $b, String $id)
	{
		return this.addTextureId($b, $id, false);
	}
	
	/**
	 * 'Uploads' texture via OpenGL and returns an autoassigned textureId,
	 * which can be used to assign textures to Object3d's. 
	 */
	public String createTextureId(Bitmap $b, boolean $generateMipMap)
	{
		return addTextureId($b, (_counter+""), $generateMipMap);
	}
	
	/**
	 * Deletes a textureId from the TextureManager,  
	 * and deletes the corresponding texture from the GPU
	 */
	public void deleteTexture(String $textureId)
	{
		int glId = _idToTextureName.get($textureId);
		Shared.renderer().deleteTexture(glId);
		_idToTextureName.remove($textureId);
		_idToHasMipMap.remove($textureId);
		
		// logContents();
		
		//xxx needs error check
	}

	/**
	 * Returns a String Array of textureId's in the TextureManager 
	 */
	public String[] getTextureIds()
	{
		Set<String> set = _idToTextureName.keySet();
		String[] a = new String[set.size()];
		set.toArray(a);
		return a;
	}
	
	/**
	 * Used by Renderer
	 * 
	 */
	int getGlTextureId(String $textureId) /*package-private*/
	{
		return _idToTextureName.get($textureId);
	}
	
	/**
	 * Used by Renderer
	 */
	boolean hasMipMap(String $textureId) /*package-private*/
	{
		return _idToHasMipMap.get($textureId);
	}
	

	public boolean contains(String $textureId)
	{
		return _idToTextureName.containsKey($textureId);
	}
	
	/*
	private String arrayToString(String[] $a)
	{
		String s = "";
		for (int i = 0; i < $a.length; i++)
		{
			s += $a[i].toString() + " | ";
		}
		return s;
	}
	
	private void logContents()
	{
		Log.v(Min3d.TAG, "TextureManager contents updated - " + arrayToString( getTextureIds() ) );		
	}
	*/
	public String getNewAtlasId() {
		return "atlas".concat(Integer.toString(_atlasId++));
	}
}

Commits for Nextrek/Android/LibrerieNextrek/src/min3d/core/TextureManager.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000