OpenGL draws the pyramid and removes it.

Source: Internet
Author: User

/**
* 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 );

}

 

**

* Pyramid, square

*/

Public class mytriangleconerenderer extends actmyrenderer {

Public void onsurfacecreated (gl10 GL, eglconfig config ){

Gl. glclearcolor (0f, 0f, 0f, 1f );
// Vertex buffer
Gl. glableclientstate (gl10.gl _ vertex_array );
// Enable color Buffer
Gl. glableclientstate (gl10.gl _ color_array );
}
 
Public void ondrawframe (gl10 GL ){
// Clear the color buffer and depth buffer
Gl. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit );
// Set the drawing color
Gl. glcolor4f (1f, 0f, 0f, 1f );

// Enable deep Test
Gl. glable (gl10.gl _ depth_test );

// Enable Surface Removal
Gl. glable (gl10.gl _ cull_face );
// Specify the preceding ()
// CCW: counter clock wise --> counter-clockwise
// Cw: clock wise --> clockwise
Gl. glfrontface (gl10.gl _ CCW );
// Remove the back
Gl. glcullface (gl10.gl _ Back );

// Gl10.gl _ smooth: Smooth coloring (default)
// Gl10.gl _ flat: monotonous mode
Gl. glshademodel (gl10.gl _ flat );

// 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 =-0.5f;

****************** ******/
// Vertex coordinate set
List <float> coordslist = new arraylist <float> ();
// Add a cone vertex
Coordslist. Add (0f );
Coordslist. Add (0f );
Coordslist. Add (0.5f );

// Vertex Color Set
List <float> colorlist = new arraylist <float> ();
Colorlist. Add (1f); // R
Colorlist. Add (0f); // G
Colorlist. Add (0f); // B
Colorlist. Add (1f); //

****************** ******/
// Cone Coordinate
List <float> coordsconebottomlist = new arraylist <float> ();
Coordsconebottomlist. Add (0f );
Coordsconebottomlist. Add (0f );
Coordsconebottomlist. Add (-0.5f );

Boolean flag = false;
// Bottom
For (float alpha = 0f; Alpha <math. Pi * 6; alpha = (float) (alpha + math. PI/8 )){
// Cone
X = (float) (R * Math. Cos (alpha ));
Y = (float) (R * Math. Sin (alpha ));
Coordslist. Add (X );
Coordslist. Add (y );
Coordslist. Add (z );

// Cone Coordinate
Coordsconebottomlist. Add (X );
Coordsconebottomlist. Add (y );
Coordsconebottomlist. Add (z );

// Vertex color value
If (flag =! Flag ){
// Yellow
Colorlist. Add (1f );
Colorlist. Add (1f );
Colorlist. Add (0f );
Colorlist. Add (1f );
}
Else {
// Red
Colorlist. Add (1f );
Colorlist. Add (0f );
Colorlist. Add (0f );
Colorlist. Add (1f );
}
}
// Vertex color value
If (flag =! Flag ){
// Yellow
Colorlist. Add (1f );
Colorlist. Add (1f );
Colorlist. Add (0f );
Colorlist. Add (1f );
}
Else {
// Red
Colorlist. Add (1f );
Colorlist. Add (0f );
Colorlist. Add (0f );
Colorlist. Add (1f );
}

// Color Buffer
Bytebuffer colorbuffer = bufferutil. list2bytebuffer (colorlist );
Gl. glcolorpointer (4, gl10.gl _ float, 0, colorbuffer );
// Draw the cone
Gl. glvertexpointer (3, gl10.gl _ float, 0, bufferutil. list2bytebuffer (coordslist ));
Gl. gldrawarrays (gl10.gl _ triangle_fan, 0, coordslist. Size ()/3 );

// Remove the front
Gl. glcullface (gl10.gl _ front );
// Draw the cone
Colorbuffer. Position (4 );
Gl. glcolorpointer (4, gl10.gl _ float, 0, colorbuffer );
Gl. glvertexpointer (3, gl10.gl _ float, 0, bufferutil. list2bytebuffer (coordsconebottomlist ));
Gl. gldrawarrays (gl10.gl _ triangle_fan, 0, coordsconebottomlist. Size ()/3 );
}
}

// Main Interface

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 mytriangleconerenderer ();

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 );

}

}

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.