# Using C++ code in VTK.js

**URL:** https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271
**Category:** Web
**Created:** [November 24, 2021, 5:13pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271 "2021-11-24T17:13:16Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![shrikant](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@shrikant](https://discourse.vtk.org/u/shrikant)
#### Post date: [November 24, 2021, 5:13pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/1 "2021-11-24T17:13:16Z")

</div>

Hi,  
I am trying to use my existing C++ code in VTK.js. I saw Emscripten related examples.  
However, it uses the docker. I simply wanted to C++ code in VTK.js and on Windows platform. is there any other way to accomplish it.

Shrikant

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [November 24, 2021, 5:51pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/2 "2021-11-24T17:51:05Z")

</div>

The docker is only meant to compile the code to emscripten/wasm. Docker aims to simplify the setup of the dev tools. But nothing prevent you to build everything yourself.

Also we don’t have any existing path to blend emscripten VTK with vtk.js.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [November 25, 2021, 9:18am UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/3 "2021-11-25T09:18:19Z")

</div>

> [@shrikant](#):
>
> I simply wanted to C++ code in VTK.js and on Windows platform.

Use VTK instead of VTK.js ?

---

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [November 29, 2021, 3:33pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/4 "2021-11-29T15:33:30Z")

</div>

I have converted my VTK C++ desktop app completely to WebAssembly without much effort. I use the docker to compile my wasm and create the counterpart javascript file. I link to those two files in an [ASP.NET](http://ASP.NET) web project and it is working great. I only have to run the docker when I need to re-compile my wasm and javascript counterpart file. The docker makes life so much easier since the environment is all set up for compiling out of the box.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 29, 2021, 6:42pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/5 "2021-11-29T18:42:05Z")

</div>

@Donny_Zimmerman this sounds very interesting. Can you send a link to your project to get an idea of how complex applications and what parts of VTK can be easily compiled to WebAssembly? A link to a live demo would be great, too.

---

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [November 30, 2021, 2:12pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/6 "2021-11-30T14:12:53Z")

</div>

@lassoan here is a link to the live demo. Just click on “Apply Site” after the WebAssembly loads.

[http://www.weatherinfosystem.com/WebAssembly/SwisWebRadarViewer/SwisWebRadarViewer.aspx](http://www.weatherinfosystem.com/WebAssembly/SwisWebRadarViewer/SwisWebRadarViewer.aspx)

I am currently building the vtkPolyData and vtkTexture manually in C++ utilizing the radar data from an emscripten\_fetch call. All of the callbacks are in C++ for my custom interactor. As you interact, notice how the county lines and the radar data switch places to give better perspective. I am calling the “SetLayer” on the renderers to get this effect.

I am I am currently working on a volume viewer for the full scan radar data. I am unable to tell you if the volume rendering is working or not as I am currently working on the algorithm to interpolate the elevation scans.

---

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [December 2, 2021, 4:40pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/7 "2021-12-02T16:40:27Z")

</div>

@lassoan Probably the biggest issue is that OpenGL ES (which WebAssembly uses) doesn’t support TextureBuffers. I had to make some modifications to the VTK code to use point scalars instead of cell scalars to get around the issue. See below thread:

> [@vtkScalarBarActor in WebAssembly](https://discourse.vtk.org/t/vtkscalarbaractor-in-webassembly/5078/9):
>
> @ken-martin Here is a screenshot of the breakpoint and the values of the parameters to bindTexture: [image] Here is the stack trace at that breakpoint: \_glBindTexture (SwisRadar.js:10994) \_\_ZN16vtkTextureObject8ActivateEv (SwisRadar.wasm:0xa5654) \_\_ZN25vtkOpenGLPolyDataMapper2D13RenderOverlayEP11vtkViewportP10vtkActor2D (SwisRadar.wasm:0x69c73) \_\_ZN10vtkActor2D13RenderOverlayEP11vtkViewport (SwisRadar.wasm:0x127ecc) \_\_ZN17vtkScalarBarActor13RenderOverlayEP11vtkViewport (SwisRadar.wasm:0…

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [December 10, 2021, 8:38pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/8 "2021-12-10T20:38:52Z")

</div>

Thank you, your demo works very nicely! Lack of texture buffer seems to be a quite significant limitation. I guess WebGPU supports texture buffers - could WebAssembly be used with WebGPU instead of OpenGL ES?

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [January 18, 2023, 10:53pm UTC](https://discourse.vtk.org/t/using-c-code-in-vtk-js/7271/9 "2023-01-18T22:53:48Z")

</div>

Yes, WebAssembly can use webgpu with chromium based browser. Emscripten has the headers in [include/webgpu](https://github.com/emscripten-core/emscripten/tree/main/system/include/webgpu) However, VTK C++ doesn’t yet implement WebGPU rendering.

There is ongoing work to emulate texture buffers with 2D textures in `vtkTextureObject`. However, that will be limited by the maximum dimensions of a 2D texture ~ typically 32768x32768
