Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Help with transform

Started by jonathanc Oct 1, 2008 at 1:49 PM 3 replies 500+ views
Original Post
jonathanc
jonathanc
Hi all, I am a beginner learning DirectX and I seem to have a problem in transformation. I managed to create a 6 vertice vertex buffer (IDirect3DVertexBuffer9* pVB) for a triangle, as shown below:

Device->CreateVertexBuffer( 6*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,   D3DPOOL_MANAGED, &pVB, NULL ); 
CUSTOMVERTEX *v;
pVB->Lock( 0, 0, (void**)&v, 0 );
v[0]=CUSTOMVERTEX(-1.0f, -1.0f, 2.0f, D3DCOLOR_XRGB(255,0,0));
v[1]=CUSTOMVERTEX(-1.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(0,255,0));
v[2]=CUSTOMVERTEX(1.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(0,0,255));
v[3]=CUSTOMVERTEX(1.0f, -1.0f, 2.0f, D3DCOLOR_XRGB(255,0,0));
v[4]=CUSTOMVERTEX(-1.0f, -1.0f, 2.0f, D3DCOLOR_XRGB(0,255,0));
v[5]=CUSTOMVERTEX(1.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(0,0,255));
pVB->Unlock();
I managed to render both parts of the triangles but when I try to transform the second one along x, the first one gets transformed too. Is there anyway around this? Tried playing with SetStreamSource but its not working out.

Device->BeginScene();

Device->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);
Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
Device->SetFVF( D3DFVF_CUSTOMVERTEX );
Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
		
D3DXMATRIX mTrans;
D3DXMatrixTranslation( &mTrans, 1, 0, 0);	// Having problems here	
Device->SetTransform( D3DTS_WORLD, &mTrans );
Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
Device->SetFVF( D3DFVF_CUSTOMVERTEX );
Device->DrawPrimitive( D3DPT_TRIANGLELIST, 3, 1 ); 	
	
Device->EndScene();
Device->Present(0,0,0,0); 
edit: not working here?
Evil Steve
Evil Steve
You need to reset the matrix after the second triangle, or before the first. The last matrix set will stay set till you change it, or you Reset() the device.

Add the following after your second DrawPrimitive() call:
D3DXMatrixTranslation( &mTrans, 0, 0, 0);Device->SetTransform( D3DTS_WORLD, &mTrans );
jonathanc
jonathanc
Quote:
Original post by Evil Steve
You need to reset the matrix after the second triangle, or before the first. The last matrix set will stay set till you change it, or you Reset() the device.

Add the following after your second DrawPrimitive() call:
D3DXMatrixTranslation( &mTrans, 0, 0, 0);Device->SetTransform( D3DTS_WORLD, &mTrans );


ah hah! That worked! Thanks!

Also , I would only have to do this is I am using vertices from the same vertex buffer right? If I declared another vertex buffer (ie for quads), this doesn't apply?
Evil Steve
Evil Steve
Quote:
Original post by jonathanc
Also , I would only have to do this is I am using vertices from the same vertex buffer right? If I declared another vertex buffer (ie for quads), this doesn't apply?
Nope, it applies to any primitives you render on that device (And you should only have one device, so it applies to all primitives).
jonathanc
jonathanc
Quote:
Original post by Evil Steve
Quote:
Original post by jonathanc
Also , I would only have to do this is I am using vertices from the same vertex buffer right? If I declared another vertex buffer (ie for quads), this doesn't apply?
Nope, it applies to any primitives you render on that device (And you should only have one device, so it applies to all primitives).


hmm ok, I see. How about scaling? How do I "reset" it?

This is what I have got so far:

ClearBuffer(D3DCOLOR_XRGB(0,0,0));

// Begin the scene
Device->BeginScene();
Device->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);
Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
Device->SetFVF( D3DFVF_CUSTOMVERTEX );
Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );

x += timeDelta/5;
D3DXMATRIX mScale;
D3DXMATRIX mTrans;
D3DXMATRIX mWorldTri2Matrix;
if (Scale <= 0) Scale = 0;
D3DXMatrixScaling( &mScale, Scale, Scale, 0);
D3DXMatrixTranslation( &mTrans, x, 0, 0);
mWorldTri2Matrix = mScale * mTrans;
Device->SetTransform( D3DTS_WORLD, &mWorldTri2Matrix );
Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
Device->SetFVF( D3DFVF_CUSTOMVERTEX );
Device->DrawPrimitive( D3DPT_TRIANGLELIST, 3, 1 );
D3DXMatrixTranslation( &mTrans, 0, 0, 0);
Device->SetTransform( D3DTS_WORLD, &mTrans );

Device->EndScene();
Device->Present(0,0,0,0);

I have declared Scale variable as 0 and it increases/decreases with VM_UP and DOWN. However I noticed one small problem, the second transformed primitive is invisible (I guess this is because Scale == 0) but if I set it to 1 it is twice as large. Any ideas?

Topic Locked

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

Sign in to reply to this topic.