0

Is there any way to draw mesh dynamically. like on press of a button send new float array to openGL ES 30 VBO. currently i draw both rectangle and triangle STATIC in Native C++ they are visible, But when i pass triangle array from java using JNI. I got an error

call to OpenGL ES API with no current context (logged once per thread)

Native C++.

GLuint fvao, bvao;
GLuint VBO[2];

void setupBuffers() {
    glGenVertexArrays(1,&bvao);
    glBindVertexArray(bvao);
    glGenBuffers(1, &VBO[0]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(rect), rect, GL_STATIC_DRAW);
    .
    .
    .

    
    // Triangle --------------------------------------
    glGenVertexArrays(1,&fvao);
    glBindVertexArray(fvao);
    glGenBuffers(1, &VBO[1]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);   // pos
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);   // color
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(2);   // texture
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glBindVertexArray(0);   //Unbind VAO

}


// UPDATE -----------------------
void updateBuffer(float triangle[]) {
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); //Bind array for OpenGL to use
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}


// update Buffer JNI
extern "C" JNIEXPORT void JNICALL
Java_com_test_nativetest_GL3JNILib_update(JNIEnv * env, jclass obj, jfloatArray triangle)
{
    updateBuffer(triangle);
}

If anyone can tell me what i am missing or doing wrong. Any help would be appreciated.

Kouki
  • 37
  • 7
  • 1
    It is difficult to update an OpenGL buffer from a thread. The OpenGL context is local to the thread. You can activate (make current) the OpenGL context in a thread, but that removes the context from the previous thread. So you need some sort of sync there. See [OpenGL Context](https://www.khronos.org/opengl/wiki/OpenGL_Context). – Rabbid76 Mar 01 '22 at 19:04
  • Oh thank you very much. Yes, i had found about sync object and its hard in practice. – Kouki Mar 02 '22 at 03:19

0 Answers0