MCPcopy Create free account
hub / github.com/MeshInspector/MeshLib / renderToImage

Function renderToImage

source/MRViewer/MRRenderToImage.cpp:24–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22{
23
24Image renderToImage( const Vector2i& resolution, const std::optional<Color>& backgroundColor, const std::function<void( FramebufferData* framebuffer )>& drawFunc )
25{
26 auto& viewer = Viewer::instanceRef();
27 if ( !viewer.isGLInitialized() )
28 return {};
29
30 bool needBindSceneTexture = getViewerInstance().isSceneTextureBound();
31
32 FramebufferData fd;
33 fd.gen( resolution, getViewerInstance().isDepthPeelingEnabled(), getMSAAPow( viewer.getRequestedMSAA() ) );
34 fd.bind( false );
35
36 if ( backgroundColor )
37 fillFramebuffer( *backgroundColor );
38
39 drawFunc( &fd );
40
41 fd.copyTextureBindDef();
42 fd.bindTexture( true, false ); // only bind color
43
44 Image result;
45 result.resolution = resolution;
46 result.pixels.resize( (size_t)resolution.x * resolution.y );
47
48 // copy texture to image
49#ifdef __EMSCRIPTEN__
50 GLuint fbo;
51 GL_EXEC( glGenFramebuffers( 1, &fbo ) );
52 GL_EXEC( glBindFramebuffer( GL_FRAMEBUFFER, fbo ) );
53 GL_EXEC( glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fd.getColorTexture(), 0 ) );
54
55 GL_EXEC( glReadPixels( 0, 0, resolution.x, resolution.y, GL_RGBA, GL_UNSIGNED_BYTE, (void*)result.pixels.data() ) );
56
57 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
58 glDeleteFramebuffers( 1, &fbo );
59#else
60 GL_EXEC( glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)result.pixels.data() ) );
61#endif
62
63 fd.del();
64
65 if ( needBindSceneTexture )
66 viewer.bindSceneTexture( true );
67
68 return result;
69}
70
71} // namespace MR

Callers

nothing calls this directly

Calls 13

getMSAAPowFunction · 0.85
fillFramebufferFunction · 0.85
isSceneTextureBoundMethod · 0.80
getRequestedMSAAMethod · 0.80
copyTextureBindDefMethod · 0.80
bindTextureMethod · 0.80
bindSceneTextureMethod · 0.80
genMethod · 0.45
isDepthPeelingEnabledMethod · 0.45
bindMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected