java实现OpenGL ES纹理映射的方法
本文实例讲述了java实现OpenGLES纹理映射的方法。分享给大家供大家参考。具体如下:
1.GlRenderer.java文件:
packagenet.obviam.opengl;
importjavax.microedition.khronos.egl.EGLConfig;
importjavax.microedition.khronos.opengles.GL10;
importandroid.content.Context;
importandroid.opengl.GLU;
importandroid.opengl.GLSurfaceView.Renderer;
publicclassGlRendererimplementsRenderer{
privateSquaresquare;//thesquare
privateContextcontext;
/**Constructortosetthehandedovercontext*/
publicGlRenderer(Contextcontext){
this.context=context;
//initialisethesquare
this.square=newSquare();
}
@Override
publicvoidonDrawFrame(GL10gl){
//clearScreenandDepthBuffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
//ResettheModelviewMatrix
gl.glLoadIdentity();
//Drawing
gl.glTranslatef(0.0f,0.0f,-5.0f);
//move5unitsINTOthescreen
//isthesameasmovingthecamera5unitsaway
//gl.glScalef(0.5f,0.5f,0.5f);
//scalethesquareto50%
//otherwiseitwillbetoolarge
square.draw(gl);//Drawthetriangle
}
@Override
publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){
if(height==0){//PreventADivideByZeroBy
height=1;//MakingHeightEqualOne
}
gl.glViewport(0,0,width,height);//ResetTheCurrentViewport
gl.glMatrixMode(GL10.GL_PROJECTION);//SelectTheProjectionMatrix
gl.glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,0.1f,100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
//SelectTheModelviewMatrix
gl.glLoadIdentity();//ResetTheModelviewMatrix
}
@Override
publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){
//Loadthetextureforthesquare
square.loadGLTexture(gl,this.context);
gl.glEnable(GL10.GL_TEXTURE_2D);//EnableTextureMapping(NEW)
gl.glShadeModel(GL10.GL_SMOOTH);//EnableSmoothShading
gl.glClearColor(0.0f,0.0f,0.0f,0.5f);//BlackBackground
gl.glClearDepthf(1.0f);//DepthBufferSetup
gl.glEnable(GL10.GL_DEPTH_TEST);//EnablesDepthTesting
gl.glDepthFunc(GL10.GL_LEQUAL);//TheTypeOfDepthTestingToDo
//ReallyNicePerspectiveCalculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);
}
}
2.Square.java文件:
packagenet.obviam.opengl;
importjava.nio.ByteBuffer;
importjava.nio.ByteOrder;
importjava.nio.FloatBuffer;
importjavax.microedition.khronos.opengles.GL10;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.opengl.GLUtils;
publicclassSquare{
privateFloatBuffervertexBuffer;//bufferholdingthevertices
privatefloatvertices[]={
-1.0f,-1.0f,0.0f,//V1-bottomleft
-1.0f,1.0f,0.0f,//V2-topleft
1.0f,-1.0f,0.0f,//V3-bottomright
1.0f,1.0f,0.0f//V4-topright
};
privateFloatBuffertextureBuffer;//bufferholdingthetexturecoordinates
privatefloattexture[]={
//Mappingcoordinatesforthevertices
0.0f,1.0f,//topleft(V2)
0.0f,0.0f,//bottomleft(V1)
1.0f,1.0f,//topright(V4)
1.0f,0.0f//bottomright(V3)
};
/**Thetexturepointer*/
privateint[]textures=newint[1];
publicSquare(){
//afloathas4bytessoweallocateforeachcoordinate4bytes
ByteBufferbyteBuffer=ByteBuffer.allocateDirect(vertices.length*4);
byteBuffer.order(ByteOrder.nativeOrder());
//allocatesthememoryfromthebytebuffer
vertexBuffer=byteBuffer.asFloatBuffer();
//fillthevertexBufferwiththevertices
vertexBuffer.put(vertices);
//setthecursorpositiontothebeginningofthebuffer
vertexBuffer.position(0);
byteBuffer=ByteBuffer.allocateDirect(texture.length*4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer=byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
/**
*Loadthetextureforthesquare
*@paramgl
*@paramcontext
*/
publicvoidloadGLTexture(GL10gl,Contextcontext){
//loadingtexture
Bitmapbitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.android);
//generateonetexturepointer
gl.glGenTextures(1,textures,0);
//...andbindittoourarray
gl.glBindTexture(GL10.GL_TEXTURE_2D,textures[0]);
//createnearestfilteredtexture
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);
//Differentpossibletextureparameters,e.g.GL10.GL_CLAMP_TO_EDGE
//gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT);
//gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT);
//UseAndroidGLUtilstospecifyatwo-dimensionaltextureimagefromourbitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bitmap,0);
//Cleanup
bitmap.recycle();
}
/**ThedrawmethodforthesquarewiththeGLcontext*/
publicvoiddraw(GL10gl){
//bindthepreviouslygeneratedtexture
gl.glBindTexture(GL10.GL_TEXTURE_2D,textures[0]);
//Pointtoourbuffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Setthefacerotation
gl.glFrontFace(GL10.GL_CW);
//Pointtoourvertexbuffer
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer);
//Drawtheverticesastrianglestrip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,vertices.length/3);
//Disabletheclientstatebeforeleaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
3.Triangle.java文件:
packagenet.obviam.opengl;
importjava.nio.ByteBuffer;
importjava.nio.ByteOrder;
importjava.nio.FloatBuffer;
importjavax.microedition.khronos.opengles.GL10;
publicclassTriangle{
privateFloatBuffervertexBuffer;//bufferholdingthevertices
privatefloatvertices[]={
-0.5f,-0.5f,0.0f,//V1-firstvertex(x,y,z)
0.5f,-0.5f,0.0f,//V2-secondvertex
0.0f,0.5f,0.0f//V3-thirdvertex
//1.0f,0.5f,0.0f//V3-thirdvertex
};
publicTriangle(){
//afloathas4bytessoweallocateforeachcoordinate4bytes
ByteBuffervertexByteBuffer=ByteBuffer.allocateDirect(vertices.length*4);
vertexByteBuffer.order(ByteOrder.nativeOrder());
//allocatesthememoryfromthebytebuffer
vertexBuffer=vertexByteBuffer.asFloatBuffer();
//fillthevertexBufferwiththevertices
vertexBuffer.put(vertices);
//setthecursorpositiontothebeginningofthebuffer
vertexBuffer.position(0);
}
/**ThedrawmethodforthetrianglewiththeGLcontext*/
publicvoiddraw(GL10gl){
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//setthecolourforthebackground
//gl.glClearColor(0.0f,0.0f,0.0f,0.5f);
//toshowthecolor(paintthescreen)weneedtoclearthecolorbuffer
//gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//setthecolourforthetriangle
gl.glColor4f(0.0f,1.0f,0.0f,0.5f);
//Pointtoourvertexbuffer
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
//Drawtheverticesastrianglestrip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,vertices.length/3);
//Disabletheclientstatebeforeleaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
4.Run.java文件:
packagenet.obviam.opengl;
importandroid.app.Activity;
importandroid.opengl.GLSurfaceView;
importandroid.os.Bundle;
importandroid.view.Window;
importandroid.view.WindowManager;
publicclassRunextendsActivity{
/**TheOpenGLview*/
privateGLSurfaceViewglSurfaceView;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//requestingtoturnthetitleOFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
//makingitfullscreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//InitiatetheOpenGLviewand
//createaninstancewiththisactivity
glSurfaceView=newGLSurfaceView(this);
//setourrenderertobethemainrendererwith
//thecurrentactivitycontext
glSurfaceView.setRenderer(newGlRenderer(this));
setContentView(glSurfaceView);
}
/**
*RemembertoresumetheglSurface
*/
@Override
protectedvoidonResume(){
super.onResume();
glSurfaceView.onResume();
}
/**
*AlsopausetheglSurface
*/
@Override
protectedvoidonPause(){
super.onPause();
glSurfaceView.onPause();
}
}
希望本文所述对大家的java程序设计有所帮助。
