How to change width/height ratio when switch one/two view?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

How to change width/height ratio when switch one/two view?

Post by mybiandou »

I want to use the same camera for both one view display and two view display.
(someone use different cameras for single view and two views, but here I want to use the same one)

So when I switch from single to two views, the width/height ratio becomes strange.

I try set the aspect ratio of camera, does not work
I also try to set the projection matrix of the camera , can not solve it too.

How can I solve this question, Can anyone give me some hints
Thank you very much

Code: Select all

	case GUI_ID_SPLIT_VIEW:
					{
						bSplitView=!bSplitView;
						menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_SPLIT_VIEW),bSplitView);						
						if(!bSplitView)
						{
														topcam->setAspectRatio(2.0f);
							leftcam->setAspectRatio(2.0f);
							frontcam->setAspectRatio(2.0f);
							perscam->setAspectRatio(2.0f);
						}
						else
						{
							topcam->setAspectRatio(0.5f);
							leftcam->setAspectRatio(0.5f);
							frontcam->setAspectRatio(0.5f);
							perscam->setAspectRatio(0.5f);
						}
						
						break;
					}
Image

Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The aspect ratio is defined as Aspect = Width / Height. This means that for an aspect ratio of 2.0, the render area is 2x as wide as it is tall (e.g., 200w / 100h = 2.0). If you split the render area in half as you show in your screenshot, the aspect ratio will actually be larger (200w / 50h = 4.0).

I suggest that instead of hardcoding your aspect ratio, you calculate them for each camera based on the viewport size. The math isn't that difficult, and the aspect ratio will always be correct.

Travis
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

Post by mybiandou »

Yes, I can define the aspect ratio based on the view size

But after I set the aspect ratio directly, I can not see anything.

I don't know What kind of effects it will bring to my camera.

Code: Select all

	//設備種類
	irr::video::E_DRIVER_TYPE driverType=driver->getDriverType();

	if(driverType=irr::video::EDT_OPENGL)
	{
			matProj.buildProjectionMatrixOrthoLH (width,height,zNear,zFar);
	else
	{
			matProj.buildProjectionMatrixOrthoRH (width,height,zNear,zFar);
	}

	//create the top/side/view camera 
	//create the top view camera
	if(!topcam)
		topcam=smgr->addCameraSceneNode(camtarget);
	topcam->setProjectionMatrix(matProj,true);
	topcam->setPosition(irr::core::vector3df(0,0,zFar));
	topcam->setUpVector(irr::core::vector3df(1,0,0));

	//create the left view camera
	if(!leftcam)
		leftcam=smgr->addCameraSceneNode(camtarget);
	leftcam->setProjectionMatrix(matProj,true);
	leftcam->setPosition(irr::core::vector3df(-zFar,0,0));
	leftcam->setUpVector(irr::core::vector3df(0,0,1));

	//create the front view camera
	if(!frontcam)
		frontcam=smgr->addCameraSceneNode(camtarget);
	frontcam->setProjectionMatrix(matProj,true);
	frontcam->setPosition(irr::core::vector3df(0,zFar,0));
	frontcam->setUpVector(irr::core::vector3df(0,0,1));

	//create the perspective view camera
	if(!perscam)
		perscam=smgr->addCameraSceneNode(camtarget);
	perscam->setProjectionMatrix(matProj,true);
	perscam->setPosition(irr::core::vector3df(-zFar,-zFar,-zFar));

Code: Select all




				case GUI_ID_SPLIT_VIEW:
					{
						bSplitView=!bSplitView;
						menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_SPLIT_VIEW),bSplitView);
						menu->setItemEnabled(SearchIndexForCommandId(menu,GUI_ID_SWITCH_ACTIVATE_VIEW),bSplitView);
						if(!bSplitView)
						{
							menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_GROUND),EnabledGround);
							menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_YZWALL),EnabledYZWall);
							menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_XZWALL),EnabledXZWall);
							topcam->setAspectRatio(single_view_size.getWidth()/single_view_size.getHeight());
							leftcam->setAspectRatio(single_view_size.getWidth()/single_view_size.getHeight());
							frontcam->setAspectRatio(single_view_size.getWidth()/single_view_size.getHeight());
							perscam->setAspectRatio(single_view_size.getWidth()/single_view_size.getHeight()); 
						}
						else
						{
							if(activeChildView==0)
							{
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_GROUND),EnabledGround_uv);
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_YZWALL),EnabledYZWall_uv);
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_XZWALL),EnabledXZWall_uv);
							}
							else if(activeChildView==1)
							{
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_GROUND),EnabledGround_lv);
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_YZWALL),EnabledYZWall_lv);
								menu->setItemChecked(SearchIndexForCommandId(menu,GUI_ID_VIEW_XZWALL),EnabledXZWall_lv);
							}
							topcam->setAspectRatio(upper_view_size.getWidth()/upper_view_size.getHeight());
							leftcam->setAspectRatio(upper_view_size.getWidth()/upper_view_size.getHeight());
							frontcam->setAspectRatio(upper_view_size.getWidth()/upper_view_size.getHeight());
							perscam->setAspectRatio(upper_view_size.getWidth()/upper_view_size.getHeight()); 
						}
						
						break;
					}

vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

mybiandou wrote:

Code: Select all

single_view_size.getWidth()/single_view_size.getHeight() 
If both of single_view_size.getWidth() and single_view_size.getHeight() are integer types, this divide is going to be rounded down... 1024/768 = 1 when you deal with integral math. You might need to throw a cast or multiply by 1.f in there so that floating point arithmetic is used.

Code: Select all

const f32 aspectRatio = 1.f * single_view_size.getWidth() / single_view_size.getHeight();
Travis
Post Reply