我做了一个活动来显示一个三角形但是在运行它时没有显示三角形;
我无法弄清楚错误的位置,但每次都有一个随机的彩色背景加上我没有任何force closes
或某些errors
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();
}
}
答案 0 :(得分:3)
我用你的代码编写了我自己的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);
颜色缓冲区的整个过程正在发挥作用。