Skip to content

Commit

Permalink
Fixed broken halo effect (credits goes to dscharrer)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienLussier committed Jul 24, 2012
1 parent b495c66 commit 1165dd9
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions src/graphics/data/TextureContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,60 +315,65 @@ TextureContainer * TextureContainer::LoadUI(const res::path & strName, TCFlags f
return Load(strName, flags | UI);
}

bool TextureContainer::CreateHalo()
{
bool TextureContainer::CreateHalo() {

Image srcImage;
if(!srcImage.LoadFromFile(m_pTexture->getFileName())) {
return false;
}

// Allocate and add the texture to the linked list of textures;
res::path haloName = m_texName.string();
haloName.append("_halo");
TextureHalo = new TextureContainer(haloName, NoMipmap | NoRefinement | NoColorKey);
if(!TextureHalo) {
return false;
}

TextureHalo->m_pTexture = GRenderer->CreateTexture2D();
if(TextureHalo->m_pTexture)
{
Image srcImage;
srcImage.LoadFromFile(m_pTexture->getFileName());

bool bLoaded = TextureHalo->m_pTexture->Init(m_dwWidth + HALO_RADIUS*2, m_dwHeight + HALO_RADIUS*2, srcImage.GetFormat());
if(bLoaded)
{
TextureHalo->m_dwWidth = TextureHalo->m_pTexture->getSize().x;
TextureHalo->m_dwHeight = TextureHalo->m_pTexture->getSize().y;

Vec2i storedSize = TextureHalo->m_pTexture->getStoredSize();
TextureHalo->uv = Vec2f(float(TextureHalo->m_dwWidth) / storedSize.x, float(TextureHalo->m_dwHeight) / storedSize.y);
TextureHalo->hd = Vec2f(.5f / storedSize.x, .5f / storedSize.y);

Image &im = TextureHalo->m_pTexture->GetImage();

// Center the image, offset by radius to contain the edges of the blur
im.Clear();
im.Copy(srcImage, HALO_RADIUS, HALO_RADIUS);

// Keep a copy of the image at this stage, in order to apply proper alpha masking later
Image copy = im;

// Convert image to grayscale, and turn it to black & white
im.ToGrayscale(Image::Format_L8A8);
im.ApplyThreshold(0, ~0);
if(!TextureHalo->m_pTexture) {
return true;
}

Image im;

int width = m_dwWidth + HALO_RADIUS * 2;
int height = m_dwHeight + HALO_RADIUS * 2;
im.Create(width, height, srcImage.GetFormat());

// Center the image, offset by radius to contain the edges of the blur
im.Clear();
im.Copy(srcImage, HALO_RADIUS, HALO_RADIUS);

// Keep a copy of the image at this stage, in order to apply proper alpha masking later
Image copy = im;

// Convert image to grayscale, and turn it to black & white
im.ToGrayscale(Image::Format_L8A8);
im.ApplyThreshold(0, ~0);

// Blur the image
im.Blur(HALO_RADIUS);
// Blur the image
im.Blur(HALO_RADIUS);

// Increase the gamma of the blur outline
im.QuakeGamma(10.0f);

// Set alpha to inverse of original image alpha
copy.ApplyColorKeyToAlpha();
im.SetAlpha(copy, true);

// adejr: assertion here seems to fail often, i don't understand why?
TextureHalo->m_pTexture->Upload();
}
}
// Increase the gamma of the blur outline
im.QuakeGamma(10.0f);

// Set alpha to inverse of original image alpha
copy.ApplyColorKeyToAlpha();
im.SetAlpha(copy, true);

TextureHalo->m_pTexture->Init(im, 0);

TextureHalo->m_dwWidth = TextureHalo->m_pTexture->getSize().x;
TextureHalo->m_dwHeight = TextureHalo->m_pTexture->getSize().y;

Vec2i storedSize = TextureHalo->m_pTexture->getStoredSize();
TextureHalo->uv = Vec2f(
float(TextureHalo->m_dwWidth) / storedSize.x,
float(TextureHalo->m_dwHeight) / storedSize.y
);
TextureHalo->hd = Vec2f(.5f / storedSize.x, .5f / storedSize.y);

return true;
}

Expand Down

0 comments on commit 1165dd9

Please sign in to comment.