android - 无法在 Android OpenGL ES 2.0 中绘制点

标签 android opengl-es

我在下面使用着色器代码,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);
}

onDrawFrame 函数:

    @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,没有错误,似乎一切正常。 我应该如何调试它?

完整代码在这里link

最佳答案

好吧,我使用了 link 中的代码,'multiplyMV' 应该是 'multiplyMM',然后它工作。 'multiplyMV' 使结果成为 4 元素列向量,因此其他 3 列为 0。

关于android - 无法在 Android OpenGL ES 2.0 中绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39178635/

相关文章:

android - Flutter:Android Studio 中的内联测试覆盖率

Android - 使用 ImageView 优于简单 View 的背景属性的优势

java - 如何使用 Maven 存储库中的项目代码

Android 如何缩放和滚动动态创建的位图

android - 当我尝试通过 ListView 项目开始新 Activity 时应用程序崩溃;由按钮实例化引起...?

android - 如何为动态壁纸放置静态背景图像?

ios - 10.5"iPad Pro 上的 CADisplayLink 帧率

android - onPause()/onResume() 中的 GLSurface 问题

opengl-es - WebGL/OpenGL : Rotate camera according to device orientation

android - 动画时出现奇怪的错误(Adreno,GL)