Archived

This topic is now archived and is closed to further replies.

2d strange problem d3d9

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a strange problem... i'm making a 2D game with Direct3D 9, i'm using Textured Quads. On ATI this work perfecly but on GeForce 4 or TNT2 M64 the pixels seems to be perturbed....does anyone know what's this problem and how to solve it ? Look the screenshots: http://chemzys.free.fr/So6/ [edited by - gamedevtmp on November 28, 2003 8:34:14 PM]

Share this post


Link to post
Share on other sites
how that ?? i have that.
i don''t use filter i think

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

d3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_NONE );
d3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_NONE );

Share this post


Link to post
Share on other sites
It''s the code of my blitter, at the end there is the fillVertice methode of the sprite




#include "C3DBlitter.h"


C3DBlitter::C3DBlitter( long resWidth, long resHeight, D3DFORMAT resFormat, HWND hWnd, BOOL bWindowedMode )
{
HRESULT hr;

GetCurrentDirectory( MAX_PATH, directory );

//Make Direct3D object

d3d = Direct3DCreate9( D3D_SDK_VERSION );

//Make sure NULL pointer was not returned

if( !d3d )
return;

//Get device capabilities

ZeroMemory( &d3dCaps, sizeof( d3dCaps ) );
if( FAILED( d3d->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps ) ) )
return;

//Setup present parameters

ZeroMemory( &d3dPresent, sizeof( d3dPresent ) );
d3dPresent.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dPresent.hDeviceWindow = hWnd;
d3dPresent.BackBufferCount = 1;
d3dPresent.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

//Check if windowed

if( bWindowedMode )
{
D3DDISPLAYMODE d3ddm;
RECT rWindow;

//Get display mode

d3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

//Get window bounds

GetClientRect( hWnd, &rWindow );

//Setup screen dimensions

this->resWidth = rWindow.right - rWindow.left;
this->resHeight = rWindow.bottom - rWindow.top;

//Setup backbuffer

d3dPresent.Windowed = true;
d3dPresent.BackBufferFormat = d3ddm.Format;
d3dPresent.BackBufferWidth = rWindow.right - rWindow.left;
d3dPresent.BackBufferHeight = rWindow.bottom - rWindow.top;
}
else
{
this->resWidth = resWidth;
this->resHeight = resHeight;

d3dPresent.Windowed = false;
d3dPresent.BackBufferWidth = resWidth;
d3dPresent.BackBufferHeight = resHeight;
d3dPresent.BackBufferFormat = resFormat;
}

//Check if hardware vertex processing is available

if( d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
//Create device with hardware vertex processing

hr = d3d->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dPresent, &d3dDevice );
}
else
{
//Create device with software vertex processing

hr = d3d->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPresent, &d3dDevice );
}

//Make sure device was created

if( FAILED( hr ) )
return;


/*for( long i = 0; i < NB_TEXTURES; i++ )
{
char fileName[MAX_PATH];
sprintf( fileName, "%s/Textures/Tex_%d.png", directory, i );
tTextures[i] = loadTexture( fileName );
}*/


// faire un undef si on veux changer !

//#define IDR_DATA1 105


tTextures[0] = loadTextureFromResource( IDR_DATA1 );
tTextures[1] = loadTextureFromResource( IDR_DATA2 );
tTextures[2] = loadTextureFromResource( IDR_DATA3 );
tTextures[3] = loadTextureFromResource( IDR_DATA4 );


////Create vertex buffer and set as stream source

d3dDevice->CreateVertexBuffer( sizeof( TLVERTEX ) * 4, 0, D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBufferStandard, NULL );
//d3dDevice->CreateVertexBuffer( sizeof( TLVERTEX ) * 4, 0, D3DFVF_TLVERTEX, D3DPOOL_DEFAULT, &vertexBufferStandard, NULL );


//Create vertex buffer and set as stream source

d3dDevice->CreateVertexBuffer( sizeof( TLVERTEX ) * NB_GAME_VERTICES, 0, D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBufferGame, NULL );
//d3dDevice->CreateVertexBuffer( sizeof( TLVERTEX ) * NB_GAME_VERTICES, 0, D3DFVF_TLVERTEX, D3DPOOL_DEFAULT, &vertexBufferGame, NULL );


restoreAll();

limitFPS = FALSE;
showFPS = FALSE;
sFps = new CGString( 0, 0, "", FONT_TYPE_0, MAX_3DBLITTER_STR_FPS, VERTEX_BUFFER_STANDARD );

//Successfully initalized Direct3D

}

C3DBlitter::~C3DBlitter( void )
{
if( sFps )
{
delete sFps;
sFps = NULL;
}

for( long i = 0; i < NB_TEXTURES; i++ )
{
if( tTextures[i] )
{
tTextures[i]->Release();
tTextures[i] = NULL;
}
}

if( vertexBufferStandard )
{
vertexBufferStandard->Release();
vertexBufferStandard = NULL;
}

if( vertexBufferGame )
{
vertexBufferGame->Release();
vertexBufferGame = NULL;
}

//Release device

if( d3dDevice )
d3dDevice->Release();

//Release d3d object

if( d3d )
d3d->Release();

//Successfully closed Direct3D

}

void C3DBlitter::restoreAll( void )
{
//Set vertex shader

d3dDevice->SetVertexShader( NULL );
d3dDevice->SetFVF( D3DFVF_TLVERTEX );

//Setup rendering states

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

d3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_NONE );
d3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_NONE );
}

IDirect3DTexture9 *C3DBlitter::getTexture( long idxTextures )
{
return tTextures[idxTextures];
}


IDirect3DTexture9 *C3DBlitter::loadTextureFromResource( long rId )
{
IDirect3DTexture9 *d3dTexture;
D3DXIMAGE_INFO SrcInfo; //Optional


//Use a black colourkey

D3DCOLOR colorkey = D3DCOLOR_ARGB( 255, 0, 0, 0 );

// Load image from file

if( FAILED( D3DXCreateTextureFromResourceEx( d3dDevice, GetModuleHandle( NULL ), MAKEINTRESOURCE( rId ), 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colorkey, &SrcInfo, NULL, &d3dTexture ) ) )

{
return NULL;
}

//Return the newly made texture

return d3dTexture;
}

void C3DBlitter::clear( void )
{
//d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE( 0.35f, 0.53f, 0.7, 1.0f ), 0.0f, 0 );

d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, 0xFF000000, 0.0f, 0 );
}

void C3DBlitter::startBlt( void )
{
d3dDevice->BeginScene();
}

void C3DBlitter::setShowFPS( BOOL showFPS )
{
this->showFPS = showFPS;
}

void C3DBlitter::setLimitFPS( BOOL limitFPS )
{
this->limitFPS = limitFPS;
}

BOOL C3DBlitter::getLimitFPS( void )
{
return limitFPS;
}

void C3DBlitter::endBlt( void )
{
static long lastT = 0;
static long fpsCounter = 0;
static long fps = 0;

long now = timeGetTime();

if( now - lastT >= 1000 )
{
lastT = now;
fps = fpsCounter;
fpsCounter = 0;

char str[MAX_3DBLITTER_STR_FPS + 1];
sprintf( str, "
FPS: %d", fps );
sFps->setText( str );
}

if( showFPS )
{
sFps->prepareBlit();
sFps->blit( 0 );
}

fpsCounter++;

d3dDevice->EndScene();
}

void C3DBlitter::present( void )
{
static BOOL readyToPresent = TRUE;
static long lastTimeFrame = 0;

switch( d3dDevice->TestCooperativeLevel() )
{
case D3DERR_DEVICELOST:
readyToPresent = FALSE;
return;

case D3DERR_DEVICENOTRESET:
d3dDevice->Reset( &d3dPresent );
restoreAll();
readyToPresent = TRUE;
return;
}

if( readyToPresent )
{
if( limitFPS )
{
while( timeGetTime() - lastTimeFrame < 15 )
;
}

lastTimeFrame = timeGetTime();


d3dDevice->Present( NULL, NULL, NULL, NULL );
}
}

void C3DBlitter::lockVertexBufferGame( TLVERTEX **vertices )
{
//Lock the vertex buffer

vertexBufferGame->Lock( 0, 0, ( void ** )vertices, NULL );
}

void C3DBlitter::unlockVertexBufferGame( void )
{
//Unlock the vertex buffer

vertexBufferGame->Unlock();
}

void C3DBlitter::blit( C3DSprite *s, DWORD vIdx, long addX, long addY )
{
long startVertice = vIdx;

if( s->getHaveClip() )
{
RECT rClip;

s->getRClip( &rClip );

D3DVIEWPORT9 vp;
memset( &vp, 0, sizeof( D3DVIEWPORT9 ) );
vp.X = rClip.left + addX;
vp.Y = rClip.top + addY;
vp.Width = rClip.right - rClip.left;
vp.Height = rClip.bottom - rClip.top;

d3dDevice->SetViewport( &vp );
}

//Set texture

d3dDevice->SetTexture( 0, tTextures[s->getIdxTexture()] );

switch( s->getVbt() )
{
case VERTEX_BUFFER_GAME:
d3dDevice->SetStreamSource( 0, vertexBufferGame, 0, sizeof( TLVERTEX ) );
break;

case VERTEX_BUFFER_STANDARD:
{
TLVERTEX *vertices;

startVertice = 0;

//Lock the vertex buffer

vertexBufferStandard->Lock( 0, 0, ( void ** )&vertices, NULL );
s->fillVertices( vertices, addX, addY );
//Unlock the vertex buffer

vertexBufferStandard->Unlock();

d3dDevice->SetStreamSource( 0, vertexBufferStandard, 0, sizeof( TLVERTEX ) );

break;
}
}

d3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, startVertice, 2 );

if( s->getHaveClip() )
{
D3DVIEWPORT9 rvp;
memset( &rvp, 0, sizeof( D3DVIEWPORT9 ) );
rvp.X = 0;
rvp.Y = 0;
rvp.Width = resWidth;
rvp.Height = resHeight;

d3dDevice->SetViewport( &rvp );
}
}










void C3DSprite::fillVertices( TLVERTEX *vertices, long addX, long addY )
{
if( visible )
{
//Setup vertices

RECT rcDest;
RECT rcSrc;

memcpy( &rcDest, &rDest, sizeof( RECT ) );
rcDest.left += addX;
rcDest.top += addY;
rcDest.right += addX;
rcDest.bottom += addY;

getCurrentRSrc( &rcSrc );

vertices[0].colour = verticesColors[0];
vertices[0].x = ( float ) rcDest.left;
vertices[0].y = ( float ) rcDest.top;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = ( bltFlag & BLIT_FLAG_MIRROR ) ? ( float )( rcSrc.right ) / texWidth : ( float )( rcSrc.left ) / texWidth;
vertices[0].v = ( bltFlag & BLIT_FLAG_FLIP ) ? ( float )( rcSrc.bottom ) / texWidth : ( float )( rcSrc.top ) / texWidth;

vertices[1].colour = verticesColors[1];
vertices[1].x = ( float ) rcDest.right;
vertices[1].y = ( float ) rcDest.top;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = ( bltFlag & BLIT_FLAG_MIRROR ) ? ( float )( rcSrc.left ) / texWidth : ( float )( rcSrc.right ) / texWidth;
vertices[1].v = ( bltFlag & BLIT_FLAG_FLIP ) ? ( float )( rcSrc.bottom ) / texWidth : ( float )( rcSrc.top ) / texWidth;

vertices[2].colour = verticesColors[2];
vertices[2].x = ( float ) rcDest.right;
vertices[2].y = ( float ) rcDest.bottom;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = ( bltFlag & BLIT_FLAG_MIRROR ) ? ( float )( rcSrc.left ) / texWidth : ( float )( rcSrc.right ) / texWidth;
vertices[2].v = ( bltFlag & BLIT_FLAG_FLIP ) ? ( float )( rcSrc.top ) / texWidth : ( float )( rcSrc.bottom ) / texWidth;

vertices[3].colour = verticesColors[3];
vertices[3].x = ( float ) rcDest.left;
vertices[3].y = ( float ) rcDest.bottom;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = ( bltFlag & BLIT_FLAG_MIRROR ) ? ( float )( rcSrc.right ) / texWidth : ( float )( rcSrc.left ) / texWidth;
vertices[3].v = ( bltFlag & BLIT_FLAG_FLIP ) ? ( float )( rcSrc.top ) / texWidth : ( float )( rcSrc.bottom ) / texWidth;
}
}

Share this post


Link to post
Share on other sites
This has to do with the problem of pixel center not being well defined in Direct3D texturing. They were supposed to fix that in Direct3D 9, but it could be either that the spec is still broken, or that one of the vendors doesn''t follow it.

The typical work-around for bad texture pixel centers is to add 0.375/texture_width to all "u" texture coordinates, and 0.375/texture_height to all "v" texture coordinates.

Another possibility is that it''s the framebuffer pixel center which isn''t lining up, and you should add 0.375 to all your x and y coordinates.

Share this post


Link to post
Share on other sites