I. Basic Knowledge:
1. Smooth coloring ):
Combine different colors of multiple vertices to create a beautiful color mix.
2. Monotonic coloring:
Apply a fixed color to the image.
3. Triangle-defined color array (smooth coloring ):
[Java]
Int one = 0x10000;
// Vertex color value of the triangle (r, g, B,)
Private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {
One, 0, 0, one,
0, one, 0, one,
0, 0, one, one,
});
Int one = 0x10000;
// Vertex color value of the triangle (r, g, B,)
Private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {
One, 0, 0, one,
0, one, 0, one,
0, 0, one, one,
});
[Java]
// Enable the color rendering function.
Gl. glableclientstate (GL10.GL _ COLOR_ARRAY );
// Set the triangle vertex color
Gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer );
// Disable the color rendering function.
Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
// Enable the color rendering function.
Gl. glableclientstate (GL10.GL _ COLOR_ARRAY );
// Set the triangle vertex color
Gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer );
// Disable the color rendering function.
Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
4. color array defined by the square (monotonous coloring ):
[Java]
/* Render the square */
// Set the current color to blue
Gl. glColor4f (1.0f, 0.5f, 1.0f, 1.0f );
/* Render the square */
// Set the current color to blue
Gl. glColor4f (1.0f, 0.5f, 1.0f, 1.0f );
Ii. code editing:
[Activity01.java]
[Java]
Package com. yarin. android. Examples_12_02;
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 com. yarin. android. Examples_12_02;
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 );
}
}
[GLRender. java]
[Java]
Package com. yarin. android. Examples_12_02;
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, // sit down
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 });
// Vertex color value of the triangle (r, g, B,)
Private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {
One, 0, 0, one,
0, one, 0, one,
0, 0, one, one,
});
@ 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 );
// Set a fixed-point Array
Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );
// Set the color array -- enable the color rendering function.
Gl. glableclientstate (GL10.GL _ COLOR_ARRAY );
// Set the triangle vertex color
Gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer );
// Set triangle vertices
Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer );
// Draw a triangle
Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );
// Disable color array -- disable color rendering.
Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
/* Render the square */
// Set the current color to blue
Gl. glColor4f (1.0f, 0.5f, 1.0f, 1.0f );
// 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 the vertex Array
Gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );
}
@ Override
Public void onSurfaceChanged (GL10 gl, int width, int height)
{
// TODO Auto-generated method stub
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 com. yarin. android. Examples_12_02;
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, // sit down
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 });
// Vertex color value of the triangle (r, g, B,)
Private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {
One, 0, 0, one,
0, one, 0, one,
0, 0, one, one,
});
@ 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 );
// Set a fixed-point Array
Gl. glableclientstate (GL10.GL _ VERTEX_ARRAY );
// Set the color array -- enable the color rendering function.
Gl. glableclientstate (GL10.GL _ COLOR_ARRAY );
// Set the triangle vertex color
Gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer );
// Set triangle vertices
Gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer );
// Draw a triangle
Gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3 );
// Disable color array -- disable color rendering.
Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );
/* Render the square */
// Set the current color to blue
Gl. glColor4f (1.0f, 0.5f, 1.0f, 1.0f );
// 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 the vertex Array
Gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );
}
@ Override
Public void onSurfaceChanged (GL10 gl, int width, int height)
{
// TODO Auto-generated method stub
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 );
}
}
Iii. Running effect: