2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.opengl;
19 import java.io.InputStream;
20 import java.io.IOException;
21 import java.nio.Buffer;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import javax.microedition.khronos.opengles.GL10;
26 import android.content.res.AssetManager;
31 public class Texture {
33 private int width, height, bpp;
34 private ByteBuffer data;
35 private int name = -1;
37 // Texture maps have the following format. All integers
38 // are 16 bits, high byte first. Pixels are in 5/6/5
39 // RGB format, low byte first.
46 // pixel (width - 1, height - 1)
48 private int readInt16(InputStream is) throws IOException {
49 return is.read() | (is.read() << 8);
52 public Texture(InputStream is) throws IOException {
53 this.width = readInt16(is);
54 this.height = readInt16(is);
57 int npixels = width*height;
58 int nbytes = npixels*bpp;
59 byte[] arr = new byte[nbytes];
62 while (idx < nbytes) {
63 int nread = is.read(arr, idx, nbytes - idx);
67 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
68 // Swap pairs of bytes on big-endian platforms
69 for (int i = 0; i < npixels; i++) {
79 this.data = ByteBuffer.allocateDirect(arr.length);
80 this.data.order(ByteOrder.nativeOrder());
85 private int loadTexture(GL10 gl,
87 int minFilter, int magFilter,
90 int width, int height,
93 int[] texture = new int[1];
94 gl.glGenTextures(1, texture, 0);
96 gl.glEnable(gl.GL_TEXTURE_2D);
97 gl.glClientActiveTexture(textureUnit);
98 gl.glBindTexture(gl.GL_TEXTURE_2D, texture[0]);
99 gl.glTexParameterf(gl.GL_TEXTURE_2D,
100 gl.GL_TEXTURE_MIN_FILTER,
102 gl.glTexParameterf(gl.GL_TEXTURE_2D,
103 gl.GL_TEXTURE_MAG_FILTER,
105 gl.glTexParameterf(gl.GL_TEXTURE_2D,
106 gl.GL_TEXTURE_WRAP_S,
108 gl.glTexParameterf(gl.GL_TEXTURE_2D,
109 gl.GL_TEXTURE_WRAP_T,
111 gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, mode);
113 gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB,
115 0, gl.GL_RGB, dataType,
121 public void setTextureParameters(GL10 gl) {
123 name = loadTexture(gl,
125 gl.GL_NEAREST, gl.GL_NEAREST,
126 gl.GL_REPEAT, gl.GL_REPEAT,
129 gl.GL_UNSIGNED_SHORT_5_6_5,
133 gl.glBindTexture(gl.GL_TEXTURE_2D, name);