OpenGL ES for Android Development

Source: Internet
Author: User

I. Basic Knowledge:

OpenGL ES currently only supports triangles, but any polygon can be split into multiple triangles, so this restriction does not matter.

1. coordinate points in OpenGL:

Each coordinate point is composed of (X, Y, Z.

Defines the vertex array of a triangle:

[Java]

Int one = 0x10000;

// Three triangle vertices

Private IntBuffer triggerBuffer = IntBuffer. wrap (new int [] {

0, one, 0, // top Vertex

-One,-one, 0, // bottom left

One,-one, 0,}); // bottom right

Int one = 0x10000;

// Three triangle vertices

Private IntBuffer triggerBuffer = IntBuffer. wrap (new int [] {

0, one, 0, // top Vertex

-One,-one, 0, // bottom left

One,-one, 0,}); // defines an array of vertices of a square at the lower right point:

[Java]

// Four vertices of a square

Private IntBuffer quaterBuffer = IntBuffer. wrap (new int [] {

One, one, 0,

-One, one, 0,

One,-one, 0,

-One,-one, 0 });

// Four vertices of a square

Private IntBuffer quaterBuffer = IntBuffer. wrap (new int [] {

One, one, 0,

-One, one, 0,

One,-one, 0,

-One,-one, 0 });

2. Coordinate System in OpenGL:

After the gl. glLoadIdentity () function is called, the current vertex is actually moved to the center of the screen,

X axis from left to right, Y axis from bottom to top, and Z axis from inside to outside.

The coordinate values at the center of the OpenGL screen are 0.0f points on the X and Y axes.

The coordinate value on the left of the center is a negative value and a positive value on the right;

Moving to the top of the screen is positive, and moving to the bottom of the screen is negative;

Moving to the depth of the screen is a negative value, and moving out of the screen is a positive value.

During painting, we can use the glTranslatef function to move the position of the paint brush so that the image is displayed in our

The desired location.

[Java]

Gl. glTranslatef (-1.5f, 0.0f,-6.0f );

Gl. glTranslatef (-1.5f, 0.0f,-6.0f); this function shifts the paint brush to 1.5f units left along the X axis, the Y axis remains unchanged, and the Z axis moves 6.0f units to the screen.

Push the view to the back of the screen to see all the scenes. Note that the unit moving inside the screen

The value must be smaller than the distance we set through the glFrustumf method. Otherwise, the value is out of the angle of view and cannot be displayed.

3. vertex array in OpenGL:

In actual drawing, we often need to locate several points, and then let OpenGL draw based on this. Before you set the vertex position,

Follow these steps to enable our vertex array:

① Enable vertex settings for Kinetic Energy:

[Java]

Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );

Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); ② set the vertex array:

[Java] view plaincopyprint? Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer );

Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer); glVertexPointer (int size, int type, int stride, Buffer pointer)

Size is used to describe the size of a vertex (XYZ is used in this example, so it is 3). type describes the vertex type, which is fixed.

GL_FIXED, stride description step, pointer Points to the vertex cache, that is, the vertex array we create.

③ Draw the vertex:

[Java]

Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // draw a triangle

Gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4); // draw a quadrilateral

Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // draw a triangle

Gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4); // draw a quadrilateral glDrawArrays (int mode, int first, int count)

Mode indicates the draw mode. first and count indicate the start position and the vertex count to be drawn respectively.

4. Example: Draw a triangle and a square.

According to the Framework Analysis in the previous section, currently, we only need to focus on the drawing Operations Section in the onDrawFrame method.

1. Edit the interface (reslayoutmain. xml ):

[Java]

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"

/>

  

  

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

  

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"

/>

  

2. code editing

(SrcwyfzclMyActivity. java ):

[Java]

Package wyf. zcl;

Import android. app. Activity;

Import android. opengl. GLSurfaceView;

Import android. opengl. GLSurfaceView. Renderer;

Import android. OS. Bundle;

Public class Activity01 extends Activity

{

Renderer render = new GLRender ();

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState)

{

Super. onCreate (savedInstanceState );

GLSurfaceView glView = new GLSurfaceView (this );

GlView. setRenderer (render );

SetContentView (glView );

}

}

Package wyf. zcl;

Import android. app. Activity;

Import android. opengl. GLSurfaceView;

Import android. opengl. GLSurfaceView. Renderer;

Import android. OS. Bundle;

Public class Activity01 extends Activity

{

Renderer render = new GLRender ();

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState)

{

Super. onCreate (savedInstanceState );

GLSurfaceView glView = new GLSurfaceView (this );

GlView. setRenderer (render );

SetContentView (glView );

}

}

(SrcwyfzclGLRender. java ):

[Java]

Package wyf. zcl;

Import java. nio. IntBuffer;

Import javax. microedition. khronos. egl. EGLConfig;

Import javax. microedition. khronos. opengles. GL10;

Import android. opengl. GLSurfaceView. Renderer;

Public class GLRender implements Renderer

{

Int one = 0x10000;

// Three triangle vertices

Private IntBuffer triggerBuffer = IntBuffer. wrap (new int [] {

0, one, 0, // top Vertex

-One,-one, 0, // bottom left

One,-one, 0,}); // bottom right

// Four vertices of a square

Private IntBuffer quaterBuffer = IntBuffer. wrap (new int [] {

One, one, 0,

-One, one, 0,

One,-one, 0,

-One,-one, 0 });

@ Override

Public void onDrawFrame (GL10 gl)

{

// Clear screen and deep Cache

Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// Move 1.5 units left and 6.0 to the screen

Gl. glTranslatef (-1.5f, 0.0f,-6.0f );

// Allow vertex setting

Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );

// Set the triangle

Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer );

// Draw a triangle

Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// Move 1.5 units left and 6.0 to the screen

Gl. glTranslatef (1.5f, 0.0f,-6.0f );

// Set and draw a square

Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, quaterBuffer );

Gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4 );

// Cancel vertex settings

Gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );

}

@ Override

Public void onSurfaceChanged (GL10 gl, int width, int height)

{

Float ratio = (float) width/height;

// Set the size of the OpenGL scenario

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

// Sets the projection matrix.

Gl. glMatrixMode (GL10.GL _ PROJECTION );

// Reset the Projection Matrix

Gl. glLoadIdentity ();

// Set the preview size

Gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10 );

// Select the model observation matrix

Gl. glMatrixMode (GL10.GL _ MODELVIEW );

// Reset the model observation matrix

Gl. glLoadIdentity ();

}

@ Override

Public void onSurfaceCreated (GL10 gl, EGLConfig config)

{

// Enable shadow Smoothing

Gl. glShadeModel (GL10.GL _ SMOOTH );

// Black background

Gl. glClearColor (0, 0, 0, 0 );

// Set the deep Cache

Gl. glClearDepthf (1.0f );

// Enable deep Test

Gl. glable (GL10.GL _ DEPTH_TEST );

// Type of the deep Test

Gl. glDepthFunc (GL10.GL _ LEQUAL );

// Notify the system to correct the Perspective

Gl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST );

}

}

Package wyf. zcl;

Import java. nio. IntBuffer;

Import javax. microedition. khronos. egl. EGLConfig;

Import javax. microedition. khronos. opengles. GL10;

Import android. opengl. GLSurfaceView. Renderer;

Public class GLRender implements Renderer

{

Int one = 0x10000;

// Three triangle vertices

Private IntBuffer triggerBuffer = IntBuffer. wrap (new int [] {

0, one, 0, // top Vertex

-One,-one, 0, // bottom left

One,-one, 0,}); // bottom right

// Four vertices of a square

Private IntBuffer quaterBuffer = IntBuffer. wrap (new int [] {

One, one, 0,

-One, one, 0,

One,-one, 0,

-One,-one, 0 });

@ Override

Public void onDrawFrame (GL10 gl)

{

// Clear screen and deep Cache

Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// Move 1.5 units left and 6.0 to the screen

Gl. glTranslatef (-1.5f, 0.0f,-6.0f );

// Allow vertex setting

Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );

// Set the triangle

Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer );

// Draw a triangle

Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// Move 1.5 units left and 6.0 to the screen

Gl. glTranslatef (1.5f, 0.0f,-6.0f );

// Set and draw a square

Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, quaterBuffer );

Gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4 );

// Cancel vertex settings

Gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );

}

@ Override

Public void onSurfaceChanged (GL10 gl, int width, int height)

{

Float ratio = (float) width/height;

// Set the size of the OpenGL scenario

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

// Sets the projection matrix.

Gl. glMatrixMode (GL10.GL _ PROJECTION );

// Reset the Projection Matrix

Gl. glLoadIdentity ();

// Set the preview size

Gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10 );

// Select the model observation matrix

Gl. glMatrixMode (GL10.GL _ MODELVIEW );

// Reset the model observation matrix

Gl. glLoadIdentity ();

}

@ Override

Public void onSurfaceCreated (GL10 gl, EGLConfig config)

{

// Enable shadow Smoothing

Gl. glShadeModel (GL10.GL _ SMOOTH );

// Black background

Gl. glClearColor (0, 0, 0, 0 );

// Set the deep Cache

Gl. glClearDepthf (1.0f );

// Enable deep Test

Gl. glable (GL10.GL _ DEPTH_TEST );

// Type of the deep Test

Gl. glDepthFunc (GL10.GL _ LEQUAL );

// Notify the system to correct the Perspective

Gl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST );

}

}

3. Running effect:

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.