/**
* Buffer tool class
*/
Public class bufferutil {
/**
* Convert a floating point group to a byte Buffer
*/
Public static bytebuffer arr2bytebuffer (float [] ARR ){
Bytebuffer Ibb = bytebuffer. allocatedirect (ARR. length * 4 );
Ibb. Order (byteorder. nativeorder ());
Floatbuffer fbb = Ibb. asfloatbuffer ();
Fbb. Put (ARR );
Ibb. Position (0 );
Return Ibb;
}
/**
* Convert list to byte Buffer
*/
Public static bytebuffer list2bytebuffer (list <float> List ){
Bytebuffer Ibb = bytebuffer. allocatedirect (list. Size () * 4 );
Ibb. Order (byteorder. nativeorder ());
Floatbuffer fbb = Ibb. asfloatbuffer ();
For (float F: List ){
Fbb. Put (f );
}
Ibb. Position (0 );
Return Ibb;
}
}
/**
* Renderer
*/
Public abstract class abstractmyrenderer implements Android. OpenGL. glsurfaceview. Renderer
{
Private float ratio;
Public float xrotate = 0f; // rotation angle around the X axis
Public float yrotate = 0f; // rotate the angle around the X axis
/**
* 1.
*/
Public void onsurfacecreated (gl10 GL, eglconfig config)
{
// Clear color
Gl. glclearcolor (0f, 0f, 0f, 1f );
// Enable the vertex buffer Array
Gl. glableclientstate (gl10.gl _ vertex_array );
}
/**
* 2.
*/
Public void onsurfacechanged (gl10 GL, int width, int height ){
// Set the viewport
Gl. glviewport (0, 0, width, height );
Ratio = (float) width/(float) height;
// Projection Matrix
Gl. glmatrixmode (gl10.gl _ projection );
// Load the Unit Matrix
Gl. glloadidentity ();
// Set the intercept
Gl. glfrustumf (-ratio, ratio,-1, 1, 3f, 7f );}
/**
* 3.
*/
Public abstract void ondrawframe (gl10 GL );
}
Public class mylinesrenderer extends actmyrenderer {
Public void ondrawframe (gl10 GL ){
// Clear the color Buffer
Gl. glclear (gl10.gl _ color_buffer_bit );
// Set the drawing color
Gl. glcolor4f (1f, 0f, 0f, 1f );
// Operation Model View Matrix
Gl. glmatrixmode (gl10.gl _ modelview );
Gl. glloadidentity ();
// Set eye Parameters
Glu. glulookat (GL, 0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f );
// Rotation Angle
Gl. glrotatef (xrotate, 1, 0, 0 );
Gl. glrotatef (yrotate, 0, 1, 0 );
// Calculate the coordinate of a vertex
Float r = 0.5f;
// Radius
Float x = 0f, y = 0f, Z = 0f;
List <float> coordslist = new arraylist <float> ();
// Draw points cyclically
For (float alpha = 0f; Alpha <math. Pi * 6; alpha = (float) (alpha + math. PI/16 )){
X = (float) (R * Math. Cos (alpha ));
Y = (float) (R * Math. Sin (alpha ));
Coordslist. Add (0f );
Coordslist. Add (0f );
Coordslist. Add (0f );
Coordslist. Add (X );
Coordslist. Add (y );
Coordslist. Add (z );
}
Gl. glvertexpointer (3, gl10.gl _ float, 0, bufferutil. list2bytebuffer (coordslist ));
Gl. gldrawarrays (gl10.gl _ lines, 0, coordslist. Size ()/3 );
}
}
/**
* Main Entry of the program
*/
Public class mainactivity extends activity {
Private abstractmyrenderer render;
Private myglsurfaceview view;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
View = new glsurfaceview (this );
Render = new mytrianglerenderer ();
View. setrenderer (render );
// Glsurfaceview. rendermode_continuously: continuous rendering (default)
// Glsurfaceview. rendermode_when_dirty: Dirty rendering, command rendering
View. setrendermode (glsurfaceview. rendermode_when_dirty );
Setcontentview (View );
}
Public Boolean onkeydown (INT keycode, keyevent event ){
Float step = 5f;
// Up
If (keycode = keyevent. keycode_dpad_up ){
Render. xrotate = render. xrotate-step;
} Else if (keycode = keyevent. keycode_dpad_down ){
Render. xrotate = render. xrotate + step;
} Else if (keycode = keyevent. keycode_dpad_left ){
Render. yrotate = render. yrotate + step;
} Else if (keycode = keyevent. keycode_dpad_right ){
Render. yrotate = render. yrotate-step;
}
// Request rendering, used with dirty Rendering
View. requestrender ();
Return super. onkeydown (keycode, event );
}
}