Android OpenGL ES image Paster example

Source: Internet
Author: User

Main Activity

public class TestGLCubeActivity extends Activity {    /** Called when the activity is first created. */private GLSurfaceView GLMainView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        GLMainView = new GLSurfaceView(this);        GLMainView.setRenderer(new MyRender06(this));        setContentView(GLMainView);    }        @Override    protected void onResume() {    // TODO Auto-generated method stub    super.onResume();GLMainView.onResume();}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();GLMainView.onPause();}}

Render

Public class myrender06 implements Renderer {private context mcontext; private cube Mcube; myrender06 (context) {mcontext = context; Mcube = new cube () ;}@ overridepublic void onsurfacecreated (gl10 GL, eglconfig config) {GL. glable (gl10.gl _ texture_2d);/* is used to enable shadow smoothness. Shadow smooths colors by using polygon, and smooths external light */GL. glshademodel (gl10.gl _ smooth);/* black background */GL. glclearcolor (0, 0, 0, 0); GL. glcleardepthf (1.0f);/* enable deep test */GL. glable (gl10.gl _ depth_test);/* Imagine the depth cache as the layer behind the screen, which constantly tracks the depth of the object into the screen. * If the depth is small or equal, if it is gl_less, then it is rendered when the depth is small */GL. gldepthfunc (gl10.gl _ lequal);/* tells the system to correct the perspective */GL. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ nicest); Mcube. loadgltexture (GL, mcontext) ;}@ overridepublic void ondrawframe (gl10 GL) {/* clear screen and depth cache */GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit);/* reset the current model observation matrix */GL. glloadidentity ();/* do not move here * // If the Z parameter is-2f, the model matrix is moved from the external screen to 2f, that is, the entire coordinate system is moved in, the figure is much smaller */GL. gltranslatef (0.0f, 0.0f,-2f); Mcube. draw (GL) ;}@ overridepublic void onsurfacechanged (gl10 GL, int width, int height) {/* set the area size to be seen by the eye */GL. glviewport (0, 0, width, height);/* sets the projection matrix to make the object look near, far, and small. */GL. glmatrixmode (gl10.gl _ projection);/* reset the projection matrix so that the origin of the coordinate system returns to the initial position */GL. glloadidentity ();/* set the size of the viewport, near-cut surface, and remote cut surface * for GL. glfrustumf (left, right, bottom, top, znear, zfar) * znear. This indicates your cutting surface, and zfar indicates the cutting surface in the distance, */float ratio = (float) width/height; GL. glfrustumf (-ratio, ratio,-1, 1, 1, 10); // The coordinates of Z are as follows: -1.0f> Z> 10.0f/* select the model observation matrix */GL. glmatrixmode (gl10.gl _ modelview);/* reset the model observation matrix */GL. glloadidentity ();}}

First draw a texture

Public class cube {private floatbuffer vertexbuffer; private floatbuffer texturebuffer; private bytebuffer indexbuffer; private int [] textures = new int [1]; private float vertices [] = {// 6 faces // vertices according to faces-1.0f,-1.0f,-1.1f, // vertex 0 BL 1.0f,-1.0f,-1.1f, // V1 Br-1.0f, 1.0f,-1.1f, // V2 HL 1.0f, 1.0f,-1.1f, // V3 HR }; private float texture [] ={// mapping coordinates for the vertices 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f ,}; private byte indices [] = {// faces definition, 3, 2, // face front}; Public cube () {// coordinate of each vertex bytebuffer bytebuf = bytebuffer. allocatedirect (vertices. length * 4); bytebuf. order (byteorder. nativeorder (); vertexbuffer = bytebuf. asfloatbuffer (); vertexbuffer. put (vertices); vertexbuffer. position (0); // point coordinate of the 2D graph bytebuf = bytebuffer. allocatedirect (texture. length * 4); bytebuf. order (byteorder. nativeorder (); texturebuffer = bytebuf. asfloatbuffer (); texturebuffer. put (texture); texturebuffer. position (0); // The Order indexbuffer = bytebuffer for Fixed-Point plotting. allocatedirect (indices. length); indexbuffer. put (indices); indexbuffer. position (0);} public void draw (gl10 GL) {// if not changed, subsequent texture operations will be performed on this texture. glbindtexture (gl10.gl _ texture_2d, textures [0]); GL. glableclientstate (gl10.gl _ vertex_array); GL. glableclientstate (gl10.gl _ texture_coord_array);/* the front is counterclockwise */GL. glfrontface (gl10.gl _ CCW); GL. glvertexpointer (3, gl10.gl _ float, 0, vertexbuffer); GL. gltexcoordpointer (2, gl10.gl _ float, 0, texturebuffer); GL. gldrawelements (gl10.gl _ triangles, indices. length, gl10.gl _ unsigned_byte, indexbuffer); GL. gldisableclientstate (gl10.gl _ vertex_array); GL. gldisableclientstate (gl10.gl _ texture_coord_array);} public void loadgltexture (gl10 GL, context) {inputstream is = context. getresources (). openrawresource (R. drawable. icon); Bitmap bitmap = NULL; try {bitmap = bitmapfactory. decodestream (is);} finally {try {is. close (); is = NULL;} catch (ioexception e) {}} // generate a texture space GL. glgentextures (1, textures, 0); // bind the generated texture to gl_texture_2dgl.glbindtexture (gl10.gl _ texture_2d, textures [0]); 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); 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); // Save the image as texture [0] glutils. teximage2d (gl10.gl _ texture_2d, 0, bitmap, 0); bitmap. recycle ();}}
Related Article

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.