OpenGL learning (2) basic graphics drawing

Continue OpenGL learning (1) basic concepts to draw basic graphics just modify GLRender

package com.android.chapter3; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; public class GLRender implements Renderer { int one = 0x10000; Float rotateTri, rotateQuad; float rotateTri, rotateQuad; Private IntBuffer = IntBuffer. Wrap (new int[]{0, one,0, -one,0, // one,-one,0,}); Private IntBuffer quaterBuffer = IntBuffer. Wrap (new int[]{one, one,0, -one,0, one,-one,0, -one,-one,0}); / / triangle vertex color value (r, g, b, a) private IntBuffer colorBuffer = IntBuffer. Wrap (new int [] {one, 0, 0, one, zero, one, zero, one, zero. 0,one,one, }); @override public void onDrawFrame(GL10 gl) {// TODO auto-generated method stub // Clean the screen first gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); Gl.glmatrixmode (gl10.gl_modelView); // reset the matrix gl.glloadidEntity (); Glu. gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); // Set model position gl.gltranslatef (-3.0f, 0.0f, -4.0f); Gl.glrotatef (rotateTri, 0.0f, 1.0f, 0.0f); // Set rotation (y axis) gl.glrotatef (rotateTri, 0.0f, 1.0f, 0.0f); Gl.glenableclientstate (gl10.gl_vertex_array); Gl.glenableclientstate (gl10.gl_COLOR_array); Gl.glcolorpointer (4, gl10.gl_fixed, 0, colorBuffer); Gl.glvertexpointer (3, gl10.gl_fixed, 0, triggerBuffer); Gl.glscalef (2.0f, 2.0f, 2.0f); // Triangles gl.glDrawArrays(gl10.gl_Triangles, 0, 3); Gl.gldisableclientstate (gl10.gl_COLOR_array); / * * * * * * * * * * square / / / set the color of the square. Gl glColor4f (0.5 f, f 0.5, 1.0 f, 1.0 f); // Reset the current model observation matrix gl.glloadidEntity (); // Set model position gl.gltranslatef (1.0f, 0.0f, -4.0f); Gl.glrotatef (rotateQuad, 1.0f, 0.0f, 0.0f); // Set rotation (x axis) gl.glrotatef (rotateQuad, 1.0f, 0.0f, 0.0f); GlVertexPointer (3, gl10.gl_fixed, 0, quaterBuffer); Gl. glDrawArrays(gl10.gl_triangle_strip, 0, 4); Gl. glDrawArrays(GL10.GL_LINES, 0, 4); //gl.glDrawArrays(GL10. Gl.gldisableclientstate (gl10.gl_vertex_array); RotateTri += 0.5f; RotateQuad - f = 0.5; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub float ratio = (float) width / height; // Set the viewport (OpenGL scene size) gl.glviewPort (0, 0, width, height); // Set the projection matrix to perspective projection gl.glMatrixMode(gl10.gl_projection); Gl.glloadidentity (); // Reset projection matrix (set to identity matrix) gl.glloadidEntity (); Gl.glfrustumf (-ratio, ratio, -1, 1, 1, 10); // Create a perspective projection matrix (set viewport size) gl.glfrustumf (-ratio, ratio, -1, 1, 1, 10); } @Override public void onSurfaceCreated(GL10 gl, Gl. glHint(gl10.gl_correction_hint) {// TODO auto-generated method stub GL10.GL_NICEST); // Set the clean screen color to gl.glclearColor (0, 0, 0, 1); Gl.glenable (gl10.gl_depth_test); }}Copy the code

Error:

Java. Lang. IllegalArgumentException: Must use a native order direct Buffer.

It turns out that you can’t create a vertex cache array this way.

package com.example.mychapter2; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; import android.util.Log; public class GLRender implements Renderer{ private String TAG = "GLRender"; float roateTri; // Angle used for triangles float roateQuad; Int one = 0x10000; /*/ private IntBuffer triggerBuffer = IntBuffer. Wrap (new int[]{0,one,0, -one,-one,0, one,-one,0,}); // Private IntBuffer quaterBuffer = IntBuffer. Wrap (new int[]{-one,one,0, one,one,0, one,-one,0,}); */ int [] colorArray = {one,0,0,one, 0,one,0, 0,one,one,}; int [] triggerArray ={ 0,one,0, -one,-one,0, one,-one,0}; int [] quaterArray = { one,one,0, -one,one,0, one,-one,0, -one,-one,0 }; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub Log.i(TAG, "onSurfaceCreated"); Gl. glHint(GL10.gl_correction_hint, GL10.gl_fastest); // Black background gl.glclearColor (0, 0, 0, 0); // Start shadow smooth gl.glshadeModel (gl10.gl_smooth); // Set the depth cache gl.glclearDepthf (1.0f); Gl.glenable (gl10.gl_depth_test); Gl.gldepthfunc (gl10.gl_lequal); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub Log.i(TAG, "onSurfaceChanged width:"+width+" height:"+height); //1920 944 float radio = (float)width/height; // Set the size of the OpenGL scene gl.glviewPort (0, 0, width, height); // Set the projection matrix, which is responsible for adding perspective gl.glMatrixMode(gl10.gl_projection) to the scene; // reset projection matrix gl.glloadidEntity (); Gl. glFrustumf(-radio, radio, -1, 1, 1, 10); gl.glFrustumf(-radio, radio, -1, 1, 1, 10); gl.glFrustumf(-radio, radio, -1, 1, 1, 10); Gl.glmatrixmode (gl10.gl_modelView); gl.glLoadIdentity(); } @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub Log.i("GLRender", "onDrawFrame"); RoateTri + = 0.5 f; RoateQuad - f = 0.5; Gl / / clear screen and the depth buffer. GlClear (GL10. GL_COLOR_BUFFER_BIT | GL10. GL_DEPTH_BUFFER_BIT); // Reset the current model observation matrix gl.glloadidEntity (); // Move the current center point 1.5 units to the left and move to screen 6.0, y unchanged // Note: the number of units moved in the screen must be smaller than the maximum distance set by the //glFrustumf method, otherwise it will not be displayed. OpenGL sets an array of vertices, so you need to tell OpenGL to set vertices. Gl.gltranslatef (-1.5f, 0.0f, -6.0f); Gl.glrotatef (roateTri, 0.0f, -1.0f, 0.0f); gl.glrotateF (roateTri, 0.0f, -1.0f, 0.0f); Gl. glEnableClientState(gl10.gl_COLOR_array); Gl. glColorPointer(4, GL10.GL_FIXED, 0, bufferUtil(colorArray)); Gl.glenableclientstate (gl10.gl_vertex_array); // Parameter 1: describes the size of the vertices, in this case the X,Y, and Z coordinates are used, so it is 3 // Parameter 2: describes the type of the vertices, in this case the data is fixed, so GL_FIXED is used for the fixed vertices // Parameter 3: describes the step size // Parameter 4: Gl.glvertexpointer (3, gl10.gl_fixed, 0, bufferUtil(triggerArray)); Using GL_TRIANGLES, say, say, using a triangle. Using Triangles, say, using triangles, is triangles. Using gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // Reset the current model observation matrix gl.glloadidEntity (); Gl.gldisableclientstate (gl10.gl_COLOR_array); GlTranslatef (1.5f, 0.0f, -6.0f); Gl. GlRotatef (roateQuad, 1.0 f, f 0.0, 0.0 f); Gl.glenableclientstate (gl10.gl_COLOR_buffer_bit); Gl.glcolor4f (0.5f, 0.5f, 1.0f, 1.0f); GlVertexPointer (3, gl10.gl_fixed, 0, bufferUtil(quaterArray)); // Set and draw square GL. glVertexPointer(3, gl10.gl_fixed, 0, bufferUtil(quaterArray)); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); Gl.gldisableclientstate (gl10.gl_COLOR_BUFFer_bit); Gl.gldisableclientstate (gl10.gl_vertex_array); } /* * OpenGL is a very low-level drawing interface, it uses the buffer storage structure is not the same as in our Java programs. Java is BigEdian, while OpenGL requires LittleEdian data. * So, we need to do some work to convert Java buffers into OpenGL usable buffers. * */ public Buffer bufferUtil(int []arr){IntBuffer mBuffer; / / initialize the first buffer, the length of the array * 4, 4 bytes because of an int ByteBuffer QBB = ByteBuffer. AllocateDirect (arr. Length * 4); NativeOrder qbb.order(byteorder.nativeOrder ()); mBuffer = qbb.asIntBuffer(); mBuffer.put(arr); mBuffer.position(0); return mBuffer; }}Copy the code