package jp.vixar.aquamotion3d;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import com.roxiga.aquamotion3d.*;
import com.roxiga.models.*;

class AquaMotion3D implements GLSurfaceView.Renderer
{
	private Scene3D _scene = new Scene3D();
    private Context _context;
	public int _width;
	public int _height;
//    private Model3D _obj = new Square();
    private Model3D _obj = new Tonia();
    private boolean _touch = false;
    private int _time = 0;

    public AquaMotion3D(Context context)
    {
        _context = context;
    }

    public void onDrawFrame(GL10 glUnused)
    {
    	move();
 
        GLES20.glClearColor(0.2f, 0.6f, 1, 1.0f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glUseProgram(_scene._program);
    	
        Matrix.setLookAtM(_scene._vMatrix, 0,
        		500, 500, 500,
        		0, 100, 0,
        		0, 1, 0);

        _obj.draw(_scene);

        _time++;
    }

    private void move()
    {
    	if ( _touch )
    	{
    		_obj._pos = _obj.forward(4,_obj._rotate._y);
    	}
    	else
    	{
    		_obj.addRotateY(1);
    	}
    }

    public void onSurfaceChanged(GL10 glUnused, int width, int height)
    {
    	_width = width;
    	_height = height;
        GLES20.glViewport(0, 0, width, height);
        
        _scene.perspective(45,(float) width / height,1,10000);
        Matrix.orthoM(_scene._orthoMatrix, 0, 0, width, height, 0, 100, -100);
    }

    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
    {
    	_scene.onSurfaceCreated();

//        _obj.setFontTexture(_context.getResources(),"フォント文字をテクスチャに");
         _obj.setTexture(_context.getResources(),R.drawable.tonia);
    }

    public void actionDown(float x,float y)
	{
    	_touch = true;
	}
	
	public void actionMove(float x,float y)
	{
	}

	public void actionUp(float x,float y)
	{
		_touch = false;
	}
}
