Jump to content
  • Advertisement
Sign in to follow this  
Endar

Correctly applying rotation matrix

This topic is 4227 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'm attempting to learn D3D at the moment by working through the tutorials that came with the Oct '06 SDK. I'm on the 3rd one, about creating and applying matrices.
float rotate = 0.0f;

void Render()
{
	// clear the back buffer to blue
	g_d3d9Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

	// begin the scene
	g_d3d9Device->BeginScene();

	// Create a DX matrix
	D3DXMATRIX matWorld;

	// set the world matrix back to the identity matrix
	D3DXMatrixIdentity(&matWorld);
	g_d3d9Device->SetTransform(D3DTS_WORLD, &matWorld );

	// set a continuous rotation on the y axis
	D3DXMatrixRotationY( &matWorld, (rotate*3.141f) / 180.0f );
	g_d3d9Device->SetTransform(D3DTS_WORLD, &matWorld );

	// increase rotation
	rotate += 1.05f;

	// DEBUG
	printf("rotate: %f\n", rotate);

	// reset matrix object back to identity
	D3DXMatrixIdentity( &matWorld );

	// get the current world transform
	g_d3d9Device->GetTransform(D3DTS_WORLD, &matWorld);

	for(int i=0; i < 4; ++i){
		for(int j=0; j < 4; ++j)
			printf("\t%f",  matWorld.m[j]);

		printf("\n");
	}

	printf("\n\n");
	// DEBUG
	

	// to render vertex buffers we need to set the stream source
	g_d3d9Device->SetStreamSource(	0,			// the stream number to use
					g_vertexBuffer,		// the pointer to the vertex buffer
					0,			// the offset from the pointer to the vertex data
					sizeof(CustomVertex) );	// the size of the vertex structure
	
	// define the vertex data layout
	g_d3d9Device->SetFVF( D3DFVF_CUSTOMVERTEX );

	// render the triangle
	g_d3d9Device->DrawPrimitive(	D3DPT_TRIANGLELIST,	// draw triangles
					0,			// the index of the first triangle
					1 );			// the number of triangles to draw (there is only 1 in the buffer)

	// end the scene
	g_d3d9Device->EndScene();

	// display the scene to the window
	g_d3d9Device->Present(NULL, NULL, NULL, NULL);

}



The vertex buffer draws a simple triangle. It's not being drawn around 0,0,0 and being translated, so as soon as the rotation starts working the triangle will be whipping around the origin instead of rotation on it's own axis. I'll fix that later. My problem is that nothing is happening. The triangle does not move at all. As you can see, after I apply the rotation matrix, I load the world matrix, and I can see that it does change with every change in the degree of rotation. What am I doing wrong? Edit:: Okay, I just changed the code a bit so it uses another matrix to set a translation matrix, and then I multiply the translation and rotation matrices together using 'D3DXMatrixMultiply', and I am drawing the triangle at a depth of 0.0f on the z axis and then it is being translated by the translation matrix. Still nothing. I also set the projection matrix (in the main function, not in the Render function), that I wasn't doing before. The triangle still just sits there. It's like D3D is just ignoring the world matrix. [Edited by - Endar on November 18, 2006 6:48:26 PM]

Share this post


Link to post
Share on other sites
Advertisement
You aren't using D3DFVF_XYZRHW are you?


BTW, the following lines of code are unnecessary because their actions immediately overridden by the code that follows them:
    // set the world matrix back to the identity matrix
D3DXMatrixIdentity(&matWorld);
g_d3d9Device->SetTransform(D3DTS_WORLD, &matWorld );

// reset matrix object back to identity
D3DXMatrixIdentity( &matWorld );

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
You aren't using D3DFVF_XYZRHW are you?

Yes, as a matter of fact. That's what the previous tutorial used for the vertices to draw the triangle, so I'm just attempting to add on to each tutorial.

<5 minutes later>

I just removed RHW and changed the vertices to exclude the RHW, so my vertex format is defined as 'D3DFVF_XYZ | D3DFVF_DIFFUSE', and I have changed the vertex structure accordingly.

Now, I can't see anything, even if I remove the rotation. It is a small improvement, because I'm getting a change.

Quote:
Original post by JohnBolton
BTW, the following lines of code are unnecessary because their actions immediately overridden by the code that follows them:
    // set the world matrix back to the identity matrix
D3DXMatrixIdentity(&matWorld);
g_d3d9Device->SetTransform(D3DTS_WORLD, &matWorld );

// reset matrix object back to identity
D3DXMatrixIdentity( &matWorld );

Yeah, I know, I was just trying to get it working and I thought that just putting that in was better than not putting it in and wondering if I was wrong about the functionality of the 'SetTransform' function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Quote:
Original post by JohnBolton
You aren't using D3DFVF_XYZRHW are you?

Yes, as a matter of fact. That's what the previous tutorial used for the vertices to draw the triangle, so I'm just attempting to add on to each tutorial.

<5 minutes later>

I just removed RHW and changed the vertices to exclude the RHW, so my vertex format is defined as 'D3DFVF_XYZ | D3DFVF_DIFFUSE', and I have changed the vertex structure accordingly.

Now, I can't see anything, even if I remove the rotation. It is a small improvement, because I'm getting a change.


If you want to rotate the triangle, then you should not use D3DFVF_XYZRHW, because D3DFVF_XYZRHW means that the vertices are already transformed and the graphics system will not transform them. I you want to rotate the triangle, then there are additional things that you must set up. See if you can find an appropriate tutorial rather than extending this one.

Share this post


Link to post
Share on other sites
And as was pointed out, you're resetting your matrix to identity anyways. Meaning all that math you do gets nullified before the DrawPrimitive line.

Also, you dont need to call SetTransform after every change you make. Get your matrix exactly how you want it, then call SetTransform, then draw.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
If you want to rotate the triangle, then there are additional things that you must set up. See if you can find an appropriate tutorial rather than extending this one.


It works!!

The things I changed were:

  • Making sure that the Z-buffer was enabled (I don't know if it is or isn't by default)
  • Setting the projection matrix
  • Setting the view matrix
  • Changing the ridiculous size of the triangle (I believe this was most of the problem)


And I now have a spinning triangle.

Thanks, guys.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!