【问题标题】:Android Opengl Point not Drawn未绘制 Android Opengl 点
【发布时间】:2016-09-08 09:46:03
【问题描述】:

我有两个类来绘制点和线。但点不在屏幕上绘制。 我收到如下错误,

distrib/android-emugl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp:glGetUniformLocation:1512 错误 0x501

Point 类无法正常工作。在我的渲染类中,我有 arraylist 来保存点 线对象和代码遍历数组以绘制这些原语。

我的积分等级

private FloatBuffer VertexBuffer;

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;" +
                "attribute vec3 vPosition;" +
                "void main() {" +
                // the matrix must be included as a modifier of gl_Position
                "  gl_Position = uMVPMatrix * vec4(vPosition,1.0f);" +
                "gl_PointSize = 30.0;" +
                "}";

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

protected static int GlProgram;
protected int PositionHandle;
protected int ColorHandle;
protected int MVPMatrixHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float PointCoords[] = {
        0.0f, 0.0f, 0.0f,
};

private final int VertexCount = PointCoords.length / COORDS_PER_VERTEX;
private final int VertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

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

public Point() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (number of coordinate values * 4 bytes per float)
            PointCoords.length * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    VertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    VertexBuffer.put(PointCoords);
    // set the buffer to read the first coordinate
    VertexBuffer.position(0);

    int vertexShader = GLRender.loadShader(GLES20.GL_VERTEX_SHADER, VertexShaderCode);
    int fragmentShader = GLRender.loadShader(GLES20.GL_FRAGMENT_SHADER, FragmentShaderCode);

    GlProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    GLES20.glAttachShader(GlProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(GlProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(GlProgram);                  // creates OpenGL ES program executables
}

public void SetPointVerts(float v0, float v1, float v2) {
    PointCoords[0] = v0;
    PointCoords[1] = v1;
    PointCoords[2] = v2;

    VertexBuffer.put(PointCoords);
    // set the buffer to read the first coordinate
    VertexBuffer.position(0);
}

public void SetPointColor(float red, float green, float blue, float alpha) {
    color[0] = red;
    color[1] = green;
    color[2] = blue;
    color[3] = alpha;
}

public void draw(float[] mvpMatrix) {
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(GlProgram);

    // get handle to vertex shader's vPosition member
    PositionHandle = GLES20.glGetAttribLocation(GlProgram, "vPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(PositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(PositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            VertexStride, VertexBuffer);

    // get handle to fragment shader's vColor member
    ColorHandle = GLES20.glGetUniformLocation(GlProgram, "vColor");

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

    // get handle to shape's transformation matrix
    MVPMatrixHandle = GLES20.glGetUniformLocation(GlProgram, "uMVPMatrix");
    //GLRender.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
    //GLRender.checkGlError("glUniformMatrix4fv");


    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_POINTS, 0, VertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(PositionHandle);
}

09-08 10:57:28.308 29430-29494/com.timescale.lenovo.sds I/OpenGLRenderer:初始化 EGL,版本 1.4 09-08 10:57:28.330 29430-29494/com.timescale.lenovo.sds D/OpenGLRenderer:启用调试模式 0 09-08 10:57:28.343 29430-29494/com.timescale.lenovo.sds W/EGL_emulation:eglSurfaceAttrib 未实现 09-08 10:57:28.343 29430-29494/com.timescale.lenovo.sds W/OpenGLRenderer:无法在表面 0xae54a080 上设置 EGL_SWAP_BEHAVIOR,错误 = EGL_SUCCESS 09-08 10:57:28.498 29430-29438/com.timescale.lenovo.sds W/art:暂停所有线程耗时:9.892ms 09-08 10:57:38.010 29430-29494/com.timescale.lenovo.sds W/EGL_emulation:eglSurfaceAttrib 未实现 09-08 10:57:38.011 29430-29494/com.timescale.lenovo.sds W/OpenGLRenderer:无法在表面 0xa30640a0 上设置 EGL_SWAP_BEHAVIOR,错误 = EGL_SUCCESS 09-08 10:57:39.324 29430-29430/com.timescale.lenovo.sds E/libEGL:在没有当前上下文的情况下调用 OpenGL ES API(每个线程记录一次) 09-08 10:57:39.392 29430-29493/com.timescale.lenovo.sds E/emuglGLESv2_enc: glDrawArrays: 没有数据绑定到命令 - 忽略 09-08 10:57:39.394 29430-29493/com.timescale.lenovo.sds E/emuglGLESv2_enc: glDrawArrays: 没有数据绑定到命令 - 忽略

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

    您将 vPosition 属性声明为 vec4(同构坐标),但在 Java 代码中您正在通过 glVertexAttribPointer 上传 vec3 顶点(COORDS_PER_VERTEX const 设置为 3)。更改几何定义或着色器代码以接受 3 个元素向量:

    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;" +
            "attribute vec3 vPosition;" +
            "void main() {" +
             // the matrix must be included as a modifier of gl_Position
             "  gl_Position = uMVPMatrix * vec4(vPosition, 1.0);" +
             "  gl_PointSize = 30.0;" +
             "}";
    

    旁注,由于您的顶点仅包含位置数据,您可以简单地将步幅设置为 0(元素紧密排列)

    【讨论】:

    • 我已经尝试过你的 cmets,但我仍然遇到同样的错误。虽然调试错误发生在下面的行中。 PositionHandle = GLES20.glGetAttribLocation(GlProgram, "vPosition");
    • 0x501 是 GL_ERROR_INVALID_VALUE 但由于您在每次调用后都没有检查它,它实际上可能是由其他调用引起的,添加一些额外的调试以查明您的问题。
    • 我在你的建议后添加了调试,如上图所示
    • 仅将 3 个组件传递给着色器代码中具有 4 个组件的属性是完全有效的,并且定义明确。向量的 w 分量将被隐式设置为 1。
    • @RetoKoradi 你是对的,我不知道这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多