1

我做了一个活动来显示一个三角形,但在运行时它没有显示三角形;我不知道错误在哪里,但每次肯定都有一个随机的彩色背景,而且我没有任何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();
}

}
4

1 回答 1

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 };

以下代码示例显示了我的完整代码。

三角对象.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);
}   
}

三角渲染器.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();
}
}

三角活动.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);

整个颜色缓冲区都在工作。

于 2012-12-29T16:15:26.250 回答