📄 chcollision.cpp
字号:
/*----------------------------------------------------------------------------
_ _ _
/\ | | | (_)
/ \ _ __ __| |_ __ ___ _ __ ___ ___ __| |_ __ _
/ /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
/ ____ \| | | | (_| | | | (_) | | | | | | __/ (_| | | (_| |
/_/ \_\_| |_|\__,_|_| \___/|_| |_| |_|\___|\__,_|_|\__,_|
The contents of this file are subject to the Andromedia Public
License Version 1.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.andromedia.com/APL/
Software distributed under the License is distributed on an
"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is Pueblo client code, released November 4, 1998.
The Initial Developer of the Original Code is Andromedia Incorporated.
Portions created by Andromedia are Copyright (C) 1998 Andromedia
Incorporated. All Rights Reserved.
Andromedia Incorporated 415.365.6700
818 Mission Street - 2nd Floor 415.365.6701 fax
San Francisco, CA 94103
Contributor(s):
--------------------------------------------------------------------------
Chaco team: Dan Greening, Glenn Crocker, Jim Doubek,
Coyote Lussier, Pritham Shetty.
Wrote and designed original codebase.
------------------------------------------------------------------------------
Implementation for collision detection in VRML/RenderLab
----------------------------------------------------------------------------*/
// $Header: /home/cvs/chaco/modules/client/msw/ChGraphx/ChCollision.cpp,v 2.10 1996/07/08 21:30:22 jimd Exp $
#include "grheader.h"
//
#ifdef CH_MSW
#include <windows.h>
#endif
#ifdef HUGE
#undef HUGE
#endif
#include <math.h>
//#include <strstrea.h>
#include "ChMaze.h"
#include "CvBound.h"
#include "CvHitTst.h"
#include "CvTrnsfm.h"
#include "ChCollision.h"
#include "ChMazCam.h"
#include "ChRCDevice.h"
#define CH_TRANSPARENCY_COLLISION_THRESHOLD 0.90
#define CH_INFINITE_RANGE 1000000
//#define NEAR_PLANE_SCALE 100.0
#define NEAR_PLANE_SCALE 1.0
#if (defined(CH_USE_D3D)) || (defined(CH_USE_RLAB))
#if 0 && (defined(CH_USE_D3D))
// Just stubs to get us going on d3d
#pragma message("D3D Collision Testing Not enabled!")
#if 1
float ChRenderContext::GetCollisionRange(GxVec3f &look, bool boolRelative /* = false*/)
{
// Return the range the current camera can be moved without colliding with something
// value is in -camera- coords
float range = CH_INFINITE_RANGE; // free to move if no scene
return range;
};
#endif
#else // must be (defined(CH_USE_RLAB))
float ChRenderContext::GetCollisionRange(GxVec3f &look, bool boolRelative /* = false*/)
{
// Return the range the current camera can be moved without colliding with something
// value is in -camera- coords
float range = CH_INFINITE_RANGE; // free to move if no scene
if(m_viewport)
{
// Lock the scene and don't be nice
LockScene();
if(!m_pCollisionSensor)
{
m_pCollisionSensor = new ChCollisionView(this, m_viewport, m_sceneFrame, m_cameraFrame);
}
if(boolRelative)
{
m_pCollisionSensor->SetRelativeDir(look);
}
else
{
m_pCollisionSensor->SetDir(look);
}
m_pCollisionSensor->Render();
bool boolUseFaces = true;
#if (defined(CH_USE_RLAB))
boolUseFaces = m_maxTransparency > CH_TRANSPARENCY_COLLISION_THRESHOLD;
#endif
range = m_pCollisionSensor->GetCollisionRange(boolUseFaces);
// Now check the immediate mode stuff
#if (defined(CH_IMMED_MODE))
GxVec3f dir(look);
if(boolRelative)
{
ChMazeCameraControl::ComputeRelativeCameraShift( dir, GetCameraDir(), GetCameraUp() );
}
ChCollisionIterator iterator(this, GetCameraLoc(), dir, GetAvatarRadius());
iterator.Attach(GetRoot());
iterator.Iterate();
float t;
ChQvInstance *pInst;
if(iterator.GetHit(pInst, t))
{
float immedRange = dir.magnitude() * t;
if(immedRange < range) range = immedRange;
}
#endif // CH_IMMED_MODE
UnlockScene();
#if BE_VERY_NOISY_ON_COLLISION_TESTING
::AfxTrace("Range %6.3f dir (%6.3f, %6.3f, %6.3f) (%s). \n",
double(range), double(look.x()), double(look.y()), double(look.z()),
(boolRelative ? "relative" : "absolute"));
#endif
}
return range;
}
#endif
#endif
float ChCollisionView::GetCollisionRange(bool boolUseFaces)
{
// Return the range the current camera can be moved without colliding with something
// value is in -camera- coords
float range = CH_INFINITE_RANGE; // free to move if no scene
#if (defined(CH_USE_RLAB))
//LockScene();
int iX = ChNrViewportGetWidth(m_view) / 2;
int iY = ChNrViewportGetHeight(m_view) / 2;
RLVisual visual;
ChNrFrame frame;
float z = GetZ();
if(/*false && */ !boolUseFaces)
{
// take range - I'm not sure if this works if we have a scaling transform before the camera
//range = ptInCameraSpace.magnitude() - ChNrViewportGetFront(m_view) * NEAR_PLANE_SCALE;
range = z - ChNrViewportGetFront(m_view) * 2 * NEAR_PLANE_SCALE;
range = max(range, 0.);
}
else if(ChNrViewportFindVisual( m_view, &visual, &frame, iX, iY) != RLNotFound)
{
// Hit -something-. How far away is it?
GxVec3f clickPoint;
ChNrFaceInfo faceInfo;
if((ChNrViewportFindFace( m_view, &faceInfo, iX, iY) != RLNotFound)
&& faceInfo.frame == frame)
{
clickPoint = *(GxVec3f*)&faceInfo.position;
ChNrVector4d screenCoord;
screenCoord.x = clickPoint.x();
screenCoord.y = clickPoint.y();
screenCoord.z = clickPoint.z();
screenCoord.w = 1;
ChNrViewportInverseTransform(m_view, (ChNrVector*)&clickPoint, &screenCoord);
}
else
{
// It's not a face visual, must be a decal
ChNrFrameGetPosition(frame, m_scene, (ChNrVector*)&clickPoint);
}
// Convert to camera coords
GxVec3f ptInCameraSpace;
ChNrFrameInverseTransform(m_camera, (ChNrVector*)&ptInCameraSpace, (ChNrVector*)&clickPoint);
//TRACE1("bufffer's z / pt.z() = %f\n", double(z / clickPoint.z()));
// take range - I'm not sure if this works if we have a scaling transform before the camera
range = ptInCameraSpace.magnitude() - ChNrViewportGetFront(m_view) * 2 * NEAR_PLANE_SCALE;
range = max(range, 0.);
}
//UnlockScene();
#if BE_VERY_NOISY_ON_COLLISION_TESTING
GxVec3f origin, loc;
ChNrFrameTransform(m_camera, (ChNrVector*)&loc, (ChNrVector*)&origin);
origin = loc;
ChNrFrameInverseTransform(m_scene, (ChNrVector*)&loc, (ChNrVector*)&origin);
::AfxTrace(" Camera is at (%6.3f, %6.3f, %6.3f) in scene. ",
double(loc.x()), double(loc.y()), double(loc.z()));
#endif
#elif (defined(CH_USE_D3D))
int iX = ChNrViewportGetWidth(m_view) / 2;
int iY = ChNrViewportGetHeight(m_view) / 2;
GxVec3f retainedPoint;
LPDIRECT3DRMVISUAL visual;
LPDIRECT3DRMFRAME pickFrame;
LPDIRECT3DRMPICKEDARRAY picked = 0;
LPDIRECT3DRMFRAMEARRAY frames;
LPDIRECT3DRMMESHBUILDER mesh;
LPDIRECT3DRMTEXTURE decal;
D3DRMPICKDESC pickInfo;
HRESULT rval = m_view->Pick(iX, iY, &picked);
int count = picked ? picked->GetSize() : 0;
float z = 0;
if(count > 0)
{
picked->GetPick(0, &visual, &frames, &pickInfo);
frames->GetElement(frames->GetSize() - 1, &pickFrame);
if (SUCCEEDED(visual->QueryInterface(IID_IDirect3DRMMeshBuilder, (void **) &mesh)))
{
mesh->Release();
}
else if (SUCCEEDED(visual->QueryInterface(IID_IDirect3DRMTexture, (void **) &decal)))
{
decal->Release();
}
pickFrame->Release();
frames->Release();
visual->Release();
GxVec3f clickPoint;
ChNrVector4d screenCoord;
screenCoord.x = pickInfo.vPosition.x;
screenCoord.y = pickInfo.vPosition.y;
screenCoord.z = pickInfo.vPosition.z;
screenCoord.w = 1;
ChNrViewportInverseTransform(m_view, (ChNrVector*)&clickPoint, &screenCoord);
// Convert to camera coords
GxVec3f ptInCameraSpace;
ChNrFrameInverseTransform(m_camera, (ChNrVector*)&ptInCameraSpace, (ChNrVector*)&clickPoint);
range = ptInCameraSpace.magnitude() - ChNrViewportGetFront(m_view) * 2 * NEAR_PLANE_SCALE;
range = max(range, 0.);
}
// ------------------------------------------------------
#if 0
RLVisual visual;
ChNrFrame frame;
if(ChNrViewportFindVisual( m_view, &visual, &frame, iX, iY) != RLNotFound)
{
// Hit -something-. How far away is it?
GxVec3f clickPoint;
ChNrFaceInfo faceInfo;
if((ChNrViewportFindFace( m_view, &faceInfo, iX, iY) != RLNotFound)
&& faceInfo.frame == frame)
{
clickPoint = *(GxVec3f*)&faceInfo.position;
ChNrVector4d screenCoord;
screenCoord.x = clickPoint.x();
screenCoord.y = clickPoint.y();
screenCoord.z = clickPoint.z();
screenCoord.w = 1;
ChNrViewportInverseTransform(m_view, (ChNrVector*)&clickPoint, &screenCoord);
}
else
{
// It's not a face visual, must be a decal
ChNrFrameGetPosition(frame, m_scene, (ChNrVector*)&clickPoint);
}
// Convert to camera coords
GxVec3f ptInCameraSpace;
ChNrFrameInverseTransform(m_camera, (ChNrVector*)&ptInCameraSpace, (ChNrVector*)&clickPoint);
//TRACE1("bufffer's z / pt.z() = %f\n", double(z / clickPoint.z()));
// take range - I'm not sure if this works if we have a scaling transform before the camera
range = ptInCameraSpace.magnitude() - ChNrViewportGetFront(m_view) * 2 * NEAR_PLANE_SCALE;
range = max(range, 0.);
}
#endif
#endif
return range;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -