Shula
Shula

Reputation: 306

Create textures in javascript and WebGL and identify them in WebAssembly

I want to create some textures from HTMLVideoElement with WebGL and continue to work with them in WebAssembly part. I'm using some context due to emscripten_webgl_create_context.

In OpenGL, I can create texture with glGenTextures and have a pointer to the texture.

Is there any way I can create texture in the Javascript part (with WebGL) and past a valid pointer or any other id to the WebAssembly part so I can identify the texture?

Upvotes: 1

Views: 1477

Answers (1)

user128511
user128511

Reputation:

Just a guess but I think you'll need to modify the emscripten OpenGL source code. If you look you'll see WebGL objects are associated with an id here. You would need to add a function to be able to register external JavaScript WebGL objects here, or you would need to add a function to let C++ make the id and then get the objects from JavaScript.

Note: One way you could do the second (make the object in C++, pass it to JavaSCript) without changing the emscripten source would be to make the id in C++, bind it, then call JavaScript and have JavaScript query it. In other words

 GLuint tex;
 glGenTextures(1, tex);
 glBindTexture(GL_TEXTURE_2D, tex);

..now call some javascript function you made..

 function someJSFunction() {
   // look up the currently bound TEXTURE_2D
   const tex = gl.getParameter(gl.TEXTURE_BINDING_2D);
   ...

The id used in C++ is hacked on to the WebGLTexture object by empscripten's OpenGL library so

 const id = tex.name

You can now pass id back to C++ anytime you want to refer to the texture

Also, using the code above you could build a c++ function that allocates a single texture in C++ that you can call from JavaScript. Since it would leave the texture it just created bound you could query the texture as shown above.

Upvotes: 2

Related Questions