• Advertisement
Sign in to follow this  

[D3D9] 'Drawing' into window

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

Hi all,

Working on a level editor for my engine (still), and I'm at a loss as to how I should go about this next bit.
I want to have the user click, drag and release to make a box (which will be turned into a plane - 2D engine).
I can't seem to get the mouse coordinates to relate to the position in the 3d view.
Heres an image just so you guys can see what I've currently got:
sy3w2u.png
Note, it has 3D views because I want this editor to be re-usable in the future.

Any help appreciated,

Thanks,
James Warner

Share this post


Link to post
Share on other sites
Advertisement
In what way can you not get the mouse coordinates to relate to 3D positions? You can use D3DXVec3Unproject() to convert from screen space to world space.

Share this post


Link to post
Share on other sites
Okay fixed the rendering engine (finally).

Heres the code I have currently for adding planes:
void Editor::addPlane(D3DXVECTOR3 oldMPos, D3DXVECTOR3 oldMDir, string texName, float* texScale, int whichForm)
{
D3DXMATRIX matView;
if(whichForm == 0)
{
fpsCamera->calculateViewMatrix(&matView);
cameraPos[whichForm] = fpsCamera->getPosition();
cameraEyePos[whichForm] = fpsCamera->getEyePos();
}
else
{
D3DXMatrixLookAtLH(&matView,
&cameraPos[whichForm],
&cameraEyePos[whichForm],
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
}
d3ddev->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45),
(FLOAT)screenSize[whichForm].x / (FLOAT)screenSize[whichForm].y,
1.0f,
100.0f);
d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection);


D3DXVECTOR3 finalPos, size, firstMDir = oldMPos, curMDir = pickRaysOrigin[whichForm];

//Get position
bool interpolatedOnY = false;
for(int i = 0; i < 100; i++)
{
if(firstMDir.y > 3.0f)
{
interpolatedOnY = true;
break;
}

firstMDir += oldMDir;
}

if(!interpolatedOnY)
return; // Wasn't available on Y axis - User managed to get Camera not to face right axis?

finalPos = firstMDir;
interpolatedOnY = false;

//Get size
for(int i = 0; i < 100; i++)
{
if(curMDir.y > 3.0f)
{
interpolatedOnY = true;
break;
}

curMDir += pickRaysDir[whichForm];
}

if(!interpolatedOnY)
return; // Wasn't available on Y axis - User managed to get Camera not to face right axis?

if(firstMDir > curMDir)
size = firstMDir - curMDir;
else
size = curMDir - firstMDir;

addPlane(D3DXVECTOR2(finalPos.x, finalPos.y), D3DXVECTOR2(size.x, size.y), 0.0f, PhysDefinition(NoPhysics, 0.0f, 0.0f, 0.0f), ForeGroundPlane, TextureDefinition(texName, D3DXVECTOR2(texScale[0], texScale[1]), 0.0f));
}

It just ends up in the entirely wrong place.

Share this post


Link to post
Share on other sites
void Editor::addPlane(D3DXVECTOR3 oldMPos, D3DXVECTOR3 oldMDir, string texName, float* texScale, int whichForm)
{
D3DXMATRIX matView;
if(whichForm == 0)
{
fpsCamera->calculateViewMatrix(&matView);
cameraPos[whichForm] = fpsCamera->getPosition();
cameraEyePos[whichForm] = fpsCamera->getEyePos();
}
else
{
D3DXMatrixLookAtLH(&matView,
&cameraPos[whichForm],
&cameraEyePos[whichForm],
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
}
d3ddev->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45),
(FLOAT)screenSize[whichForm].x / (FLOAT)screenSize[whichForm].y,
1.0f,
100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

POINT mousePos;
GetCursorPos(&mousePos);

D3DXVECTOR3 mouseOut, mouseOut2, mouseFinal;
D3DVIEWPORT9 viewPort;
D3DXMATRIX matWorld;

d3ddev->GetTransform(D3DTS_WORLD, &matWorld);
d3ddev->GetViewport(&viewPort);

ScreenToClient(hWnd[whichForm], &mousePos);

D3DXVec3Unproject(&mouseOut, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MinZ), &viewPort, &matProjection, &matView, &matWorld);
D3DXVec3Unproject(&mouseOut2, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MaxZ), &viewPort, &matProjection, &matView, &matWorld);

float wz = 3.0f;
float wx = ((wz - mouseOut.z) * (mouseOut2.x - mouseOut.x)) / (mouseOut2.z - mouseOut.z) + mouseOut.x;
float wy = ((wz - mouseOut.z) * (mouseOut2.y - mouseOut.y)) / (mouseOut2.z - mouseOut.z) + mouseOut.y;

mouseFinal = D3DXVECTOR3(wx, wy, wz);

addPlane(D3DXVECTOR2(mouseFinal.x, mouseFinal.y), D3DXVECTOR2(1.0f, 1.0f), 0.0f, PhysDefinition(NoPhysics, 0.0f, 0.0f, 0.0f), ForeGroundPlane, TextureDefinition(texName, D3DXVECTOR2(texScale[0], texScale[1]), 0.0f));
}

Thats my new code to try out (still not working, but slightly better). I would upload the editor files for you to see for your self, but my ISP has my internet connection capped down to 25KB/s for the next 2 days.

Share this post


Link to post
Share on other sites
I've uploaded the editor so you guys can see what happens (Make a new Map file, and right click on the bottom right hand render screen)
http://jameswarner.co.cc/Files/Other/Editor.rar
Heres what happens when I right click (on the red dot). I clicked a few times and it seems to alternate between the 2 different positions that the Planes spawn at.
152l301.png

Share this post


Link to post
Share on other sites
I don't have time to look in-depth just now, but I'd suggest printing out the various coordinates fed in and out of your picking code to see why the position is flickering between two locations like that. If you can get it so that it's in one position, that'll make it easier to narrow down. Then, if it's still wrong by a fixed offset, check that the top left coordinate of the viewport is correct, that your input mouse coordinates are relative to the viewport and not in screen space, and so on.

Share this post


Link to post
Share on other sites

I don't have time to look in-depth just now, but I'd suggest printing out the various coordinates fed in and out of your picking code to see why the position is flickering between two locations like that. If you can get it so that it's in one position, that'll make it easier to narrow down. Then, if it's still wrong by a fixed offset, check that the top left coordinate of the viewport is correct, that your input mouse coordinates are relative to the viewport and not in screen space, and so on.

Thats made me think, is there a viewport per-swapchain? Im just using GetViewport() to get a viewport, and using that in the D3DXVec3Unproject()
Also, I think I've fixed it from flickering between places (restores the world matrix from when the app started instead of using the current world matrix).
Its still off from the mouse position though.

Share this post


Link to post
Share on other sites
Ok, gonna post some more code and upload the latest version for you guys..
Heres the picking code at the moment:</p>
void Editor::addPlane(D3DXVECTOR3 oldMPos, D3DXVECTOR3 oldMDir, string texName, float* texScale, int whichForm)
{
D3DXMATRIX matView, matWorld;
d3ddev->SetTransform(D3DTS_WORLD, &matStart);
matWorld = matStart;

if(whichForm == 0)
{
fpsCamera->calculateViewMatrix(&matView);
cameraPos[whichForm] = fpsCamera->getPosition();
cameraEyePos[whichForm] = fpsCamera->getEyePos();
}
else
{</div>
D3DXMatrixLookAtLH(&amp;matView,
&cameraPos[whichForm],
&cameraEyePos[whichForm],
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
}
d3ddev->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&amp;matProjection,
D3DXToRadian(45),
(FLOAT)screenSize[whichForm].x / (FLOAT)screenSize[whichForm].y,
1.0f,
100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

POINT mousePos;
GetCursorPos(&mousePos);

D3DXVECTOR3 mouseOut, mouseOut2;
D3DVIEWPORT9 viewPort;

d3ddev->GetViewport(&viewPort);

ScreenToClient(hWnd[whichForm], &mousePos);

D3DXVec3Unproject(&mouseOut, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MinZ), &viewPort, &matProjection, &matView, &matWorld);
D3DXVec3Unproject(&mouseOut2, &D3DXVECTOR3(mousePos.x, mousePos.y, viewPort.MaxZ), &viewPort, &matProjection, &matView, &matWorld);

float wz = 3.0f;
float wx = ((wz - mouseOut.z) * (mouseOut2.x - mouseOut.x)) / (mouseOut2.z - mouseOut.z) + mouseOut.x;
float wy = ((wz - mouseOut.z) * (mouseOut2.y - mouseOut.y)) / (mouseOut2.z - mouseOut.z) + mouseOut.y;
D3DXVECTOR3 mouseFinal = D3DXVECTOR3(wx, wy, wz);

addPlane(D3DXVECTOR2(mouseFinal.x, mouseFinal.y), D3DXVECTOR2(1.0f, 1.0f), 0.0f, PhysDefinition(NoPhysics, 0.0f, 0.0f, 0.0f), ForeGroundPlane, TextureDefinition(texName, D3DXVECTOR2(texScale[0], texScale[1]), 0.0f));
}

Heres the latest build:
http://jameswarner.co.cc/Files/Other/Editor.rar
And heres whats happening currently:
33mmjwo.png

Any ideas?

Share this post


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

  • Advertisement