diff --git a/indra/newview/fsareasearch.cpp b/indra/newview/fsareasearch.cpp index 35c245fd86..0edd2dd3f1 100644 --- a/indra/newview/fsareasearch.cpp +++ b/indra/newview/fsareasearch.cpp @@ -56,7 +56,9 @@ #include "fswsassetblacklist.h" #include "llworld.h" #include "lltrans.h" // getString() - +#include "llagentcamera.h" // gAgentCamera +#include "llviewerjoystick.h" // For disabling/re-enabling when requested to look at an object. +#include "llmoveview.h" // For LLPanelStandStopFlying::clearStandStopFlyingMode // max number of objects that can be (de-)selected in a single packet. const S32 MAX_OBJECTS_PER_PACKET = 255; @@ -1495,6 +1497,13 @@ bool FSPanelAreaSearchList::onContextMenuItemEnable(const LLSD& userdata) // return true if just one item is selected. return (mResultList->getNumSelected() == 1); } + else if (parameter == "in_dd") + { + // return true if the object is within the draw distance. + LLUUID object_id = mResultList->getFirstSelected()->getUUID(); + LLViewerObject* objectp = gObjectList.findObject(object_id); + return dist_vec_squared(gAgent.getPositionGlobal(), objectp->getPositionGlobal()) < gAgentCamera.mDrawDistance * gAgentCamera.mDrawDistance; + } else { // return true if more then one is selected, but not just one. @@ -1502,6 +1511,79 @@ bool FSPanelAreaSearchList::onContextMenuItemEnable(const LLSD& userdata) } } +// This function calculates the aspect ratio and the world aligned components of a selection bounding box. Direct C&P from LLViewerMediaFocus::getBBoxAspectRatio() +F32 FSPanelAreaSearchList::getBBoxAspectRatio(const LLBBox& bbox, const LLVector3& normal, F32* height, F32* width, F32* depth) +{ + // Convert the selection normal and an up vector to local coordinate space of the bbox + LLVector3 local_normal = bbox.agentToLocalBasis(normal); + LLVector3 z_vec = bbox.agentToLocalBasis(LLVector3(0.0f, 0.0f, 1.0f)); + + LLVector3 comp1(0.f,0.f,0.f); + LLVector3 comp2(0.f,0.f,0.f); + LLVector3 bbox_max = bbox.getExtentLocal(); + F32 dot1 = 0.f; + F32 dot2 = 0.f; + + LL_DEBUGS("FSAreaSearch") << "bounding box local size = " << bbox_max << ", local_normal = " << local_normal << LL_ENDL; + + // The largest component of the localized normal vector is the depth component + // meaning that the other two are the legs of the rectangle. + local_normal.abs(); + + // Using temporary variables for these makes the logic a bit more readable. + bool XgtY = (local_normal.mV[VX] > local_normal.mV[VY]); + bool XgtZ = (local_normal.mV[VX] > local_normal.mV[VZ]); + bool YgtZ = (local_normal.mV[VY] > local_normal.mV[VZ]); + + if(XgtY && XgtZ) + { + LL_DEBUGS("FSAreaSearch") << "x component of normal is longest, using y and z" << LL_ENDL; + comp1.mV[VY] = bbox_max.mV[VY]; + comp2.mV[VZ] = bbox_max.mV[VZ]; + *depth = bbox_max.mV[VX]; + } + else if(!XgtY && YgtZ) + { + LL_DEBUGS("FSAreaSearch") << "y component of normal is longest, using x and z" << LL_ENDL; + comp1.mV[VX] = bbox_max.mV[VX]; + comp2.mV[VZ] = bbox_max.mV[VZ]; + *depth = bbox_max.mV[VY]; + } + else + { + LL_DEBUGS("FSAreaSearch") << "z component of normal is longest, using x and y" << LL_ENDL; + comp1.mV[VX] = bbox_max.mV[VX]; + comp2.mV[VY] = bbox_max.mV[VY]; + *depth = bbox_max.mV[VZ]; + } + + // The height is the vector closest to vertical in the bbox coordinate space (highest dot product value) + dot1 = comp1 * z_vec; + dot2 = comp2 * z_vec; + if(fabs(dot1) > fabs(dot2)) + { + *height = comp1.length(); + *width = comp2.length(); + + LL_DEBUGS("FSAreaSearch") << "comp1 = " << comp1 << ", height = " << *height << LL_ENDL; + LL_DEBUGS("FSAreaSearch") << "comp2 = " << comp2 << ", width = " << *width << LL_ENDL; + } + else + { + *height = comp2.length(); + *width = comp1.length(); + + LL_DEBUGS("FSAreaSearch") << "comp2 = " << comp2 << ", height = " << *height << LL_ENDL; + LL_DEBUGS("FSAreaSearch") << "comp1 = " << comp1 << ", width = " << *width << LL_ENDL; + } + + LL_DEBUGS("FSAreaSearch") << "returning " << (*width / *height) << LL_ENDL; + + // Return the aspect ratio. + return *width / *height; +} + + bool FSPanelAreaSearchList::onContextMenuItemClick(const LLSD& userdata) { std::string action = userdata.asString(); @@ -1560,6 +1642,7 @@ bool FSPanelAreaSearchList::onContextMenuItemClick(const LLSD& userdata) case 'b': // buy case 'p': // p_teleport + case 'q': // q_zoom { LLUUID object_id = mResultList->getFirstSelected()->getUUID(); LLViewerObject* objectp = gObjectList.findObject(object_id); @@ -1573,6 +1656,118 @@ bool FSPanelAreaSearchList::onContextMenuItemClick(const LLSD& userdata) case 'p': // p_teleport gAgent.teleportViaLocation(objectp->getPositionGlobal()); break; + case 'q': // q_zoom + { + // Disable flycam if active. Without this, the requested look-at doesn't happen because the flycam code overrides all other camera motion. + bool fly_cam_status(LLViewerJoystick::getInstance()->getOverrideCamera()); + if (fly_cam_status) + { + LLViewerJoystick::getInstance()->setOverrideCamera(false); + LLPanelStandStopFlying::clearStandStopFlyingMode(LLPanelStandStopFlying::SSFM_FLYCAM); + // *NOTE: Above may not be the proper way to disable flycam. What I really want to do is just be able to move the camera and then leave the flycam in the the same state it was in, just moved to the new location. ~Cron + } + + LLViewerJoystick::getInstance()->setCameraNeedsUpdate(true); // Fixes an edge case where if the user has JUST disabled flycam themselves, the camera gets stuck waiting for input. + + gAgentCamera.setFocusOnAvatar(FALSE, ANIMATE); + + gAgentCamera.setLookAt(LOOKAT_TARGET_SELECT, objectp); + + // Place the camera looking at the object, along the line from the camera to the object, + // and sufficiently far enough away for the object to fill 3/4 of the screen, + // but not so close that the bbox's nearest possible vertex goes inside the near clip. + // Logic C&P'd from LLViewerMediaFocus::setCameraZoom() and then edited as needed + + LLBBox bbox = objectp->getBoundingBoxAgent(); + LLVector3d center(gAgent.getPosGlobalFromAgent(bbox.getCenterAgent())); + F32 height; + F32 width; + F32 depth; + F32 angle_of_view; + F32 distance; + + LLVector3d target_pos(center); + LLVector3d camera_dir(gAgentCamera.getCameraPositionGlobal() - target_pos); + camera_dir.normalize(); + + // We need the aspect ratio, and the 3 components of the bbox as height, width, and depth. + F32 aspect_ratio(getBBoxAspectRatio(bbox, LLVector3(camera_dir), &height, &width, &depth)); + F32 camera_aspect(LLViewerCamera::getInstance()->getAspect()); + + // We will normally use the side of the volume aligned with the short side of the screen (i.e. the height for + // a screen in a landscape aspect ratio), however there is an edge case where the aspect ratio of the object is + // more extreme than the screen. In this case we invert the logic, using the longer component of both the object + // and the screen. + bool invert((camera_aspect > 1.0f && aspect_ratio > camera_aspect) || (camera_aspect < 1.0f && aspect_ratio < camera_aspect)); + + // To calculate the optimum viewing distance we will need the angle of the shorter side of the view rectangle. + // In portrait mode this is the width, and in landscape it is the height. + // We then calculate the distance based on the corresponding side of the object bbox (width for portrait, height for landscape) + // We will add half the depth of the bounding box, as the distance projection uses the center point of the bbox. + if(camera_aspect < 1.0f || invert) + { + angle_of_view = llmax(0.1f, LLViewerCamera::getInstance()->getView() * LLViewerCamera::getInstance()->getAspect()); + distance = width * 0.5 * 1.1 / tanf(angle_of_view * 0.5f); + } + else + { + angle_of_view = llmax(0.1f, LLViewerCamera::getInstance()->getView()); + distance = height * 0.5 * 1.1 / tanf(angle_of_view * 0.5f); + } + + distance += depth * 0.5; + + + // Verify that the bounding box isn't inside the near clip. Using OBB-plane intersection to check if the + // near-clip plane intersects with the bounding box, and if it does, adjust the distance such that the + // object doesn't clip. + LLVector3d bbox_extents(bbox.getExtentLocal()); + LLVector3d axis_x = LLVector3d(1, 0, 0) * bbox.getRotation(); + LLVector3d axis_y = LLVector3d(0, 1, 0) * bbox.getRotation(); + LLVector3d axis_z = LLVector3d(0, 0, 1) * bbox.getRotation(); + //Normal of nearclip plane is camera_dir. + F32 min_near_clip_dist = bbox_extents.mdV[0] * (camera_dir * axis_x) + bbox_extents.mdV[1] * (camera_dir * axis_y) + bbox_extents.mdV[2] * (camera_dir * axis_z); // http://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?page=7 + F32 camera_to_near_clip_dist(LLViewerCamera::getInstance()->getNear()); + F32 min_camera_dist(min_near_clip_dist + camera_to_near_clip_dist); + if (distance < min_camera_dist) + { + // Camera is too close to object, some parts MIGHT clip. Move camera away to the position where clipping barely doesn't happen. + distance = min_camera_dist; + } + + + LLVector3d camera_pos(target_pos + camera_dir * distance); + + if (camera_dir == LLVector3d::z_axis || camera_dir == LLVector3d::z_axis_neg) + { + // If the direction points directly up, the camera will "flip" around. + // We try to avoid this by adjusting the target camera position a + // smidge towards current camera position + // *NOTE: this solution is not perfect. All it attempts to solve is the + // "looking down" problem where the camera flips around when it animates + // to that position. You still are not guaranteed to be looking at the + // object in the correct orientation. What this solution does is it will + // put the camera into position keeping as best it can the current + // orientation with respect to the direction wanted. In other words, if + // before zoom the object appears "upside down" from the camera, after + /// zooming it will still be upside down, but at least it will not flip. + LLVector3d cur_camera_pos = LLVector3d(gAgentCamera.getCameraPositionGlobal()); + LLVector3d delta = (cur_camera_pos - camera_pos); + F64 len = delta.length(); + delta.normalize(); + // Move 1% of the distance towards original camera location + camera_pos += 0.01 * len * delta; + } + + gAgentCamera.setCameraPosAndFocusGlobal(camera_pos, target_pos, objectp->getID()); + + // *TODO: Re-enable joystick flycam if we disabled it earlier... Have to find some form of callback as re-enabling at this point causes the camera motion to not happen. ~Cron + //if (fly_cam_status) + //{ + // LLViewerJoystick::getInstance()->toggleFlycam(); + //} + } + break; default: break; } diff --git a/indra/newview/fsareasearch.h b/indra/newview/fsareasearch.h index 9959507270..25600b7788 100644 --- a/indra/newview/fsareasearch.h +++ b/indra/newview/fsareasearch.h @@ -310,6 +310,8 @@ private: bool onContextMenuItemClick(const LLSD& userdata); bool onContextMenuItemEnable(const LLSD& userdata); + F32 getBBoxAspectRatio(const LLBBox& bbox, const LLVector3& normal, F32* height, F32* width, F32* depth); + LLVector3d mAgentLastPosition; FSAreaSearch* mFSAreaSearch; diff --git a/indra/newview/skins/default/xui/en/menu_fs_area_search.xml b/indra/newview/skins/default/xui/en/menu_fs_area_search.xml index d44c7355ed..e0f219834b 100644 --- a/indra/newview/skins/default/xui/en/menu_fs_area_search.xml +++ b/indra/newview/skins/default/xui/en/menu_fs_area_search.xml @@ -31,6 +31,20 @@ function="AreaSearch.Enable" parameter="one"/> + + + + +