Sample Code: Set Optimized Camera Position and Camera Target Ratio in 3DF Part Viewer

Language: C++

Ideally, the camera should be set up such that the “distance from the camera position to the camera target should be 2.5 times the field width” – per our docs under “Camera Set Up Guidelines”:

To that end, below is a brief code sample which can be executed as an extra slot code in the HOOPS 3DF Part Viewer:

  1. Open “samples_v14x.sln” found in the root directory of the 3DF service package.
  2. In the VS project “hoopspartviewer_v14x”, navigate to the C++ file called “CSolidHoopsView.cpp”. The user slots are at the bottom of the file. Add the following code:
void CSolidHoopsView::OnExtraSlot1()
{
	HCamera old_camera;

	HC_Open_Segment_By_Key(m_pHView->GetSceneKey()); {
		HC_Show_Net_Camera(&old_camera.position, &old_camera.target, &old_camera.up_vector, &old_camera.field_width,
			&old_camera.field_height, old_camera.projection);

		//Calculate view vector and normalize it
		HVector v(old_camera.target.x - old_camera.position.x, old_camera.target.y - old_camera.position.y, old_camera.target.z - old_camera.position.z);
		HVector vn;
		HC_Compute_Normalized_Vector(&v, &vn);

		//Calculate the optimized distance (based on recommended on 2.5x)
		float new_distance = 2.5 * old_camera.field_width;

		//Set the new camera position taking into account the 2.5x ratio
		HVector new_position(old_camera.target.x + new_distance * -vn.x, old_camera.target.y + new_distance * -vn.y, old_camera.target.z + new_distance * -vn.z);

		//Set the new optimized camera position
		HC_Set_Camera_Position(new_position.x, new_position.y, new_position.z);

	} HC_Close_Segment();

	m_pHView->Update();
}