OpenGL getting started

Source: Internet
Author: User
Tags eye vector

/**

* Entry, triangle

*/

// Custom Renderer

Class myrenderer implements Android. OpenGL. glsurfaceview. Renderer {

Private float ratio;

// When the surface layer is created

Public void onsurfacecreated (gl10 GL, eglconfig config ){

// Set the screen color

Gl. glclearcolor (0, 0, 0, 1 );

// Enable the vertex buffer.

Gl. glableclientstate (gl10.gl _ vertex_array );

}

// When the surface size is

Public void onsurfacechanged (gl10 GL, int width, int height ){

// Set the view and output area.

Gl. glviewport (0, 0, width, height );

Ratio = (float) width/(float) height;

// Matrix mode, projection matrix, OpenGL Based on State Machine

Gl. glmatrixmode (gl10.gl _ projection );

// Load the Unit Matrix

Gl. glloadidentity ();

// Intercept

Gl. glfrustumf (-1f, 1f,-ratio, ratio, 3, 7 );

}

// Drawing

Public void ondrawframe (gl10 GL ){

// Clear the color cache area

Gl. glclear (gl10.gl _ color_buffer_bit );

// Model View Matrix

Gl. glmatrixmode (gl10.gl _ modelview );

Gl. glloadidentity (); // load the Unit Matrix

// Eyex, eyey, and Eyez: coordinates of the eye

// Centerx, centery, and centerz: eye observation point.

// UPX, UPX, and UPX: Specify the eye Vector

Glu. glulookat (GL, 0, 0, 5, 0, 0, 0, 0, 1, 0 );

// Draw a triangle

// Draw an array

// Triangle coordinates

Float [] coords = {

0f, ratio, 2f,

-1f,-ratio, 2f,

1f,-ratio, 2f };

// Allocate byte cache space to store vertex coordinate data

Bytebuffer Ibb = bytebuffer. allocatedirect (coords. length * 4 );

// Set the order (Local Order)

Ibb. Order (byteorder. nativeorder ());

// Place the vertex coordinate Array

Floatbuffer fbb = Ibb. asfloatbuffer ();

Fbb. Put (coords );

// Locate the pointer and read the vertex data from this position

Ibb. Position (0 );

// Set the drawing color in red.

Gl. glcolor4f (1f, 0f, 0f, 1f );

// 3-dimensional point, which uses three coordinate values to represent a point

// Type: Data Type of each vertex

// Stride: 0, span.

// Ibb: Specify the vertex buffer.

Gl. glvertexpointer (3, gl10.gl _ float, 0, Ibb );

// Draw a triangle

// 0: start point:

// 3: number of points to be drawn

Gl. gldrawarrays (gl10.gl _ triangles, 0, 3 );

}

}

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.