Java implementation of OpenGL ES texture mapping method _java

Source: Internet
Author: User
Tags clear screen

In this paper, we illustrate the method of implementing OpenGL ES texture mapping in java. Share to everyone for your reference. Specifically as follows:

1. Glrenderer.java Documents:

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;
    /** constructor to set of 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_buffe R_bit |
    Gl10.gl_depth_buffer_bit);
    Reset the Modelview Matrix gl.glloadidentity ();
    Drawing Gl.gltranslatef (0.0f, 0.0f, -5.0f);
Move 5 units to the screen/are the same as moving the camera 5 units away//Gl.glscalef (0.5f, 0.5f, 0.5f); Scale the square to 50%//otherwise it'll 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) Widt
    H/(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 T
    Exture 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 does//really nice perspective calculations Gl.glhint (gl10.gl_perspective_correction_ 
  HINT, gl10.gl_nicest);

 }
}

2. Square.java File:

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-to
  P 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 = Bytebu
    Ffer.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.draw
    Able.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 the 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" GL Context */public void Draw (GL10 GL) {//Bind the previously Gen
    erated 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 File:

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 and the GL context * * public void Draw (GL10 gl) {gl.glenableclientstate (GL
    10.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 File:

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 the activity is a.
    * * @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.layout
    Params.flag_fullscreen); 
    Initiate the "Open GL View and//create an instance with the" Glsurfaceview = new Glsurfaceview (this); Set our renderer to is the main renderer with//the current Activity context glsurfaceview.setrenderer (NE
    W 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 ();

 }
}

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.