도전2022

MainView.java opengles android 본문

작업/work2013

MainView.java opengles android

hotdigi 2013. 11. 29. 11:35
SMALL

package com.book.opengl.texture;



import java.io.BufferedInputStream;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

import java.nio.ShortBuffer; //


import android.content.Context;

import android.content.res.AssetManager;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.opengl.GLUtils;

import android.util.AttributeSet;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import javax.microedition.khronos.egl.EGL10;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.egl.EGLContext;

import javax.microedition.khronos.egl.EGLDisplay;

import javax.microedition.khronos.egl.EGLSurface;

import javax.microedition.khronos.opengles.GL10;


public class MainView extends SurfaceView 

implements SurfaceHolder.Callback

{

MainThread mMainThread;

Context    mMainContext;

SurfaceHolder mHolder;

FloatBuffer vertexBuffer;


ShortBuffer indexBuffer; //

FloatBuffer vertexBufferTex;

int texMonkey;

Bitmap bitmapMonkey;

EGL10 mEGL;

EGLDisplay mGLDisplay;

EGLConfig mGLConfig;

EGLSurface mGLSurface;

EGLContext mGLContext;

GL10 mGL;

boolean mIsDraw = false;

int mWidth = 0;

int mHeight = 0;


int VERTEX_SIZE = 4*4; //

public MainView(Context r, AttributeSet a){

super(r, a);

getHolder().addCallback(this);

setFocusable(true);

mMainContext = r;

mHolder = getHolder();

mHolder.addCallback(this);

// mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);

}

public void init(int screenWidth, int screenHeight){

mWidth = screenWidth;

mHeight = screenHeight;

}

public void glInit(

EGL10 eGL,

EGLDisplay gLDisplay,

EGLConfig gLConfig,

EGLSurface gLSurface,

EGLContext gLContext,

GL10 gL ){

mEGL = eGL;

mGLDisplay = gLDisplay;

mGLConfig = gLConfig;

mGLSurface = gLSurface;

mGLContext = gLContext;

mGL = gL;

mGL.glViewport(0,0,mWidth, mHeight);

mGL.glClear(GL10.GL_COLOR_BUFFER_BIT);

mGL.glMatrixMode(GL10.GL_PROJECTION);

mGL.glLoadIdentity();

// mGL.glOrthof(0, 480,0,800,1,-1);

mGL.glOrthof(0, 1000, 0, 2000,1,-1);

float[] vertexArray ={ 

100.0f, 100.0f, 0.0f, 1.0f,

900.0f, 100.0f, 1.0f, 1.0f, 

900.0f, 1900.0f, 1.0f, 0.0f,

100.0f, 1900.0f, 0.0f, 0.0f};

/*

float[] vertexArray ={ 

0.0f, 0.0f, 

480.0f, 0.0f, 

480.0f, 800.0f};

*/

// ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexArray.length*4); //VERTEX_SIZE*4);

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE*4); 


byteBuffer.order(ByteOrder.nativeOrder());

vertexBuffer = byteBuffer.asFloatBuffer();

vertexBuffer.put(vertexArray);

vertexBuffer.flip();

bitmapMonkey = loadBitmap("monkey.png");

int[] textures = new int[1];

mGL.glGenTextures(1, textures, 0);

texMonkey = textures[0];

mGL.glBindTexture(GL10.GL_TEXTURE_2D, texMonkey);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0 ,bitmapMonkey,0);

mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);

mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);

mGL.glBindTexture(GL10.GL_TEXTURE_2D, 0);

bitmapMonkey.recycle();

//////////////////

short[] indexArray= new short[] {0,1,2,0,2,3};

ByteBuffer indBuffer = ByteBuffer.allocateDirect(indexArray.length*2); //3,2,4

indBuffer.order(ByteOrder.nativeOrder());

indexBuffer = indBuffer.asShortBuffer();

indexBuffer.put(indexArray);

indexBuffer.flip();

//////////////////

mIsDraw = true;

}

public void surfaceChanged(SurfaceHolder holder, 

int format, int width, int height){

}

public void surfaceCreated(SurfaceHolder holder){

mWidth = this.getWidth();

mHeight = this.getHeight();

mMainThread = new MainThread(this, mWidth, mHeight);

mMainThread.start();

mIsDraw = true;

}

public void surfaceDestroyed(SurfaceHolder holder){

        if (mMainThread != null) {

        mMainThread.requestStop();

        }

}

public void drawOpenGL(){

if(mIsDraw == false)

return;

/*

mGL.glClear(GL10.GL_COLOR_BUFFER_BIT);

mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);

mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

vertexBuffer.position(0);

mGL.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);

mGL.glEnable(GL10.GL_TEXTURE_2D);

mGL.glBindTexture(GL10.GL_TEXTURE_2D,texMonkey);

mGL.glTexCoordPointer(2, GL10.GL_FLOAT, 0, vertexBufferTex);

mGL.glDrawArrays(GL10.GL_TRIANGLES, 0,3);

*/

mGL.glEnable(GL10.GL_TEXTURE_2D);

mGL.glBindTexture(GL10.GL_TEXTURE_2D,texMonkey);

mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);

mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

vertexBuffer.position(0);

mGL.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);

vertexBuffer.position(2);

mGL.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);

mGL.glDrawArrays(GL10.GL_TRIANGLES, 0,3);

mGL.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indexBuffer);

mEGL.eglSwapBuffers(mGLDisplay,  mGLSurface);

}

private Bitmap loadBitmap(String filename){

Bitmap bm = null;

try{

AssetManager am = mMainContext.getAssets();

BufferedInputStream buf = new BufferedInputStream(am.open(filename) );

bm = BitmapFactory.decodeStream(buf);

}

catch(Exception ex){

}

return bm;

}

}

LIST