Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
I'd go the other way around, use a C/C++ engine that can be "compiled" to Javascript, and use WebGL for rendering. Unity 5 for instance should be able to do that very soon. But I don't think there's any open source engine supporting that for the moment. –  Laurent Couvidou Aug 12 '14 at 21:14
    
Looks like there's some attempts to port Ogre 3D: github.com/joeyview/ogre/wiki/document, for instance. Also, check this ticket: github.com/kripken/emscripten/issues/485. –  Laurent Couvidou Aug 12 '14 at 21:32

1 Answer 1

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");
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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