使用GL_POINTS生成多个点

时间:2016-11-01 17:41:50

标签: android opengl-es opengl-es-2.0

我正在尝试使用Android中的OpenGL ES生成随机等值线图。我已经为屏幕上的绘制点创建了一个Point类,但我不知道我应该如何绘制随机点。

例如,为了显示2个点,我正在创建2个对象

mPoint = new Point();

mPoint2 = new Point();

在我的渲染器中,并使用onDrawFrame方法

绘制它们
mPoint.draw(scratch,0.0f,0.0f,0.0f);
mPoint2.draw(scratch,0.0f,0.5f,0.0f);

我现在要生成500个点并且创建500个可能重叠的对象(相同的x,y,z坐标)听起来不对。到目前为止,我的绘图方法是否正确?我应该如何继续创建包含大量积分的视图?

我的点类

public class Point {
private  int mProgram, mPositionHandle, mColorHandle, mMVPMatrixHandle;


private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;" +
                "uniform vec4 vPosition;" +
        "void main() {" +
                "   gl_Position = uMVPMatrix * vPosition;" +
                "   gl_PointSize = 20.0;" +
                "}";

private final String fragmentShaderCode =
        "precision mediump float;" +
                "uniform vec4 vColor;" +
                "void main() {" +
                "   gl_FragColor = vColor;" +
                "}";


// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public Point(){
    int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    // create empty OpenGL ES Program
    mProgram = GLES20.glCreateProgram();

    // create empty OpenGL ES Program
    mProgram = GLES20.glCreateProgram();

    // add the vertex shader to program
    GLES20.glAttachShader(mProgram, vertexShader);

    // add the fragment shader to program
    GLES20.glAttachShader(mProgram, fragmentShader);

    // creates OpenGL ES program executables
    GLES20.glLinkProgram(mProgram);
}

public void draw(float[] mvpMatrix,float px,float py,float pz) {
    float positionCoords[] = {px,py,pz,1.0f};

    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetUniformLocation(mProgram, "vPosition");

    mColorHandle = GLES20.glGetUniformLocation(mProgram,"vColor");

    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    GLES20.glUniform4fv(mPositionHandle, 1, positionCoords, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    //TRANSLATION
    float[] transMatrix = new float[16];

    Matrix.setIdentityM(transMatrix,0);
    Matrix.translateM(transMatrix,0,0.5f,0,0);
    Matrix.multiplyMM(transMatrix,0,mvpMatrix,0,transMatrix,0);


    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, transMatrix, 0);

    GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
}

}

编辑:我希望能够每10秒生成一次。

1 个答案:

答案 0 :(得分:1)

这不是一个好方法。每点一次绘制调用将产生巨大的驱动程序开销和整体糟糕的GPU性能。您应该将位置和颜色批量处理为一个唯一的顶点缓冲区,并且只绘制一次。

在链接着色器程序后,每个制服也要调用GetUniformLocation()。它可能非常慢或者驱动程序可以缓存它们,但是自己动手并没有坏处。