15.5 C
New York
Thursday, April 24, 2025

c++ – The way to rotate the “high” sky texture with Directx9?


I am engaged on designing and animating the sky with a cubemap. The surfaces aside from the “TOP” transfer in a approach that follows one another. Nevertheless, I might by no means work out the best way to adapt the “TOP” half to this motion and I disabled it for now. Under is the code of how I did it. The “TOP” path is disabled as a result of after I embrace it, it separates from the opposite surfaces and slides as if it have been alone. Whereas the encircling instructions scroll in a single path (proper to left and many others.), I considered animating the “TOP” texture to rotate like a wheel and match it with the opposite textures. However I could not succeed.

I am utilizing 4×3 cross sky texture. (4096×3072)

void CSkyBox::GetCrossUVCoordinates(int faceIndex, float& uMin, float& uMax, float& vMin, float& vMax)
{
    const float texWidth = 4096.0f;
    const float texHeight = 3072.0f;

    const float faceWidth = texWidth / 4.0f;
    const float faceHeight = texHeight / 3.0f;

    float pixelOverlapU = 1.0f / texWidth;
    float pixelOverlapV = 1.0f / texHeight;

    int col = 0, row = 0;

    swap (faceIndex)
    {
    case 0: // (+Z)
        col = 2; row = 1;
        break;
    case 1: //  (-Z)
        col = 0; row = 1;
        break;
    case 2: //  (-X)
        col = 1; row = 1;
        break;
    case 3: //  (+X)
        col = 3; row = 1;
        break;
    case 4: //  (+Y)
        col = 1; row = 0;
        break;
    case 5: //  (-Y)
        col = 1; row = 2;
        break;
    default:
        uMin = 0.0f; uMax = 1.0f;
        vMin = 0.0f; vMax = 1.0f;
        return;
    }

    float pxMinU = col * faceWidth;
    float pxMaxU = (col + 1) * faceWidth;
    float pxMinV = row * faceHeight;
    float pxMaxV = (row + 1) * faceHeight;

    uMin = (pxMinU / texWidth) + pixelOverlapU;
    uMax = (pxMaxU / texWidth) - pixelOverlapU;
    vMin = (pxMinV / texHeight) + pixelOverlapV;
    vMax = (pxMaxV / texHeight) - pixelOverlapV;
}
void CSkyBox::Render()
{

    STATEMANAGER.SaveRenderState(D3DRS_ZENABLE, TRUE);
    STATEMANAGER.SaveRenderState(D3DRS_ZWRITEENABLE, FALSE);
    STATEMANAGER.SaveRenderState(D3DRS_LIGHTING, FALSE);
    STATEMANAGER.SaveRenderState(D3DRS_FOGENABLE, FALSE);
    STATEMANAGER.SaveRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

    STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
    STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

    STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

    STATEMANAGER.SetTexture(1, NULL);
    STATEMANAGER.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
    STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

    STATEMANAGER.SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);

    STATEMANAGER.SetTransform(D3DTS_WORLD, &m_matWorld);

    
    static float fOffsetU = 0.0f; 
    static float fOffsetV = 0.0f; 
    const float fScrollSpeed = 0.0009f; 
    float fTime = timeGetTime() / 1000.0f; 
    fOffsetU = fmod(fTime * fScrollSpeed, 1.0f); 
    fOffsetV = fmod(fTime * fScrollSpeed, 1.0f); 

    // Doku remodel matrisi oluÅŸtur
    D3DXMATRIX matTexture;
    D3DXMatrixIdentity(&matTexture);
    matTexture._31 = fOffsetU; 
    matTexture._32 = 0.0f; 

    D3DXMATRIX matOldTexture;
    STATEMANAGER.GetTransform(D3DTS_TEXTURE0, &matOldTexture); 
    STATEMANAGER.SaveTransform(D3DTS_TEXTURE0, &matOldTexture);

    STATEMANAGER.SetTransform(D3DTS_TEXTURE0, &matTexture);
    STATEMANAGER.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);

    // Render Face
    if (m_ucRenderMode == CSkyObject::SKY_RENDER_MODE_TEXTURE)
    {
        CGraphicImageInstance* pFaceImageInstance = m_GraphicImageInstanceMap[m_Faces[0].m_strFaceTextureFileName];
        if (!pFaceImageInstance)
        {
            TraceError("Skybox Face Texture NULL!");
            return;
        }
        STATEMANAGER.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
        STATEMANAGER.SaveSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
        STATEMANAGER.SaveSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
        STATEMANAGER.SetTexture(0, pFaceImageInstance->GetTextureReference().GetD3DTexture());

        for (unsigned int i = 0; i < 6; ++i)
        {
            if (i == 4) // (+Y)
            {
                // Animate: off
                D3DXMATRIX matIdentity;
                D3DXMatrixIdentity(&matIdentity);
                STATEMANAGER.SetTransform(D3DTS_TEXTURE0, &matIdentity);
                STATEMANAGER.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
            }
            else
            {
                STATEMANAGER.SetTransform(D3DTS_TEXTURE0, &matTexture);
                STATEMANAGER.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
            }
            m_Faces[i].Render();
        }

        STATEMANAGER.RestoreSamplerState(0, D3DSAMP_ADDRESSU);
        STATEMANAGER.RestoreSamplerState(0, D3DSAMP_ADDRESSV);
    }
    else
    {
        for (unsigned int i = 0; i < 6; ++i)
        {
            m_Faces[i].Render();
        }
    }

    STATEMANAGER.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
    STATEMANAGER.RestoreTransform(D3DTS_TEXTURE0);

    STATEMANAGER.RestoreRenderState(D3DRS_LIGHTING);
    STATEMANAGER.RestoreRenderState(D3DRS_ZENABLE);
    STATEMANAGER.RestoreRenderState(D3DRS_ZWRITEENABLE);
    STATEMANAGER.RestoreRenderState(D3DRS_FOGENABLE);
    STATEMANAGER.RestoreRenderState(D3DRS_ALPHABLENDENABLE);

    STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLOROP);
    STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLORARG1);
    STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLORARG2);
}
```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles