1. Open GL View를 만든다.
mGLSurfaceView = new GLSurfaceView(this);

2. Renderer를 설정한다.
mGLSurfaceView.setRenderer(new CubeRenderer(false));

3.onResume해준다.
mGLSurfaceView.onResume();

4.onPause
mGLSurfaceView.onPause();를 해준다.


5. Renderer

class CubeRenderer implements GLSurfaceView.Renderer
{
	//표면을 만들어질때 불리어진다.
	public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
	//움직임 효과를 빠르게 해준다. (흔들림)을 꺼둔다.
	gl.glDisable(GL10.GL_DITHER); 
	//드라이버에 원근보정을 가장 빠르게 요청한다. 하드웨어에 따라서 달라진다.
	gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
	//컬러를 지운다. 
	gl.glClearColor(0,0,0,0);
	//폴리곤을 추려난다. (어느방향으로 돌면서 앞면인지 ..)
	gl.glEnable(GL10.GL_CULL_FACE);
	//스무스하게 만든다.
	gl.glShadeModel(GL10.GL_SMOOTH);
	//물체를 하나 그리고 그 앞쪽으로 물체를 하나 더 그리면 
	//처음에 그렸던 물체에 나중에 그린 물체가 가리는 현상이 생길 수 있는다.
	gl.glEnable(GL10.GL_DEPTH_TEST); 
	}

	//화면이 변경되었을 경우 실행 된다.
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height); //뷰포트를 정의 한다.
		float ratio = (float) width / height;
		gl.glMatrixMode(GL10.GL_PROJECTION); //투영을 위한 행렬을 정의 한다.
		gl.glLoadIdentity(); //좌표계 초기화를 한다.
		//투영을 비율을 정의 한다. 1:화면비율(w/h)
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); 
	}

	//프래임을 그려준다.
	public void onDrawFrame(GL10 gl) 
	{
		//화면을 클리어한다. (컬러와 Depth를 클리어 한다)
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glMatrixMode(GL10.GL_MODELVIEW); //투영을 위한 행렬을 정의 한다.
		gl.glLoadIdentity(); //좌표계 초기화를 한다.
		
		gl.glTranslatef(0, 0, -3.0f); //이동
		gl.glRotatef(mAngle, 0, 1, 0); //회전
		gl.glRotatef(mAngle*0.25f, 1, 0, 0);
		
		//정점 Array를 사용할 수 있게 선언한다.
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
		//Color Array를 사용할 수 있게 선언한다. 
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 
		
		//큐뷰를 화면을 그린다.
		mCube.draw(gl);
		
		gl.glRotatef(mAngle*2.0f, 0, 1, 1); //이동
		gl.glTranslatef(0.5f, 0.5f, 0.5f); //회전
		
		//큐뷰를 화면에 그린다.
		mCube.draw(gl); 
		
		mAngle += 1.2f;
	}
}



6. 큐브 클래스


class Cube
{
	public Cube()
	{
		//정점의 변환 GL_FIXED 와 GL_FLOAT 의 관계
		// 0x10000 = 1.0f와 같음
		//int int_coord = (int)(float_coord*65536)). 
		int one = 0x10000;
		
		//X,Y,Z
		int vertices[] = { 
		-one, -one, -one, //Index 0
		one, -one, -one, //Index 1
		one, one, -one, //Index 2
		-one, one, -one, //Index 3
		-one, -one, one, //Index 4
		one, -one, one, //Index 5
		one, one, one, //Index 6
		-one, one, one, //Index 7
		};
		
		//RGBA
		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
		};
		
		//배열의 갯수*sizeof(int) 
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
		vbb.order(ByteOrder.nativeOrder()); //바이트오더 정의
		mVertexBuffer = vbb.asIntBuffer(); //인티저버퍼로 가져온다.
		mVertexBuffer.put(vertices); //데이터를 푹한다.
		mVertexBuffer.position(0); //위치를 0으로 정의 한다.
		
		ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
		cbb.order(ByteOrder.nativeOrder());
		mColorBuffer = cbb.asIntBuffer();
		mColorBuffer.put(colors);
		mColorBuffer.position(0);
		
		mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
		mIndexBuffer.put(indices);
		mIndexBuffer.position(0);
	}

	public void draw(GL10 gl)
	{
		//시계방향으로 감기
		gl.glFrontFace(gl.GL_CW);
		//3: 배열의 3개씩 읽어온다.
		gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
		//4: 배열의 4개씩 읽어온다.
		gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer); 
		//삼각형형태로 그린다. 36:인덱스의수
		gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
	}
	
	private IntBuffer mVertexBuffer;
	private IntBuffer mColorBuffer;
	private ByteBuffer mIndexBuffer;
}


'Android > Class' 카테고리의 다른 글

[RecyclerView]  (0) 2015.09.18
[Android] SoundPool  (0) 2015.08.07
[PopupWindow] PopupWindow의 터치 처리  (0) 2015.03.10
[Exception] Exception의 종류  (0) 2014.08.28
[InputMethodManager] Keyboard 컨트롤  (0) 2014.07.24
Posted by 소망아기
: