致命信号 11(SIGSEGV),代码 1(SEGV_MAPERR),tid 14795(GLThread 39)中的故障地址 0x0,pid 14605(.example.opengl)

问题描述 投票:0回答:0

我收到以下错误致命信号 11 (SIGSEGV)、代码 1 (SEGV_MAPERR)、故障地址 0x0 in tid 14795 (GLThread 39)、pid 14605 (.example.opengl) 当我尝试创建 3D 视差时通过在 android studio 中使用 Open GL ES 将深度图应用于 2D 图像来产生效果。

这是我的渲染器类

package com.example.opengl;

public class Renderer implements GLSurfaceView.Renderer {

    float[] vertices = {
            -1, -1,
            1, -1,
            1, 1,
            -1, -1,
            1, 1,
            -1, 1
    };

    float[] texcoords = {
            0, 0,
            1, 0,
            1, 1,
            0, 0,
            1, 1,
            0, 1
    };

    FloatBuffer texcoordBuffer;
    int[] textures;

    private final String vertexShaderCode =
            "attribute vec2 position;\n" +
                    "attribute vec2 texcoord;\n" +
                    "uniform mat3 u_matrix;\n" +
                    "varying vec2 v_texcoord;\n" +
                    "void main() {\n" +
                    "    gl_Position = vec4(u_matrix * vec3(position, 1), 1);\n" +
                    "    v_texcoord = texcoord;\n" +
                    "}\n";

    // Fragment shader source code
    private final String fragmentShaderCode =
            "precision mediump float;\n" +
                    "uniform vec2 u_mouse;\n" +
                    "uniform sampler2D u_originalImage;\n" +
                    "uniform sampler2D u_mapImage;\n" +
                    "varying vec2 v_texcoord;\n" +
                    "void main() {\n" +
                    "    vec4 depthDistortion = texture2D(u_mapImage, v_texcoord);\n" +
                    "    float parallaxMult = depthDistortion.r;\n" +
                    "    vec2 parallax = (u_mouse) * parallaxMult;\n" +
                    "    vec4 original = texture2D(u_originalImage, (v_texcoord + parallax));\n" +
                    "    gl_FragColor = original;\n" +
                    "}\n";

    // OpenGL program handle
    private int mProgram;

    // OpenGL attribute handles
    private int mPositionHandle;
    private int mTexcoordHandle;

    // OpenGL uniform handles
    private int mMatrixHandle;
    private int mMouseHandle;
    FloatBuffer vertexBuffer;
    private int mOriginalImageHandle;
    private int mMapImageHandle;
    int originalTexture;
    int mapTexture;
    float mouseX;
    float mouseY;
    Context context;

    // Matrix used to transform vertices
    private final float[] mMatrix = new float[9];

    public Renderer(Context context) {
        this.context = context;
    }


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Load and compile shaders
        initBuffers();
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        // Create OpenGL program
        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);

        // Get attribute and uniform handles
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position");
        mTexcoordHandle = GLES20.glGetAttribLocation(mProgram, "texcoord");
        mMatrixHandle = GLES20.glGetUniformLocation(mProgram, "u_matrix");
        mMouseHandle = GLES20.glGetUniformLocation(mProgram, "u_mouse");
        mOriginalImageHandle = GLES20.glGetUniformLocation(mProgram, "u_originalImage");
        mMapImageHandle = GLES20.glGetUniformLocation(mProgram, "u_mapImage");
        textures = new int[2];
        GLES20.glGenTextures(2, textures, 0);

// Load the original image texture
        Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mind);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, originalBitmap, 0);
        originalBitmap.recycle();

// Set texture parameters
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

// Load the map image texture
        Bitmap mapBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_depth_map);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[1]);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, mapBitmap, 0);
        mapBitmap.recycle();

// Set texture parameters
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

// Unbind the textures
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        originalTexture = textures[0];
        mapTexture = textures[1];
        mouseX = 0.5f;
        mouseY = 0.5f;


        // Initialize the transformation matrix to identity
        Matrix.setIdentityM(mMatrix, 0);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Set the viewport
        GLES20.glViewport(0, 0, width, height);
    }


    @Override
    public void onDrawFrame(GL10 gl10) {
        // Use the program
        GLES20.glUseProgram(mProgram);

// Set the attribute data
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        GLES20.glEnableVertexAttribArray(mTexcoordHandle);
        GLES20.glVertexAttribPointer(mTexcoordHandle, 2, GLES20.GL_FLOAT, false, 0, texcoordBuffer);

// Set the uniform data
        GLES20.glUniformMatrix3fv(mMatrixHandle, 1, false, mMatrix, 0);
        GLES20.glUniform2f(mMouseHandle, mouseX, mouseY);
        GLES20.glUniform1i(mOriginalImageHandle, 0);
        GLES20.glUniform1i(mMapImageHandle, 1);

// Bind the textures
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, originalTexture);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mapTexture);

// Draw the triangles
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);

// Unbind the textures and disable the attribute arrays
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTexcoordHandle);
    }

    // Load a shader from source code
    private int loadShader(int type, String source) {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, source);
        GLES20.glCompileShader(shader);
        return shader;
    }
    private void initBuffers() {
        vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        texcoordBuffer = ByteBuffer.allocateDirect(texcoords.length * 4)
                .order(ByteOrder.nativeOrder())

        texcoordBuffer.put(texcoords);
        texcoordBuffer.position(0);
    }

}type here

我的活动代码

package com.example.opengl;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GLSurfaceView glSurfaceView = findViewById(R.id.my_sdsd);
        Renderer renderer = new Renderer(this);                
        glSurfaceView.setRenderer(renderer);                  
    }
}

在调试代码时,我发现它在按照以下方法创建着色器时崩溃了

 private int loadShader(int type, String source) {
        **int shader = GLES20.glCreateShader(type);** // crash at this line
        GLES20.glShaderSource(shader, source);
        GLES20.glCompileShader(shader);
        return shader;
    }

有人可以帮助我吗,因为我对 Open GL 和图形编程还很陌生。我只想使用 2D 图像和传感器值创建一个假的 3D 效果。

java android-studio opengl-es-2.0 fragment-shader vertex-shader
© www.soinside.com 2019 - 2024. All rights reserved.