OpenGL Cropping Area

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

 

/**
* Cropping Area
*/
Public class myscissorrenderer extends actmyrenderer {

Private int width;
Private int height;
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 );
}
 
Public void onsurfacechanged (gl10 GL, int width, int height ){
This. width = width;
This. Height = height;

Gl. glviewport (0, 0, width, height );
Ratio = (float) width/(float) height;
Gl. glmatrixmode (gl10.gl _ projection );
Gl. glloadidentity ();
Gl. glfrustumf (-ratio, ratio,-1, 1, 3f, 7f );
}
 
Public void ondrawframe (gl10 GL ){
Gl. glclear (gl10.gl _ color_buffer_bit );
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 );

// Enable Cropping
Gl. glable (gl10.gl _ scissor_test );

// Rotation Angle
Gl. glrotatef (xrotate, 1, 0, 0 );
Gl. glrotatef (yrotate, 0, 1, 0 );

Float [] coords = {
-Ratio, 1f, 2f,
-Ratio,-1f, 2f,
Ratio, 1f, 2f,
Ratio,-1f, 2f
};

// Color array
Float [] [] colors = {
{1f, 0f, 0f, 1f },
{0f, 1f, 0f, 1f },
{0f, 0f, 1f, 1f },
{1f, 1f, 0f, 1f },
{0f, 1f, 1f, 1f },
{1f, 0f, 1f, 1f },
};

Int step = 20;
For (INT I = 0; I <6; I ++ ){
// Set the Cropping Area
Gl. glscissor (I * 20, I * 20, width-(I * 20*2), height-(I * 20*2 ));
// Set the color
Gl. glcolor4f (colors [I] [0], colors [I] [1], colors [I] [2], colors [I] [3]);
Gl. glvertexpointer (3, gl10.gl _ float, 0, bufferutil. arr2bytebuffer (coords ));
Gl. gldrawarrays (gl10.gl _ triangle_strip, 0, 4 );
}
}
}

 

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

}

}

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.