OpenGL函数拒绝渲染三角形

时间:2012-07-02 05:32:28

标签: java android opengl-es

这可能与我的转变有关,但是现在我无法弄清楚这一点,这让我心急如焚。我已经包装了绘制代码,以便我可以轻松定义新的三角形。但是,当我把它放入一个函数时,它只显示一个灰色的屏幕。功能代码如下:

 public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle,  int mColorHandle, int mMVPMatrixHandle)
{

    long time = SystemClock.uptimeMillis() % 10000L;
    float angleInDegrees = (360.0f / 10000.0f) * ((int) time);      

    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);     

    aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat)
    .order(ByteOrder.nativeOrder()).asFloatBuffer();



    //aBuffer.position(mPositionOffset);
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
            mStrideBytes, aBuffer);        

    GLES20.glEnableVertexAttribArray(mPositionHandle);        

    // Pass in the color information
    aBuffer.position(mColorOffset);
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
            mStrideBytes, aBuffer);        

    GLES20.glEnableVertexAttribArray(mColorHandle);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 
}

工作的代码是:

        public void onDrawFrame(GL10 glUnused) 
{
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);                    



    // Do a complete rotation every 10 seconds.
    long time = SystemClock.uptimeMillis() % 10000L;
    float angleInDegrees = (360.0f / 10000.0f) * ((int) time);

    // Draw the triangle facing straight on.
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);        
    drawTriangle(mTriangle1Vertices);

    // Draw one translated a bit down and rotated to be flat on the ground.
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f);
    Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);        
    drawTriangle(mTriangle2Vertices);

    // Draw one translated a bit to the right and rotated to be facing to the left.
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f);
    Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
    drawTriangle(mTriangle3Vertices);
    */

    /*
    for (int x = 0; x < staticHolder.objectList.size(); x++)
    {
        staticHolder.objectList.get(x).Draw(mViewMatrix, mModelMatrix, mProjectionMatrix, mPositionHandle, mColorHandle, mMVPMatrixHandle);
    }
    */
}   

/**
 * Draws a triangle from the given vertex data.
 * 
 * @param aTriangleBuffer The buffer containing the vertex data.
 */
private void drawTriangle(final FloatBuffer aTriangleBuffer)
{       
    // Pass in the position information
    aTriangleBuffer.position(mPositionOffset);
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
            mStrideBytes, aTriangleBuffer);        

    GLES20.glEnableVertexAttribArray(mPositionHandle);        

    // Pass in the color information
    aTriangleBuffer.position(mColorOffset);
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
            mStrideBytes, aTriangleBuffer);        

    GLES20.glEnableVertexAttribArray(mColorHandle);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);                               
}

我传入相同的变量,这里使用的最终变量初始化相同。在封装功能中还有其他一些工作要做。知道为什么它拒绝在函数中渲染吗?

以下代码加载列表中的对象:

  final float[] triangle1VerticesData = {
            // X, Y, Z, 
            // R, G, B, A
            -0.5f, -0.25f, 0.0f, 
            1.0f, 0.0f, 0.0f, 1.0f,

            0.5f, -0.25f, 0.0f,
            0.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.559016994f, 0.0f, 
            0.0f, 1.0f, 0.0f, 1.0f};

    final float[] triangle2VerticesData = {
            // X, Y, Z, 
            // R, G, B, A
            -0.5f, -0.25f, 0.0f, 
            1.0f, 1.0f, 0.0f, 1.0f,

            0.5f, -0.25f, 0.0f, 
            0.0f, 1.0f, 1.0f, 1.0f,

            0.0f, 0.559016994f, 0.0f, 
            1.0f, 0.0f, 1.0f, 1.0f};

    // This triangle is white, gray, and black.
    final float[] triangle3VerticesData = {
            // X, Y, Z, 
            // R, G, B, A
            -0.5f, -0.25f, 0.0f, 
            1.0f, 1.0f, 1.0f, 1.0f,

            0.5f, -0.25f, 0.0f, 
            0.5f, 0.5f, 0.5f, 1.0f,

            0.0f, 0.559016994f, 0.0f, 
            0.0f, 0.0f, 0.0f, 1.0f};

    staticHolder.objectList.add(new Triangle(triangle1VerticesData));
    staticHolder.objectList.add(new Triangle(triangle2VerticesData));
    staticHolder.objectList.add(new Triangle(triangle3VerticesData));

接收类是:

    public class Triangle extends shape 
    {

     public Triangle(float[] data)
     {
          verts = data;
     }

     }

1 个答案:

答案 0 :(得分:1)

在下面的代码之后:

aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();

您必须将顶点放入缓冲区(否则,它是空白的!):

aBuffer.put(verts);

这不是代码有效的原因,是因为预先分配了这三组顶点的缓冲区,然后将顶点放入其中(初始化时)。它们只是每次传递给方法,因此它们不必再次put()

在这个注意事项中,您将希望避免在Draw方法中进行分配,因为每帧调用多次并且可能导致渲染速度变慢。分配aBuffer一次,每次都将新顶点放入其中。