Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Centreing a Orthographic projected quad to 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 ankhd   Members   -  Reputation: 1356

Like
0Likes
Like

Posted 31 March 2014 - 05:50 AM

Hi.

I'm using a orthographic projection view to render my game ui but, can't get the offset from my quad to align to the true centre
of the screen.

this is what Im doing I fist get the size of the quad by finding the min and max vertices of the quad then get the distance from
min and max I use this value to get the percentage of the screen, with this percentage divided by 2 I get the size in relation to the screen, then these locations get projected to 3d world space so they can be added to the unprojected screens 0,0 location in 3d space.
But the quad is not centre its off to the right what could be wrong if its all not wrong.

thanks for any help.

 

The quad we exported from 3dsmax at (0.0.0) in 3ds max world but the quads axis is the topleft of the quad if it helps.

here is the code I use to calculate the percentage and offsets

width and height are the quads size in object space(how its exported.)

//calculate the percentage of the ui quad to the screen
float px = (width / viewport.Width) * 100.0;
float py = (height / viewport.Height) * 100.0;
	
px *= 0.5;//we want half the percentage
py *= 0.5;

//we now want the percentage in size in relation to the screen
float screenpercentx =  (viewport.Width  * px / 100);
float screenpercenty =  (viewport.Height * py / 100);

	
//now we want half and offseted from screen centre
screenpercentx = ((viewport.Width * 0.5)  - (screenpercentx * 0.5));
screenpercenty = ((viewport.Height * 0.5) - (screenpercenty * 0.5));

	
DialogBox_GameLobby->SetCamera(&Cam);


D3DXMATRIX matView ;
Cam.getViewMatrix(&matView);
	
DialogBox_GameLobby->ReSetCam(&Cam);

D3DXVECTOR3 v;
D3DXVECTOR3 rayOrigin(0.0f, 0.0f, 0.0f);


v.x =  screenpercentx;
v.y =  screenpercenty;
v.z = 0.0f;

	
//unproject the screen base ofset 3d space
D3DXVec3Unproject(&rayOrigin,
 &v,
 &viewport,
&OrthograpicProjection,
&matView,
&Identityworld);	

//set the conversion
screenpercentx = rayOrigin.x;
screenpercenty = rayOrigin.y;


	

	
rayOrigin= D3DXVECTOR3 (0.0f, 0.0f, 0.0f);

//the left side of the screen 
v.x =  0.0;
v.y =  0.0;
v.z = 0.0f;

	
//unproject the the left side of the screen to 3d space
D3DXVec3Unproject(&rayOrigin,
 &v,
&viewport,&
OrthograpicProjection,
&matView,
 &Identityworld);	

	

	
//
//we have the left side of the screen projected to 3d world space
//add the quads projected location to the left side
Dialog_GameLobby->SetWorldPosition( rayOrigin.x - screenpercentx ,rayOrigin.y - screenpercenty, -1.0, 1.0, 1.0, 1.0);


Sponsor:

#2 JohnnyCode   Members   -  Reputation: 291

Like
3Likes
Like

Posted 31 March 2014 - 11:14 AM

projection space is a defined square(cube) with size of sides -1.0,1.0. The 0.0 of the projection space is very center of the square. positive to the right x, negaitive to left, y postive up , negative y down. If in 3ds max you create a square plane of size 2.0, centered at 0,0,0 in x,y plane,  you would be actualy spectating your square aligned version of screen- orthogonal projection. You would be able to define proportioned other objects on it. You might use those objects without any further transformation and render them in their very object space .



#3 ankhd   Members   -  Reputation: 1356

Like
1Likes
Like

Posted 01 April 2014 - 04:17 AM

No the above code of mine does not work I changed the screen size and its not centre. it should be as JohnnyCode said but the mesh is to big for some reason.

 

See the meshes (Quad) is 749 units wide so half that is 374, But if I off set that to -374 the end of the screen in ortho is -327 SO the quads centre off set is of the screen to the left

How do I find the scale based on the orthographic projection.

 

This is how I make my Projection
 

float zoom = 2.2;//1.8f;

D3DXMatrixOrthoOffCenterLH(&OrthograpicProjection, (-Width/2)/zoom, (Width/2)/zoom, (-Height/2)/zoom, (Height/2)/zoom, 0.0f,1000.0f);

my projection is zooming out by 2.2

the centre is x-255.2, y159.2 ,

 

found by draging menu by the mouse to the real centre.

 

what should I be doing with the meshes(quad) size to get the -255.2 to centre it



#4 JohnnyCode   Members   -  Reputation: 291

Like
1Likes
Like

Posted 01 April 2014 - 09:48 AM

ortho projection in general (after view matrix position and rotation), spectates a scene of given wide. You define width and height and those 2 numbers devide everything, so that values will fall to scope of -1.0,1.0. The bigger width and height, the more of world space will fall to observation. In a nut shell ortho porjection will just devide x and y by width and height, and devide z component by (far-near). You are using a function D3DXMatrixOrthoOffCenterLH , which , seeing its name, can couse a spooky action at a distance (though unlikely).

 

In case the projection matrix you build provides standard ortho projection, is should be transformation as

T(x,y,z,w)=(x/widh,y/height,z/(far-near),w). With such a matrix, your will put to projection a space 2.0*width and 2.0*height range big, far-near deep.

 

In case you wish to transform your quad by projection matrix and have it result to -1.0,1.0 perfect fit, is should have verticies with respect to +/-width and +/-height, what is volatile in your case (zoom variable). The quad I adviced you with in 3ds max is already in projection space ( likely, depending on corelation between 3ds max object space and exporter postwork). You should be defining objects in projection space straight, wheather by hand, or by help of max, and not transform them, mainly if you alter projection matrix (zoom).



#5 ankhd   Members   -  Reputation: 1356

Like
0Likes
Like

Posted 02 April 2014 - 03:59 AM

I'm not getting it its not right. The funny thing is it works when the screen is 1440 x 900, but its out when using 1904 x 1134
just like some 80 pixels or so out.

here is the code now dividing by ht zoom value that seems to work some times

if(mc)
	{
		if(mc->pMesh)
		{
			GetExtentsX(mc->pMesh, min, max);// * zoom;//y value510;//
			width	= GetTheDiemention(min, max)/ zoom;
			//width = 510.0;



			//height	= GetMeshDepth(mc->pMesh) / zoom;//z value318;//
			GetExtentsZ(mc->pMesh, min, max);// * zoom;//y value510;//
			height	= GetTheDiemention(min, max)/  zoom;

			
		}
	}

//calculate the percentage of the ui quad to the screen
float px = (width  / (viewport.Width/zoom))  * 100.0;
float py = (height / (viewport.Height/zoom)) * 100.0;
	
	
//we now want the percentage in size in relation to the screen
float screenpercentx =  ((viewport.Width/zoom)  * px / 100);
float screenpercenty =  ((viewport.Height/zoom) * py / 100);

	
//now we want half and offseted from screen centre
screenpercentx = (((viewport.Width/zoom)  - screenpercentx)/2);//  - (screenpercentx * 0.5));
screenpercenty = (((viewport.Height/zoom) - screenpercenty)/2);

	
DialogBox_GameLobby->SetCamera(&Cam);


D3DXMATRIX matView ;
Cam.getViewMatrix(&matView);
	
DialogBox_GameLobby->ReSetCam(&Cam);

D3DXVECTOR3 v;
D3DXVECTOR3 rayOrigin(0.0f, 0.0f, 0.0f);


v.x =  screenpercentx;
v.y =  screenpercenty;
v.z = -1.0f;//this value here make the y value larger

	
//unproject the screen base ofset 3d space
D3DXVec3Unproject(&rayOrigin,
&v,
&viewport,
									  &OrthograpicProjection,
&matView,
 &Identityworld);	




DialogBox_GameLobby->SetWorldPosition(rayOrigin.x , rayOrigin.y, -1, 1.0, 1.0, 1.0);






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