背景中的点不显示

时间:2011-10-21 09:05:42

标签: android opengl-es

我正在创建一个Android 2d太空射击游戏,作为学习OpenGL的一种方式,并希望在游戏中拥有星空背景。我的想法是用白点打散背景开始,但点不显示。首先是渲染器代码:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

public void onSurfaceChanged(GL10 gl, int w, int h) {
    gl.glViewport(0, 0, w, h);
    width = w;
    height = h;
    gl.glMatrixMode(GL10.GL_PROJECTION);
}

public void onDrawFrame(GL10 gl) {
    // black background
    gl.glClearColor(0f, 0f, 0f, 1.0f);
    // clear the color buffer to show the ClearColor we called above...
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();

    stars.draw(gl);
}

然后绘制星星:

private final FloatBuffer vertexBuffer;
private int count;

public Stars() {
    Random r = new Random();
    count = (int) ((r.nextFloat() * 200.0f) + 200.0f);
    count *= 3;

    ByteBuffer buffer = ByteBuffer.allocateDirect(count * Float.SIZE);
    buffer.order(ByteOrder.nativeOrder());

    // allocate _count number of floats
    vertexBuffer = buffer.asFloatBuffer();

    float rVtx = (r.nextFloat() * 2.0f) - 1.0f;
    for (int i = 0; i < count; i += 3) {
        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(rVtx);
        rVtx = (r.nextFloat() * 2.0f) - 1.0f;

        vertexBuffer.put(0.0f);
    }
}

public void draw(GL10 gl) {
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glPointSize(3);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glDrawArrays(GL10.GL_POINTS, 0, count / 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

什么似乎是错误?

此致

1 个答案:

答案 0 :(得分:0)

您需要在onSurfaceChanged中设置默认投影矩阵。类似的东西:

gl.glMatrixMode( GL10.GL_PROJECTION );                
gl.glLoadIdentity();        
gl.glOrthof( -1, 1, -1, 1, -1, 1 );

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

在Stars()的最后,把它作为最后一行:

vertexBuffer.position(0);