java实现OpenGL ES纹理映射的方法
程序员文章站
2024-03-04 09:59:05
本文实例讲述了java实现opengl es纹理映射的方法。分享给大家供大家参考。具体如下:
1. glrenderer.java文件:
package net...
本文实例讲述了java实现opengl es纹理映射的方法。分享给大家供大家参考。具体如下:
1. glrenderer.java文件:
package net.obviam.opengl; import javax.microedition.khronos.egl.eglconfig; import javax.microedition.khronos.opengles.gl10; import android.content.context; import android.opengl.glu; import android.opengl.glsurfaceview.renderer; public class glrenderer implements renderer { private square square; // the square private context context; /** constructor to set the handed over context */ public glrenderer(context context) { this.context = context; // initialise the square this.square = new square(); } @override public void ondrawframe(gl10 gl) { // clear screen and depth buffer gl.glclear(gl10.gl_color_buffer_bit | gl10.gl_depth_buffer_bit); // reset the modelview matrix gl.glloadidentity(); // drawing gl.gltranslatef(0.0f, 0.0f, -5.0f); // move 5 units into the screen // is the same as moving the camera 5 units away // gl.glscalef(0.5f, 0.5f, 0.5f); // scale the square to 50% // otherwise it will be too large square.draw(gl); // draw the triangle } @override public void onsurfacechanged(gl10 gl, int width, int height) { if(height == 0) { //prevent a divide by zero by height = 1; //making height equal one } gl.glviewport(0, 0, width, height); //reset the current viewport gl.glmatrixmode(gl10.gl_projection); //select the projection matrix gl.glloadidentity(); //reset the projection matrix //calculate the aspect ratio of the window glu.gluperspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); gl.glmatrixmode(gl10.gl_modelview); //select the modelview matrix gl.glloadidentity(); //reset the modelview matrix } @override public void onsurfacecreated(gl10 gl, eglconfig config) { // load the texture for the square square.loadgltexture(gl, this.context); gl.glenable(gl10.gl_texture_2d); //enable texture mapping ( new ) gl.glshademodel(gl10.gl_smooth); //enable smooth shading gl.glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); //black background gl.glcleardepthf(1.0f); //depth buffer setup gl.glenable(gl10.gl_depth_test); //enables depth testing gl.gldepthfunc(gl10.gl_lequal); //the type of depth testing to do //really nice perspective calculations gl.glhint(gl10.gl_perspective_correction_hint, gl10.gl_nicest); } }
2. square.java文件:
package net.obviam.opengl; import java.nio.bytebuffer; import java.nio.byteorder; import java.nio.floatbuffer; import javax.microedition.khronos.opengles.gl10; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.opengl.glutils; public class square { private floatbuffer vertexbuffer; // buffer holding the vertices private float vertices[] = { -1.0f, -1.0f, 0.0f, // v1 - bottom left -1.0f, 1.0f, 0.0f, // v2 - top left 1.0f, -1.0f, 0.0f, // v3 - bottom right 1.0f, 1.0f, 0.0f // v4 - top right }; private floatbuffer texturebuffer; // buffer holding the texture coordinates private float texture[] = { // mapping coordinates for the vertices 0.0f, 1.0f, // top left (v2) 0.0f, 0.0f, // bottom left (v1) 1.0f, 1.0f, // top right (v4) 1.0f, 0.0f // bottom right (v3) }; /** the texture pointer */ private int[] textures = new int[1]; public square() { // a float has 4 bytes so we allocate for each coordinate 4 bytes bytebuffer bytebuffer = bytebuffer.allocatedirect(vertices.length * 4); bytebuffer.order(byteorder.nativeorder()); // allocates the memory from the byte buffer vertexbuffer = bytebuffer.asfloatbuffer(); // fill the vertexbuffer with the vertices vertexbuffer.put(vertices); // set the cursor position to the beginning of the buffer vertexbuffer.position(0); bytebuffer = bytebuffer.allocatedirect(texture.length * 4); bytebuffer.order(byteorder.nativeorder()); texturebuffer = bytebuffer.asfloatbuffer(); texturebuffer.put(texture); texturebuffer.position(0); } /** * load the texture for the square * @param gl * @param context */ public void loadgltexture(gl10 gl, context context) { // loading texture bitmap bitmap = bitmapfactory.decoderesource(context.getresources(), r.drawable.android); // generate one texture pointer gl.glgentextures(1, textures, 0); // ...and bind it to our array gl.glbindtexture(gl10.gl_texture_2d, textures[0]); // create nearest filtered texture 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); //different possible texture parameters, 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); // use android glutils to specify a two-dimensional texture image from our bitmap glutils.teximage2d(gl10.gl_texture_2d, 0, bitmap, 0); // clean up bitmap.recycle(); } /** the draw method for the square with the gl context */ public void draw(gl10 gl) { // bind the previously generated texture gl.glbindtexture(gl10.gl_texture_2d, textures[0]); // point to our buffers gl.glenableclientstate(gl10.gl_vertex_array); gl.glenableclientstate(gl10.gl_texture_coord_array); // set the face rotation gl.glfrontface(gl10.gl_cw); // point to our vertex buffer gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer); gl.gltexcoordpointer(2, gl10.gl_float, 0, texturebuffer); // draw the vertices as triangle strip gl.gldrawarrays(gl10.gl_triangle_strip, 0, vertices.length / 3); //disable the client state before leaving gl.gldisableclientstate(gl10.gl_vertex_array); gl.gldisableclientstate(gl10.gl_texture_coord_array); } }
3. triangle.java文件:
package net.obviam.opengl; import java.nio.bytebuffer; import java.nio.byteorder; import java.nio.floatbuffer; import javax.microedition.khronos.opengles.gl10; public class triangle { private floatbuffer vertexbuffer; // buffer holding the vertices private float vertices[] = { -0.5f, -0.5f, 0.0f, // v1 - first vertex (x,y,z) 0.5f, -0.5f, 0.0f, // v2 - second vertex 0.0f, 0.5f, 0.0f // v3 - third vertex // 1.0f, 0.5f, 0.0f // v3 - third vertex }; public triangle() { // a float has 4 bytes so we allocate for each coordinate 4 bytes bytebuffer vertexbytebuffer = bytebuffer.allocatedirect(vertices.length * 4); vertexbytebuffer.order(byteorder.nativeorder()); // allocates the memory from the byte buffer vertexbuffer = vertexbytebuffer.asfloatbuffer(); // fill the vertexbuffer with the vertices vertexbuffer.put(vertices); // set the cursor position to the beginning of the buffer vertexbuffer.position(0); } /** the draw method for the triangle with the gl context */ public void draw(gl10 gl) { gl.glenableclientstate(gl10.gl_vertex_array); // set the colour for the background // gl.glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); // to show the color (paint the screen) we need to clear the color buffer // gl.glclear(gl10.gl_color_buffer_bit); // set the colour for the triangle gl.glcolor4f(0.0f, 1.0f, 0.0f, 0.5f); // point to our vertex buffer gl.glvertexpointer(3, gl10.gl_float, 0, vertexbuffer); // draw the vertices as triangle strip gl.gldrawarrays(gl10.gl_triangle_strip, 0, vertices.length / 3); //disable the client state before leaving gl.gldisableclientstate(gl10.gl_vertex_array); } }
4. run.java文件:
package net.obviam.opengl; import android.app.activity; import android.opengl.glsurfaceview; import android.os.bundle; import android.view.window; import android.view.windowmanager; public class run extends activity { /** the opengl view */ private glsurfaceview glsurfaceview; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // requesting to turn the title off requestwindowfeature(window.feature_no_title); // making it full screen getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); // initiate the open gl view and // create an instance with this activity glsurfaceview = new glsurfaceview(this); // set our renderer to be the main renderer with // the current activity context glsurfaceview.setrenderer(new glrenderer(this)); setcontentview(glsurfaceview); } /** * remember to resume the glsurface */ @override protected void onresume() { super.onresume(); glsurfaceview.onresume(); } /** * also pause the glsurface */ @override protected void onpause() { super.onpause(); glsurfaceview.onpause(); } }
希望本文所述对大家的java程序设计有所帮助。