使用OpenGl绘制三角形,但没有出现?

3

我正在尝试制作基于OpenGl的第一个应用程序。我试图绘制一个三角形,但是当运行应用程序时,它只显示黑屏,没有三角形。

1-我不知道我的错误在哪里? 2-有没有适合OpenGL ES Android初学者的好书/教程?

三角形类:

public class Triangle {

private FloatBuffer vertxBuffer;

protected static byte indices[] = {
    //Face definition:
    0,1,3, //lower-right triangle of the face is drawn with vertices vertices[0]->vertices[1]->vertices[3] (->vertices[0])
    0,3,2  //upper-right triangle of the face is drawn with vertices vertices[0]->vertices[3]->vertices[2] (->vertices[0])          
};

public float vertices[] = {
        -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
         0.0f,  0.5f, 0.0f
};

public Triangle() {

    // float has 4 bytes, so we allocate for each coordinate 4 bytes.
    //what is the difference between ByteBuffer.allocateDirect AND ByteBuffer.allocate???
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vertexByteBuffer.order(ByteOrder.nativeOrder());

    // allocate the memory from the byte buffer
    vertxBuffer = vertexByteBuffer.asFloatBuffer();

    //fill the vertex buffer with the vertices
    vertxBuffer.put(vertices);

    // set the cursor position to the beginning of the buffer
    vertexByteBuffer.position(0);       
}
protected static ByteBuffer indexBuffer;    
static {
    indexBuffer = ByteBuffer.allocateDirect(indices.length);
    indexBuffer.put(indices);
    indexBuffer.position(0);
}
public void draw(GL10 gl) {
    // Because we store the Triangle vertices " Coordinates " in a FloatBuffer
    // we need to enable OpenGL to read from it.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    //set the color for the Triangle (r, g, b, alpha) alpha is between 0-1
    gl.glColor4f(2.0f, 1.0f, 0.0f, 0.5f);

    // point to our vertex buffer to extract the vertices from it.
    //(numberOfVertices, Which type of data the buffer Holds, offset, our Buffer containing the Vertices)
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertxBuffer);

    //draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3 );

    gl.glDrawElements(gl.GL_TRIANGLES, indices.length, gl.GL_UNSIGNED_BYTE, indexBuffer);

    //disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

GLRenderer类:

public class GLRenderer implements Renderer{

private Triangle triangle;

public GLRenderer() {
    this.triangle = new Triangle();
}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub

    // clear screen and depth buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // reset the mode view matrix
    gl.glLoadIdentity();

    // Drawing 
    gl.glTranslatef(0.0f, 0.0f, -5.0f);
    triangle.draw(gl); 
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub

    if (height == 0) {
        height = 1;
    }

    //Reset the current View Port
    gl.glViewport(0, 0, width, height);

    //Select the Projection Matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);

    // Reset the Projection Matrix
    gl.glLoadIdentity();

    // calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width/(float) height, 0.1f , 100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub

    gl.glClearColor(0, 0, 0, 1.0f);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glDisable(GL10.GL_DITHER);

}

OpenGlRenderActivity类:

public class OpenGLRenderActivity extends Activity {
/** Called when the activity is first created. */

private GLSurfaceView gLSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    gLSurfaceView = new GLSurfaceView(this);
    gLSurfaceView.setRenderer(new GLRenderer());
    setContentView(gLSurfaceView);
}

@Override
protected void onResume() {
    super.onResume();
    gLSurfaceView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    gLSurfaceView.onPause();
}

}

3个回答

4
我认为您需要声明顶点的索引(以便组成线条和面):
protected static byte indices[] = {
        //Face definition:
        0,1,3, //lower-right triangle of the face is drawn with vertices vertices[0]->vertices[1]->vertices[3] (->vertices[0])
        0,3,2  //upper-right triangle of the face is drawn with vertices vertices[0]->vertices[3]->vertices[2] (->vertices[0])          
    };

protected static ByteBuffer indexBuffer;    
    static {
        indexBuffer = ByteBuffer.allocateDirect(indices.length);
        indexBuffer.put(indices);
        indexBuffer.position(0);
    }

然后将这些索引传递给您的绘制调用:

(将

替换为
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3 );

使用

gl.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_BYTE, indexBuffer);

在教程方面,您可以在这里找到Nehe教程的出色Android版本:

http://insanitydesign.com/wp/projects/nehe-android-ports/

它比文字说明更多的是代码,但是它会从非常简单的部分开始,并且代码被很好地注释了。实际上,我认为最初的一个教程就是展示如何绘制一个三角形,就像您在此处尝试的那样。


谢谢,我尝试了你的代码,但仍然存在同样的问题。我将发布经过新修改的代码。 - LetsamrIt
很奇怪,我尝试了你的代码,一天后为了测试我尝试了原始代码(在这里),现在它可以工作了。真是太奇怪了,前一天它还不能正常工作。 - Adil Mammadov

2

您的三角形被近裁剪平面截断。尝试将其移动到gluPerspective的近值和远值之间的范围内。


是的,我差点说了同样的话,但当OP在onDrawFrame(...)中执行gl.glTranslatef(0.0f, 0.0f, -5.0f);时,他不是已经这样做了吗? - Shivan Dragon
抱歉,请您再解释清楚一些,我对此还不熟悉。我应该添加/删除哪些方法?非常抱歉给您带来不便。 - LetsamrIt

1

这个Android教程提到了需要一个顶点着色器、片段着色器和程序。我可能错过了,但是在你的代码中我没有看到任何相关的内容。在我的情况下,修复看不见的三角形的方法是修复了我的片段着色器中的拼写错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接