Jump to content

  • Log In with Google      Sign In   
  • Create Account

[D3D9] 'Drawing' into window


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 12 February 2012 - 09:41 PM

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:
Posted Image
Note, it has 3D views because I want this editor to be re-usable in the future.

Any help appreciated,

Thanks,
James Warner

Sponsor:

#2 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 13 February 2012 - 08:56 PM

Bump.. Anyone able to help?

#3 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 14 February 2012 - 06:41 AM

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.

Steve Macpherson
Systems Programmer

Rockstar North


#4 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 15 February 2012 - 02:45 AM

Cool thanks, will look into it when I fix the rendering engine part :P

#5 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 16 February 2012 - 03:04 AM

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.

#6 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 16 February 2012 - 08:53 PM

Bump.

#7 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 16 February 2012 - 11:57 PM

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.

#8 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 17 February 2012 - 08:06 PM

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.
Posted Image

#9 Evil Steve   Members   -  Reputation: 1987

Like
1Likes
Like

Posted 21 February 2012 - 08:07 AM

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.

Steve Macpherson
Systems Programmer

Rockstar North


#10 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 22 February 2012 - 02:25 AM

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.

#11 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 25 February 2012 - 05:13 PM

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:
Posted Image

Any ideas?

#12 exextatic   Members   -  Reputation: 111

Like
-2Likes
Like

Posted 26 February 2012 - 12:48 PM

Bump

#13 exextatic   Members   -  Reputation: 111

Like
-1Likes
Like

Posted 26 February 2012 - 10:01 PM

Bump

#14 exextatic   Members   -  Reputation: 111

Like
-1Likes
Like

Posted 27 February 2012 - 09:03 PM

Bump again :/

#15 exextatic   Members   -  Reputation: 111

Like
-1Likes
Like

Posted 28 February 2012 - 10:32 PM

Bump again :/

#16 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 29 February 2012 - 11:58 PM

Bump, and who went through all my bumps downvoting them? lol




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS