package com.android.draw3d;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

public class Model {
	private Context mContext;
	private IntBuffer   mVertexBuffer;
    private ShortBuffer	mIndexBuffer;
    
    public Model(Context context){
    	//Load vertices and indices (worked)
    	int vertices[] = mContext.getResources().getIntArray(R.array.vertices);
    	int indicesInt[] = mContext.getResources().getIntArray(R.array.indices);
    	
    	short indices[] = new short[indicesInt.length];
    	for(int i=0 ; i<indicesInt.length ; i++){
    		indices[i] = (short) indicesInt[i];
    	}
    	
    	
    	ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asIntBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);
        
        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*4);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }
    
    public void draw(GL10 gl){
    	gl.glTranslatef(0.4f, -0.5f, 4.5f);
    	gl.glRotatef(90, 0, 0, 1);
    	gl.glTranslatef(0.0f, -3.0f, 0.0f);
    	
    	gl.glDisable(GL10.GL_CULL_FACE);
    	
    	gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
        
        gl.glDrawElements(GL10.GL_TRIANGLES, mIndexBuffer.capacity(), GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
        gl.glScalef(0.5f, 0.5f, 0.5f);
    }
}
