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 
17 package com.example.android.apis.graphics.kube;
18 
19 import android.util.Log;
20 
21 import java.nio.ShortBuffer;
22 import java.util.ArrayList;
23 
24 public class GLFace {
25 
GLFace()26 	public GLFace() {
27 
28 	}
29 
30 	// for triangles
GLFace(GLVertex v1, GLVertex v2, GLVertex v3)31 	public GLFace(GLVertex v1, GLVertex v2, GLVertex v3) {
32 		addVertex(v1);
33 		addVertex(v2);
34 		addVertex(v3);
35 	}
36 	// for quadrilaterals
GLFace(GLVertex v1, GLVertex v2, GLVertex v3, GLVertex v4)37 	public GLFace(GLVertex v1, GLVertex v2, GLVertex v3, GLVertex v4) {
38 		addVertex(v1);
39 		addVertex(v2);
40 		addVertex(v3);
41 		addVertex(v4);
42 	}
43 
addVertex(GLVertex v)44 	public void addVertex(GLVertex v) {
45 		mVertexList.add(v);
46 	}
47 
48 	// must be called after all vertices are added
setColor(GLColor c)49 	public void setColor(GLColor c) {
50 
51 		int last = mVertexList.size() - 1;
52 		if (last < 2) {
53 			Log.e("GLFace", "not enough vertices in setColor()");
54 		} else {
55 			GLVertex vertex = mVertexList.get(last);
56 
57 			// only need to do this if the color has never been set
58 			if (mColor == null) {
59 				while (vertex.color != null) {
60 					mVertexList.add(0, vertex);
61 					mVertexList.remove(last + 1);
62 					vertex = mVertexList.get(last);
63 				}
64 			}
65 
66 			vertex.color = c;
67 		}
68 
69 		mColor = c;
70 	}
71 
getIndexCount()72 	public int getIndexCount() {
73 		return (mVertexList.size() - 2) * 3;
74 	}
75 
putIndices(ShortBuffer buffer)76 	public void putIndices(ShortBuffer buffer) {
77 		int last = mVertexList.size() - 1;
78 
79 		GLVertex v0 = mVertexList.get(0);
80 		GLVertex vn = mVertexList.get(last);
81 
82 		// push triangles into the buffer
83 		for (int i = 1; i < last; i++) {
84 			GLVertex v1 = mVertexList.get(i);
85 			buffer.put(v0.index);
86 			buffer.put(v1.index);
87 			buffer.put(vn.index);
88 			v0 = v1;
89 		}
90 	}
91 
92 	private ArrayList<GLVertex> mVertexList = new ArrayList<GLVertex>();
93 	private GLColor mColor;
94 }
95