Skip to main content
gamedev.net
🔒 Locked

Direct3D Textures

Started by JSCFaith Dec 3, 2001 at 7:49 AM 8 replies 300+ views
Original Post
JSCFaith
JSCFaith
Hey, I am having a little problem. I have started learning Direct3D for the last 2 days, but I have stumbled on a problem that I can't seem to resolve. I am trying to make a square and then put a texture on it. Now, when I render the square, it is just a solid color. I don't know why. If I put a texture that is mainly yellow though, the solid color will be a shade of yellow. Or if I put a texture in that is relatively blue. The solid color will be dark blue. Here is the rendering code I use:
    

    // Clear the viewport

    m_pd3dDevice->Clear( 0L, NULL,   D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x000000ff, 1.0f, 0L );

	// Begin the scene

    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
		m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );

		m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
		m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
	    m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

        // Output statistics

        m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
        m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

      // End the scene.

      m_pd3dDevice->EndScene();
    }

  

And here is the Texture loading code:

      
    // Load the texture for the background image

    D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),&m_pBackgroundTexture);
       

  

Here is my VertexBuffer Structer:


struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; vertex
    FLOAT tu, tv;		// texture coordinates

};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)

Finally, here is my code that fills the vertex buffer.

      

    // fill the vertex buffer with the new data

    D3DVIEWPORT8 vp;
    m_pd3dDevice->GetViewport(&vp);

    // Initialize four vertices for rendering two triangles

    CUSTOMVERTEX cvVertices[] =
    {
        {  0.0f,  0.0f, 0.5f, 1.0f, 0.0f, 0.0f, }, // x, y, z, rhw, tu, tv

		{  (float)vp.Width,  0.0f, 0.5f, 1.0f, 1.0f, 0.0f, },
        {  (float)vp.Width, (float)vp.Height, 0.5f, 1.0f, 1.0f, 1.0f, },
        {  0.0f, (float)vp.Height, 0.5f, 1.0f, 0.0f, 1.0f},
    };
	m_dwSizeofVertices = sizeof(cvVertices);

    // Create the vertex buffer

    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_MANAGED, &m_pVB ) ) )
        return E_FAIL;

    VOID* pVertices;
    if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
        return E_FAIL;

    memcpy( pVertices, cvVertices, m_dwSizeofVertices);

    m_pVB->Unlock();

    
I am using Directx 8.1 and Visual C++. If anyone could help. I would appreciate it. Later, James Edited by - JSCFaith on December 3, 2001 8:51:46 AM
NightShade
NightShade
Hiya..
Off the top of my head (and only suggested because these are a couple of easy things to try), try one or both of the following:

Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0 );
Device->SetTextureStageState(0 ,D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
Device->SetTextureStageState(0 ,D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );

Really I thought these were the defaults, but it wouldn''t hurt to try. Also, add this to your FVF format:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0))

Hope this helps!

-ns-
JSCFaith
JSCFaith
THanks for the help, but sadly, it didn''t work. I have a Geforce 3 running WinXP, and this code doesn''t seem to work on my computer. Yet, on my friends computer it runs perfect. He has Geforce 2 Ultra, and Windows98. Could it possibly be WinXP? I thought XP supported DirectX 8.x fully, but maybe not. Well, thanks.
dankydoo
dankydoo
Try bumping your rhw down to .9
EbonySeraph
EbonySeraph
I see a syntactical problem which would prevent you from compiling...but why is "vertex" written after the semicolon after the rhw in your vertex struct definition?

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
JSCFaith
JSCFaith
Well, I found the problem. It is in the RestoreDeviceObjects in the common files. When I call Device->GetViewport(), it returns, D3DERR_INVALIDCALL. Here is the code I use to call it.

D3DVIEWPORT8 vp;
HRESULT hr = m_pd3dDevice->GetViewport(&vp);

Are you not supposed to call GetViewport in RestoreDeviceObjects()? If I can''t call GetViewport where can I?

Thanks.

xyzzy00
xyzzy00
If you want to be able to do any kind of "Get", make sure that you are not using the D3DCREATE_PUREDEVICE flag when you create your device.

xyzzy
JSCFaith
JSCFaith
THanks, that was the problem. If I use D3DCREATE_PUREDEVICE anyway, is there any other function I could use to get the Viewport Parameters?
Metron
Metron
Unfortunately all Get* functions are disable within the pure device. It is given to the user to keep track of all currently set render states, texture states etc.

Since you have to create the window or the full screen device by yourself when creating the device, you should now which viewport parameters have been used (ie, 800*600)... normally, it should not be difficult to keep track of that, too...
----------------------------------------http://www.sidema.be----------------------------------------
JSCFaith
JSCFaith
Ok, well thanks for all the help guys. I appreciate it.

Later,
James

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.