Android OpenGL ES Part1, Blue Screen
원문 출처: http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/
프로그램을 시작하면 파란색 배경 전체화면에 채워져 나타난다
참고 사이트 1 : http://www.arcsynthesis.org/gltut/
참고 사이트 2 : http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html
적용실습 : 위의 자료를 참고하여, 배경 색상이 적색 -> 초록 -> 청색 순으로 1초마다 변경되도록 코드를 작성해보세요
Activity
package gl.test1;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class OpenGLESPart01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 전체화면을 사용하기 위한 설정 */
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
}
}
package gl.lesson01;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class OpenGLRenderer implements Renderer {
float r, g, b;
long prevTime, currentTime;
public OpenGLRenderer() {
r = 1f;
g = 0f;
b = 0f;
prevTime = System.currentTimeMillis();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background color to black ( rgba ).
gl.glClearColor(r, g, b, 0.5f);
// Enable Smooth Shading, default not really needed.
gl.glShadeModel(GL10.GL_SMOOTH);
// Depth buffer setup.
gl.glClearDepthf(1.0f);
// Enables depth testing.
gl.glEnable(GL10.GL_DEPTH_TEST);
// The type of depth testing to do.
gl.glDepthFunc(GL10.GL_LEQUAL);
// Really nice perspective calculations.
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
public void onDrawFrame(GL10 gl) {
gl.glClearColor(r, g, b, 0.5f);
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
currentTime = System.currentTimeMillis();
if(currentTime-prevTime<1000) return;
if(r==1f) {
r = 0f;
g = 1f;
}else if(g==1f) {
g = 0f;
b = 1f;
}else if(b==1f) {
b = 0f;
r = 1f;
}
prevTime = currentTime;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// Sets the current view port to the new size.
gl.glViewport(0, 0, width, height);
// Select the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
gl.glLoadIdentity();
// Calculate the aspect ratio of the window
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
// Select the modelview matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);
// Reset the modelview matrix
gl.glLoadIdentity();
}
}