Tuesday, April 25, 2017

Android OpenGL GLSurfaceView 예제

안드로이드에서 OpenGL ES를 사용하려면 가장 간단하게 GLSurfaceView를 사용하면 된다.
GLSurfaceView를 생성하고 GLSurfaceView.Renderer를 구현하면 기본적인 뼈대가 완성 된다.
아래의 예제는 화면을 빨간색으로 지우는 단순한 예제이다.

package com.duongame.opengl;

import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    GLSurfaceView glSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        glSurfaceView = new GLSurfaceView(this);
        glSurfaceView.setRenderer(new MainRenderer());
        setContentView(glSurfaceView);

        //setContentView(R.layout.activity_main);
    }
}

package com.duongame.opengl;

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

public class MainRenderer implements GLSurfaceView.Renderer {
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(1,0,0,1);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }
}

No comments:

Post a Comment