aboutsummaryrefslogtreecommitdiff
path: root/engine/src/android/com/jme3/renderer/android/TextureUtil.java
blob: 53b96b400392f302f008751e084dd0937d72fa2a (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.jme3.renderer.android;

import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import com.jme3.asset.AndroidImageInfo;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
import java.nio.ByteBuffer;
import javax.microedition.khronos.opengles.GL10;

public class TextureUtil {

    public static int convertTextureFormat(Format fmt){
        switch (fmt){
            case Alpha16:
            case Alpha8:
                return GL10.GL_ALPHA;
            case Luminance8Alpha8:
            case Luminance16Alpha16:
                return GL10.GL_LUMINANCE_ALPHA;
            case Luminance8:
            case Luminance16:
                return GL10.GL_LUMINANCE;
            case RGB10:
            case RGB16:
            case BGR8:
            case RGB8:
            case RGB565:
                return GL10.GL_RGB;
            case RGB5A1:
            case RGBA16:
            case RGBA8:
                return GL10.GL_RGBA;
                
            case Depth:
                return GLES20.GL_DEPTH_COMPONENT;
            case Depth16:
                return GLES20.GL_DEPTH_COMPONENT16;
            case Depth24:
            case Depth32:
            case Depth32F:
                throw new UnsupportedOperationException("Unsupported depth format: " + fmt);   
                
            case DXT1A:
                throw new UnsupportedOperationException("Unsupported format: " + fmt);
            default:
                throw new UnsupportedOperationException("Unrecognized format: " + fmt);
        }
    }

    private static void buildMipmap(Bitmap bitmap) {
        int level = 0;
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();

        while (height >= 1 || width >= 1) {
            //First of all, generate the texture from our bitmap and set it to the according level
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);

            if (height == 1 || width == 1) {
                break;
            }

            //Increase the mipmap level
            level++;

            height /= 2;
            width /= 2;
            Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

            bitmap.recycle();
            bitmap = bitmap2;
        }
    }

    /**
     * <code>uploadTextureBitmap</code> uploads a native android bitmap
     * @param target
     * @param bitmap
     * @param generateMips
     * @param powerOf2
     */
    public static void uploadTextureBitmap(final int target, Bitmap bitmap, boolean generateMips, boolean powerOf2)
    {
        if (!powerOf2)
        {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (!FastMath.isPowerOfTwo(width) || !FastMath.isPowerOfTwo(height))
            {
                // scale to power of two
                width = FastMath.nearestPowerOfTwo(width);
                height = FastMath.nearestPowerOfTwo(height);
                Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);
                bitmap.recycle();
                bitmap = bitmap2;
            }
        }

        if (generateMips)
        {
            buildMipmap(bitmap);
        }
        else
        {
            GLUtils.texImage2D(target, 0, bitmap, 0);
            //bitmap.recycle();
        }
    }

    public static void uploadTexture(
                                     Image img,
                                     int target,
                                     int index,
                                     int border,
                                     boolean tdc,
                                     boolean generateMips,
                                     boolean powerOf2){

        if (img.getEfficentData() instanceof AndroidImageInfo){
            // If image was loaded from asset manager, use fast path
            AndroidImageInfo imageInfo = (AndroidImageInfo) img.getEfficentData();
            uploadTextureBitmap(target, imageInfo.getBitmap(), generateMips, powerOf2);
            return;
        }

        // Otherwise upload image directly. 
        // Prefer to only use power of 2 textures here to avoid errors.
        
        Image.Format fmt = img.getFormat();
        ByteBuffer data;
        if (index >= 0 || img.getData() != null && img.getData().size() > 0){
            data = img.getData(index);
        }else{
            data = null;
        }

        int width = img.getWidth();
        int height = img.getHeight();
        int depth = img.getDepth();

        boolean compress = false;
        int internalFormat = -1;
        int format = -1;
        int dataType = -1;

        switch (fmt){
            case Alpha16:
            case Alpha8:
                format = GLES20.GL_ALPHA;
                dataType = GLES20.GL_UNSIGNED_BYTE;                
                break;
            case Luminance8:
                format = GLES20.GL_LUMINANCE;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case Luminance8Alpha8:
                format = GLES20.GL_LUMINANCE_ALPHA;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case Luminance16Alpha16:
                format = GLES20.GL_LUMINANCE_ALPHA;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case Luminance16:
                format = GLES20.GL_LUMINANCE;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case RGB565:
                format = GLES20.GL_RGB;
                internalFormat = GLES20.GL_RGB565;
                dataType = GLES20.GL_UNSIGNED_SHORT_5_6_5;
                break;
            case ARGB4444:
                format = GLES20.GL_RGBA;
                dataType = GLES20.GL_UNSIGNED_SHORT_4_4_4_4;
                break;
            case RGB10:
                format = GLES20.GL_RGB;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case RGB16:
                format = GLES20.GL_RGB;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case RGB5A1:
                format = GLES20.GL_RGBA;
                internalFormat = GLES20.GL_RGB5_A1;
                dataType = GLES20.GL_UNSIGNED_SHORT_5_5_5_1;
                break;
            case RGB8:
                format = GLES20.GL_RGB;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case BGR8:
                format = GLES20.GL_RGB;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case RGBA16:
                format = GLES20.GL_RGBA;
                internalFormat = GLES20.GL_RGBA4;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case RGBA8:
                format = GLES20.GL_RGBA;                
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case DXT1A:
                format = GLES20.GL_COMPRESSED_TEXTURE_FORMATS;
                dataType = GLES20.GL_UNSIGNED_BYTE;
            case Depth:
                format = GLES20.GL_DEPTH_COMPONENT;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case Depth16:
                format = GLES20.GL_DEPTH_COMPONENT;
                internalFormat = GLES20.GL_DEPTH_COMPONENT16;
                dataType = GLES20.GL_UNSIGNED_BYTE;
                break;
            case Depth24:
            case Depth32:
            case Depth32F:
                throw new UnsupportedOperationException("Unsupported depth format: " + fmt);                
            default:
                throw new UnsupportedOperationException("Unrecognized format: " + fmt);
        }
        
        if (internalFormat == -1)
        {
            internalFormat = format;
        }

        if (data != null)
            GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);

        int[] mipSizes = img.getMipMapSizes();
        int pos = 0;
        if (mipSizes == null){
            if (data != null)
                mipSizes = new int[]{ data.capacity() };
            else
                mipSizes = new int[]{ width * height * fmt.getBitsPerPixel() / 8 };
        }

        // XXX: might want to change that when support
        // of more than paletted compressions is added..
        if (compress){
            data.clear();
            GLES20.glCompressedTexImage2D(GLES20.GL_TEXTURE_2D,
                                      1 - mipSizes.length,
                                      format,
                                      width,
                                      height,
                                      0,
                                      data.capacity(),
                                      data);
            return;
        }

        for (int i = 0; i < mipSizes.length; i++){
            int mipWidth =  Math.max(1, width  >> i);
            int mipHeight = Math.max(1, height >> i);
            int mipDepth =  Math.max(1, depth  >> i);

            if (data != null){
                data.position(pos);
                data.limit(pos + mipSizes[i]);
            }

            if (compress && data != null){
                GLES20.glCompressedTexImage2D(GLES20.GL_TEXTURE_2D,
                                          i,
                                          format,
                                          mipWidth,
                                          mipHeight,
                                          0,
                                          data.remaining(),
                                          data);
            }else{
                GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,
                                i,
                                internalFormat,
                                mipWidth,
                                mipHeight,
                                0,
                                format,
                                dataType,
                                data);
            }

            pos += mipSizes[i];
        }
    }

}