Android Opengles Learning 1

Source: Internet
Author: User

Recently, libgdx was used in the project, so I took the time to read opengles, and there was not much information about Android. However, the code pages of opengles platforms are similar, and IOS information is rich, one triangle rotation is found most on the Internet. We recommend some things to learn before learning opengl, on Wikipedia, it is clear that matrix is very important. In opengl, many effects are implemented by matrix changes. The coordinates in opengl are different from those in android. algorithm Ray pickup, surrounded by algorithms... .. First paste the Code [java] public class OpenglLess1Activity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); GLSurfaceView view = new GLSurfaceView (this); view. setRenderer (new OpenglLess1Render (); setContentView (view);} public class OpenglLess1Activity extends Activity {@ Overrideprotected void onCreate (Bun Dle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); GLSurfaceView view = new GLSurfaceView (this); view. setRenderer (new OpenglLess1Render (); setContentView (view) ;}} [java] view plaincopyprint? Import java. nio. byteBuffer; import java. nio. byteOrder; import java. nio. floatBuffer; import javax. microedition. khronos. egl. EGLConfig; import javax. microedition. khronos. opengles. GL10; import android. opengl. GLSurfaceView. renderer; import android. util. log; public class OpenglLess1Render implements Renderer {protected float near = 1; protected float far = 20; protected float ratio; float [] lines = new float [] {-1f, 0f, 0f, // vertex coordinate 1.0f, 0f, 0f, 0f, 1.0f, 0f}; @ Override public void onSurfaceCreated (GL10 gl, EGLConfig config) {// TODO Auto-generated method stub // modify the gl for the perspective. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST); // enable shadow smoothing gl. glShadeModel (GL10.GL _ SMOOTH); gl. glClearColor (0, 0, 0, 0); // sets the deep cache gl. glClearDepthf (1.0f); // enable the deep test gl. glable (GL10.GL _ DEPTH_TEST); // as the type of deep test gl. glDepthFunc (GL10.GL _ LEQUAL) ;}@ Override public void onSurfaceChanged (GL10 gl, int width, int height) {// TODO Auto-generated method stub ratio = (float) width/height; Log. e ("lin", "width =" + width + "height =" + height + "ratio =" + ratio); // set the size of the OpenGl scenario gl. glViewport (0, 0, width, height); // sets the matrix projection gl. glMatrixMode (GL10.GL _ PROJECTION); gl. glLoadIdentity (); // set the serial port size near. The distance from the far field of view is assumed that the eyes are at the origin point and are in the negative direction of the Z axis, and the gl is invisible. glFrustumf (-ratio, ratio,-1, 1, near, far); // select the model observation matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); gl. glLoadIdentity () ;}@ Override public void onDrawFrame (GL10 gl) {// TODO Auto-generated method stub // clear color, deep cache gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // enable the model to try to matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); // loading a basic matrix can be understood as resetting the matrix because the original matrix is retained after each draw operation. If the matrix is not reset, when translating, the object will be farther and farther away. gl. glLoadIdentity (); // If the eye field distance is-1 and-20, only the objects in this range can be seen and moved to z =-3 gl. glTranslatef (0, 0,-3f); // defines the coordinate array Specifies the number of coordinates per vertex. must be 2, 3, or 4. the initial value is 4. gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, getFloatBuffer (lines); // enable the fixed-point array to correspond to the following disable gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); // sets the paint brush color gl. glColor4f (1.0F, 0F, 0F, 1.0F); // draw a triangle gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );} /*** allocate memory space. In versions earlier than 2.2, This is not required. * floating point variables occupy 4 bytes * @ param coords * @ return */public FloatBuffer getFloatBuffer (float [] coords) {ByteBuffer vbb = ByteBuffer. allocateDirect (coords. length * 4); vbb. order (ByteOrder. nativeOrder (); FloatBuffer mFVertexBuffer = vbb. asFloatBuffer (); mFVertexBuffer. put (coords); mFVertexBuffer. position (0); return mFVertexBuffer;} import java. nio. byteBuffer; import java. nio. byteOrder; import java. nio. floatBuffer; import javax. microedition. khronos. egl. EGLConfig; import javax. microedition. khronos. opengles. GL10; import android. opengl. GLSurfaceView. renderer; import android. util. log; public class OpenglLess1Render implements Renderer {protected float near = 1; protected float far = 20; protected float ratio; float [] lines = new float [] {-1f, 0f, 0f, // vertex coordinate 1.0f, 0f, 0f, 0f, 1.0f, 0f}; @ Overridepublic void onSurfaceCreated (GL10 gl, EGLConfig config) {// TODO Auto-generated method stub // modify the gl for the perspective. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST); // enable shadow smoothing gl. glShadeModel (GL10.GL _ SMOOTH); gl. glClearColor (0, 0, 0, 0); // sets the deep cache gl. glClearDepthf (1.0f); // enable the deep test gl. glable (GL10.GL _ DEPTH_TEST); // as the type of deep test gl. glDepthFunc (GL10.GL _ LEQUAL) ;}@ Overridepublic void onSurfaceChanged (GL10 gl, int width, int height) {// TODO Auto-generated method stub ratio = (float) width/height; log. e ("lin", "width =" + width + "height =" + height + "ratio =" + ratio); // set the size of the OpenGl scenario gl. glViewport (0, 0, width, height); // sets the matrix projection gl. glMatrixMode (GL10.GL _ PROJECTION); gl. glLoadIdentity (); // set the serial port size near. The distance from the far field of view is assumed that the eyes are at the origin point and are in the negative direction of the Z axis, and the gl is invisible. glFrustumf (-ratio, ratio,-1, 1, near, far); // select the model observation matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); gl. glLoadIdentity () ;}@ Overridepublic void onDrawFrame (GL10 gl) {// TODO Auto-generated method stub // clear color, deep cache gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // enable the model to try to matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); // loading a basic matrix can be understood as resetting the matrix because the original matrix is retained after each draw operation. If the matrix is not reset, when translating, the object will be farther and farther away. gl. glLoadIdentity (); // If the eye field distance is-1 and-20, only the objects in this range can be seen and moved to z =-3 gl. glTranslatef (0, 0,-3f); // defines the coordinate array Specifies the number of coordinates per vertex. must be 2, 3, or 4. the initial value is 4. gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, getFloatBuffer (lines); // enable the fixed-point array to correspond to the following disable gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); // sets the paint brush color gl. glColor4f (1.0F, 0F, 0F, 1.0F); // draw a triangle gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); gl. glDisableClientState (GL10.GL _ VERTEX_ARRAY );} /*** allocate memory space. In versions earlier than 2.2, This is not required. * floating point variables occupy 4 bytes * @ param coords * @ return */public FloatBuffer getFloatBuffer (float [] coords) {ByteBuffer vbb = ByteBuffer. allocateDirect (coords. length * 4); vbb. order (ByteOrder. nativeOrder (); FloatBuffer mFVertexBuffer = vbb. asFloatBuffer (); mFVertexBuffer. put (coords); mFVertexBuffer. position (0); return mFVertexBuffer ;}}

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.