Skip to content

Commit 20513cd

Browse files
authored
refactor(view): Split contents of W3DView::buildCameraTransform(), W3DView::calcCameraAreaConstraints() (TheSuperHackers#2403)
1 parent 38a3056 commit 20513cd

2 files changed

Lines changed: 50 additions & 36 deletions

File tree

Core/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ class W3DView : public View, public SubsystemInterface
286286
Bool m_recalcCamera; ///< Recalculates the camera transform in the next render update
287287

288288
void setCameraTransform(); ///< set the transform matrix of m_3DCamera, based on m_pos & m_angle
289-
void buildCameraTransform(Matrix3D *transform); ///< calculate (but do not set) the transform matrix of m_3DCamera, based on m_pos & m_angle
289+
void buildCameraPosition(Vector3 &sourcePos, Vector3 &targetPos);
290+
void buildCameraTransform(Matrix3D *transform, const Vector3 &sourcePos, const Vector3 &targetPos); ///< calculate (but do not set) the transform matrix of m_3DCamera, based on m_pos & m_angle
290291
void calcCameraAreaConstraints(); ///< Recalculates the camera area constraints
292+
Real calcCameraAreaOffset(Real maxEdgeZ);
291293
Bool isWithinCameraHeightConstraints() const;
292294
virtual void setUserControlled(Bool value);
293295
Bool hasScriptedState(ScriptedState state) const;

Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,8 @@ void W3DView::setOrigin( Int x, Int y)
247247
/** @todo This is inefficient. We should construct the matrix directly using vectors. */
248248
//-------------------------------------------------------------------------------------------------
249249
#define MIN_CAPPED_ZOOM (0.5f) //WST 10.19.2002. JSC integrated 5/20/03.
250-
void W3DView::buildCameraTransform( Matrix3D *transform )
250+
void W3DView::buildCameraPosition( Vector3& sourcePos, Vector3& targetPos )
251251
{
252-
Vector3 sourcePos, targetPos;
253-
254252
Real groundLevel = m_groundLevel; // 93.0f;
255253

256254
Real zoom = getZoom();
@@ -361,7 +359,10 @@ void W3DView::buildCameraTransform( Matrix3D *transform )
361359
}
362360
#endif
363361
}
362+
}
364363

364+
void W3DView::buildCameraTransform( Matrix3D *transform, const Vector3 &sourcePos, const Vector3 &targetPos )
365+
{
365366
//m_3DCamera->Set_View_Plane(DEG_TO_RADF(50.0f));
366367
//DEBUG_LOG(("zoom %f, SourceZ %f, posZ %f, groundLevel %f CamOffZ %f",
367368
// zoom, sourcePos.Z, pos.z, groundLevel,m_cameraOffset.z));
@@ -438,8 +439,6 @@ Note the following restrictions on camera constraints!
438439
*/
439440
void W3DView::calcCameraAreaConstraints()
440441
{
441-
// const Matrix3D& cameraTransform = m_3DCamera->Get_Transform();
442-
443442
// DEBUG_LOG(("*** rebuilding cam constraints"));
444443

445444
// ok, now check to ensure that we can't see outside the map region,
@@ -449,43 +448,49 @@ void W3DView::calcCameraAreaConstraints()
449448
Region3D mapRegion;
450449
TheTerrainLogic->getExtent( &mapRegion );
451450

452-
Real maxEdgeZ = m_groundLevel;
453-
Coord3D center, bottom;
454-
ICoord2D screen;
451+
Real offset = calcCameraAreaOffset(m_groundLevel);
452+
m_cameraAreaConstraints.lo.x = mapRegion.lo.x + offset;
453+
m_cameraAreaConstraints.hi.x = mapRegion.hi.x - offset;
454+
m_cameraAreaConstraints.lo.y = mapRegion.lo.y + offset;
455+
m_cameraAreaConstraints.hi.y = mapRegion.hi.y - offset;
455456

456-
//Pick at the center
457-
screen.x=0.5f*getWidth()+m_originX;
458-
screen.y=0.5f*getHeight()+m_originY;
457+
m_cameraAreaConstraintsValid = true;
458+
}
459+
}
459460

460-
Vector3 rayStart,rayEnd;
461+
//-------------------------------------------------------------------------------------------------
462+
Real W3DView::calcCameraAreaOffset(Real maxEdgeZ)
463+
{
464+
Coord3D center, bottom;
465+
ICoord2D screen;
461466

462-
getPickRay(&screen,&rayStart,&rayEnd);
467+
//Pick at the center
468+
screen.x=0.5f*getWidth()+m_originX;
469+
screen.y=0.5f*getHeight()+m_originY;
463470

464-
center.x = Vector3::Find_X_At_Z(maxEdgeZ, rayStart, rayEnd);
465-
center.y = Vector3::Find_Y_At_Z(maxEdgeZ, rayStart, rayEnd);
466-
center.z = maxEdgeZ;
471+
Vector3 rayStart,rayEnd;
467472

468-
screen.y = m_originY+ 0.95f*getHeight();
469-
getPickRay(&screen,&rayStart,&rayEnd);
470-
bottom.x = Vector3::Find_X_At_Z(maxEdgeZ, rayStart, rayEnd);
471-
bottom.y = Vector3::Find_Y_At_Z(maxEdgeZ, rayStart, rayEnd);
472-
bottom.z = maxEdgeZ;
473-
center.x -= bottom.x;
474-
center.y -= bottom.y;
473+
getPickRay(&screen,&rayStart,&rayEnd);
475474

476-
Real offset = center.length();
475+
center.x = Vector3::Find_X_At_Z(maxEdgeZ, rayStart, rayEnd);
476+
center.y = Vector3::Find_Y_At_Z(maxEdgeZ, rayStart, rayEnd);
477+
center.z = maxEdgeZ;
477478

478-
if (TheGlobalData->m_debugAI) {
479-
offset = -1000; // push out the constraints so we can look at staging areas.
480-
}
479+
screen.y = m_originY+ 0.95f*getHeight();
480+
getPickRay(&screen,&rayStart,&rayEnd);
481+
bottom.x = Vector3::Find_X_At_Z(maxEdgeZ, rayStart, rayEnd);
482+
bottom.y = Vector3::Find_Y_At_Z(maxEdgeZ, rayStart, rayEnd);
483+
bottom.z = maxEdgeZ;
484+
center.x -= bottom.x;
485+
center.y -= bottom.y;
481486

482-
m_cameraAreaConstraints.lo.x = mapRegion.lo.x + offset;
483-
m_cameraAreaConstraints.hi.x = mapRegion.hi.x - offset;
484-
m_cameraAreaConstraints.lo.y = mapRegion.lo.y + offset;
485-
m_cameraAreaConstraints.hi.y = mapRegion.hi.y - offset;
487+
Real offset = center.length();
486488

487-
m_cameraAreaConstraintsValid = true;
489+
if (TheGlobalData->m_debugAI) {
490+
offset = -1000; // push out the constraints so we can look at staging areas.
488491
}
492+
493+
return offset;
489494
}
490495

491496
//-------------------------------------------------------------------------------------------------
@@ -528,7 +533,6 @@ void W3DView::setCameraTransform()
528533
return;
529534

530535
m_cameraHasMovedSinceRequest = true;
531-
Matrix3D cameraTransform;
532536

533537
Real farZ = 1200.0f;
534538

@@ -553,7 +557,11 @@ void W3DView::setCameraTransform()
553557
{
554558
if (!m_cameraAreaConstraintsValid)
555559
{
556-
buildCameraTransform(&cameraTransform);
560+
Vector3 sourcePos;
561+
Vector3 targetPos;
562+
buildCameraPosition(sourcePos, targetPos);
563+
Matrix3D cameraTransform;
564+
buildCameraTransform(&cameraTransform, sourcePos, targetPos);
557565
m_3DCamera->Set_Transform( cameraTransform );
558566
calcCameraAreaConstraints();
559567
}
@@ -577,7 +585,11 @@ void W3DView::setCameraTransform()
577585
#endif
578586

579587
// rebuild it (even if we just did it due to camera constraints)
580-
buildCameraTransform( &cameraTransform );
588+
Vector3 sourcePos;
589+
Vector3 targetPos;
590+
buildCameraPosition(sourcePos, targetPos);
591+
Matrix3D cameraTransform;
592+
buildCameraTransform(&cameraTransform, sourcePos, targetPos);
581593
m_3DCamera->Set_Transform( cameraTransform );
582594

583595
if (TheTerrainRenderObject)

0 commit comments

Comments
 (0)