Jump to content
  • Advertisement
Sign in to follow this  

Matrix Transformation not being applied

This topic is 3124 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

So I've been struggling with this all day, searching the forums here and elsewhere, and can't seem to get matrix transformations to get applied to the world. In my program I want to click at a point, remember it and then as I move the mouse it will draw a line between my original point and the mouse. Lines are thin though so I want the line to be a quad that is a little thicker (6 pixels or so wide). How I'm doing this is first creating 4 vertices, putting them in a vertex buffer, and then every time the mouse is dragged I figure out the length of the line, what location it should be at and its angle and do the appropriate scaling/rotation/translation and store it in a matrix in my class. Then when I draw I set the that matrix as the transformation on the world and draw the quad. The problem I'm having is that the line isn't being drawn where I want, it's showing up in its untransformed location in the top left corner.
class variables:
	D3DXMATRIX matTransform; //the transformation matrix we will use to draw the line
	D3DXMATRIX identity; //identity matrix used to reset world transformations
	const D3DXVECTOR2 &mousePos; //the current mouse position
	D3DXVECTOR2 anchor; //the start point when the mouse is clicked
	LPDIRECT3DVERTEXBUFFER9 v_buf; //the buffer used to store the line in progress for drawing

struct VERTEX
	D3DXVECTOR3 pos;    // from the D3DFVF_XYZ flag
	float rhw;
	DWORD color;    // from the D3DFVF_DIFFUSE flag
	pos(x, y, z),  rhw(1.0f), color(aColor)
	{	}
	pos(x, y, z), rhw(arhw), color(aColor)
	{	}

	VOID *pVoid;
	float thickness = 5.0f;
	struct VERTEX lineInProgress[4];
	lineInProgress[0] = struct VERTEX(0.0f, -thickness, 0.0f, 0xA00000FF);
	lineInProgress[1] = struct VERTEX(1.0f, -thickness, 0.0f, 0xA00000FF);
	lineInProgress[2] = struct VERTEX(0.0f, thickness, 0.0f, 0xA00000FF);
	lineInProgress[3] = struct VERTEX(1.0f, thickness, 0.0f, 0xA00000FF);

	engine->d3ddev->CreateVertexBuffer(4*sizeof(VERTEX), 0, VERTEXFVF, D3DPOOL_MANAGED,&v_buf,NULL);

	// locks v_buffer, the buffer we made earlier
	v_buf->Lock(0, 0, (void**)&pVoid, 0);
	// copy vertices to the vertex buffer
	memcpy(pVoid, lineInProgress, sizeof(lineInProgress));

	D3DXMATRIX matScale, matTranslate,  matRotation;
	float length;
	double angle;

	//find the length and angle of our line
	angle = atan2(anchor.y - mousePos.y, anchor.x - mousePos.x);
	length = sqrt((anchor.x - mousePos.x)*(anchor.x - mousePos.x) + (anchor.y - mousePos.y)*(anchor.y - mousePos.y));

	POUT("Length: " << length << "   Angle: " << angle);
	//this will stretch the line to the correct length
	D3DXMatrixScaling(&matScale, length, 1.0f, 1.0f);
	//we already have the angle we want
	D3DXMatrixRotationZ(&matRotation, (float)angle);
	//set the translation based on anchor point
	D3DXMatrixTranslation(&matTranslate, anchor.x, anchor.y, 0.0f);

	//now our transformation matrix has the correct values
	matTransform = matScale * matRotation * matTranslate;

	engine->d3ddev->SetTransform(D3DTS_WORLD, &matTransform);
	engine->d3ddev->SetStreamSource(0, v_buf, 0, sizeof(VERTEX));
	engine->d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
	engine->d3ddev->SetTransform(D3DTS_WORLD, &identity);

Anyone have any idea on what might be going wrong?

Share this post

Link to post
Share on other sites
Since you are using a pretransformed vertex type (D3DFVF_XYZRHW), all transformations will be ignored, that's why.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!