1/*
2 * Copyright (C) 2008 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 com.example.android.apis.graphics.kube;
18
19import java.nio.ShortBuffer;
20import java.util.ArrayList;
21import java.util.Iterator;
22
23public class GLShape {
24
25	public GLShape(GLWorld world) {
26		mWorld = world;
27	}
28
29	public void addFace(GLFace face) {
30		mFaceList.add(face);
31	}
32
33	public void setFaceColor(int face, GLColor color) {
34		mFaceList.get(face).setColor(color);
35	}
36
37	public void putIndices(ShortBuffer buffer) {
38		Iterator<GLFace> iter = mFaceList.iterator();
39		while (iter.hasNext()) {
40			GLFace face = iter.next();
41			face.putIndices(buffer);
42		}
43	}
44
45	public int getIndexCount() {
46		int count = 0;
47		Iterator<GLFace> iter = mFaceList.iterator();
48		while (iter.hasNext()) {
49			GLFace face = iter.next();
50			count += face.getIndexCount();
51		}
52		return count;
53	}
54
55	public GLVertex addVertex(float x, float y, float z) {
56
57		// look for an existing GLVertex first
58		Iterator<GLVertex> iter = mVertexList.iterator();
59		while (iter.hasNext()) {
60			GLVertex vertex = iter.next();
61			if (vertex.x == x && vertex.y == y && vertex.z == z) {
62				return vertex;
63			}
64		}
65
66		// doesn't exist, so create new vertex
67		GLVertex vertex = mWorld.addVertex(x, y, z);
68		mVertexList.add(vertex);
69		return vertex;
70	}
71
72	public void animateTransform(M4 transform) {
73		mAnimateTransform = transform;
74
75		if (mTransform != null)
76			transform = mTransform.multiply(transform);
77
78		Iterator<GLVertex> iter = mVertexList.iterator();
79		while (iter.hasNext()) {
80			GLVertex vertex = iter.next();
81			mWorld.transformVertex(vertex, transform);
82		}
83	}
84
85	public void startAnimation() {
86	}
87
88	public void endAnimation() {
89		if (mTransform == null) {
90			mTransform = new M4(mAnimateTransform);
91		} else {
92			mTransform = mTransform.multiply(mAnimateTransform);
93		}
94	}
95
96	public M4						mTransform;
97	public M4						mAnimateTransform;
98	protected ArrayList<GLFace>		mFaceList = new ArrayList<GLFace>();
99	protected ArrayList<GLVertex>	mVertexList = new ArrayList<GLVertex>();
100	protected ArrayList<Integer>	mIndexList = new ArrayList<Integer>();	// make more efficient?
101	protected GLWorld mWorld;
102}
103