# Can't get matrices to work D3D [solved]

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

## Recommended Posts

I've got some experience with OGL and decided to check out DirectX & D3D. Orthographic mode works fine for me, but I can't get the matrices to DO anything (world, view, proj). I've tried following the "tutorials" in the DirectX sdk manual to the letter, and the matrices are constructed correctly (as seen in debugger), but they aren't actually changing my render at all and I can't figure out what else I'm missing. Graphics constructor (D3D initialization):
Graphics::Graphics(HWND hWnd)
{
if( NULL == ( mD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
throw E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &mD3DDevice ) ) )
throw E_FAIL;

text = new Text(this->mD3DDevice);
}


Graphics Render method:
void Graphics::Render()
{
mD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);
mD3DDevice->BeginScene();
// draw shit

D3DXMATRIX perspmatrix;
D3DXMatrixPerspectiveFovLH(&perspmatrix, 3.14159 / 3, 4.f / 3.f, 0.1f, 100.f);
mD3DDevice->SetTransform(D3DTS_PROJECTION, &perspmatrix);

D3DXVECTOR3 eye(0.f, 0.f, -5.f), at(0.f, 0.f, 1.f), up(0.f, 1.f, 0.f);

D3DXMATRIX viewmatrix;
D3DXMatrixLookAtLH(&viewmatrix, &eye, &at, &up);
mD3DDevice->SetTransform(D3DTS_VIEW, &viewmatrix);

D3DXMATRIX* world = new D3DXMATRIX();
D3DXMatrixTranslation(world, 200.f, 20.f, .0f);
graphics->mD3DDevice->SetTransform(D3DTS_WORLD, world);

currentState->Render();
text->Render();

mD3DDevice->EndScene();
mD3DDevice->Present( NULL, NULL, NULL, NULL );
}


currentState->Render() (called during the graphics class render loop, inside beginscene and endscene)
void PlayState::Render()
{
graphics->mD3DDevice->SetStreamSource( 0, mVB, 0, sizeof(CUSTOMVERTEX) );
graphics->mD3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
graphics->mD3DDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
graphics->mD3DDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 2 );
graphics->mD3DDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 8, 2 );
graphics->mD3DDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
}


text->Render() (called during the graphics class render loop, inside beginscene and endscene)
void Text::Render()
{
std::map<std::string, Message>::iterator it = mMessages.begin();
std::map<std::string, Message>::iterator end = mMessages.end();
while (it != end)
{
mFont->DrawText(0,        //pSprite
it->second.message,      //pString
-1,          //Count
&(it->second.rect),  //pRect
DT_LEFT|DT_WORDBREAK, //Format,
it->second.color); //Color
++it;
}
}


I'd really appreciate any help - I have the feeling I'm missing 1 method to set some system setting or something like that. [Edited by - c0uchm0nster on May 30, 2008 1:52:29 AM]

##### Share on other sites
Does your FVF use D3DFVF_XYZRHW for position? If so, it shouldn't [smile]

Regards,
ViLiO

##### Share on other sites
Looks like your eyept (at z=-1) is looking at z=1, which is okay, but you're setting up the world matrix to draw everything at x=200. You're probably not seeing anything. Depends on how you have your vertices setup, of course.

First thing to try: don't set the world matrix. It will default to (0,0,0) and draw everything translated to the origin (or not translated at all, really).

Do you set up your vertices around the origin?

Are the triangles oriented for viewing CW? I think OGL culls CW and shows CCW (it's been a while - can't remember). Normal DirectX culls CCW.

Second thing: set the cullmode to CULL_NONE to ensure you always see your objects. When all appears correctly, change that to CULL_CCW (or CW if you prefer).

Alternate "second thing": change your eyept to some +z and look at -z to view your triangles from the other direction.

Hmm. Are your triangles facing +/-z? If they're oriented in the z-plane, you won't see them.

In general, DirectX is very much like OGL, except that it's very different in most respects.

##### Share on other sites
Quote:
 Original post by ViLiODoes your FVF use D3DFVF_XYZRHW for position? If so, it shouldn't [smile]Regards,ViLiO

I am using that:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

What should I use instead (or better yet, where can i read some explanation about this stuff, since I don't get why I have to define my own custom type anyways).

As for the weird values in the matrices those were just different extremes I was trying to get some visible difference, everything is still projecting as if it's orthographic, with an identity for world matrix.

thanks

##### Share on other sites
Quote:
 Original post by c0uchm0nsterWhat should I use instead (or better yet, where can i read some explanation about this stuff, since I don't get why I have to define my own custom type anyways).
D3DFVF

The key thing being that D3DFVF_XYZRHW includes the position of a "transformed vertex". This means you are telling D3D that your vertex is fine as it is and don't transform it further. D3DFVF_XYZ should be what you are after.

All the DirectX documentation also comes with your DirectX SDK in a handy searchable .chm file. It is a seriously good reference [smile]

All the best,
ViLiO

##### Share on other sites
Great! Thanks very much both of you... now I just have to figure out where they all disappeared to lol (and yes, I got rid of the translation by 200)

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632167
• Total Posts
3004527

×