CBox Class

Idea
Whole world in this project is made by boxes as mentioned in previous post: Carlos – World Class – First Prototype.

Feature

  • Different texture for every box can be used
  • Box can be set in every place – simply calculated on world array
  • Feature to show where box is set
  • Every Box can have set up is if is passable or not

Future Development
In future I want to make this class more sophisticated for instance

  • transparent boxes (windows),
  • disappearing  boxes (traps),
  • position of box changed in real time (lifts, doors)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 
package com.fps.carlos;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
 
import javax.microedition.khronos.opengles.GL10;
 
import android.util.Log;
 
public class CBox {
 
/** The initial vertex definition */
 
/** The buffer holding the vertices */
public FloatBuffer vertexBuffer;
/** The buffer holding the texture coordinates */
public FloatBuffer textureBuffer;
/** The buffer holding the indices */
private ByteBuffer indexBuffer;
 
private boolean passAble;
 
/** The initial indices definition */
private byte indices[] = {
// Faces definition
0, 1, 3, 0, 3, 2, 		// Face front
4, 5, 7, 4, 7, 6, 		// Face right
8, 9, 11, 8, 11, 10, 	// ...
12, 13, 15, 12, 15, 14,
16, 17, 19, 16, 19, 18,
20, 21, 23, 20, 23, 22, };
 
private float cVertices[] = {
//Vertices according to faces
1f, 1f, 2f, //v0
2f, 1f, 2f, 	//v1
1f, 2f, 2f, 	//v2
2f, 2f, 2f, 	//v3
 
2f, 1f, 2f, 	// ...
2f, 1f, 1f,
2f, 2f, 2f,
2f, 2f, 1f,
 
2f, 1f, 1f,
1f, 1f, 1f,
2f, 2f, 1f,
1f, 2f, 1f,
 
1f, 1f, 1f,
1f, 1f, 2f,
1f, 2f, 1f,
1f, 2f, 2f,
 
1f, 1f, 1f,
2f, 1f, 1f,
1f, 1f, 2f,
2f, 1f, 2f,
 
1f, 2f, 2f,
2f, 2f, 2f,
1f, 2f, 1f,
2f, 2f, 1f, };
 
/** The initial texture coordinates (u, v) */
private float texture[] = {
//Mapping coordinates for the vertices
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
 
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
 
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
 
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
 
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
 
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
};
 
private int x, y, z, type;
 
CBox(int x, int y, int z, int type)
{
Log.v("cube", x+":"+y+":"+z+" ");
this.x = x;
this.y = y;
this.z = z;
this.type = type;
 
this.setPassable(type);
this.setTexCoords();
this.createVertices();
this.createBuffers();
 
//Log.v("Cybe", this.toString());
}
 
void setPassable(int type)
{
if(type == 0)
{
this.setPassAble(false);
}else
{
this.setPassAble(true);
}
}
 
void setTexCoords()
{
//texture[] = {
//need to find a way of doing that in flexible way
 
float size = 0.5f;
if(this.type == 1)
{
//top left corner as texture
for(int i = 0; i< texture.length; i++)
{
texture[i] = texture[i]*size;
}
}else if(this.type == 2)
{
//top right corner as texture
for(int i = 0; i< texture.length; i++)
{
if(i%2 == 0){
//x cord
texture[i] = texture[i]*size+size;
}else{
texture[i] = texture[i]*size;
//y cord
}
}
}else if(this.type == 3){
//bottom left
for(int i = 0; i< texture.length; i++)
{
if(i%2 == 0){
//x cord
texture[i] = texture[i]*size;
}else{
texture[i] = texture[i]*size+size;
//y cord
}
}
}else{
//bottom right
 
for(int i = 0; i< texture.length; i++)
{
if(i%2 == 0){
//x cord
texture[i] = texture[i]*size;
}else{
texture[i] = texture[i]*size+size;
//y cord
}
}
}
}
 
void createVertices()
{
//top right corner as texture
for(int i = 0; i< cVertices.length; i++)
{
if(i%3 == 0){
//x cord
cVertices[i] = (cVertices[i])+(this.x)-1.0f;
}else if(i%3==1){
cVertices[i] = (cVertices[i])+(this.y)-2.0f;
//y cord
}else {
//z cord
cVertices[i] = (cVertices[i])+(this.z)-1.0f;
}
}
 
}
 
void createBuffers()
{
//Build the buffers
ByteBuffer byteBuf = ByteBuffer.allocateDirect(cVertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(cVertices);
vertexBuffer.position(0);
 
//
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
 
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
 
}
 
public void setY(int y) {
this.y = y;
}
 
public int getY() {
return y;
}
 
public void setZ(int z) {
this.z = z;
}
 
public int getZ() {
return z;
}
 
public void setX(int x) {
this.x = x;
}
 
public int getX() {
return x;
}
 
public void setType(int type) {
this.type = type;
}
 
public int getType() {
return type;
}
 
public GL10 draw(GL10 gl) {
 
//Log.v("Drawing ", "I am");
//Point to our buffers
if(this.type != 0)
{
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
 
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
}
 
return gl;
}
 
public void setPassAble(boolean passAble) {
this.passAble = passAble;
}
 
public boolean isPassAble() {
return passAble;
}
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *