Jump to content

  • Log In with Google      Sign In   
  • Create Account

Drawing coordinates system axes in corner of screen


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

#1 adi0149   Members   -  Reputation: 106

Like
0Likes
Like

Posted 23 May 2013 - 08:16 AM

Hi! I'd like to start off saying i don't have any programming training and very few knowledge, but i have messed around with several programming languages, designing tools to aid my everyday job (so i am a total amateur).

 

This time i'm working on something harder - it's a direct3d - mfc software and i am having lots of problems with it, since directx seems a bit hard.

 

Anyways... I have a direct3d environment in a mfc window and i would like to draw the coordinate system axes in the corner of the screen, as any 3d software has. I thought this would be no problem, since i found this topic, and that worked great but problems  occurred  when i started moving the camera around. I need the object to appear to be in the same spot, no matter how i pan, zoom or rotate the world (actually, i do these operations on the view matrix).

 

But it seems that i'm doing something wrong, and i was hoping someone could point me on the right direction, since the object i'm drawing is not scaled accordingly when i zoom, but it works perfect when panning or rotating.

 

I have also put up a youtube video to show you the symptoms.

 

This is my code for drawing the object:

 

void CDEMView::DrawSomeBox()
{
	// Define the needed matrices - object world, view and project 
	D3DXMATRIX matObjectWorld;
        D3DXMatrixIdentity (&matObjectWorld);	// object world matrix
	D3DXMATRIX matView;				
        D3DXMatrixIdentity (&matView);		// view matrix
	D3DXMATRIX matProjection;	
        D3DXMatrixIdentity (&matProjection);	// projection matrix
	
	// Get the needed matrices
	_device->GetTransform(D3DTS_VIEW, &matView);
	_device->GetTransform(D3DTS_PROJECTION, &matProjection);

	// Get the viewport
	D3DVIEWPORT9 viewport;
	_device->GetViewport(&viewport);

	// Get the center point of the object		
	D3DXVECTOR3* p_centerPoint = BoxCenterVector; // this is from an external variable

	// Get the point on the creen that is the screen projection of the object
	D3DXVECTOR3 projectPoint;
	D3DXVec3Project(&projectPoint, p_centerPoint ,&viewport, &matProjection, &matView, &matObjectWorld);

	// choose the screen point where the object is to be drawn, relative to the Viewport's dimensions
	D3DXVECTOR3 screenPoint;
	screenPoint.x = 0.1*viewport.Width;	// x position (horizontal) is 10% of the width of the screen (0% is left, 100% is right)
	screenPoint.y = 0.9*viewport.Height;	// y position (vertical) is 90% of the height of the screen (0% is top, 100% is bottom)
	screenPoint.z = projectPoint.z;		// 1-projectPoint.z*60/(-zoom);

	//transform the screen position to a world position
	D3DXVECTOR3 worldPoint;
	D3DXVec3Unproject( &worldPoint, &screenPoint, &viewport, &matProjection, &matView, &matObjectWorld );

	// now define how much to translate the box in order to get it to the point we want it to be (WorldPoint)
	float transX, transY, transZ;
	transX = worldPoint.x;
	transY = worldPoint.y;
	transZ = worldPoint.z;
	
	// define a mesh to store the object into and create the object
	ID3DXMesh* _SomeBox;
	float boxSize = 2.0f;
	D3DXCreateBox(_device,boxSize,boxSize,boxSize,&_SomeBox,NULL);

	// define a material and set its color
	D3DMATERIAL9 mat;

	// Set the RGBA for diffuse reflection.
	mat.Diffuse.r = 255;
	mat.Diffuse.g = 0;
	mat.Diffuse.b = 0;
	mat.Diffuse.a = 0.5;

	_device->SetMaterial(&mat);
	_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // D3DFILL_SOLID

	// apply the translation matrix
	D3DXMatrixTranslation(&matObjectWorld, transX, transY, transZ);
	_device->SetTransform(D3DTS_WORLD, &matObjectWorld);

	// draw the object
	_SomeBox->DrawSubset(0);
	// release the mesh
	_SomeBox->Release();

	// some debugging variables
	debug1 = transX;
	debug2 = transY;
	debug3 = transZ;
	debug4 = boxSize;
	//debug5 = scaleFactor;
	manole = 0;
}

 

Any help would be greatly appreciated. Thanks!

 

Edit: made a small change to the code to reflect my software.


Edited by adi0149, 23 May 2013 - 02:43 PM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13629

Like
1Likes
Like

Posted 23 May 2013 - 04:54 PM

To draw the red cube:

#1: Set D3DTS_VIEW back to identity.

#2: Create an orthographic projection matrix.

#3: Set the scale of the cube to x = (1.0f / ScreenWidth), y = (1.0f / ScreenHeight).

#4: Set the rotation of the cube to equal that of the rotation of the world-view matrix that you used to render the first time (copy the 3×3 part of the world-view matrix).

#5: Position the cube in the lower-left of the screen (screen coordinates).

#6: Profit.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 adi0149   Members   -  Reputation: 106

Like
0Likes
Like

Posted 24 May 2013 - 03:03 AM

Thanks for the reply!

 

I  applied  your changes as follows:

 

... everything is unchanged up to the point where i found transX, transY, transZ. Now it follows:

 

        // #1: Set D3DTS_VIEW back to identity.
        D3DXMATRIX matView2; D3DXMatrixIdentity (&matView2);

        // #2: Create an orthographic projection matrix.
	D3DXMATRIX P;
	D3DXMatrixOrthoLH( &P, viewport.Width/20, viewport.Height/20, 1.0f, 100.0f );
	_device->SetTransform(D3DTS_PROJECTION, &P);

	// #3: Set the scale of the cube to x = (1.0f / ScreenWidth), y = (1.0f / ScreenHeight).
	D3DXMATRIX mScale; D3DXMatrixIdentity(&mScale);
	D3DXMatrixScaling( &mScale, (1.0f / viewport.Width), (1.0f / viewport.Height), 1 );

	D3DXMatrixMultiply( &matObjectWorld, &matObjectWorld, &mScale );

	// #4: Set the rotation of the cube to equal that of the rotation of the world-view matrix that you used to render the first time (copy the 3×3 part of the world-view matrix).
	for (int i=1; i<4; i++)
	{
		for (int j=1; j<4; j++)
		{
			matView2.m[i][j] = matView.m[i][j];
		}
	}
	_device->SetTransform(D3DTS_VIEW, &matView);

       // ... i create the cube here ...

	// #5: Position the cube in the lower-left of the screen (screen coordinates).
	D3DXMatrixTranslation(&matObjectWorld, transX, transY, transZ);
	_device->SetTransform(D3DTS_WORLD, &matObjectWorld);

 

If i do this, it works but all the scene is rendered in an orthographic projection and i lose perspective of things. So i decided to add 

_device->SetTransform(D3DTS_PROJECTION, &matProjection);

 

just after i render the cube so everything else is kept in the initial perspective projection (i draw the cube just at the end of the scene, so everything is in perspective and just the cube is in ortho). However, if i do this, then the cube changes position when zooming, appearing to go towards the origin along Z axis when zooming in and  vice-versa  when zooming out.

 

So.. the thing is i would like to keep my perspective projection on all things (maybe except the cube, which looks better when is in ortho), but this doesn't work as supposed to. If you'd like, i can put up a new video to express the symptoms better.

 

Thanks again for the reply!



#4 Jason Z   Crossbones+   -  Reputation: 5086

Like
0Likes
Like

Posted 24 May 2013 - 07:55 AM

I think the problem is that you are treating the cube like it is part of your overall scene.  Essentially what you want to do is take the transformation information from the object you are manipulating, and remove the scaling and translation information.  You only want the rotation part of your transform to be applied to the little cube.  So you build the world matrix from only that part of the transform of the manipulated object.

 

Once you have the transform of the cube built, then you just want to render it in a constant location within the field of view.  This is done like L.Spiro indicated, by setting the view matrix to be identity - only when rendering the cube!  You should not affect the rest of your scene with this change, but instead use a separate rendering pass to make the 'UI' portion of your scene.



#5 adi0149   Members   -  Reputation: 106

Like
0Likes
Like

Posted 24 May 2013 - 08:26 AM

Wow... that's a lot of words i don't understand... like:

use a separate rendering pass

 

Anyway... i did solve my problem (in a very  unprofessional way, i'm sure of that). You can check out a video of how it looks like here.

 

So... what i did was to choose a visible dimension on screen (as a ratio of the viewport's height or width) and compute the size of the object at each frame, so that it appears to have that dimension that i chose. here's the code:

 

        // choose the size of the object with respect to vieport's width
	float mysize = 0.05*viewport.Width;
	
	// now find the point on the screen that is offseted with half the chosen size
	screenPoint.x = screenPoint.x - mysize/2;
	screenPoint.y = projectPoint.y - mysize/2;
	screenPoint.z = projectPoint.z;

	// now transform the screen position into a world position
	D3DXVec3Unproject( &worldPoint, &screenPoint, &viewport, &matProjection, &matView, &matObjectWorld );

	// coorect size with rotation
	float boxSize = 2.0f; // assign some default size for the object, in case something goes wrong
	if (cos(angley) == 0) // make sure i don't devide by zero
	{
		boxSize = abs(transY - worldPoint.y);
	}
	else
	{
		boxSize = abs(transX - worldPoint.x) / abs(cos(angley)) ;
	}

 

and finally... 

D3DXCreateBox(_device,boxSize,boxSize,boxSize,&_SomeBox,NULL);

 

Now.. what you've seen in the video was accomplished by drawing 3  cylinders with different  colors  and one cube in the middle. If anyone desires, i could put up my entire function here to have a clearer idea.

 

Thanks all for the advice. Any further comments would be appreciated.






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