2
\$\begingroup\$

I would like, if it's possible, to use Emscripten code generated from C/C++ with the Javascript library three.js. For example, from emscripten code I want to pass rendering information to the three.js that will be showed in the browser.

So, can someone give me an idea of how to do that?

\$\endgroup\$
2
\$\begingroup\$

You have multiple ways to interact from c++ to javascript. I can show you one way I find quite handy:

in the js library file

// here you write JS "handlers"
mergeInto(LibraryManager.library, {

   new_cube: function(size, color) {
      var cube = new THREE.Mesh(
         new THREE.BoxGeometry(size, size, size),
         new THREE.MeshBasicMaterial({ color: color })
      );
   }

});

in c++

extern "C" {
    extern void new_cube(const int size, const char* color);
}

int main() {
  new_cube(10, "red");
  return 1;
}

in the build command

emcc source.cpp --js-library src/library.js

for easy debugging (but very slow)

#include <emscripten.h>
void emscripten_log(const char* string, bool escape = true)
{
    char buff[1024];
    sprintf(buff, (escape ? "console.log('%s');" : "console.log(%s);"), string);
    emscripten_run_script(buff);
}
void emscripten_debugger()
{
    emscripten_run_script("debugger");
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.