我做了一项活动以显示三角形,但是在运行时没有显示三角形。我无法弄清楚错误在哪里,但是每次都有随机的彩色背景,而且我没有任何force closes
或某些errors
triangleobj.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);
}}
trianglerenderer.class:
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();
}
}
我使用您的代码编写自己的OpenGL应用程序,该应用程序在带有随机颜色的背景前显示了一个彩色三角形。我认识到设置颜色缓冲区的问题。因此,我不使用颜色缓冲区,而是在三角形绘制功能中使用以下代码线来设置三角形的颜色。您可能会这样做,因为它可以防止三角形被绘制。
gl.glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
我还写了自己的顶点和索引阵列,因为我不了解您的顶点,颜色和索引阵列。
private float[] vertices = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f };
private byte[] indices = { 0, 1, 2 };
以下代码样本显示我的完整代码。
triangleObject.java
public class TriangleObject {
private FloatBuffer vertexBuffer;
private ByteBuffer indexBuffer;
private float[] vertices = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f };
private byte[] indices = { 0, 1, 2 };
public TriangleObject() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CCW);
gl.glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
trianglerenderer.java
public class TriangleRenderer implements Renderer {
TriangleObject t;
public TriangleRenderer() {
t = new TriangleObject();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
Random r = new Random();
gl.glClearColor(r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat());
gl.glClearDepthf(1.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
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();
}
}
triangleactivity.java
public class TriangleActivity extends Activity {
private GLSurfaceView glSurface;
private TriangleRenderer glRenderer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurface = new GLSurfaceView(this);
glRenderer = new TriangleRenderer();
glSurface.setRenderer(glRenderer);
setContentView(glSurface);
glSurface.requestFocus();
glSurface.setFocusableInTouchMode(true);
}
@Override
protected void onPause() {
super.onPause();
glSurface.onPause();
}
@Override
protected void onResume() {
super.onResume();
glSurface.onResume();
}
}
编辑:
我继续弄清楚为什么颜色缓冲区不起作用。每个顶点需要一个由四个浮点值(RGBA)组成的颜色。因此,我添加了以下颜色数组。
private float[] colors = { 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f};
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
colorBuffer = byteBuf.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
和
...
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
...
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
...
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
颜色缓冲区的整个过程都在起作用。