0

我试图在我的显示器上显示一个正方形,但我做不到。我的问题是什么?我怎样才能在屏幕上显示它(屏幕中央)?

这是我的渲染类:

public class GLRenderEx implements Renderer {

    private GLCube cube;
    Context c;

    GLCube quad; // ( NEW )

    // Constructor
    public GLRenderEx(Context context) {
        // Set up the data-array buffers for these shapes ( NEW )
        quad = new GLCube(); // ( NEW )
    }

    // Call back when the surface is first created or re-created.
    @Override
       public void onSurfaceCreated(GL10 gl, EGLConfig config) {
          // NO CHANGE - SKIP
       }

    // Call back after onSurfaceCreated() or whenever the window's size changes.
    @Override
       public void onSurfaceChanged(GL10 gl, int width, int height) {
          // NO CHANGE - SKIP
       }

    // Call back to draw the current frame.
    @Override
    public void onDrawFrame(GL10 gl) {
        // Clear color and depth buffers using clear-values set earlier
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity(); // Reset model-view matrix ( NEW )
        gl.glTranslatef(-1.5f, 0.0f, -6.0f); // Translate left and into the
                                                // screen ( NEW )

        // Translate right, relative to the previous translation ( NEW )
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        quad.draw(gl); // Draw quad ( NEW )
    }
}

这是我的方形课:

public class GLCube {

    private FloatBuffer vertexBuffer; // Buffer for vertex-array

    private float[] vertices = { // Vertices for the square
    -1.0f, -1.0f, 0.0f, // 0. left-bottom
            1.0f, -1.0f, 0.0f, // 1. right-bottom
            -1.0f, 1.0f, 0.0f, // 2. left-top
            1.0f, 1.0f, 0.0f // 3. right-top
    };

    // Constructor - Setup the vertex buffer
    public GLCube() {
        // Setup vertex array buffer. Vertices in float. A float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder()); // Use native byte order
        vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
        vertexBuffer.put(vertices); // Copy data into buffer
        vertexBuffer.position(0); // Rewind
    }

    // Render the shape
    public void draw(GL10 gl) {
        // Enable vertex-array and define its buffer
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        // Draw the primitives from the vertex-array directly
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

}
4

1 回答 1

0

我没有看到任何投影矩阵,所以我必须假设您使用单位投影矩阵。在这种情况下,唯一呈现的东西将在所有轴上的 -1 和 1 之间。

以 (1.5, 0, -6) 为中心的四边形将在 z 轴上太远而无法看到。

尝试删除两个翻译调用,看看它是否可见。

于 2012-09-28T21:39:39.020 回答