App is crashing when trying to import OpenGL C++ project

  android, android-ndk, c++, opengl

I’m new to Android NDK. I have project in C++ which draws a triangle using OpenGL. Also this project has some custom libraries. I’ve added it to my src/main/cpp folder and wrote CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)

project("triangleapp")

add_library(
        gl2jni
        SHARED
        tr.cpp)

add_library(
        tr_lib
        SHARED
        IMPORTED
)
set_target_properties(
        tr_lib
        PROPERTIES IMPORTED_LOCATION
        ${PROJECT_SOURCE_DIR}/tr_lib/tr_lib.c
)
find_library(
        log-lib
        log)

target_link_libraries(
        gl2jni
        android
        EGL
        GLESv2
        tr-lib
        ${log-lib})

And my activity code:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        glSurfaceView = findViewById(R.id.gl_surface_view)
        glSurfaceView.setRenderer(MyRenderer())
    }

    companion object {
        init {
            System.loadLibrary("gl2jni")
        }
        external fun drawTriangle(width: Float, height: Float)
    }

class MyRenderer() : GLSurfaceView.Renderer {
    override fun onDrawFrame(p0: GL10?) {
    }

    override fun onSurfaceChanged(p0: GL10?, p1: Int, p2: Int) {
        MainActivity.drawTriangle(p1.toFloat(), p2.toFloat())
    }

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {

    }
}

Here’s the call of draw method:

extern "C" JNIEXPORT void JNICALL
Java_kz_app_triangleapp_MainActivity_00024Companion_drawTriangle(JNIEnv *env, jobject obj,
                                                                 jfloat width, jfloat height) {
    auto *triangle = new Triangle();
    triangle -> draw(width, height, 1.0f, 0.0f);
}

When I run the project, app is crashing with the next error:
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 2683 (GLThread 25452), pid 2644
Any ideas how can I fix it?

Source: Windows Questions C++

LEAVE A COMMENT