在手机和模拟器上都不能显示这个三角形。但是去掉这个能显示但是不是3D效果代码如下:gl.glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f);请求帮忙处理谢谢:
package com.opengl;import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;public class OpenGLActivity extends Activity
{ /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
GLSurfaceView mGlSurfaceView = new GLSurfaceView(this);
mGlSurfaceView.setRenderer(new opengl());
setContentView(mGlSurfaceView);
} class opengl implements Renderer
{ private int one = 0x10000; int[] triggerArray =
{ 0, one, 0, -one, -one, 0, one, -one, 0 }; @Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{ gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glClearDepthf(1.0f);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); } @Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{ float ratio = (float) width / height; gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity();
} @Override
public void onDrawFrame(GL10 gl)
{ gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// 清楚颜色缓冲和深度缓冲 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);// 将屏幕颜色设为白色 gl.glLoadIdentity();
gl.glColor4f(0.5f, 0.5f, 0,0); gl.glTranslatex(1, 1, 0); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, bufferUtil(triggerArray)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } public Buffer bufferUtil(int[] arr)
{ IntBuffer mBuffer;
// 先初始化buffer,数组的长度*4,因为一个int占4个字节
ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
// 数组排列用nativeOrder
qbb.order(ByteOrder.nativeOrder()); mBuffer = qbb.asIntBuffer();
mBuffer.put(arr);
mBuffer.position(0); return mBuffer;
} }}