Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e97df97b authored by Romain Guy's avatar Romain Guy Committed by Android (Google) Code Review
Browse files

Merge "Remove unused private APIs"

parents e4895352 97eda365
Loading
Loading
Loading
Loading
+0 −155
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import javax.microedition.khronos.opengles.*;

class MaterialIndices {

    private Material material = null;
    private ShortBuffer indexBuffer = null;

    public MaterialIndices(Material material, ShortBuffer indexBuffer) {
        this.material = material;
        this.indexBuffer = indexBuffer;
    }

    public Material getMaterial() {
        return material;
    }

    public ShortBuffer getIndexBuffer() {
        return indexBuffer;
    }
}

/**
 * {@hide}
 */
public class Group {

    private Object3D parent;
    private String name;

    private List<MaterialIndices> materialIndices =
        new ArrayList<MaterialIndices>();

    public Group(Object3D parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public void load(DataInputStream dis) throws IOException {
        dis.readInt(); // name length
        this.name = dis.readUTF();

        int numMaterials = dis.readInt();

        for (int i = 0; i < numMaterials; i++) {
            dis.readInt(); // material name length
            String matName = dis.readUTF();
            Material material = parent.getMaterial(matName);

            int numIndices = dis.readInt();
            byte[] indicesBytes = new byte[numIndices * 2];
            dis.readFully(indicesBytes);

            // Swap bytes from network to native order if necessary
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                int idx = 0;
                for (int j = 0; j < numIndices; j++) {
                    byte b0 = indicesBytes[idx];
                    byte b1 = indicesBytes[idx + 1];
                    indicesBytes[idx] = b1;
                    indicesBytes[idx + 1] = b0;
                    idx += 2;
                }
            }

            ByteBuffer ibb = ByteBuffer.allocateDirect(2*numIndices);
            ibb.order(ByteOrder.nativeOrder());
            ibb.put(indicesBytes);
            ibb.position(0);

            ShortBuffer sb = ibb.asShortBuffer();
            materialIndices.add(new MaterialIndices(material, sb));
        }
    }

    public int getNumTriangles() {
        int numTriangles = 0;
        Iterator<MaterialIndices> iter = materialIndices.iterator();
        while (iter.hasNext()) {
            MaterialIndices matIdx = iter.next();
            ShortBuffer indexBuffer = matIdx.getIndexBuffer();
            numTriangles += indexBuffer.capacity()/3;
        }
        return numTriangles;
    }

    public void draw(GL10 gl) {
        gl.glDisableClientState(gl.GL_COLOR_ARRAY);

        gl.glVertexPointer(3, gl.GL_FIXED, 0, parent.getVertexBuffer());
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);

        gl.glNormalPointer(gl.GL_FIXED, 0, parent.getNormalBuffer());
        gl.glEnableClientState(gl.GL_NORMAL_ARRAY);

        if (parent.hasTexcoords()) {
            gl.glTexCoordPointer(2, gl.GL_FIXED, 0, parent.getTexcoordBuffer());
            gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);
            gl.glEnable(gl.GL_TEXTURE_2D);
        } else {
            gl.glDisable(gl.GL_TEXTURE_2D);
        }

        Iterator<MaterialIndices> iter = materialIndices.iterator();
        while (iter.hasNext()) {
            MaterialIndices matIdx = iter.next();
            ShortBuffer indexBuffer = matIdx.getIndexBuffer();
            Material mat = matIdx.getMaterial();
            mat.setMaterialParameters(gl);
            if (parent.hasTexcoords() && mat.getMap_Kd().length() > 0) {
                Texture texture = parent.getTexture(mat.getMap_Kd());
                texture.setTextureParameters(gl);
            }

            gl.glDrawElements(gl.GL_TRIANGLES,
                    indexBuffer.capacity(),
                    gl.GL_UNSIGNED_SHORT,
                    indexBuffer);
        }
    }

    public String toString() {
        return "Group[" +
        "name=" + name +
        "]";
    }
}
+0 −120
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.opengl;

import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.khronos.opengles.GL10;

/**
 * {@hide}
 */
public class Material {

    private Object3D parent;
    private String name;
    private String map_kd;
    private float[] ka = new float[4];
    private float[] kd = new float[4];
    private float[] ks = new float[4];
    private float ns;
    private int illum;
    private float d;

    private static float[] black = { 0.0f, 0.0f, 0.0f, 1.0f };

    public Material(Object3D parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public String getMap_Kd() {
        return map_kd;
    }

    public void setMaterialParameters(GL10 gl) {
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
        gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
                Math.min(Math.max(ns, 0), 128));

//      if (illum == 0) {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
//      } else {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, ka, 0);
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
//      }

//      if (illum > 1) {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
//      gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
//      Math.min(Math.max(ns, 0), 128));
//      } else {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, black, 0);
//      }
    }

    public void load(DataInputStream dis) throws IOException {
        dis.readInt(); // name length
        this.name = dis.readUTF();

        dis.readInt(); // map_kdLength
        this.map_kd = dis.readUTF();

        if (parent.hasTexcoords() && map_kd.length() > 0) {
            parent.loadTexture(map_kd);
        }

        this.ka[0] = dis.readFloat();
        this.ka[1] = dis.readFloat();
        this.ka[2] = dis.readFloat();
        this.ka[3] = dis.readFloat();

        this.kd[0] = dis.readFloat();
        this.kd[1] = dis.readFloat();
        this.kd[2] = dis.readFloat();
        this.kd[3] = dis.readFloat();

        this.ks[0] = dis.readFloat();
        this.ks[1] = dis.readFloat();
        this.ks[2] = dis.readFloat();
        this.ks[3] = dis.readFloat();

        this.ns = dis.readFloat();
        this.illum = dis.readInt();
        this.d = dis.readFloat();
    }

    public String toString() {
        return "Material[" +
        "name=\"" + name + "\"," +
        "ka={" + ka[0] + "," + ka[1] + "," + ka[2] + "}," +
        "kd={" + kd[0] + "," + kd[1] + "," + kd[2] + "}," +
        "ks={" + ks[0] + "," + ks[1] + "," + ks[2] + "}," +
        "ns=" + ns + "," +
        "map_kd=\"" + 
        (map_kd == null ? "" : map_kd) +
        "\"," +
        "illum=" + illum + "," +
        "d=" + d +
        "]";
    }
}
+0 −245
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.opengl;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.microedition.khronos.opengles.*;

/**
 * {@hide}
 */
public abstract class Object3D {

    private boolean mHasTexcoords = false;

    private float mBoundsMinX = Float.MAX_VALUE;
    private float mBoundsMaxX = Float.MIN_VALUE;
    private float mBoundsMinY = Float.MAX_VALUE;
    private float mBoundsMaxY = Float.MIN_VALUE;
    private float mBoundsMinZ = Float.MAX_VALUE;
    private float mBoundsMaxZ = Float.MIN_VALUE;

    private IntBuffer mVertexBuffer;
    private IntBuffer mNormalBuffer;
    private IntBuffer mTexcoordBuffer;

    // All groups, by name
    private Map<String, Group> mGroups;

    // All materials, by name
    private Map<String, Material> mMaterials;

    // All texture maps, by name
    private Map<String, Texture> mTextures;

    public Object3D() {
        reset();
    }

    /**
     * Override this method with an implementation that contructs
     * and InputStream from the given filename.  For example, if the
     * source files are to be retrieved using an AssetManager,
     * the implementation would use AssetManager.load() to
     * get the input stream.
     */
    public abstract InputStream readFile(String filename) throws IOException;

    private void reset() {
        mVertexBuffer = mNormalBuffer = mTexcoordBuffer = null;

        mGroups = new HashMap<String,Group>();
        mMaterials = new HashMap<String,Material>();
        mTextures = new HashMap<String,Texture>();
    }

    public Material getMaterial(String name) {
        Material mat = mMaterials.get(name);
        return mat;
    }

    public Texture getTexture(String name) {
        return mTextures.get(name);
    }

    public IntBuffer getVertexBuffer() {
        return mVertexBuffer;
    }

    public IntBuffer getNormalBuffer() {
        return mNormalBuffer;
    }

    public IntBuffer getTexcoordBuffer() {
        return mTexcoordBuffer;
    }

    public int getNumTriangles() {
        int numTriangles = 0;
        Iterator<Group> iter = mGroups.values().iterator();
        while (iter.hasNext()) {
            numTriangles += iter.next().getNumTriangles();
        }
        return numTriangles;
    }

    public boolean hasTexcoords() {
        return mHasTexcoords;
    }

    public float getBoundsMinX() {
        return mBoundsMinX;
    }

    public float getBoundsMaxX() {
        return mBoundsMaxX;
    }

    public float getBoundsMinY() {
        return mBoundsMinY;
    }

    public float getBoundsMaxY() {
        return mBoundsMaxY;
    }

    public float getBoundsMinZ() {
        return mBoundsMinZ;
    }

    public float getBoundsMaxZ() {
        return mBoundsMaxZ;
    }

    public void loadTexture(String name) throws IOException {
        InputStream is = readFile(name + ".raw");
        Texture texture = new Texture(is);
        mTextures.put(name, texture);
    }

    private static void verifyByte(DataInputStream dis, int b) 
    throws IOException {
        int x = dis.read() & 0xff;
        if (x != b) {
            throw new RuntimeException("Bad byte: " +
                    x +
                    " (expected " + b + ")");
        }
    }

    public void load(String filename) throws IOException {
        reset();

        DataInputStream dis = new DataInputStream(readFile(filename));
        verifyByte(dis, 'g' + 128);
        verifyByte(dis, 'l');
        verifyByte(dis, 'e');
        verifyByte(dis, 's');

        int numTuples = dis.readInt();

        this.mBoundsMinX = dis.readFloat();
        this.mBoundsMaxX = dis.readFloat();
        this.mBoundsMinY = dis.readFloat();
        this.mBoundsMaxY = dis.readFloat();
        this.mBoundsMinZ = dis.readFloat();
        this.mBoundsMaxZ = dis.readFloat();

        this.mHasTexcoords = dis.readInt() == 1;

        int intsPerTuple = mHasTexcoords ? 8 : 6;
        int numInts = numTuples*intsPerTuple;

        int len = 4*numTuples*(mHasTexcoords ? 8 : 6);

        byte[] tmp = new byte[len];
        int tidx = 0;
        while (tidx < len) {
            tidx += dis.read(tmp, tidx, len - tidx);
        }
        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            for (int i = 0; i < len; i += 4) {
                byte tmp0 = tmp[i];
                byte tmp1 = tmp[i + 1];
                byte tmp2 = tmp[i + 2];
                byte tmp3 = tmp[i + 3];
                tmp[i] = tmp3;
                tmp[i + 1] = tmp2;
                tmp[i + 2] = tmp1;
                tmp[i + 3] = tmp0;
            }
        }

        ByteBuffer allbb = ByteBuffer.allocateDirect(len);
        allbb.order(ByteOrder.nativeOrder());
        allbb.put(tmp);

        allbb.position(0);
        allbb.limit(4*3*numTuples);
        ByteBuffer vbb = allbb.slice();
        this.mVertexBuffer = vbb.asIntBuffer();
        mVertexBuffer.position(0);

        if (mHasTexcoords) {
            allbb.position(allbb.limit());
            allbb.limit(allbb.position() + 4*2*numTuples);
            ByteBuffer tbb = allbb.slice();
            this.mTexcoordBuffer = tbb.asIntBuffer();
            mTexcoordBuffer.position(0);
        }

        allbb.position(allbb.limit());
        allbb.limit(allbb.position() + 4*3*numTuples);
        ByteBuffer nbb = allbb.slice();
        this.mNormalBuffer = nbb.asIntBuffer();
        mNormalBuffer.position(0);

        int numMaterials = dis.readInt();
        for (int i = 0; i < numMaterials; i++) {
            Material mat = new Material(this);
            mat.load(dis);
            mMaterials.put(mat.getName(), mat);
        }

        int numGroups = dis.readInt();
        for (int i = 0; i < numGroups; i++) {
            Group g = new Group(this);
            g.load(dis);
            mGroups.put(g.getName(), g);
        }
    }

    public void draw(GL10 gl) {
        Iterator<Group> iter = mGroups.values().iterator();
        while (iter.hasNext()) {
            iter.next().draw(gl);
        }
    }
}
+0 −135
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.opengl;

import java.io.InputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;

import android.content.res.AssetManager;

/**
 * {@hide}
 */
public class Texture {

    private int width, height, bpp;
    private ByteBuffer data;
    private int name = -1;

    // Texture maps have the following format.  All integers
    // are 16 bits, high byte first.  Pixels are in 5/6/5
    // RGB format, low byte first.
    //
    // width
    // height
    // pixel (0, 0)
    // pixel (1, 0)
    // ...
    // pixel (width - 1, height - 1)

    private int readInt16(InputStream is) throws IOException {
        return is.read() | (is.read() << 8);
    }

    public Texture(InputStream is) throws IOException {
        this.width  = readInt16(is);
        this.height  = readInt16(is);
        this.bpp = 2;

        int npixels = width*height;
        int nbytes = npixels*bpp;
        byte[] arr = new byte[nbytes];

        int idx = 0;
        while (idx < nbytes) {
            int nread = is.read(arr, idx, nbytes - idx);
            idx += nread;
        }

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            // Swap pairs of bytes on big-endian platforms
            for (int i = 0; i < npixels; i++) {
                int j = i*2;
                int k = j + 1;

                byte tmp = arr[j];
                arr[j] = arr[k];
                arr[k] = tmp;
            }
        }

        this.data = ByteBuffer.allocateDirect(arr.length);
        this.data.order(ByteOrder.nativeOrder());
        data.put(arr);
        data.position(0);
    }

    private int loadTexture(GL10 gl,
            int textureUnit,
            int minFilter, int magFilter,
            int wrapS, int wrapT,
            int mode,
            int width, int height,
            int dataType,
            Buffer data) {
        int[] texture = new int[1];
        gl.glGenTextures(1, texture, 0);

        gl.glEnable(gl.GL_TEXTURE_2D);
        gl.glClientActiveTexture(textureUnit);
        gl.glBindTexture(gl.GL_TEXTURE_2D, texture[0]);
        gl.glTexParameterf(gl.GL_TEXTURE_2D,
                gl.GL_TEXTURE_MIN_FILTER,
                minFilter);
        gl.glTexParameterf(gl.GL_TEXTURE_2D,
                gl.GL_TEXTURE_MAG_FILTER,
                magFilter);
        gl.glTexParameterf(gl.GL_TEXTURE_2D,
                gl.GL_TEXTURE_WRAP_S,
                wrapS);
        gl.glTexParameterf(gl.GL_TEXTURE_2D,
                gl.GL_TEXTURE_WRAP_T,
                wrapT);
        gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, mode);

        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB,
                width, height,
                0, gl.GL_RGB, dataType,
                data);

        return texture[0];
    }

    public void setTextureParameters(GL10 gl) {
        if (name < 0) {
            name = loadTexture(gl,
                    gl.GL_TEXTURE0,
                    gl.GL_NEAREST, gl.GL_NEAREST,
                    gl.GL_REPEAT, gl.GL_REPEAT,
                    gl.GL_MODULATE,
                    width, height,
                    gl.GL_UNSIGNED_SHORT_5_6_5,
                    data);
        }

        gl.glBindTexture(gl.GL_TEXTURE_2D, name);
    }
}