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 java.nio.IntBuffer;
20 
21 public class GLVertex {
22 
23     public float x;
24     public float y;
25     public float z;
26     final short index; // index in vertex table
27     GLColor color;
28 
GLVertex()29     GLVertex() {
30         this.x = 0;
31         this.y = 0;
32         this.z = 0;
33         this.index = -1;
34     }
35 
GLVertex(float x, float y, float z, int index)36     GLVertex(float x, float y, float z, int index) {
37         this.x = x;
38         this.y = y;
39         this.z = z;
40         this.index = (short)index;
41     }
42 
43     @Override
equals(Object other)44     public boolean equals(Object other) {
45         if (other instanceof GLVertex) {
46             GLVertex v = (GLVertex)other;
47             return (x == v.x && y == v.y && z == v.z);
48         }
49         return false;
50     }
51 
toFixed(float x)52     static public int toFixed(float x) {
53         return (int)(x * 65536.0f);
54     }
55 
put(IntBuffer vertexBuffer, IntBuffer colorBuffer)56     public void put(IntBuffer vertexBuffer, IntBuffer colorBuffer) {
57         vertexBuffer.put(toFixed(x));
58         vertexBuffer.put(toFixed(y));
59         vertexBuffer.put(toFixed(z));
60         if (color == null) {
61             colorBuffer.put(0);
62             colorBuffer.put(0);
63             colorBuffer.put(0);
64             colorBuffer.put(0);
65         } else {
66             colorBuffer.put(color.red);
67             colorBuffer.put(color.green);
68             colorBuffer.put(color.blue);
69             colorBuffer.put(color.alpha);
70         }
71     }
72 
update(IntBuffer vertexBuffer, M4 transform)73     public void update(IntBuffer vertexBuffer, M4 transform) {
74         // skip to location of vertex in mVertex buffer
75         vertexBuffer.position(index * 3);
76 
77         if (transform == null) {
78             vertexBuffer.put(toFixed(x));
79             vertexBuffer.put(toFixed(y));
80             vertexBuffer.put(toFixed(z));
81         } else {
82             GLVertex temp = new GLVertex();
83             transform.multiply(this, temp);
84             vertexBuffer.put(toFixed(temp.x));
85             vertexBuffer.put(toFixed(temp.y));
86             vertexBuffer.put(toFixed(temp.z));
87         }
88     }
89 }
90