OpenGl拖动方块

时间:2013-09-06 21:00:05

标签: android opengl-es vertex

我正在创建一个由onTouchEvent()触发的类似动画的窗帘,你可以拖动方块的一端使其变大或变小。 我唯一的问题是,不是在整个屏幕上都有一个正方形,我在屏幕顶部得到一条小线,我可以扩展和去扩展该线。 为什么这段代码不会画一个正方形?

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
    gl.glLoadIdentity();                        // reset the matrix to its default state    
    gl.glOrthof(0, height, width, 0, -3, 8);

}

顶点:

private float vertices[] = { 
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right

};

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

Square中的绘制方法:

public void draw(GL10 gl,float x,float y) {
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
    //Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
    //Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

1 个答案:

答案 0 :(得分:1)

你错过了吗?设置投影模式后。

gl.glMatrixMode(GL10.GL_MODELVIEW);        // set modelview matrix to identity.
gl.glLoadIdentity();