Android OpenGL ES

Source: Internet
Author: User

First:

Render:

Package com. example. OpenGL. Render;

Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. opengles. gl10;

Import com.example.opengl.doc ument. cylinder;

Import Android. OpenGL. glsurfaceview;
Import Android. OpenGL. glsurfaceview. Renderer;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. View. ontouchlistener;

Public class cylinderrender implements Renderer {
Public static class mytouchlistener implements ontouchlistener {
Private cylinderrender Tgl;
Private glsurfaceview surface;
Private float previusx;
Private float previusy;
Public mytouchlistener (cylinderrender Tgl, glsurfaceview surface)
{
This. Tgl = Tgl;
This. Surface = surface;
}

@ Override
Public Boolean ontouch (view arg0, motionevent arg1 ){
If (arg1.getaction () = motionevent. action_move)
{
Float dx = (arg1.getx ()-previusx) * 0.4f;
Float DY = (arg1.gety ()-previusy) * 0.4f;

Tgl. sety (dx );
Tgl. setx (dy );

}
Previusx = arg1.getx ();
Previusy = arg1.gety ();
Return true;
}

}

Private cylinder clinder = new cylinder (10, 2, 5, 18 );
@ Override
Public void ondrawframe (gl10 GL ){
// Enable background color and deep Cache
Gl. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit );

Gl. glmatrixmode (gl10.gl _ modelview );
Gl. glloadidentity ();
// Set the coordinate offset
Gl. gltranslatef (0, 0,-10f );

// Gl. glpushmatrix ();
// Draw a cylinder
Clinder. drawself (GL );
// Gl. glpopmatrix ();
}

@ Override
Public void onsurfacechanged (gl10 GL, int width, int height ){
Gl. glviewport (0, 0, width, height );
Gl. glmatrixmode (gl10.gl _ projection );
Gl. glloadidentity ();
Float ratio = (float) width/height;
Gl. glfrustumf (-ratio, ratio,-1,100 );
}

@ Override
Public void onsurfacecreated (gl10 GL, eglconfig config ){
Gl. glclearcolor (1, 1, 1, 1 );
Gl. glshademodel (gl10.gl _ smooth );
Gl. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest );
Gl. glable (gl10.gl _ depth_test );
}
Private void setx (float X ){
Clinder. setxangle (X );
}
Private void sety (float y ){
Clinder. setyangle (y );
}
Private void setz (float Z ){
Clinder. setzangle (z );
}

}

 

There is also an encapsulated cylindrical class:

Package com.example.opengl.doc ument;

Import java. NiO. bytebuffer;
Import java. NiO. byteorder;
Import java. NiO. floatbuffer;
Import java. util. arraylist;
Import java. util. List;

Import javax. microedition. khronos. opengles. gl10;

Public class cylinder {
Private floatbuffer vertexbuffer;
Private int vcount;
Private float xangle;
Private float yangle;
Private float zangle;

Public cylinder (float length, float radius, int blocks, float sectorblocks)
{
Float Len = length/blocks;
Float sectorangle = 360/sectorblocks;

List <float> arraylist = new arraylist <float> ();

For (INT I = 0; I <blocks; I ++)
{
For (float angle = 360; angle> 0; angle = angle-sectorangle)
{
// Vertex coordinates in the upper left corner
Float X1 =-(length/2-I * Len );
Float Y1 = (float) (radius * Math. Sin (math. toradians (angle )));
Float z1 = (float) (radius * Math. Cos (math. toradians (angle )));

// Vertex coordinates in the upper-right corner
Float X2 = X1 + Len;
Float y2 = Y1;
Float Z2 = Z1;

// Vertex coordinates in the lower right corner
Float X3 = x2;
Float Y3 = (float) (radius * Math. Sin (math. toradians (angle-sectorangle )));
Float Z3 = (float) (radius * Math. Cos (math. toradians (angle-sectorangle )));

// Vertex coordinates in the lower left corner
Float X4 = x1;
Float Y4 = Y3;
Float Z4 = Z3;

// The first line
Arraylist. Add (X1 );
Arraylist. Add (Y1 );
Arraylist. Add (Z1 );

Arraylist. Add (X2 );
Arraylist. Add (Y2 );
Arraylist. Add (Z2 );

// Second line
Arraylist. Add (X2 );
Arraylist. Add (Y2 );
Arraylist. Add (Z2 );

Arraylist. Add (X4 );
Arraylist. Add (Y4 );
Arraylist. Add (Z4 );

// The third line
Arraylist. Add (X4 );
Arraylist. Add (Y4 );
Arraylist. Add (Z4 );

Arraylist. Add (X1 );
Arraylist. Add (Y1 );
Arraylist. Add (Z1 );

// Line 4

Arraylist. Add (X2 );
Arraylist. Add (Y2 );
Arraylist. Add (Z2 );

Arraylist. Add (X3 );
Arraylist. Add (Y3 );
Arraylist. Add (Z3 );

// Line 5

Arraylist. Add (X3 );
Arraylist. Add (Y3 );
Arraylist. Add (Z3 );

Arraylist. Add (X4 );
Arraylist. Add (Y4 );
Arraylist. Add (Z4 );

}
}
Int size = arraylist. Size ();
Vcount = size/3;
Float [] vertex = new float [size];
For (INT I = 0; I <size; I ++)
{
Vertex [I] = arraylist. Get (I );
}

Bytebuffer VB = bytebuffer. allocatedirect (size * 4 );
VB. Order (byteorder. nativeorder ());
Vertexbuffer = VB. asfloatbuffer ();
Vertexbuffer. Put (vertex );
Vertexbuffer. Flip ();
Vertexbuffer. Position (0 );

}

Public void drawself (gl10 GL)
{
Gl. glableclientstate (gl10.gl _ vertex_array );
Gl. glvertexpointer (3, gl10.gl _ float, 0, vertexbuffer );
Gl. glcolor4f (0.2f, 0.2f, 0.2f, 0.2f); // you can specify the color of the drawn line.
Gl. glrotatef (xangle, 1, 0, 0 );
Gl. glrotatef (yangle, 0, 1, 0 );
Gl. glrotatef (zangle, 0, 0, 1 );
Gl. gldrawarrays (gl10.gl _ lines, 0, vcount );
}

/**
* @ Param xangle the xangle to set
*/
Public void setxangle (float xangle ){
This. xangle + = xangle;
}

/**
* @ Param yangle the yangle to set
*/
Public void setyangle (float yangle ){
This. yangle + = yangle;
}

/**
* @ Param zangle the zangle to set
*/
Public void setzangle (float zangle ){
This. zangle + = zangle;
}
}

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.