Jump to content

  • Log In with Google      Sign In   
  • Create Account

Tracing a ray to a certain X value


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
23 replies to this topic

#1 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 March 2012 - 01:34 AM

Hi all,

I'm trying to work on an editor for my game, and I'm having issues tracing a ray to a certain point.
On click, the Editor needs to trace the mouse ray to the Z point of 3, and add a 'plane' (rectangle) at that point.
It seems to be getting in the right sort of area on click, but its always off.
Posted Image
The red dot illustrates where I've clicked.
I've uploaded the files for you to play with (easier than me trying to explain)
http://jameswarner.c...ther/Editor.rar

And heres the code that handles the tracing of the ray and adding plane:
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
{
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;
D3DVIEWPORT9 viewPort;

d3ddev->GetViewport(&viewPort);

ScreenToClient(hWnd[whichForm], &mousePos);

RECT temp;
GetClientRect(hWnd[whichForm], &temp);

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));
}

Any help appreciated

Thanks,
James

Sponsor:

#2 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 March 2012 - 02:32 PM

Anyone?

#3 Evil Steve   Members   -  Reputation: 1982

Like
1Likes
Like

Posted 02 March 2012 - 07:41 AM

I downloaded the app and gave it a try - the picking is correct when picking the centre of a viewport, but gets more and more wrong as you pick towards the edges, which indicates that the viewport size or FOV is possibly incorrect (I.e. the pick values don't match the render values).

I notice you use GetViewport() to get the viewport - are you sure this is returning the same viewport as you're clicking in?

You could try manually hacking the viewport values until it looks correct - I.e. try a viewport that's 10px bigger in X and Y, or try a slightly different FOV or something similar. When you get a viewport that gives you the values you expect, it might be obvious what the problem is by how much you had to fudge the values.

Steve Macpherson
Systems Programmer

Rockstar North


#4 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 02 March 2012 - 09:02 PM

Using
RECT temp;
GetClientRect(hWnd[whichForm], &temp);
Gets a different window size to that of the viewport and the recorded window size I have. (recorded window size and viewport size are the same)
May that be the cause?

#5 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 03 March 2012 - 11:56 PM

Bump?

#6 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 04 March 2012 - 10:32 PM

Bump, would the backbuffer size affect this? Ive had issues with the backbuffer before so might that be it?

#7 Evil Steve   Members   -  Reputation: 1982

Like
0Likes
Like

Posted 05 March 2012 - 07:47 AM

Yes, definitely. Your backbuffer, client area, and viewport should all have the same dimensions. Are you making sure to use AdjustWindowRect when creating your windows? Remember that CreateWindow() takes the size of the window, which may be larger than the size of the client area.

E.g. if you had a "normal" WS_OVERLAPPED style window, and you passed 800,600 for the width and height to CreateWindow, you may end up with a client area of 792x554 because of the extra space taken up by the window title bar and border. If your backbuffer is still 800x600, then it'll be stretched to fit your window at Present() time which may blur your output, and will certainly throw off any picking values you're using.

Steve Macpherson
Systems Programmer

Rockstar North


#8 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 March 2012 - 12:43 PM

All the swap chain window sizes are converted and stored like so:
RECT wr = {0, 0, windowWidths[0], windowHeights[0]};    
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
screenSize[0].x = wr.right;
screenSize[0].y = wr.bottom;

wr.right = windowWidths[1]; wr.bottom = windowHeights[1];    
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
screenSize[1].x = wr.right;
screenSize[1].y = wr.bottom;

wr.right = windowWidths[2]; wr.bottom = windowHeights[2];        
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
screenSize[2].x = wr.right;
screenSize[2].y = wr.bottom;

wr.right = windowWidths[3]; wr.bottom = windowHeights[3];        
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
screenSize[3].x = wr.right;
screenSize[3].y = wr.bottom;

All the windows are initialised like so:
for(int i = 0; i < 4; i++)
{
HRESULT hr;
d3dpp.BackBufferHeight = screenSize[i].y;
d3dpp.BackBufferWidth = screenSize[i].x;
d3dpp.hDeviceWindow = hWnd[i];
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

if(i > 0)
hr = d3ddev->CreateAdditionalSwapChain(&d3dpp, &swapchains[i]);

}

Heres what happens when one of the windows resizes:
void Editor::updateBackBuffer(int width, int height, short which)
{
RECT wr = {0, 0, width, height};    
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); 
screenSize[which].x = wr.right;
screenSize[which].y = wr.bottom;

d3dpp.BackBufferHeight = screenSize[which].y;
d3dpp.BackBufferWidth = screenSize[which].x;
d3dpp.hDeviceWindow = hWnd[which];


hasWindowResized = true;
}
hasWindowResized basically tells it to call this:
void Editor::doWindowUpdate()
{
invalidateDeviceObjects();

d3dpp.BackBufferHeight = screenSize[0].y;
d3dpp.BackBufferWidth = screenSize[0].x;
d3dpp.hDeviceWindow = hWnd[0];

HRESULT hr = d3ddev->Reset(&d3dpp);

if(hr != D3D_OK)
{
MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
}

restoreDeviceObjects();

hasWindowResized = false;
}

Is there a way to check the backbuffer size while the app is running?

#9 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 06 March 2012 - 03:04 AM

Bump

#10 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 06 March 2012 - 12:46 PM

Bump again :/

#11 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 07 March 2012 - 01:44 AM

Ok I managed to get the BackBuffers size with the following code:
D3DSURFACE_DESC tempDesc;
pBackBuffer->GetDesc(&tempDesc);
Unluckily, they had the right size so its not an issue with the BackBuffer size.

#12 Evil Steve   Members   -  Reputation: 1982

Like
1Likes
Like

Posted 07 March 2012 - 05:38 AM

1. AdjustWindowRect() returns the window size for a given client area size. But it's the client area size you need for the backbuffer dimensions. What you're doing is asking for a e.g. 800x600 client area, and then setting the backbuffer size to e.g. 808x642.

2. You can't just use the right and bottom edges of the RECT updated by AdjustWindowRect - you need to do right-left and bottom-top, since the top and left values are also updated (And can contain negative values).

3. Please leave at least 24 hours between bumps of a thread.

Steve Macpherson
Systems Programmer

Rockstar North


#13 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 07 March 2012 - 12:44 PM

1. AdjustWindowRect() returns the window size for a given client area size. But it's the client area size you need for the backbuffer dimensions. What you're doing is asking for a e.g. 800x600 client area, and then setting the backbuffer size to e.g. 808x642.

2. You can't just use the right and bottom edges of the RECT updated by AdjustWindowRect - you need to do right-left and bottom-top, since the top and left values are also updated (And can contain negative values).

3. Please leave at least 24 hours between bumps of a thread.

Ok, I'll remember not to bump so often. I'll take a look into this later as I've got to go out now.

#14 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 08 March 2012 - 02:46 AM

1. AdjustWindowRect() returns the window size for a given client area size. But it's the client area size you need for the backbuffer dimensions. What you're doing is asking for a e.g. 800x600 client area, and then setting the backbuffer size to e.g. 808x642.


Okay, so I only need that value when resizing the backbuffer?

2. You can't just use the right and bottom edges of the RECT updated by AdjustWindowRect - you need to do right-left and bottom-top, since the top and left values are also updated (And can contain negative values).


This ones easy :P

Edit:
Still not working with this code:
Window Update function:
void Editor::doWindowUpdate()
{
	invalidateDeviceObjects();

	RECT wr = {0, 0, screenSize[0].x, screenSize[0].y};    
	AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); 
	d3dpp.BackBufferHeight = wr.bottom - wr.top;
	d3dpp.BackBufferWidth = wr.right - wr.left;
	d3dpp.hDeviceWindow = hWnd[0];

	HRESULT hr = d3ddev->Reset(&d3dpp);

	if(hr != D3D_OK)
	{
		MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
	}

	restoreDeviceObjects();

	hasWindowResized = false;
}

Backbuffer update code:
void Editor::updateBackBuffer(int width, int height, short which)
{
	screenSize[which].x = width;// = wr.right;
	screenSize[which].y = height;// = wr.bottom;

	RECT wr = {0, 0, screenSize[which].x, screenSize[which].y};    
	AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); 
	d3dpp.BackBufferHeight = wr.bottom - wr.top;
	d3dpp.BackBufferWidth = wr.right - wr.left;
	d3dpp.hDeviceWindow = hWnd[which];


	hasWindowResized = true;
}

Swap chain initialization code:
for(int i = 0; i < 4; i++)
	{
		HRESULT hr;

		RECT wr = {0, 0, screenSize[i].x, screenSize[i].y};    
		AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); 
		d3dpp.BackBufferHeight = wr.bottom - wr.top;
		d3dpp.BackBufferWidth = wr.right - wr.left;
		d3dpp.hDeviceWindow = hWnd[i];
		d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

		if(i > 0)
			hr = d3ddev->CreateAdditionalSwapChain(&d3dpp, &swapchains[i]);

	}
Window size code (at start)
screenSize[0].x = windowWidths[0];
	screenSize[0].y = windowHeights[0];
	screenSize[1].x = windowWidths[1];
	screenSize[1].y = windowHeights[1];
	screenSize[2].x = windowWidths[2];
	screenSize[2].y = windowHeights[2];
	screenSize[3].x = windowWidths[3];
	screenSize[3].y = windowHeights[3];


#15 Evil Steve   Members   -  Reputation: 1982

Like
0Likes
Like

Posted 08 March 2012 - 04:51 AM

You still want to use AdjustWindowRect() when creating the window. It's usually done something like (semi-psuedo-code):

void initWindow(int width, int height)
{
   // Get window dimensions that will give a client area of width x height
   RECT rcWindow;
   rcWindow.left = 0;
   rcWindow.top = 0;
   rcWindow.right = width;
   rcWindow.bottom = height;
   AdjustWindowRect(&rcWindow, WS_OVERLAPPEDWINDOW, FALSE);

   // Create window with adjusted dimensions
   int windowW = rcWindow.right - rcWindow.left;
   int windowH = rcWindow.bottom - rcWindow.top;
   HWND hWnd = CreateWindow(..., windowW, windowH, ...);

   // [D3D setup code here]

   // Setup backbuffer with client area width and height
   presentParams.BackBufferWidth = width;
   presentParams.BackBufferWidth = height;
   pD3D->CreateDevice(...)
}
And similarly for resizing the window. If it's working correctly, then GetClientRect() will give you a RECT with the exact same dimensions as your backbuffer.

Steve Macpherson
Systems Programmer

Rockstar North


#16 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 09 March 2012 - 01:22 AM

You still want to use AdjustWindowRect() when creating the window. It's usually done something like (semi-psuedo-code):

void initWindow(int width, int height) { // Get window dimensions that will give a client area of width x height RECT rcWindow; rcWindow.left = 0; rcWindow.top = 0; rcWindow.right = width; rcWindow.bottom = height; AdjustWindowRect(&rcWindow, WS_OVERLAPPEDWINDOW, FALSE); // Create window with adjusted dimensions int windowW = rcWindow.right - rcWindow.left; int windowH = rcWindow.bottom - rcWindow.top; HWND hWnd = CreateWindow(..., windowW, windowH, ...); // [D3D setup code here] // Setup backbuffer with client area width and height presentParams.BackBufferWidth = width; presentParams.BackBufferWidth = height; pD3D->CreateDevice(...) }
And similarly for resizing the window. If it's working correctly, then GetClientRect() will give you a RECT with the exact same dimensions as your backbuffer.


I never call CreateWindow(). The windows are managed by the Managed C++ code that handles the forms (one of the reasons all the rendering code is in an unmanaged DLL). I just take the window sizes given (and the hWnd of each window). Would that affect it?

#17 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 10 March 2012 - 12:36 AM

I think its been 24 hours so bump?
Edit: 1 hour out sorry...

#18 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 11 March 2012 - 01:12 PM

Bump again

#19 Bacterius   Crossbones+   -  Reputation: 8880

Like
0Likes
Like

Posted 11 March 2012 - 03:53 PM

I never call CreateWindow(). The windows are managed by the Managed C++ code that handles the forms (one of the reasons all the rendering code is in an unmanaged DLL). I just take the window sizes given (and the hWnd of each window). Would that affect it?

You should still make sure the client size (i.e. excluding titlebar and borders) is equal to your backbuffer, otherwise you will get some ugly downsampling and messed up picking as Evil Steve noted. I'm not sure what you would use in C++ but it should be something like "yourwindow.ClientSize = (800, 600)" in pseudocode.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#20 exextatic   Members   -  Reputation: 111

Like
0Likes
Like

Posted 11 March 2012 - 08:48 PM

I never call CreateWindow(). The windows are managed by the Managed C++ code that handles the forms (one of the reasons all the rendering code is in an unmanaged DLL). I just take the window sizes given (and the hWnd of each window). Would that affect it?

You should still make sure the client size (i.e. excluding titlebar and borders) is equal to your backbuffer, otherwise you will get some ugly downsampling and messed up picking as Evil Steve noted. I'm not sure what you would use in C++ but it should be something like "yourwindow.ClientSize = (800, 600)" in pseudocode.


When the 'window' is resized, the managed C++ code puts the new size (excluding bars) into the directx code to resize window.. I think it always has the correct window size.




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