Skip to content

Commit

Permalink
added transparent image saving
Browse files Browse the repository at this point in the history
  • Loading branch information
sava41 committed Oct 11, 2024
1 parent d6d1b98 commit d26f099
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
10 changes: 5 additions & 5 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ namespace mc
wgpu::ShaderModule shaderModule = app->device.CreateShaderModule( &shaderDesc );

wgpu::BlendState blendState;
// Usual alpha blending for the color:

blendState.color.srcFactor = wgpu::BlendFactor::SrcAlpha;
blendState.color.dstFactor = wgpu::BlendFactor::OneMinusSrcAlpha;
blendState.color.operation = wgpu::BlendOperation::Add;
// We leave the target alpha untouched:
blendState.alpha.srcFactor = wgpu::BlendFactor::Zero;
blendState.alpha.dstFactor = wgpu::BlendFactor::One;

blendState.alpha.srcFactor = wgpu::BlendFactor::One;
blendState.alpha.dstFactor = wgpu::BlendFactor::OneMinusSrcAlpha;
blendState.alpha.operation = wgpu::BlendOperation::Add;

wgpu::ColorTargetState colorTarget;
Expand Down Expand Up @@ -482,7 +482,7 @@ namespace mc
copyTextureDescritor.origin = { 0, 0 };

wgpu::ImageCopyBuffer copyBufferDescriptor;
copyBufferDescriptor.buffer = copyBuffer;
copyBufferDescriptor.buffer = copyBuffer;

// this needs to be multiple of 256
copyBufferDescriptor.layout.bytesPerRow = textureWidthPadded * 4;
Expand Down
9 changes: 6 additions & 3 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,13 @@ SDL_AppResult SDL_AppIterate( void* appstate )

app->device.GetQueue().WriteBuffer( app->viewParamBuf, 0, &outputViewParams, sizeof( mc::Uniforms ) );

wgpu::Color backgroundColor = mc::getSaveWithTransparency()
? wgpu::Color{ 0.0, 0.0, 0.0, 0.0f }
: wgpu::Color{ Spectrum::ColorR( Spectrum::Static::BONE ), Spectrum::ColorG( Spectrum::Static::BONE ),
Spectrum::ColorB( Spectrum::Static::BONE ), 1.0f };

wgpu::RenderPassEncoder outputRenderPassEnc =
mc::createRenderPassEncoder( encoder, app->textureManager.get( *app->copyTextureHandle.get() ).textureView,
wgpu::Color{ Spectrum::ColorR( Spectrum::Static::BONE ), Spectrum::ColorG( Spectrum::Static::BONE ),
Spectrum::ColorB( Spectrum::Static::BONE ), 1.0f } );
mc::createRenderPassEncoder( encoder, app->textureManager.get( *app->copyTextureHandle.get() ).textureView, backgroundColor );

if( app->layers.length() > 0 )
{
Expand Down
28 changes: 27 additions & 1 deletion source/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace mc
FontManager::Font g_inputTextFont = FontManager::Arial;
bool g_setinputTextFocus = false;

bool g_saveWithTransparency = true;

void changeMode( Mode newMode )
{
if( g_appMode != newMode )
Expand Down Expand Up @@ -355,13 +357,32 @@ namespace mc

if( ImGui::Button( ICON_LC_IMAGE_DOWN, buttonSize ) )
{
submitEvent( Events::SaveImageRequest );
ImGui::OpenPopup( "Saving" );
}

if( ImGui::IsItemHovered( ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_Stationary ) )
ImGui::SetItemTooltip( "Save Image" );
ImGui::PopStyleColor( 1 );
ImGui::PopStyleVar( 1 );

ImGui::SetNextWindowPos( glm::vec2( buttonSpacing * 3 + buttonSize.x, buttonSpacing * 2 + buttonSize.y + 5.0f ) );
if( ImGui::BeginPopup( "Saving" ) )
{

if( ImGui::MenuItem( "Save With Background" ) )
{
g_saveWithTransparency = false;
submitEvent( Events::SaveImageRequest );
}
if( ImGui::MenuItem( "Save With Transparency" ) )
{
g_saveWithTransparency = true;
submitEvent( Events::SaveImageRequest );
}

ImGui::EndPopup();
}

ImGui::SetNextWindowPos( glm::vec2( buttonSpacing * 2, buttonSpacing * 2 + buttonSize.y + 5.0f ) );
if( ImGui::BeginPopup( "Menu" ) )
{
Expand Down Expand Up @@ -946,4 +967,9 @@ namespace mc
{
return g_appMode;
}

bool getSaveWithTransparency()
{
return g_saveWithTransparency;
}
} // namespace mc
2 changes: 2 additions & 0 deletions source/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ namespace mc
float getInputTextOutline();
FontManager::Alignment getInputTextAlignment();
FontManager::Font getInputTextFont();

bool getSaveWithTransparency();
} // namespace mc

0 comments on commit d26f099

Please sign in to comment.