package edu.ups.gamedev.terrain.tile;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.TriMesh;
import com.jme.scene.batch.TriangleBatch;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.util.geom.BufferUtils;
public class Tile extends TriMesh {
private Texture baseTexture;
private Texture overTexture;
private Vector2f baseTextureCoord;
private Vector2f overTextureCoord;
private FloatBuffer baseTextureBuffer;
private FloatBuffer overTextureBuffer;
private Vector3f origin;
public Tile( Vector3f origin, int[] heightMap, Texture baseTexture, Texture overTexture,
Vector2f baseTextureCoord, Vector2f overTextureCoord) {
super();
this.baseTexture = baseTexture;
this.overTexture = overTexture;
this.baseTextureCoord = baseTextureCoord;
this.overTextureCoord = overTextureCoord;
this.origin = origin;
if( overTexture == null || overTextureCoord == null ) {
System.exit(-1);
}
this.initialize();
}
public Tile( Vector3f origin, int[] heightMap, Texture baseTexture, Vector2f baseTextureCoord) {
super();
this.baseTexture = baseTexture;
this.baseTextureCoord = baseTextureCoord;
this.origin = origin;
this.initialize();
}
public void initialize() {
FloatBuffer vertex = BufferUtils.createVector3Buffer( 4 );
IntBuffer index = BufferUtils.createIntBuffer( 6 );
baseTextureBuffer = BufferUtils.createVector3Buffer( 4 );
System.out.println( "origin = " + origin.toString() );
System.out.println( origin.add( new Vector3f( -1.0f, 1.0f, 0.0f ) ).toString() );
System.out.println( origin.add( new Vector3f( -1.0f, -1.0f, 0.0f ) ).toString() );
System.out.println( origin.add( new Vector3f( 1.0f, -1.0f, 0.0f ) ).toString() );
System.out.println( origin.add( new Vector3f( 1.0f, 1.0f, 0.0f ) ).toString() );
BufferUtils.setInBuffer( origin.add( new Vector3f( -1.0f, 1.0f, 0.0f )), vertex, 0 );
BufferUtils.setInBuffer( origin.add( new Vector3f( -1.0f, -1.0f, 0.0f )), vertex, 1 );
BufferUtils.setInBuffer( origin.add( new Vector3f( 1.0f, -1.0f, 0.0f )), vertex, 2 );
BufferUtils.setInBuffer( origin.add( new Vector3f( 1.0f, 1.0f, 0.0f )), vertex, 3 );
index.put( 0 );
index.put( 1 );
index.put( 2 );
index.put( 0 );
index.put( 2 );
index.put( 3 );
BufferUtils.setInBuffer( new Vector2f( baseTextureCoord.x, baseTextureCoord.y + TileTexture.TILE_HEIGHT ), baseTextureBuffer, 0 );
BufferUtils.setInBuffer( baseTextureCoord, baseTextureBuffer, 1 );
BufferUtils.setInBuffer( new Vector2f( baseTextureCoord.x + TileTexture.TILE_WIDTH, baseTextureCoord.y ), baseTextureBuffer, 2 );
BufferUtils.setInBuffer( new Vector2f( baseTextureCoord.x + TileTexture.TILE_WIDTH, baseTextureCoord.y + TileTexture.TILE_HEIGHT ), baseTextureBuffer, 3 );
if( overTextureCoord != null ) {
FloatBuffer vertexOver = BufferUtils.createVector3Buffer( 4 );
IntBuffer indexOver = BufferUtils.createIntBuffer( 6 );
overTextureBuffer = BufferUtils.createVector3Buffer( 4 );
BufferUtils.setInBuffer( origin.add( new Vector3f( -1.0f, 1.0f, 0.0f )), vertexOver, 0 );
BufferUtils.setInBuffer( origin.add( new Vector3f( -1.0f, -1.0f, 0.0f )), vertexOver, 1 );
BufferUtils.setInBuffer( origin.add( new Vector3f( 1.0f, -1.0f, 0.0f )), vertexOver, 2 );
BufferUtils.setInBuffer( origin.add( new Vector3f( 1.0f, 1.0f, 0.0f )), vertexOver, 3 );
indexOver.put( 0 );
indexOver.put( 1 );
indexOver.put( 2 );
indexOver.put( 0 );
indexOver.put( 2 );
indexOver.put( 3 );
BufferUtils.setInBuffer( new Vector2f( overTextureCoord.x, overTextureCoord.y + TileTexture.TILE_HEIGHT ), overTextureBuffer, 0 );
BufferUtils.setInBuffer( overTextureCoord, overTextureBuffer, 1 );
BufferUtils.setInBuffer( new Vector2f( overTextureCoord.x + TileTexture.TILE_WIDTH, overTextureCoord.y ), overTextureBuffer, 2 );
BufferUtils.setInBuffer( new Vector2f( overTextureCoord.x + TileTexture.TILE_WIDTH, overTextureCoord.y + TileTexture.TILE_HEIGHT ), overTextureBuffer, 3 );
this.addBatch( new TriangleBatch() );
this.getBatch( 1 ).setVertexCount( 4 );
this.getBatch( 1 ).setTriangleQuantity( 2 );
this.getBatch( 1 ).setVertexBuffer( vertexOver );
this.getBatch( 1 ).setTextureBuffer( overTextureBuffer, 0 );
this.getBatch( 1 ).setIndexBuffer( indexOver );
}
this.getBatch( 0 ).setVertexCount( 4 );
this.getBatch( 0 ).setTriangleQuantity( 2 );
this.getBatch( 0 ).setVertexBuffer( vertex );
this.getBatch( 0 ).setTextureBuffer( baseTextureBuffer, 0 );
this.getBatch( 0 ).setIndexBuffer( index );
this.setModelBound( new BoundingBox() );
this.updateModelBound();
this.updateWorldBound();
TextureState ts = (TextureState) this.getRenderState( RenderState.RS_TEXTURE );
if( ts == null ) {
ts = TileGame.getDisplay().getRenderer().createTextureState();
}
ts.setTexture( baseTexture );
this.getBatch( 0 ).setRenderState( ts );
this.getBatch( 0 ).updateRenderState();
// this.getBatch( 0 ).draw( r );
if( overTextureCoord != null ) {
TextureState ts2 = TileGame.getDisplay().getRenderer().createTextureState();
AlphaState as = (AlphaState) this.getRenderState( RenderState.RS_ALPHA );
if( as == null ) {
as = TileGame.getDisplay().getRenderer().createAlphaState();
as.setBlendEnabled( true );
as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
as.setTestEnabled(true);
as.setTestFunction(AlphaState.TF_GREATER);
this.getBatch( 1 ).setRenderState( as );
}
ts2.setTexture( overTexture );
this.getBatch( 1 ).setRenderState( ts2 );
this.getBatch( 1 ).setTextureBuffer( overTextureBuffer, 0);
this.getBatch( 1 ).updateRenderState();
}
}
public void onDraw( Renderer r ) {
this.getBatch( 0 ).draw( r );
if( this.getBatchCount() != 1 ) {
this.getBatch( 1 ).draw( r );
}
}
}