본문 바로가기

카테고리 없음

안드로이드 OpenGL ES 예제, Flat Coloring

2개의 사각형이 각각 다른 색상을 가지고 Y축을 중심으로 회전하는 예

Activity

package gl.test3;


import android.app.Activity;

import android.opengl.GLSurfaceView;

import android.os.Bundle;

import android.view.Window;

import android.view.WindowManager;


public class TutorialPartIII 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);

    }

}


Renderer

package gl.test3;


import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;


import android.opengl.GLU;

import android.opengl.GLSurfaceView.Renderer;


public class OpenGLRenderer implements Renderer {

private Square square;

private float angle = 0;

public OpenGLRenderer() {

square = new Square();

}


public void onSurfaceCreated(GL10 gl, EGLConfig config) {

// Set the background color to black ( rgba ).

gl.glClearColor(0.0f, 0.0f, 0.0f, 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) {

// Clears the screen and depth buffer.

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// Replace the current matrix with the identity matrix

gl.glLoadIdentity();

// Translates 10 units into the screen.

gl.glTranslatef(0, -4, -30);

// Rotate square A counter-clockwise.

gl.glRotatef(angle, 0, 1, 0);

gl.glTranslatef(7f, 0f, 0f);

gl.glRotatef(-angle, 0, 1, 0);

gl.glPushMatrix();

// Draw square A.

square.color = "RED";

square.draw(gl);  // 적색 큰 사각형

gl.glPopMatrix();

gl.glRotatef(-angle*3, 0, 1, 0);

gl.glTranslatef(2f, 0f, 0f);

gl.glRotatef(angle*3, 0, 1, 0);

gl.glScalef(.5f, .5f, .5f);

square.color = "BLUE";

square.draw(gl);  // 청색 작은 사각형

// Increse the angle.

angle+=5;

}

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();

}

}



Square

package gl.test3;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;


public class Square {

String color;

private float vertices[] = {

     -1.0f,  1.0f, 0.0f,  // 0, Top Left

     -1.0f, -1.0f, 0.0f,  // 1, Bottom Left

      1.0f, -1.0f, 0.0f,  // 2, Bottom Right

      1.0f,  1.0f, 0.0f,  // 3, Top Right

};

// The order we like to connect them.

private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.

private FloatBuffer vertexBuffer;


// Our index buffer.

private ShortBuffer indexBuffer;

public Square() {

// a float is 4 bytes, therefore we multiply the number if 

// vertices with 4.

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);

vbb.order(ByteOrder.nativeOrder());

vertexBuffer = vbb.asFloatBuffer();

vertexBuffer.put(vertices);

vertexBuffer.position(0);

// short is 2 bytes, therefore we multiply the number if 

// vertices with 2.

ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);

ibb.order(ByteOrder.nativeOrder());

indexBuffer = ibb.asShortBuffer();

indexBuffer.put(indices);

indexBuffer.position(0);

}


public void draw(GL10 gl) {

if(color.equals("RED")) gl.glColor4f(1f, 0f, 0f, 1f);

     else if(color.equals("BLUE")) gl.glColor4f(0f, 0f, 1f, 1f);

// Counter-clockwise winding.

//gl.glFrontFace(GL10.GL_CCW);

// Enable face culling.

//gl.glEnable(GL10.GL_CULL_FACE);

// What faces to remove with the face culling.

//gl.glCullFace(GL10.GL_BACK);

// Enabled the vertices buffer for writing and to be used during 

// rendering.

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// Specifies the location and data format of an array of vertex

// coordinates to use when rendering.

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 

GL10.GL_UNSIGNED_SHORT, indexBuffer);

// Disable the vertices buffer.

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// Disable face culling.

gl.glDisable(GL10.GL_CULL_FACE);

}

}