package com.roxiga.hypermotion3d;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

public class BoneModel3D extends Model3D
{
	public BoneModel3D()
	{
		super();
	}
	
	protected void addB(double x,double y,double z,int i)
	{
    	_vertices.add(new Vector3D((float)x,(float)y,(float)z));
		_boneIndices.add(i);
	}

	protected void addA3()
	{
		_animations.add(new ArrayList<ArrayList<Matrix3D>>());
		_currentAnimation = _animations.size() - 1;
	}

	protected void addA2()
	{
		ArrayList<ArrayList<Matrix3D>> anim = _animations.get(_currentAnimation);
		anim.add(new ArrayList<Matrix3D>());
		_current = anim.size() - 1;
	}

	protected void addA(
			double m0,double m1,double m2,double m3,
			double m4,double m5,double m6,double m7,
			double m8,double m9,double m10,double m11,
			double m12,double m13,double m14,double m15)
	{
			ArrayList<ArrayList<Matrix3D>> anim = _animations.get(_currentAnimation);
			ArrayList<Matrix3D> anim2 = anim.get(_current);
			anim2.add(new Matrix3D(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15));
	}
    
    protected void init()
    {
    	int i;

    	ByteBuffer vbb = ByteBuffer.allocateDirect(_indices.size()*3*4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer = vbb.asFloatBuffer();

        ByteBuffer ibb = ByteBuffer.allocateDirect(_indices.size()*2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer = ibb.asShortBuffer();
        for ( short index = 0; index < _indices.size(); index++)
        {
			_indexBuffer.put(index);
		}
		_indexBuffer.position(0);

		ByteBuffer uvbb = ByteBuffer.allocateDirect(_uv.size() * 4);
		uvbb.order(ByteOrder.nativeOrder());
		_textureBuffer = uvbb.asFloatBuffer();
        for ( i = 0; i < _uv.size(); i++)
        {
			_textureBuffer.put(_uv.get(i));
		}
		_textureBuffer.position(0);
		
		_currentAnimation = 0;
    }

	public void draw(GL10 gl)
    {
    	if ( !_visible )
    	{
    		return;
    	}
    	int i;

    	gl.glPushMatrix();
    	setTransform(gl);

    	ArrayList<ArrayList<Matrix3D>> anim = _animations.get(_currentAnimation);
 
    	FloatBuffer vertexBuffer;
        ByteBuffer vbb = ByteBuffer.allocateDirect(_indices.size()*3*4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
        for ( i = 0; i < _indices.size(); i++)
        {
        	int index = _indices.get(i);
        	ArrayList<Matrix3D> a = anim.get(_boneIndices.get(index));
        	Matrix3D m = a.get(_time);
        	Vector3D v = _vertices.get(index);
        	Vector3D v2 = m.affine(v);
			vertexBuffer.put(v2._x);
			vertexBuffer.put(v2._y);
			vertexBuffer.put(v2._z);
		}
        vertexBuffer.position(0);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        //テクスチャ管理番号バインド
		gl.glBindTexture(GL10.GL_TEXTURE_2D, _textureNo);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, _textureBuffer);
       // 描画
        gl.glDrawElements(GL10.GL_TRIANGLES, _indices.size(), GL10.GL_UNSIGNED_SHORT, _indexBuffer);

        _time++;
        ArrayList<Matrix3D> anim2 = anim.get(0);
        if ( _time >= anim2.size() )
        {
        	if ( _loopAnimation )
        	{
        		_time = 0;
        	}
        	else if ( _animationLast )
        	{
        		_time = anim2.size() - 1;
        	}
        }
        
        gl.glPopMatrix();
    }
}
