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

1 Answer 1

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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.