1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.opengl;
18
19import java.nio.ByteBuffer;
20import java.nio.ByteOrder;
21import java.nio.ShortBuffer;
22import java.io.DataInputStream;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Iterator;
27import javax.microedition.khronos.opengles.*;
28
29class MaterialIndices {
30
31    private Material material = null;
32    private ShortBuffer indexBuffer = null;
33
34    public MaterialIndices(Material material, ShortBuffer indexBuffer) {
35        this.material = material;
36        this.indexBuffer = indexBuffer;
37    }
38
39    public Material getMaterial() {
40        return material;
41    }
42
43    public ShortBuffer getIndexBuffer() {
44        return indexBuffer;
45    }
46}
47
48/**
49 * {@hide}
50 */
51public class Group {
52
53    private Object3D parent;
54    private String name;
55
56    private List<MaterialIndices> materialIndices =
57        new ArrayList<MaterialIndices>();
58
59    public Group(Object3D parent) {
60        this.parent = parent;
61    }
62
63    public String getName() {
64        return name;
65    }
66
67    public void load(DataInputStream dis) throws IOException {
68        dis.readInt(); // name length
69        this.name = dis.readUTF();
70
71        int numMaterials = dis.readInt();
72
73        for (int i = 0; i < numMaterials; i++) {
74            dis.readInt(); // material name length
75            String matName = dis.readUTF();
76            Material material = parent.getMaterial(matName);
77
78            int numIndices = dis.readInt();
79            byte[] indicesBytes = new byte[numIndices * 2];
80            dis.readFully(indicesBytes);
81
82            // Swap bytes from network to native order if necessary
83            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
84                int idx = 0;
85                for (int j = 0; j < numIndices; j++) {
86                    byte b0 = indicesBytes[idx];
87                    byte b1 = indicesBytes[idx + 1];
88                    indicesBytes[idx] = b1;
89                    indicesBytes[idx + 1] = b0;
90                    idx += 2;
91                }
92            }
93
94            ByteBuffer ibb = ByteBuffer.allocateDirect(2*numIndices);
95            ibb.order(ByteOrder.nativeOrder());
96            ibb.put(indicesBytes);
97            ibb.position(0);
98
99            ShortBuffer sb = ibb.asShortBuffer();
100            materialIndices.add(new MaterialIndices(material, sb));
101        }
102    }
103
104    public int getNumTriangles() {
105        int numTriangles = 0;
106        Iterator<MaterialIndices> iter = materialIndices.iterator();
107        while (iter.hasNext()) {
108            MaterialIndices matIdx = iter.next();
109            ShortBuffer indexBuffer = matIdx.getIndexBuffer();
110            numTriangles += indexBuffer.capacity()/3;
111        }
112        return numTriangles;
113    }
114
115    public void draw(GL10 gl) {
116        gl.glDisableClientState(gl.GL_COLOR_ARRAY);
117
118        gl.glVertexPointer(3, gl.GL_FIXED, 0, parent.getVertexBuffer());
119        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
120
121        gl.glNormalPointer(gl.GL_FIXED, 0, parent.getNormalBuffer());
122        gl.glEnableClientState(gl.GL_NORMAL_ARRAY);
123
124        if (parent.hasTexcoords()) {
125            gl.glTexCoordPointer(2, gl.GL_FIXED, 0, parent.getTexcoordBuffer());
126            gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);
127            gl.glEnable(gl.GL_TEXTURE_2D);
128        } else {
129            gl.glDisable(gl.GL_TEXTURE_2D);
130        }
131
132        Iterator<MaterialIndices> iter = materialIndices.iterator();
133        while (iter.hasNext()) {
134            MaterialIndices matIdx = iter.next();
135            ShortBuffer indexBuffer = matIdx.getIndexBuffer();
136            Material mat = matIdx.getMaterial();
137            mat.setMaterialParameters(gl);
138            if (parent.hasTexcoords() && mat.getMap_Kd().length() > 0) {
139                Texture texture = parent.getTexture(mat.getMap_Kd());
140                texture.setTextureParameters(gl);
141            }
142
143            gl.glDrawElements(gl.GL_TRIANGLES,
144                    indexBuffer.capacity(),
145                    gl.GL_UNSIGNED_SHORT,
146                    indexBuffer);
147        }
148    }
149
150    public String toString() {
151        return "Group[" +
152        "name=" + name +
153        "]";
154    }
155}
156