Win32API Alpha Blend

Win32API Alpha Blend (1)

https://www.youtube.com/watch?v=UfqerWnMolk&list=PL4SIC1d_ab-ZLg4TvAO5R4nqlJTyJXsPK&index=57

 

< 배우게 된 내용 >

bmp 포멧은 픽셀을 표현할때 3 byte로 함 ( 0~ 255 )

Alpha Blend를 하려면 알파라는 1byte가 하나 더 있어야함 ( alpha가 0이라면 안보임 )

 

반투명한 물체(A)의 alpha를 127 ( 50%) 정도로 주고, 이미 그려진 물체에 이 반투명한 물체를 가져댄다고 가정했을때

캔버스에 원래 물체의 색상이 있을텐데 A에 의해 통과해서 보여져야 하니까

이미 캔버스에 그려진 물체의 RGB에 * 0.5, 덧붙여지는 A물체의 RGB에 0.5를 곱해서 더해주면 된다

 

alpha가 있는 bmp는 비트수준이 32임 ( 8 * 4(RGBA))

 

 

 

alphaBlend 함수 윈도우에서 제공

BlendFunction 구조체를 만들어서 줘야함 ( 옵션값을 채워줘야함 )

 

void CPlayer::render(HDC _dc)
{
    // 컴포넌트 (충돌체, etc...) 가 있는 경우 렌더
    // component_render(_dc);
    CTexture* pTex = CResMgr::GetInst()->LoadTexture(L"Player33", L"texture\\Player_A.bmp");

    Vec2 vPos = GetPos();
    vPos = CCamera::GetInst()->GetRenderPos(vPos);

    float width = pTex->Width();
    float height = pTex->Height();

    BLENDFUNCTION bf = {};
    
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.AlphaFormat = AC_SRC_ALPHA;
    bf.SourceConstantAlpha = 127;

    AlphaBlend(_dc, vPos.x - pTex->Width() / 2.f, vPos.y - height / 2.f, width, height, pTex->GetDC(), 0, 0, width, height, bf );
}

 

SourceConstantAlpha은 해당 원본 텍스쳐의 alpha값에 또 alpha값을 127 즉 50퍼만큼 또 적용시킴

 

 


Win32API 강의 58화. Win32API Alpha Blend (2)

https://www.youtube.com/watch?v=va0Jent4PKM&list=PL4SIC1d_ab-ZLg4TvAO5R4nqlJTyJXsPK&index=58

 

< 배우게 된 내용 >

카메라 특수효과 Fade In  ( 밝아짐 ) , Fade Out  ( 화면 전체가 검은색으로 채워져야함 )

 

 

 

AlphaBlend의 SourceConstantAlpha값을 현재 지나간 시간을 구해서 fRatio에 따라 값을 조절하면서 Fade Out 을 구현

 

void CCamera::render(HDC _dc)
{
    if (CAM_EFFECT::NONE == m_eEffect)
        return;
    
    float fRatio = 0.f; // 이펙트 진행 비율

    if (CAM_EFFECT::FADE_OUT == m_eEffect)
    {
        m_fCurTime += fDT;

        // 진행 시간이 이펙트 최대 지정 시간을 넘어선 경우
        if (m_fEffectDuration < m_fCurTime)
        {
            // 효과 종료
            m_eEffect = CAM_EFFECT::NONE;
            return;
        }

        fRatio = m_fCurTime / m_fEffectDuration;
    }

    int iAlpha = (int)(255.f * fRatio);
     
    BLENDFUNCTION bf = {};

    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.AlphaFormat = 0;
    bf.SourceConstantAlpha = iAlpha;

    AlphaBlend(_dc, 0, 0
        , (int)m_pVeilTex->Width()
        , (int)m_pVeilTex->Height()
        , m_pVeilTex->GetDC()
        , 0, 0
        , (int)m_pVeilTex->Width()
        , (int)m_pVeilTex->Height(), bf);
}

 


Win32API 강의 59화. Win32API Alpha Blend (3)

https://www.youtube.com/watch?v=XILXI0NoRH0&list=PL4SIC1d_ab-ZLg4TvAO5R4nqlJTyJXsPK&index=59

 

 

< 배우게 된 내용 >

카메라 효과를 list에 넣어 순서대로 적용 시키기

 

 

 

카메라 효과 정보들을 구조체에 넣어서 관리

 

enum class CAM_EFFECT
{
    FADE_IN,
    FADE_OUT,
    NONE
};

// 카메라 이펙트 효과
struct tCamEffect
{
    CAM_EFFECT eEffect; // 카메라 효과
    float fDuration; // 효과 최대 진행 시간
    float fCurTime; // 카메라 효과 현재 진행된 시간
};

 

 

카메라 효과의 정보에 따라 Alpha값 계산을 다르게 하기

( FadeOut 이면 255 -> 0 )

( Fade In 이면 0 -> 255 )

 

void CCamera::render(HDC _dc)
{
    if (m_listCamEffect.empty())
        return;

    tCamEffect& effect = m_listCamEffect.front();
    effect.fCurTime += fDT;

    float fRatio = 0.f; // 이펙트 진행 비율
    fRatio = effect.fCurTime / effect.fDuration;

    if (fRatio < 0.f)
        fRatio = 0.f;
    if (fRatio > 1.f)
        fRatio = 1.f;

    int iAlpha = 0;

    if (CAM_EFFECT::FADE_OUT == effect.eEffect)
    {
        iAlpha = (int)(255.f * fRatio);
    }
    else if (CAM_EFFECT::FADE_IN == effect.eEffect)
    {
        iAlpha = (int)(255.f * (1.f - fRatio));
    }
     
    BLENDFUNCTION bf = {};

    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.AlphaFormat = 0;
    bf.SourceConstantAlpha = iAlpha;

    AlphaBlend(_dc, 0, 0
        , (int)m_pVeilTex->Width()
        , (int)m_pVeilTex->Height()
        , m_pVeilTex->GetDC()
        , 0, 0
        , (int)m_pVeilTex->Width()
        , (int)m_pVeilTex->Height(), bf);

    // 진행 시간이 이펙트 최대 지정 시간을 넘어선 경우
    if (effect.fDuration < effect.fCurTime)
    {
        // 효과 종료
        m_listCamEffect.pop_front();
        return;
    }

 

 

'Win32API' 카테고리의 다른 글

Win32API FSM ( Finite State Machine )  (0) 2025.01.27
Win32API 렌더링 최적화  (0) 2025.01.27
Win32API 파일 입출력  (0) 2025.01.26
Win32API UI  (0) 2025.01.25
Win32API Tool  (0) 2025.01.21
  Comments