17 #ifdef GLVIS_USE_LIBPNG
21 #include <SDL2/SDL_hints.h>
22 #include <emscripten/bind.h>
23 #include <emscripten/html5.h>
24 #include <emscripten/val.h>
38 namespace em = emscripten;
48 std::stringstream ss(input);
51 const int field_type = stream_state.
ReadStream(ss, data_type);
57 double minv = 0.0, maxv = 0.0;
62 std::cout <<
"parsing 'keys'" << std::endl;
63 ss >> stream_state.
keys;
65 else if (line ==
"valuerange")
67 std::cout <<
"parsing 'valuerange'" << std::endl;
72 std::cout <<
"unknown line '" << line <<
"'" << std::endl;
76 if (field_type < 0 || field_type > 2)
89 double mesh_range = -1.0;
90 if (field_type == 0 || field_type == 2)
94 stream_state.
grid_f->GetNodalValues(stream_state.
sol);
96 if (stream_state.
mesh->SpaceDimension() == 2)
99 if (stream_state.
normals.Size() > 0)
122 else if (stream_state.
mesh->SpaceDimension() == 3)
133 if (stream_state.
mesh->Dimension() == 3)
156 mesh_range = stream_state.
grid_f->Max() + 1.0;
160 mesh_range = stream_state.
sol.Max() + 1.0;
164 else if (field_type == 1)
166 if (stream_state.
mesh->SpaceDimension() == 2)
178 else if (stream_state.
mesh->SpaceDimension() == 3)
189 stream_state.
solv, stream_state.
solw);
202 if (mesh_range > 0.0)
207 if (stream_state.
mesh->SpaceDimension() == 2 && field_type == 2)
231 std::stringstream ss(stream);
233 if (data_type !=
"solution")
235 std::cerr <<
"unsupported data type '" << data_type <<
"' for stream update" <<
242 double mesh_range = -1.0;
246 if (mesh_range > 0.0)
256 cout <<
"Stream: field type does not match!" << endl;
268 std::cout <<
"glvis: setting canvas id to " <<
id << std::endl;
274 SDL_EventState(SDL_KEYDOWN, SDL_DISABLE);
275 SDL_EventState(SDL_KEYUP, SDL_DISABLE);
280 SDL_EventState(SDL_KEYDOWN, SDL_ENABLE);
281 SDL_EventState(SDL_KEYUP, SDL_ENABLE);
286 SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT,
id.c_str());
294 void processKey(
int sym,
bool ctrl=
false,
bool shift=
false,
bool alt=
false)
297 mod |= ctrl ? KMOD_CTRL : 0;
298 mod |= shift ? KMOD_SHIFT : 0;
299 mod |= alt ? KMOD_ALT : 0;
306 std::cout <<
"glvis: adding resize callback for " <<
id << std::endl;
307 auto err = emscripten_set_resize_callback(
id.c_str(),
nullptr,
308 true, [](
int eventType,
const EmscriptenUiEvent *uiEvent,
309 void *userData) -> EM_BOOL
311 std::cout <<
"got resize event" << std::endl;
314 if (err != EMSCRIPTEN_RESULT_SUCCESS)
316 std::cerr <<
"error (emscripten_set_resize_callback): " << err << std::endl;
335 const size_t buffer_size = w*h*4;
336 if (screen_state ==
nullptr)
338 screen_state =
new std::vector<unsigned char>(buffer_size);
340 else if (buffer_size > screen_state->size())
342 screen_state->resize(buffer_size);
345 glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, screen_state->data());
351 auto * flip =
new std::vector<unsigned char>(buffer_size);
354 for (
int j = 0; j < h; ++j)
356 for (
int i = 0; i < w*4; ++i)
358 (*flip)[4*w*j + i] = (*orig)[4*w*(h-j-1) + i];
365 return em::val(em::typed_memory_view(screen_state->size(),
366 screen_state->data()));
369 #ifdef GLVIS_USE_LIBPNG
372 constexpr
const char * filename =
"im.png";
382 fprintf(stderr,
"unable to generate png\n");
383 return em::val::null();
387 std::ifstream ifs(filename, std::ios::binary);
390 fprintf(stderr,
"unable to load png\n");
391 return em::val::null();
396 screen_state =
new std::vector<unsigned char>(std::istreambuf_iterator<char>
401 screen_state->assign(std::istreambuf_iterator<char>
405 return em::val(em::typed_memory_view(screen_state->size(),
406 screen_state->data()));
408 #endif // GLVIS_USE_LIBPNG
421 em::function(
"setKeyboardListeningElementId",
432 #ifdef GLVIS_USE_LIBPNG
std::unique_ptr< GridFunction > ProjectVectorFEGridFunction(std::unique_ptr< GridFunction > gf)
void SendExposeEvent()
Send expose event. In our case MyReshape is executed and Draw after it.
thread_local GeometryRefiner GLVisGeometryRefiner
void setCanvasId(const std::string &id)
void SetValueRange(double, double)
void getGLDrawSize(int &w, int &h)
void setupResizeEventCallback(const std::string &id)
thread_local SdlWindow * wnd
gl3::MeshRenderer & getRenderer()
int updateVisualization(std::string data_type, std::string stream)
virtual void AutoRefine()=0
VisualizationScene * GetVisualizationScene()
em::val getScreenBuffer(bool flip_y=false)
virtual std::string GetHelpString() const
void MyExpose(GLsizei w, GLsizei h)
void SetUseTexture(int ut)
void SetVisualizationScene(VisualizationScene *scene, int view, const char *keys)
virtual void SetShading(int, bool)=0
std::unique_ptr< mfem::GridFunction > grid_f
thread_local string plot_caption
void SetLight(bool light_set)
thread_local VisualizationSceneScalarData * vs
thread_local StreamState stream_state
void processKeys(const std::string &keys)
void setCanvasId(std::string canvas_id)
bool SetNewMeshAndSolution(StreamState new_state, VisualizationScene *vs)
void SetIndex(int num)
Sets the palette texture to bind.
void callKeyDown(SDL_Keycode k, Uint16 mod=0)
EMSCRIPTEN_BINDINGS(js_funcs)
int SaveAsPNG(const char *fname, int w, int h, bool is_hidpi, bool with_alpha, std::function< void(int, void *)> get_row)
std::unique_ptr< mfem::Mesh > mesh
bool isHighDpi() const
This property is set by createWindow().
void disableKeyHandling()
std::vector< unsigned char > * screen_state
em::val getPNGByteArray()
void ResizeWindow(int w, int h)
void SetLightMatIdx(unsigned i)
bool startVisualization(const std::string input, const std::string data_type, int w, int h)
SdlWindow * GetAppWindow()
void SetGridFunction(GridFunction &u)
thread_local string extra_caption
void processKey(int sym, bool ctrl=false, bool shift=false, bool alt=false)
std::string getHelpString()
void SetAutoscale(int _autoscale)
int InitVisualization(const char name[], int x, int y, int w, int h)
Initializes the visualization and some keys.
void setAntialiasing(bool aa_status)
int ReadStream(std::istream &is, const std::string &data_type)
void CallKeySequence(const char *seq)
void setKeyboardListeningElementId(const std::string &id)
void SetGridFunction(GridFunction *gf)