0

I have created a vertex buffer which holds the vertices for a circle at (0, 0) with radius 2.5.

I want to draw this circle wherever the mouse is so I captured WM_MOUSEMOVE and I store the mouse's position in a point P. I've already setup by world, view, and projection upon creation of my Window (my look at is fine).

Upon window creation I did:

D3DXMATRIX World, View, Projection;
D3DXVECTOR3 Camera = {0.0f, 0.0f, 10.0f};
D3DXVECTOR3 LookAt = {0.0f, 0.0f, 0.0f};
D3DXVECTOR3 UpVector = {0.0f, 1.0f, 0.0f};

D3DXMatrixIdentity(&World);
D3DXMatrixLookAtLH(&View, &Camera, &LookAt, &UpVector);
D3DXMatrixPerspectiveFovLH(&Projection, D3DXToRadian(45), static_cast<float>(WindowWidth) / static_cast<float>(WindowHeight), 1.0f, 100.0f);

d3ddevice->SetTransform(D3DTS_WORLD, &World);
d3ddevice->SetTransform(D3DTS_VIEW, &View);
d3ddevice->SetTransform(D3DTS_PROJECTION, &Projection);

To draw the vertex buffer I do:

void DrawFilledCircle(float mx, float my, float r, D3DCOLOR colour)
{
    if (!vertexbuffer)
    {
        CreateVertexBuffer_FilledCircle(mx, my, r, colour, 10); //perfectly fine.
    }

    d3ddevice->SetFVF(VERTEX_FVF_TEX);
    d3ddevice->SetStreamSource(0, vertexbuffer, 0, sizeof(D3DVertex));
    d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 10 - 2);
}

void RenderD3D()
{
    d3ddevice->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 40, 100, 255), 1.0f, 0);
    d3ddevice->BeginScene();

        D3DXMATRIX World;
        D3DXMatrixIdentity(&World);
        D3DXMatrixTranslation(&World, P.x, P.y, 0);
        d3ddevice->SetTransform(D3DTS_WORLD, &World);

        DrawFilledCircle(P.x, P.y, 2.5, D3DCOLOR_RGBA(0xFF, 0x00, 0x00, 0xFF));

    d3ddevice->EndScene();
    d3ddevice->Present(nullptr, nullptr, nullptr, nullptr);
}

However, the transformations aren't happening. My circle is drawing fine but it is still drawn at (0, 0).

Any ideas why my transformations aren't being applied?

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • Did you check in the debugger that your mouseposition `P` contains valid data of your current position? You could try to set P to an arbitary value, for testing where the problem lies within - the drawing code or the positiontracking. – Gnietschow Feb 01 '14 at 15:23
  • Yes I checked that the value of P contains valid data. It contains (0, 0) at the start and as I move the mouse it updates fine.. – Brandon Feb 01 '14 at 16:16
  • One problem I see (although I think that it's not the root cause; just a contributing factor) is the mapping from pixels to world units. I guess, `P` is measured in pixels. For the circle to follow the mouse, you would need to translate these values to world units (based on the perspective transformation). At what depth are you drawing the circle? Maybe it moves, but so little that you can't recognize it? You are sure to redraw the scene, right? – Nico Schertler Feb 04 '14 at 23:25

1 Answers1

0

I am not sure what your d3ddevice->SetTransform does, but if you are translating both the world matrix and then doing an additional translation in your DrawFilledCircle method, then both of those will cancel out. Either don't translate the world or don't translate the circle. If this still doesn't work out, maybe give us more detail on the inner workings of those two methods.

Valentin
  • 1,731
  • 2
  • 19
  • 29