【问题标题】:Drawing Triangle using OpenGl, does not appear?使用OpenGL绘制三角形,不出现?
【发布时间】:2012-03-12 12:04:33
【问题描述】:

我正在尝试制作我的第一个应用程序。基于 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();
}

}

【问题讨论】:

    标签: android opengl-es opengl-es-2.0


    【解决方案1】:

    我认为您需要为您的顶点声明索引(以组成线条和面):

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

    )

    至于教程,您可以在此处获得出色的 Android 版本的 Nehe 教程:

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

    它比文字解释更多的代码,但它们让你开始很慢,而且代码注释很好。实际上,我认为最早的教程之一只是展示了如何绘制三角形,就像您在这里尝试的那样。

    【讨论】:

    • 谢谢,我试过你的代码,但我还是有同样的问题,我会发布带有新修改的代码
    • 太奇怪了,我试过你的代码,一天后,为了测试,我尝试了原始代码(here),它现在可以工作了,太奇怪了,它没有工作前一天。
    【解决方案2】:

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

    【讨论】:

    • 是的,我几乎说了同样的话,但是当他执行 gl.glTranslatef(0.0f, 0.0f, -5.0f); 时,OP 不是已经这样做了在 onDrawFrame(...) 中?
    • 对不起,请您澄清更多,我是新手。我应该添加/删除哪些方法。很抱歉给您带来不便
    【解决方案3】:

    This android tutorial 提到了对 vertexShader、fragmentShader 和 Program 的要求。我可能错过了它,但我在您的代码中看不到任何与此相关的内容。在我的情况下,对不可见三角形的修复是在我的 fragmentShader 中修复一个 type-o。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      相关资源
      最近更新 更多