Android OpenGL ES color

Source: Internet
Author: User

I. Basic Knowledge: 1. Smooth coloring: combines different colors of multiple vertices to create a beautiful color mix. 2. monotonous coloring: Apply a fixed color to the image. 3. color array defined by the triangle (smooth coloring): [java] int one = 0x10000; // vertex color value of the triangle (r, g, B,) private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {one, one, 0, one, 0, one,}); [java // enable color rendering. gl. glableclientstate (GL10.GL _ COLOR_ARRAY); // sets the color of the triangle vertex gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer); // disable the color rendering function. gl. glDisableClientState (GL10.GL _ COLOR_ARRAY); 4. color array defined by a square (monotonic colored): [java] view plaincopy/* render square * // set the current color to Blue gl. glColor4f (1.0f, 0.5f, 1.0f, 1.0f); 2. code editing: [Activity01.java] [java] mples_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 vertices of a triangle: private IntBuffer triggerBuffer = IntBuffer. wrap (new int [] {0, one, 0, // top vertex-one,-one, 0, // sit down and click one,-one, 0 ,}); // bottom right vertex // four vertices of the square private IntBuffer quaterBuffer = IntBuffer. wrap (new int [] {one, one, 0,-one, one, 0, one,-one, 0 }); // The vertex color value of the triangle (r, g, B, a) private IntBuffer colorBuffer = IntBuffer. wrap (new int [] {one, 0, one, 0, one, 0, one, 0, 0, one, one,}); @ Override public void onDrawFrame (GL10 gl) {// clear the 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 to the left and 6.0 gl to the screen. glTranslatef (-1.5f, 0.0f,-6.0f); // you can specify a fixed-point array. glableclientstate (GL10.GL _ VERTEX_ARRAY); // sets the color array-enable the color rendering function. gl. glableclientstate (GL10.GL _ COLOR_ARRAY); // sets the color of the triangle vertex gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer); // sets the vertex gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer); // draw a triangle gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // close the color array-Disable the color rendering function. gl. glDisableClientState (GL10.GL _ COLOR_ARRAY);/* render 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 to the left and 6.0 gl to the screen. 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 gl in OpenGL. glViewport (0, 0, width, height); // sets the gl projection matrix. glMatrixMode (GL10.GL _ PROJECTION); // reset the PROJECTION matrix gl. glLoadIdentity (); // you can specify the size of the gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10); // select the model observation matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); // resets the model observation matrix gl. glLoadIdentity ();} www.2cto.com @ Override public void onSurfaceCreated (GL10 gl, EGLConfig config) {// enable shadow smooth gl. glShadeModel (GL10.GL _ SMOOTH); // black background gl. glClearColor (0, 0, 0, 0); // sets the deep cache gl. glClearDepthf (1.0f); // enable the deep test gl. glable (GL10.GL _ DEPTH_TEST); // type of the deep test gl. glDepthFunc (GL10.GL _ LEQUAL); // tells the system to modify gl for pivoting. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST) ;}} III. Running effect:

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.