androidのopenglesをつかってみる。立方体の描画

ひきつづき、Cubeの描画。

activityは変更なし。

public class MainActivity extends Activity {
	private GLSurfaceView mGLView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        mGLView = new GLSurfaceView(this);
        mGLView.setRenderer(new GLRenderer());
        this.setContentView(mGLView);
    }
    @Override
    protected void onPause(){
    	super.onPause();
    	mGLView.onPause();
    }
    @Override
    protected void onResume(){
    	super.onResume();
    	mGLView.onResume();
    }
}

レンダリング
変わったのは gl.glEnable(GL10.GL_CULL_FACE) の部分で、カリングをonにして立方体の見えない部分を描画しないようにしている。

import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class GLRenderer implements GLSurfaceView.Renderer{
	public GLRenderer(){
		mCube = new Cube();
		mAngle = 0;
	}
	// Renderer implements
	//-------------------------------
	@Override
	public void onDrawFrame(GL10 gl){
		// Display Clear
		gl.glClearColor(1.0f,1.0f,0, 0);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		// model mode
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		// move view point
		gl.glTranslatef(0, 0, -5.0f);
		gl.glRotatef( mAngle, 1.0f, 1.0f, 0);
		
		// draw cube
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glEnable(GL10.GL_CULL_FACE); //enable background culling
		mCube.draw(gl);
		
		mAngle+=1.2f;
	}
	
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height){
		// set viewport
		gl.glViewport(0, 0, width, height);
		// frustum rendering area in perspective projection system
		float ratio = (float) width/ height;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config){
		
	}
	
	private Cube mCube;
	private float mAngle;
}

Cube。

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;

public class Cube{
	public Cube(){
		int one = 0x10000;
		int vertices[] = {
				-one, -one, -one,
				one, -one, -one,
				one, one, -one,
				
				-one, one, -one,
				-one, -one, one,
				one, -one, one,
				
				one, one, one,
				-one, one, one
		};
		int colors[] = {
				0, 0, 0, one,
				one, 0, 0, one,
				one, one, 0, one,
				0, one, 0, one,
				0, 0, one, one,
				one, 0, one, one,
				one, one, one, one,
				0, one, one, one
		};
		byte indices[] = {
				0,4,5, 0,5,1,
				1,5,6, 1,6,2,
				2,6,7, 2,7,3,
				3,7,4, 3,4,0,
				4,7,6, 4,6,5,
				3,0,1, 3,1,2
		};
		
		mVertexBuffer = setupIntBuffer(vertices);
		mColorBuffer = setupIntBuffer(colors);
		mIndexBuffer = setupByteBuffer(indices);
	}
	
	// draw
	//-------------------------
	public void draw(GL10 gl){
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
		gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, mIndexBuffer.capacity(), GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
	}
	
	// util
	//-------------------------
	private IntBuffer setupIntBuffer( int[] arrays ){
		// prepare vertex
		ByteBuffer arrayBuffer = ByteBuffer.allocateDirect(arrays.length*4);
		arrayBuffer.order(ByteOrder.nativeOrder());
		IntBuffer intBuffer = arrayBuffer.asIntBuffer();
		intBuffer.put(arrays);
		intBuffer.position(0);
		return intBuffer;
	}
	private ByteBuffer setupByteBuffer( byte []arrays ){
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(arrays.length);
		byteBuffer.put(arrays);
		byteBuffer.position(0);
		return byteBuffer;
	}
	private IntBuffer mColorBuffer;
	private IntBuffer mVertexBuffer;
	private ByteBuffer mIndexBuffer;
}

こんな感じだよ。