我做了一个活动来显示一个三角形,但在运行时它没有显示三角形;我不知道错误在哪里,但每次肯定都有一个随机的彩色背景,而且我没有任何force closes
或一些errors
三角obj.class:
package com.exmple.open_gl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;
import javax.microedition.khronos.opengles.GL10;
public class Triangleobj {
// -------------------------
FloatBuffer VBuff, CBuff;
ByteBuffer Ibuff;
private Random r = new Random();
private float[] vertices = { -1.0f, -1.0f, 1.0f, -1.0f, 0f, 1.0f },
color = { r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), };
private byte[] indices = { 0, 1, 2 };
// -------------------------
public Triangleobj() {
// ----------------------------------------------------------
ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
bb.order(ByteOrder.nativeOrder());
VBuff = bb.asFloatBuffer();
VBuff.put(vertices);
VBuff.position(0);
// ----------------------------------------------------------
Ibuff = ByteBuffer.allocateDirect(indices.length);
Ibuff.order(ByteOrder.nativeOrder());
Ibuff.put(indices);
Ibuff.position(0);
// ----------------------------------------------------------
bb = ByteBuffer.allocateDirect(color.length * 4);
bb.order(ByteOrder.nativeOrder());
CBuff = bb.asFloatBuffer();
CBuff.put(color);
CBuff.position(0);
// ----------------------------------------------------------
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, VBuff);
gl.glColorPointer(4, GL10.GL_BYTE, 0, CBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_BYTE, Ibuff);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}}
三角形渲染器类:
package com.exmple.open_gl;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.SystemClock;
public class triangleRenderer implements Renderer {
Triangleobj t;
public triangleRenderer() {
t = new Triangleobj();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
t.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 1.0f, 0f, 1.0f, -1.0f, 1.0f);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
Random r = new Random();
gl.glClearColor(r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat());
gl.glClearDepthf(1.0f);
}
}
三角类:
package com.exmple.open_gl;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
public class Triangle extends Activity {
private GLSurfaceView gls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gls = new GLSurfaceView(this);
gls.setRenderer(new triangleRenderer());
setContentView(gls);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
gls.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
gls.onResume();
}
}