Sign in to follow this  
Kagedbeast

Matrix Transformation not being applied

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

#define VERTEXFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct VERTEX
{
	D3DXVECTOR3 pos;    // from the D3DFVF_XYZ flag
	float rhw;
	DWORD color;    // from the D3DFVF_DIFFUSE flag
	VERTEX(){};
	VERTEX(FLOAT x, FLOAT y, FLOAT z, DWORD aColor) :
	pos(x, y, z),  rhw(1.0f), color(aColor)
	{	}
	VERTEX(FLOAT x, FLOAT y, FLOAT z, FLOAT arhw, DWORD aColor) :
	pos(x, y, z), rhw(arhw), color(aColor)
	{	}
};

init{
	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));
	v_buf->Unlock(); 
}

updateMousePosition{
	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;
}

draw{
	engine->d3ddev->SetFVF(VERTEXFVF);
	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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this