CBox

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


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;
}

}

Published by

Luke Iwanski

Senior Graphics Programmer @ CD Projekt RED

Leave a Reply

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