Sign in to follow this  
Idej

2D in Direct3D8 Texture Problem

Recommended Posts

First, I'll give you some pseudo-code of the environment I'm working in before I give you the problem. Everything is pretty normal in the way that people set up everything and draw vertices in 2D for Direct3D8, but I'll just give it so that people don't nag me for it.
/***********************
*Initialization crap...*
***********************/
ZeroMemory(&m_D3DPParameters,sizeof(D3DPRESENT_PARAMETERS));
m_D3DPParameters.BackBufferFormat                = D3DFMT_A8R8G8B8;
m_D3DPParameters.BackBufferHeight                = 768;
m_D3DPParameters.BackBufferWidth                 = 1024;
m_D3DPParameters.EnableAutoDepthStencil          = FALSE;
m_D3DPParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
m_D3DPParameters.FullScreen_RefreshRateInHz      = D3DPRESENT_RATE_DEFAULT;
m_D3DPParameters.MultiSampleType                 = D3DMULTISAMPLE_NONE;
m_D3DPParameters.SwapEffect                      = D3DSWAPEFFECT_DISCARD;
m_D3DPParameters.Windowed                        = FALSE;
m_dwBehaviorFlags                                = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

m_lpD3DDevice8->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
m_lpD3DDevice8->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
m_lpD3DDevice8->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
m_lpD3DDevice8->SetRenderState(D3DRS_LIGHTING,FALSE);
m_lpD3DDevice8->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
m_lpD3DDevice8->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);

/*************************************
*Setting up the vertex for drawing...*
*************************************/
class CVTX
{
public:
    float m_fX,
          m_fY,
          m_fZ,
          m_fRHW;
    DWORD m_dwDiffuse;
    float m_fTU,
          m_fTV;
};

//all x and y set to destination...
//all z set to .5f...
//all rhw set to 1...
//all diffuse set to D3DCOLOR_ARGB(255,255,255,255)...

//u and v set for normal...
//0 0
//1 0
//1 1
//0 1

//if u flipped set u: 1,0,0,1
//if v flipped set v: 1,1,0,0

//and rotation stuff...
float fCosine  = 0,
      fHeight  = m_fHeight/2,
      fSine    = 0,
      fWidth   = m_fWidth/2;

p_fAngle *= .01745329f;
fCosine   = cosf(p_fAngle);
fSine     = sinf(p_fAngle);
fX        = -fWidth;
fY        = -fHeight;

m_CVTX[0].m_fX += (fX    *fCosine-fY     *fSine  +fWidth);
m_CVTX[0].m_fY += (fX    *fSine  +fY     *fCosine+fHeight);
m_CVTX[1].m_fX += (fWidth*fCosine-fY     *fSine  +fWidth);
m_CVTX[1].m_fY += (fWidth*fSine  +fY     *fCosine+fHeight);
m_CVTX[2].m_fX += (fWidth*fCosine-fHeight*fSine  +fWidth);
m_CVTX[2].m_fY += (fWidth*fSine  +fHeight*fCosine+fHeight);
m_CVTX[3].m_fX += (fX    *fCosine-fHeight*fSine  +fWidth);
m_CVTX[3].m_fY += (fX    *fSine  +fHeight*fCosine+fHeight);

/**********************
*Drawing the vertex...*
**********************/
//#define D3DFVF (D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_XYZRHW)
m_lpD3DDevice8->SetVertexShader(D3DFVF_VTX);
m_pCDX8->m_lpD3DDevice8->SetTexture(0,m_plpD3DTexture8[m_ulTexture]);
m_pCDX8->m_lpD3DDevice8->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,static_cast<CONST void *>(m_CVTX),sizeof(CVTX));
m_pCDX8->m_lpD3DDevice8->SetTexture(0,0);


/******** *Problem* ********/ If I just draw it normal, I don't have any problems at all. But if I mirror it [using the technique above or even using 1,1,2,1,2,2,1,2 using SetTextureStageState(D3DTSS_ADDRESSU,D3DTADDRESS_MIRROR)] the pixels at the end of the image wrap around. Then one day I read this article: http://www.gamedev.net/reference/articles/article1972.asp. Then I started subtracting -.5f from all of my x's and y's, but on some of the images it would mess up. I found out that images with even width need to be subtracted by -.5f and odd width shouldn't, but it'd then mess up when changing the x and/or y. Then I'd have to test the x and y each time, it'd be too much of a waste. Also, with the problem fixed, rotating the texture would still cause it to have this problem. The only solution that works at all is allowing my images to have a pixel border around it (the pixel color being the color key color) within the file itself. Then I don't use -.5f at all or anything. Even rotating works perfectly. But I'm still curious, is there not a better solution? I know the cause has something to do with converting pixels to texels, but howcome always subtracting -.5f messes up? I even tried setting all of the antialiased stuff off (which should be by default) using the following: m_lpD3DDevice8->SetRenderState(D3DRS_EDGEANTIALIAS,FALSE); m_lpD3DDevice8->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS,FALSE); It still didn't work. Also, I set RHW to 0 and it worked, but it didn't work for rotating images. Is there a solution? -Thanks, Idej [Edited by - Coder on June 8, 2005 10:37:28 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this