无法在Android OpenGL ES 2.0中绘制点

3
我使用下面的着色器代码,与OpenGL es 2.0一起使用。
    final String vertexShader =
                        "uniform mat4 u_MVPMatrix;      \n"       
                      + "attribute vec4 a_Position;     \n"     
                      + "void main()                    \n"
                      + "{                              \n"
                      + "   gl_Position = u_MVPMatrix   \n"
                      + "               * a_Position;   \n"
                      + "   gl_PointSize = 10.0;       \n"
                      + "}                              \n";

    final String fragmentShader =
                        "precision mediump float;       \n"                           
                      + "void main()                    \n"
                      + "{                              \n"
                      + "   gl_FragColor = vec4(1.0,    \n" 
                      + "   1.0, 1.0, 1.0);             \n"
                      + "}                              \n";    

@Override

public void onSurfaceChanged(GL10 glUnused, int width, int height)
{
    // Set the OpenGL viewport to the same size as the surface.
    GLES20.glViewport(0, 0, width, height);

    // Create a new perspective projection matrix. The height will stay the same
    // while the width will vary as per aspect ratio.
    final float ratio = (float) width / height;
    final float left = -ratio;
    final float right = ratio;
    final float bottom = -1.0f;
    final float top = 1.0f;
    final float near = 1.0f;
    final float far = 10.0f;

    Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
}

The onDrawFrame function:

    @Override
    public void onDrawFrame(GL10 glUnused) 
    {
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);                    

        Matrix.setIdentityM(mModelMatrix, 0);
        //Push to the distance - note this will have no effect on a point size
        Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -5.0f);
        Matrix.multiplyMV(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
        Matrix.multiplyMV(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

        //Send the vertex
        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuf);
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        //Draw the point
        GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);        

    }   

这是我在Android Studio中使用的代码,但它不起作用,只有背景。 我已经设置了gl_PointSize,并且没有错误,似乎一切都很好。 我该如何调试它?

完整代码在此处链接


你没有使用glViewport()。试一下。 - Sung
已经在 onSurfaceChanged 中设置了它,但是代码太多了,我不得不删除一些。 - aboutqx
mViewMatrix 在哪里初始化的?顶点的坐标是什么? - Reto Koradi
@RetoKoradi 看更新 - aboutqx
1个回答

0

嗯,我使用了链接中的代码,'multiplyMV' 应该改为 'multiplyMM',这样就可以正常工作了。 'multiplyMV' 会将结果变成一个四元素列向量,所以其他三列都是0。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接