最近开始学OpenGL,本人想在画个三角形和正方形,但是屏幕除了黑屏,什么也没画出来,请大家看看package org.example.demo;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.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;public class GLRenderer implements Renderer{
static final String TAG="测试信息:";
private int one=0x10000;
private Context context=null;
int a1[]=new int[]{
0,0,-one,
one,0,0,
0,one,0
};
 
//颜色数组
int b1[]=new int[]{
0,0,one,one,
one,0,0,one,
0,one,0,one
};

int c1[]=new int[]{
0,0,-one,
one,0,0,
0,one,0,
-one,0,0
};
public GLRenderer(Context cx){
context=cx;
}

@Override
public void onDrawFrame(GL10 gl) {
Log.d(TAG,"onDrawFrame 开始");
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 0.5f, 1.0f);
Log.d(TAG,"onDrawFrame 三角形绘制完");
//重置当前的模型视图矩阵
gl.glLoadIdentity();
//向y平移0.5f
gl.glTranslatef(0.0f, 0.5f, 0.0f);
//启用定点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//设置三角形三顶点
ByteBuffer bytebuf=ByteBuffer.allocateDirect(a1.length*4);
bytebuf.order(ByteOrder.nativeOrder());
IntBuffer ibuf=bytebuf.asIntBuffer();
ibuf.put(a1);
ibuf.position(0);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, ibuf);
//转化为本地直接缓存
ByteBuffer bytebuf1=ByteBuffer.allocateDirect(b1.length*4);
bytebuf1.order(ByteOrder.nativeOrder());
IntBuffer ibuf1=bytebuf1.asIntBuffer();
ibuf1.put(b1);
ibuf1.position(0);
gl.glColorPointer(4, GL10.GL_FIXED, 0, ibuf1);
//画三角形
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
Log.d(TAG,"onDrawFrame 三角形绘制完");
gl.glLoadIdentity();
gl.glTranslatef(2.0f, 0.0f,0.0f);

ByteBuffer bytebuf2=ByteBuffer.allocateDirect(c1.length*4);
bytebuf2.order(ByteOrder.nativeOrder());
IntBuffer ibuf2=bytebuf2.asIntBuffer();
ibuf2.put(c1);
ibuf2.position(0);
gl.glVertexPointer(3,GL10.GL_FIXED,0,ibuf2);
gl.glDrawArrays(GL10.GL_FIXED, 0, 4);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

Log.d(TAG,"onDrawFrame 结束");

} @Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
float ratio=width/height;
//设置场景大小
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
//设置视口大小
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
} @Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
//设置平滑阴影
gl.glShadeModel(GL10.GL_SMOOTH);
//清除屏幕
gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
//设置修正透视
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
//清除深度缓存
gl.glClearDepthf(1.0f);
//启用深度测试
gl.glEnable(GL10.GL_DEPTH_BITS);
//所做深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);
Log.d(TAG,"onSurfaceCreated");
}
}