• Advertisement
Sign in to follow this  

strange Texture problem

This topic is 3805 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 notized a strange texture problem in a very simple program. i took two polygons (4 vertices) and was laying a texture over the rectangle the result was, that only the first color in the upper left corner of the picture for the texture gets displayed i made a picture of the original bmp and the result in the program:
//-----------------------------------------------------------------------------
#include <d3d9.h>
#include <d3dx9.h>
#include <mmsystem.h>
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 ) 

#include <Windows.h>




//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold Vertices
//LPDIRECT3DCAPS          g_caps       = NULL;
D3DCAPS9 * pCaps =NULL;
LPDIRECT3DINDEXBUFFER9 g_pIB=NULL;    //Indexbuffer



// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex
    DWORD color;        // The vertex color
};


struct VERTEXPOSITIONCOLOR
{
	FLOAT x,y,z;
	DWORD color;
};

struct VERTEXPOSITIONTEXTURE
{
	FLOAT x,y,z;
	
	FLOAT tu,tv;
};


struct VERTEXPOSITIONCOLORTEXTUR1
{
	FLOAT x,y,z;
	DWORD color;
	FLOAT Tu,Tv;
};
struct VERTEXPOSITIONNORMALCOLORTEXTUR1
{
	FLOAT x,y,z;
	FLOAT xn,yn,zn;
	DWORD color;
	
	FLOAT Tu,Tv;
};



// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#define FVF_POSCOLOR (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define FVF_POSCOLORTXT1 (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX0)
#define FVF_POSTXT1 (D3DFVF_XYZ|D3DFVF_TEX0)

HRESULT InitTex1();

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

HRESULT Test;

pCaps = new D3DCAPS9();
    // Create the D3DDevice
g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,pCaps);
int vp = 0;
if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
	vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;		//yes
else
	vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;	

if (pCaps->DevCaps & D3DCREATE_PUREDEVICE)
	vp|=D3DCREATE_PUREDEVICE;





	
	
//D3DDEVCAPS_HWTRANSFORMANDLIGHT 
	
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      vp   ,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Device state would normally be set here


	  g_pd3dDevice->SetRenderState( D3DRS_CULLMODE,D3DCULL_CCW ); //D3DCULL_CCW 


	//  g_pd3dDevice->SetRenderState(D3DRS_WRAP0, D3DWRAPCOORD_0);

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
	//g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );


	


	if(InitTex1()==E_FAIL)
		return E_FAIL;


	
    return S_OK;
}


LPDIRECT3DTEXTURE9 pTexture;
D3DXMATRIX out;
D3DXMATRIX matProj;
HRESULT InitTex1()
{
	D3DXVECTOR3 eye(0,0,-1000);
	D3DXVECTOR3 at(0,0,0);
	D3DXVECTOR3 up(0,1,0);
	D3DXMatrixLookAtLH(&out, &eye, &at, &up);

		
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
	

	if(FAILED(D3DXCreateTextureFromFile( g_pd3dDevice, "tiger.bmp", &pTexture)))
		return E_FAIL;
	else
	{
		
		return S_OK;
	}

	
	
	
	
}




HRESULT InitVB()
{

	VERTEXPOSITIONTEXTURE Vertices[]=
		 {
{  100.0f,-100.0f, 0.0f, 1.0f,1.0f },
 
	  { -100.0f,-100.0f, 0.0f, 0.0f,1.0f },
	  { -100.0f,100.0f, 0.0f, 0.0f,0.0f },
	  {  100.0f, 100.0f, 0.0f, 1.0f,0.0f },


 };


	int graindexes[6] =
	{0,1,2,2,3,0};

	if(g_pd3dDevice->CreateIndexBuffer(sizeof(int)*6,D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_MANAGED,&g_pIB,NULL)!=D3D_OK)
	{
		return E_FAIL;
	}
	else
	{
		//lock
		//fill
		//unlock
		int * pBuffer;
		g_pIB->Lock(0,6,(void**)(&pBuffer),NULL);
		memcpy(pBuffer,graindexes,sizeof(int)*6);
		g_pIB->Unlock();


	}

if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEXPOSITIONTEXTURE),
                   0, FVF_POSTXT1, D3DPOOL_MANAGED, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }
	 VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(Vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, Vertices, sizeof(Vertices) );
    g_pVB->Unlock();

    return S_OK;
	
}


VOID Cleanup()
{
   if(pTexture!=NULL)
		pTexture->Release();
	
	if( g_pVB != NULL )        
        g_pVB->Release();

    if( g_pd3dDevice != NULL ) 
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )       
        g_pD3D->Release();
	
}



VOID MakeCamera1()
{
	
	

	
	
	//LOcale Koordinates in Worldcoordinates
	
	D3DXVECTOR3 V(1,1,0);
	D3DXMATRIX matWorld;

	D3DXMATRIX matW2, matW3;
	D3DXMatrixIdentity(&matWorld);


	//D3DXMatrix
        g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );


	
//Worldtcoordinates in cameracoordinates	


	

	g_pd3dDevice->SetTransform( D3DTS_VIEW, &out);



//Cameracoordinates in projectionscoordinates

g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );




 

	

}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{	

	
    // Clear the backbuffer to a blue color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER , D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );//| D3DCLEAR_ZBUFFER

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {

 g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
   g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
   g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);   //Ignored
 g_pd3dDevice->SetTexture( 0, pTexture );

MakeCamera1();



		




g_pd3dDevice->SetFVF( FVF_POSTXT1 );
		
 g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(VERTEXPOSITIONTEXTURE) );
        
 
 g_pd3dDevice->SetIndices(g_pIB);	
		 g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0,6,0,2 );
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the vertex buffer
        if( SUCCEEDED( InitVB() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}
do i have a problem with my presentation parameters? or is something else missing? i read many turorials and also changed the texture-file, bit it happens to any files i use- sometimes the displayed color is a mix of alle colors of the texture EDIT: Please remember to use 'source' tags in future, and for reference we use regular HTML 'img' tags to embed images. [Edited by - jollyjeffers on September 19, 2007 3:34:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
#define FVF_POSCOLORTXT1 (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX0)
#define FVF_POSTXT1 (D3DFVF_XYZ|D3DFVF_TEX0)


You're telling D3D there are no texture coordinates so I'd imagine it'll be defaulting to 0,0 for all sampling locations. Try changing it to D3DFVF_TEX1.

hth
Jack

[EDIT by Promit] Fixed your colors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement