| 22 | { |
| 23 | |
| 24 | Image 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 |
nothing calls this directly
no test coverage detected