java - OpenGL函数拒绝渲染三角形

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

     }

最佳答案

在下面的代码之后:

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

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

aBuffer.put(verts);

这在代码中不起作用的原因是因为这三组顶点的缓冲区是预先分配的,然后顶点被放入其中(在初始化时)。每次都将它们简单地传递给方法,因此它们不必再次被 put() 放入。

关于这一点,您需要避免在您的 Draw 方法中进行分配,因为它每帧调用多次并且可能导致渲染缓慢。分配一次aBuffer,每次都将新的顶点放入其中。

关于java - OpenGL函数拒绝渲染三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288197/

相关文章:

java - 使用 Apache POI 确定安装的 Microsoft Excel 版本?

java - 使用 PowerMock 时出现 ExceptionInInitializerError

android - 如何使用 Eclipse 混淆 Android 库项目?

java - 没有接口(interface)方法setExtras

java - 检测可用的 OpenGL ES 版本

java - 如何使用 SvnKit 获取两个分支之间的日志?

java - 如何在Java中创建一个对象的多个实例

java - android.util.Base64 编码/解码标志参数

安卓 OpenGL ES 2.0

java - 如何在android中显示3D模型?