Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameclient): place director panes by asking the view itself

Olcay Seygan31527c59a873
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
index 055666e25..04423b4ce 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
@@ -161,46 +161,14 @@ void ObserverCamera_paneCircles( const Real *rays, Int count, Int width, Int hei
/// how far from around the farthest of the things within reach of it lie, on the ground, and never less
/// than least
Real ObserverCamera_extentAround( const std::vector< DirectorHeat > &things, const Coord2D &around, Real reach, Real least );
-/// where a pixel of a width by height picture falls on flat ground, from where its four corners fall
-/// (top left, top right, bottom right, bottom left), through the plane's projection, which is exact
-/// for a perspective camera over flat ground
-Coord2D ObserverCamera_pixelToGround( const Coord2D &pixel, Int width, Int height, const Coord2D *corners );
-/// the other way: the pixel a point on the ground is drawn at
-Coord2D ObserverCamera_groundToPixel( const Coord2D &ground, Int width, Int height, const Coord2D *corners );
-/// how the screen's corners fall on the ground, from the point the view looks at, at any zoom. The eye
-/// stands (zoom - zoomAtPivot) times a fixed offset back from a target above the look point, so where
-/// a corner falls on the target's own height scales with that, and the ground below it adds a shift
-/// along the corner's ray that no zoom changes
-struct ObserverCameraGround
-{
- Coord2D perZoom[ 4 ];
- Coord2D shift[ 4 ];
- Real zoomAtPivot;
-};
-/// the corners of ground at zoom
-void ObserverCamera_cornersAtZoom( const ObserverCameraGround &ground, Real zoom, Coord2D *corners );
-/// the point a camera whose corners are these, measured from the point it looks at, looks at to draw
-/// subject on pixel; every pane camera is placed through this
-Coord2D ObserverCamera_lookPoint( const Coord2D &subject, const Coord2D &pixel, Int width, Int height, const Coord2D *corners );
-/// how far round a circle on the screen reaches on the ground, the nearest of its four sides, with the
-/// corners as above
-Real ObserverCamera_groundRadius( const Coord2D &centre, Real radius, Int width, Int height, const Coord2D *corners );
-/// the lowest zoom from nearest to farthest at which the circle round centre reaches a subject extent
-/// across on the ground with a margin; farthest when none does
-Real ObserverCamera_fitZoom( Real extent, const Coord2D &centre, Real radius, Int width, Int height,
- const ObserverCameraGround &ground, Real nearest, Real farthest );
+/// the zoom from nearest to farthest at which a pane's circle reaches reach on the ground, from the
+/// reach measured at two zooms: the ground a picture covers grows in a straight line with the zoom,
+/// which is how far the eye stands back
+Real ObserverCamera_zoomForReach( Real zoomA, Real reachA, Real zoomB, Real reachB, Real reach, Real nearest, Real farthest );
/// the corners of what pane shows of a width by height picture, the rays meeting at origin: its wedge
/// cut by the screen's edges, at most eight points; the number of them
Int ObserverCamera_panePolygon( const Real *rays, Int count, Int pane, const Coord2D &origin, Int width, Int height,
Coord2D *vertices );
-/// look moved the least that keeps the ground under every vertex of a pane, drawn through corners
-/// from look, inside the map
-Coord2D ObserverCamera_keepPaneInMap( const Coord2D &look, const Coord2D *vertices, Int vertexCount, Int width, Int height,
- const Coord2D *corners, const Region2D &map );
-/// the highest zoom, from nearest up to wanted, at which a camera putting subject on pixel shows no
-/// ground past the map anywhere in the pane's vertices; nearest when none does
-Real ObserverCamera_zoomInMap( const Coord2D &subject, const Coord2D &pixel, const Coord2D *vertices, Int vertexCount,
- Int width, Int height, const ObserverCameraGround &ground, const Region2D &map, Real nearest, Real wanted );
/// how far an animation of length frames that started on start is on frame, eased in and out, 0 to 1
Real ObserverCamera_easeFrames( UnsignedInt frame, UnsignedInt start, UnsignedInt length );
/// the dark edge each side of the gold of a line between panes and of the radar's frame, in pixels
@@ -281,8 +249,13 @@ private:
void driveHeight( Real extra );
Bool takenByHand( const ViewLocation &current ) const;
Coord2D keepInMap( const Coord2D &place, const ViewLocation &current ) const;
- void measureCorners( void );
- Coord2D paneLookPoint( Int pane, const Coord2D &subject, const Coord2D &pixel, Real zoom, Real *shifted ) const;
+ void aimView( const Coord2D &look, Real zoom );
+ Coord2D placeOnPixel( const Coord2D &subject, const Coord2D &pixel, Real zoom );
+ Real pixelError( const Coord2D &subject, const Coord2D &pixel );
+ Coord2D mapShift( Int pane, const Coord2D &origin, const Coord2D &look, Coord2D *worst, Real *past );
+ Real groundReach( const Coord2D &subject, const Coord2D &centre, Real radius, Real zoom );
+ Real zoomInMap( Int pane, const Coord2D &subject, Real nearest, Real wanted );
+ Coord2D placePane( Int pane, const Coord2D &subject, const Coord2D &pixel, Real zoom );
void logPanes( UnsignedInt frame ) const;
void pickIntroBases( void );
Region2D mapRegion( void ) const;
@@ -351,8 +324,12 @@ private:
Bool m_paneFitValid; ///< m_paneFit holds a fit of these panes
UnsignedInt m_paneFitFrame; ///< the logic frame m_paneFit last moved on
Coord2D m_radarHalf; ///< half the framed radar's size, taken from its last draw
- ObserverCameraGround m_ground; ///< where the screen's corners fall on the ground at any zoom, measured each update
+ ViewLocation m_aimFrom; ///< the angle, pitch and height every pane camera is tried at this update
Real m_paneShifted[ OBSERVER_MOST_PANES ]; ///< how far the map moved each pane's look point off its subject last frame
+ Real m_paneError[ OBSERVER_MOST_PANES ]; ///< how far from its pixel the view drew each pane's subject last frame, before the map's shift
+ Coord2D m_paneShown[ OBSERVER_MOST_PANES ]; ///< the subject each pane was placed on last frame
+ Coord2D m_paneWorst[ OBSERVER_MOST_PANES ]; ///< the ground under each pane's vertex furthest past the map last frame
+ Real m_paneOutside[ OBSERVER_MOST_PANES ]; ///< how far past the map that vertex still was once the look point moved
Coord2D m_paneOrigin; ///< where the rays meet now, in pixels
Real m_cornerRadarSlide;
IRegion2D m_radarFrame;
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
index 57da782bd..0c859e04d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
@@ -258,9 +258,9 @@ public:
virtual void addScreenDistortion( const Coord3D *epicenter, const ScreenDistortionInfo &info ) { }
virtual Real getFXPitch( void ) const { return 1.0f; } ///< returns the FX pitch angle
- /// how the eye stands back with the zoom: (zoom - zoomAtPivot) times a fixed offset from the point
- /// looked at, which stands pivotHeight above zero
- virtual void getZoomPivot( Real *zoomAtPivot, Real *pivotHeight ) const { *zoomAtPivot = 0.0f; *pivotHeight = 0.0f; }
+ /// build the camera from the view's location now rather than at the next draw, so picks and
+ /// projections answer for where it was just put
+ virtual void aimCamera( void ) {}
virtual void forceCameraConstraintRecalc(void) {}
virtual void applyCameraConstraint( void ) {} ///< move the look point inside the camera constraint now, where the next draw would
virtual void setGuardBandBias( const Coord2D *gb ) = 0;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
index 813a2341e..238ff79af 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
@@ -570,135 +570,14 @@ Real ObserverCamera_extentAround( const std::vector< DirectorHeat > &things, con
}
//-------------------------------------------------------------------------------------------------
-/** The projection taking the unit square, (0,0) top left to (1,1) bottom right, onto the four ground
- * corners, as the rows of a 3x3 matrix with its last entry 1 (Heckbert's square to quad). */
-//-------------------------------------------------------------------------------------------------
-static void squareToQuad( const Coord2D *corners, Real *matrix )
-{
- const Real x0 = corners[ 0 ].x, y0 = corners[ 0 ].y;
- const Real x1 = corners[ 1 ].x, y1 = corners[ 1 ].y;
- const Real x2 = corners[ 2 ].x, y2 = corners[ 2 ].y;
- const Real x3 = corners[ 3 ].x, y3 = corners[ 3 ].y;
- const Real sumX = x0 - x1 + x2 - x3;
- const Real sumY = y0 - y1 + y2 - y3;
- Real g = 0.0f;
- Real h = 0.0f;
- if( sumX != 0.0f || sumY != 0.0f )
- {
- const Real dx1 = x1 - x2, dx2 = x3 - x2;
- const Real dy1 = y1 - y2, dy2 = y3 - y2;
- const Real below = dx1 * dy2 - dx2 * dy1;
- g = ( sumX * dy2 - dx2 * sumY ) / below;
- h = ( dx1 * sumY - sumX * dy1 ) / below;
- }
- matrix[ 0 ] = x1 - x0 + g * x1;
- matrix[ 1 ] = x3 - x0 + h * x3;
- matrix[ 2 ] = x0;
- matrix[ 3 ] = y1 - y0 + g * y1;
- matrix[ 4 ] = y3 - y0 + h * y3;
- matrix[ 5 ] = y0;
- matrix[ 6 ] = g;
- matrix[ 7 ] = h;
- matrix[ 8 ] = 1.0f;
-}
-
-//-------------------------------------------------------------------------------------------------
-Coord2D ObserverCamera_pixelToGround( const Coord2D &pixel, Int width, Int height, const Coord2D *corners )
-{
- Real m[ 9 ];
- squareToQuad( corners, m );
- const Real u = pixel.x / width;
- const Real v = pixel.y / height;
- const Real w = m[ 6 ] * u + m[ 7 ] * v + m[ 8 ];
- Coord2D ground;
- ground.x = ( m[ 0 ] * u + m[ 1 ] * v + m[ 2 ] ) / w;
- ground.y = ( m[ 3 ] * u + m[ 4 ] * v + m[ 5 ] ) / w;
- return ground;
-}
-
-//-------------------------------------------------------------------------------------------------
-/** The projection's inverse is its adjugate, the scale of a projective matrix not mattering. */
-//-------------------------------------------------------------------------------------------------
-Coord2D ObserverCamera_groundToPixel( const Coord2D &ground, Int width, Int height, const Coord2D *corners )
-{
- Real m[ 9 ];
- squareToQuad( corners, m );
- const Real a = m[ 4 ] * m[ 8 ] - m[ 5 ] * m[ 7 ];
- const Real b = m[ 2 ] * m[ 7 ] - m[ 1 ] * m[ 8 ];
- const Real c = m[ 1 ] * m[ 5 ] - m[ 2 ] * m[ 4 ];
- const Real d = m[ 5 ] * m[ 6 ] - m[ 3 ] * m[ 8 ];
- const Real e = m[ 0 ] * m[ 8 ] - m[ 2 ] * m[ 6 ];
- const Real f = m[ 2 ] * m[ 3 ] - m[ 0 ] * m[ 5 ];
- const Real g = m[ 3 ] * m[ 7 ] - m[ 4 ] * m[ 6 ];
- const Real h = m[ 1 ] * m[ 6 ] - m[ 0 ] * m[ 7 ];
- const Real i = m[ 0 ] * m[ 4 ] - m[ 1 ] * m[ 3 ];
- const Real w = g * ground.x + h * ground.y + i;
- Coord2D pixel;
- pixel.x = ( a * ground.x + b * ground.y + c ) / w * width;
- pixel.y = ( d * ground.x + e * ground.y + f ) / w * height;
- return pixel;
-}
-
+/** The eye stands back from what it looks at in a straight line with the zoom, and the ground a
+ * circle on the screen covers with it, so the two measures give the line and the reach wanted is
+ * read off it. */
//-------------------------------------------------------------------------------------------------
-Real ObserverCamera_groundRadius( const Coord2D &centre, Real radius, Int width, Int height, const Coord2D *corners )
+Real ObserverCamera_zoomForReach( Real zoomA, Real reachA, Real zoomB, Real reachB, Real reach, Real nearest, Real farthest )
{
- const Coord2D middle = ObserverCamera_pixelToGround( centre, width, height, corners );
- const Real stepX[ 4 ] = { radius, -radius, 0.0f, 0.0f };
- const Real stepY[ 4 ] = { 0.0f, 0.0f, radius, -radius };
- Real nearest = 0.0f;
- for( Int side = 0; side < 4; side++ )
- {
- Coord2D edge;
- edge.x = centre.x + stepX[ side ];
- edge.y = centre.y + stepY[ side ];
- const Coord2D ground = ObserverCamera_pixelToGround( edge, width, height, corners );
- const Real dx = ground.x - middle.x;
- const Real dy = ground.y - middle.y;
- const Real reach = sqrtf( dx * dx + dy * dy );
- nearest = side == 0 ? reach : min( nearest, reach );
- }
- return nearest;
-}
-
-//-------------------------------------------------------------------------------------------------
-void ObserverCamera_cornersAtZoom( const ObserverCameraGround &ground, Real zoom, Coord2D *corners )
-{
- const Real out = zoom - ground.zoomAtPivot;
- for( Int corner = 0; corner < 4; corner++ )
- {
- corners[ corner ].x = ground.perZoom[ corner ].x * out + ground.shift[ corner ].x;
- corners[ corner ].y = ground.perZoom[ corner ].y * out + ground.shift[ corner ].y;
- }
-}
-
-//-------------------------------------------------------------------------------------------------
-/** The ground a circle reaches grows with the zoom, the camera standing further back; the lowest
- * zoom that reaches far enough is found by halving. */
-//-------------------------------------------------------------------------------------------------
-Real ObserverCamera_fitZoom( Real extent, const Coord2D &centre, Real radius, Int width, Int height,
- const ObserverCameraGround &ground, Real nearest, Real farthest )
-{
- enum { HALVINGS = 14 };
- const Real needed = extent * ( 1.0f + PANE_FIT_MARGIN );
- Real lowest = nearest;
- Real highest = farthest;
- for( Int halving = 0; halving < HALVINGS; halving++ )
- {
- const Real tried = halving == 0 ? nearest : ( lowest + highest ) * 0.5f;
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( ground, tried, corners );
- if( ObserverCamera_groundRadius( centre, radius, width, height, corners ) >= needed )
- {
- highest = tried;
- if( halving == 0 )
- break;
- }
- else
- {
- lowest = tried;
- }
- }
- return highest;
+ const Real zoom = zoomA + ( reach - reachA ) * ( zoomB - zoomA ) / ( reachB - reachA );
+ return min( max( zoom, nearest ), farthest );
}
//-------------------------------------------------------------------------------------------------
@@ -766,68 +645,6 @@ Int ObserverCamera_panePolygon( const Real *rays, Int count, Int pane, const Coo
return found;
}
-//-------------------------------------------------------------------------------------------------
-/** The ground under each vertex, from look, held inside the map the way the screen's corners are. */
-//-------------------------------------------------------------------------------------------------
-Coord2D ObserverCamera_keepPaneInMap( const Coord2D &look, const Coord2D *vertices, Int vertexCount, Int width, Int height,
- const Coord2D *corners, const Region2D &map )
-{
- Coord2D grounds[ 8 ];
- for( Int vertex = 0; vertex < vertexCount; vertex++ )
- grounds[ vertex ] = ObserverCamera_pixelToGround( vertices[ vertex ], width, height, corners );
- return ObserverCamera_keepInMap( look, grounds, vertexCount, map );
-}
-
-//-------------------------------------------------------------------------------------------------
-/** The point to look at so that subject is drawn on pixel: the corners are measured from the look
- * point, so pixel's ground is too, and the look point is subject less it. */
-//-------------------------------------------------------------------------------------------------
-Coord2D ObserverCamera_lookPoint( const Coord2D &subject, const Coord2D &pixel, Int width, Int height, const Coord2D *corners )
-{
- const Coord2D onPixel = ObserverCamera_pixelToGround( pixel, width, height, corners );
- Coord2D look;
- look.x = subject.x - onPixel.x;
- look.y = subject.y - onPixel.y;
- return look;
-}
-
-//-------------------------------------------------------------------------------------------------
-/** The look point that puts subject on pixel, and the map keeps it where the pane shows nothing past
- * the edge. Showing more ground the higher it stands, the highest zoom that needs no keeping is
- * found by halving. */
-//-------------------------------------------------------------------------------------------------
-Real ObserverCamera_zoomInMap( const Coord2D &subject, const Coord2D &pixel, const Coord2D *vertices, Int vertexCount,
- Int width, Int height, const ObserverCameraGround &ground, const Region2D &map, Real nearest, Real wanted )
-{
- enum { HALVINGS = 12 };
- const Real kept = 1.0f;
- Real fits = nearest;
- Real tried = wanted;
- Real lowest = nearest;
- Real highest = wanted;
- for( Int halving = 0; halving <= HALVINGS; halving++ )
- {
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( ground, tried, corners );
- const Coord2D look = ObserverCamera_lookPoint( subject, pixel, width, height, corners );
- const Coord2D inMap = ObserverCamera_keepPaneInMap( look, vertices, vertexCount, width, height, corners, map );
- const Bool clear = fabs( inMap.x - look.x ) <= kept && fabs( inMap.y - look.y ) <= kept;
- if( clear )
- {
- fits = tried;
- if( halving == 0 )
- break;
- lowest = tried;
- }
- else
- {
- highest = tried;
- }
- tried = ( lowest + highest ) * 0.5f;
- }
- return fits;
-}
-
//-------------------------------------------------------------------------------------------------
Real ObserverCamera_easeFrames( UnsignedInt frame, UnsignedInt start, UnsignedInt length )
{
@@ -904,12 +721,7 @@ void ObserverCamera::reset( void )
m_paneFitValid = FALSE;
m_paneFitFrame = 0;
m_radarHalf.x = m_radarHalf.y = 0.0f;
- for( Int corner = 0; corner < 4; corner++ )
- {
- m_ground.perZoom[ corner ].x = m_ground.perZoom[ corner ].y = 0.0f;
- m_ground.shift[ corner ].x = m_ground.shift[ corner ].y = 0.0f;
- }
- m_ground.zoomAtPivot = 0.0f;
+ m_aimFrom = ViewLocation();
m_paneOrigin.x = m_paneOrigin.y = 0.0f;
m_cornerRadarSlide = 0.0f;
m_radarFrame.lo.x = m_radarFrame.lo.y = m_radarFrame.hi.x = m_radarFrame.hi.y = 0;
@@ -923,6 +735,10 @@ void ObserverCamera::reset( void )
m_paneExtent[ pane ] = PANE_FIT_LEAST_EXTENT;
m_paneFit[ pane ] = 1.0f;
m_paneShifted[ pane ] = 0.0f;
+ m_paneError[ pane ] = 0.0f;
+ m_paneShown[ pane ].x = m_paneShown[ pane ].y = 0.0f;
+ m_paneWorst[ pane ].x = m_paneWorst[ pane ].y = 0.0f;
+ m_paneOutside[ pane ] = 0.0f;
m_paneGlide[ pane ] = ViewLocation();
m_paneVelocity[ pane ].x = m_paneVelocity[ pane ].y = m_paneVelocity[ pane ].z = 0.0f;
m_paneVelocity[ pane ].angle = m_paneVelocity[ pane ].pitch = m_paneVelocity[ pane ].zoom = 0.0f;
@@ -1139,50 +955,185 @@ Region2D ObserverCamera::mapRegion( void ) const
}
//-------------------------------------------------------------------------------------------------
-/** Where the screen's corners fall from the point the view looks at, as m_ground: on the height of
- * the camera's target they scale with the eye's distance, and the ground under the look point adds
- * each ray's own shift. Taken before this frame moves the view, so the corners and the zoom are
- * the ones the last picture was drawn with; the last draw of a frame is always pane 0's. */
+/** The view looking at look from zoom, at this update's angle, pitch and height, its camera built
+ * now, so the view's own picks and projections answer for that camera before anything draws. Every
+ * pane camera is placed by asking the view: two cuts that predicted its projection both drew the
+ * subject off its pixel and the map's edge into the picture. */
//-------------------------------------------------------------------------------------------------
-void ObserverCamera::measureCorners( void )
+void ObserverCamera::aimView( const Coord2D &look, Real zoom )
{
- Coord3D at;
- TheTacticalView->getPosition( &at );
- Real pivotHeight = 0.0f;
- TheTacticalView->getZoomPivot( &m_ground.zoomAtPivot, &pivotHeight );
- Coord3D onPivot[ 4 ];
- TheTacticalView->getScreenCornerWorldPointsAtZ( &onPivot[ 0 ], &onPivot[ 1 ], &onPivot[ 2 ], &onPivot[ 3 ], pivotHeight );
- Coord3D onGround[ 4 ];
- TheTacticalView->getScreenCornerWorldPointsAtZ( &onGround[ 0 ], &onGround[ 1 ], &onGround[ 2 ], &onGround[ 3 ],
- TheTerrainLogic->getGroundHeight( at.x, at.y ) );
- const Real out = TheTacticalView->getZoom() - m_ground.zoomAtPivot;
- for( Int corner = 0; corner < 4; corner++ )
+ ViewLocation aimed;
+ aimed.init( look.x, look.y, m_aimFrom.getPosition().z, m_aimFrom.getAngle(), m_aimFrom.getPitch(), zoom );
+ TheTacticalView->setLocation( &aimed );
+ TheTacticalView->aimCamera();
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The look point at zoom that draws subject on pixel. The camera moves with its look point, so the
+ * ground under pixel, picked on the subject's own height, is moved onto the subject; again, for the
+ * view's constraint and its lift over a hill, which do not move with it. The view is left aimed
+ * there. */
+//-------------------------------------------------------------------------------------------------
+Coord2D ObserverCamera::placeOnPixel( const Coord2D &subject, const Coord2D &pixel, Real zoom )
+{
+ enum { PLACE_PASSES = 3 };
+ const Real ground = TheTerrainLogic->getGroundHeight( subject.x, subject.y );
+ ICoord2D at;
+ at.x = REAL_TO_INT( pixel.x );
+ at.y = REAL_TO_INT( pixel.y );
+ Coord2D look = subject;
+ for( Int pass = 0; pass < PLACE_PASSES; pass++ )
{
- m_ground.perZoom[ corner ].x = ( onPivot[ corner ].x - at.x ) / out;
- m_ground.perZoom[ corner ].y = ( onPivot[ corner ].y - at.y ) / out;
- m_ground.shift[ corner ].x = onGround[ corner ].x - onPivot[ corner ].x;
- m_ground.shift[ corner ].y = onGround[ corner ].y - onPivot[ corner ].y;
+ aimView( look, zoom );
+ Coord3D hit;
+ TheTacticalView->screenToWorldAtZ( &at, &hit, ground );
+ look.x += subject.x - hit.x;
+ look.y += subject.y - hit.y;
}
+ aimView( look, zoom );
+ return look;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** How far from pixel the view, as it is aimed now, draws subject. */
+//-------------------------------------------------------------------------------------------------
+Real ObserverCamera::pixelError( const Coord2D &subject, const Coord2D &pixel )
+{
+ Coord3D world;
+ world.x = subject.x;
+ world.y = subject.y;
+ world.z = TheTerrainLogic->getGroundHeight( subject.x, subject.y );
+ ICoord2D drawn;
+ TheTacticalView->worldToScreenTriReturn( &world, &drawn );
+ const Real dx = drawn.x - pixel.x;
+ const Real dy = drawn.y - pixel.y;
+ return sqrtf( dx * dx + dy * dy );
}
//-------------------------------------------------------------------------------------------------
-/** The point a camera at zoom looks at to draw subject on pixel, moved the least that keeps what the
- * pane shows now, its wedge round the meeting point where it is, inside the map. The same
- * projection the fit was worked out with, so a subject stays on its circle's centre at every zoom.
- * shifted is how far the map moved it. */
+/** What the pane shows from the view as it is aimed now, its wedge round origin cut by the screen,
+ * picked onto the terrain at every corner: the move of look that brings all of it inside the map.
+ * worst is the ground under the corner furthest past the map and past how far, below zero inside.
+ * A ray that meets no terrain, over the horizon or past the heightmap's overhang, is taken on the
+ * look point's height. */
//-------------------------------------------------------------------------------------------------
-Coord2D ObserverCamera::paneLookPoint( Int pane, const Coord2D &subject, const Coord2D &pixel, Real zoom, Real *shifted ) const
+Coord2D ObserverCamera::mapShift( Int pane, const Coord2D &origin, const Coord2D &look, Coord2D *worst, Real *past )
{
const Int width = TheDisplay->getWidth();
const Int height = TheDisplay->getHeight();
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( m_ground, zoom, corners );
Coord2D vertices[ 8 ];
- const Int vertexCount = ObserverCamera_panePolygon( m_paneRays, m_paneCount, pane, m_paneOrigin, width, height, vertices );
- const Coord2D look = ObserverCamera_lookPoint( subject, pixel, width, height, corners );
- const Coord2D kept = ObserverCamera_keepPaneInMap( look, vertices, vertexCount, width, height, corners, mapRegion() );
- *shifted = sqrtf( ( kept.x - look.x ) * ( kept.x - look.x ) + ( kept.y - look.y ) * ( kept.y - look.y ) );
- return kept;
+ const Int vertexCount = ObserverCamera_panePolygon( m_paneRays, m_paneCount, pane, origin, width, height, vertices );
+ const Region2D map = mapRegion();
+ const Real lookHeight = TheTerrainLogic->getGroundHeight( look.x, look.y );
+ Coord2D grounds[ 8 ];
+ for( Int vertex = 0; vertex < vertexCount; vertex++ )
+ {
+ ICoord2D at;
+ at.x = min( max( REAL_TO_INT( vertices[ vertex ].x ), 0 ), width - 1 );
+ at.y = min( max( REAL_TO_INT( vertices[ vertex ].y ), 0 ), height - 1 );
+ Coord3D hit;
+ if( !TheTacticalView->screenToTerrain( &at, &hit ) )
+ TheTacticalView->screenToWorldAtZ( &at, &hit, lookHeight );
+ grounds[ vertex ].x = hit.x - look.x;
+ grounds[ vertex ].y = hit.y - look.y;
+ const Real beyond = max( max( map.lo.x - hit.x, hit.x - map.hi.x ), max( map.lo.y - hit.y, hit.y - map.hi.y ) );
+ if( vertex == 0 || beyond > *past )
+ {
+ *past = beyond;
+ worst->x = hit.x;
+ worst->y = hit.y;
+ }
+ }
+ const Coord2D kept = ObserverCamera_keepInMap( look, grounds, vertexCount, map );
+ Coord2D shift;
+ shift.x = kept.x - look.x;
+ shift.y = kept.y - look.y;
+ return shift;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** How far a circle of radius pixels round centre reaches on the ground at zoom, subject put on
+ * centre: the nearest of eight points round it, picked on the subject's height. */
+//-------------------------------------------------------------------------------------------------
+Real ObserverCamera::groundReach( const Coord2D &subject, const Coord2D &centre, Real radius, Real zoom )
+{
+ enum { REACH_POINTS = 8 };
+ placeOnPixel( subject, centre, zoom );
+ const Real ground = TheTerrainLogic->getGroundHeight( subject.x, subject.y );
+ Real nearest = 0.0f;
+ for( Int point = 0; point < REACH_POINTS; point++ )
+ {
+ const Real angle = point * 2.0f * PI / REACH_POINTS;
+ ICoord2D at;
+ at.x = REAL_TO_INT( centre.x + radius * cosf( angle ) );
+ at.y = REAL_TO_INT( centre.y + radius * sinf( angle ) );
+ Coord3D hit;
+ TheTacticalView->screenToWorldAtZ( &at, &hit, ground );
+ const Real dx = hit.x - subject.x;
+ const Real dy = hit.y - subject.y;
+ const Real reach = sqrtf( dx * dx + dy * dy );
+ nearest = point == 0 ? reach : min( nearest, reach );
+ }
+ return nearest;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The highest zoom from nearest up to wanted at which the pane, all in and its subject on its
+ * circle's centre, shows no ground past the map at any corner; nearest when none does. More ground
+ * shows the higher the camera stands, so it is found by halving. */
+//-------------------------------------------------------------------------------------------------
+Real ObserverCamera::zoomInMap( Int pane, const Coord2D &subject, Real nearest, Real wanted )
+{
+ enum { HALVINGS = 6 };
+ Coord2D middle;
+ middle.x = TheDisplay->getWidth() * 0.5f;
+ middle.y = TheDisplay->getHeight() * 0.5f;
+ Real fits = nearest;
+ Real tried = wanted;
+ Real lowest = nearest;
+ Real highest = wanted;
+ for( Int halving = 0; halving <= HALVINGS; halving++ )
+ {
+ const Coord2D look = placeOnPixel( subject, m_paneCentres[ pane ], tried );
+ Coord2D worst;
+ Real past = 0.0f;
+ mapShift( pane, middle, look, &worst, &past );
+ if( past <= 0.0f )
+ {
+ fits = tried;
+ if( halving == 0 )
+ break;
+ lowest = tried;
+ }
+ else
+ {
+ highest = tried;
+ }
+ tried = ( lowest + highest ) * 0.5f;
+ }
+ return fits;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The look point for a pane's camera this frame: subject on pixel at zoom, then moved the least that
+ * keeps what the pane shows, its wedge round the meeting point where it is now, inside the map.
+ * What it measured is kept for the log. The view is left aimed there. */
+//-------------------------------------------------------------------------------------------------
+Coord2D ObserverCamera::placePane( Int pane, const Coord2D &subject, const Coord2D &pixel, Real zoom )
+{
+ Coord2D look = placeOnPixel( subject, pixel, zoom );
+ m_paneShown[ pane ] = subject;
+ m_paneError[ pane ] = pixelError( subject, pixel );
+ const Coord2D shift = mapShift( pane, m_paneOrigin, look, &m_paneWorst[ pane ], &m_paneOutside[ pane ] );
+ m_paneShifted[ pane ] = sqrtf( shift.x * shift.x + shift.y * shift.y );
+ if( m_paneOutside[ pane ] > 0.0f )
+ {
+ look.x += shift.x;
+ look.y += shift.y;
+ aimView( look, zoom );
+ mapShift( pane, m_paneOrigin, look, &m_paneWorst[ pane ], &m_paneOutside[ pane ] );
+ }
+ return look;
}
//-------------------------------------------------------------------------------------------------
@@ -1429,10 +1380,9 @@ void ObserverCamera::logPanes( UnsignedInt frame ) const
return;
for( Int pane = 0; pane < m_paneCount; pane++ )
{
- const Coord2D &subject = pane == 0 && !m_intro ? m_panesLeftPlace : m_paneSubject[ pane ];
- DEBUG_LOG(( "OBSCAM frame %u pane %d subject (%.0f,%.0f) extent %.0f zoom %.2f fit %.2f base %.2f map shift %.0f\n",
- frame, pane, subject.x, subject.y, m_paneExtent[ pane ], paneZoom( pane ), m_paneFit[ pane ], m_paneBaseZoom,
- m_paneShifted[ pane ] ));
+ DEBUG_LOG(( "OBSCAM frame %u pane %d subject (%.0f,%.0f) error %.1f px extent %.0f zoom %.2f fit %.2f base %.2f map shift %.0f worst (%.0f,%.0f) past %.0f\n",
+ frame, pane, m_paneShown[ pane ].x, m_paneShown[ pane ].y, m_paneError[ pane ], m_paneExtent[ pane ], paneZoom( pane ),
+ m_paneFit[ pane ], m_paneBaseZoom, m_paneShifted[ pane ], m_paneWorst[ pane ].x, m_paneWorst[ pane ].y, m_paneOutside[ pane ] ));
}
}
@@ -1466,25 +1416,20 @@ void ObserverCamera::fitPanes( UnsignedInt frame )
m_paneExtent[ 1 ] = ObserverCamera_extentAround( m_fights, subjects[ 1 ], DIRECTOR_GATHER_RADIUS, PANE_FIT_LEAST_EXTENT );
}
- // a fit is lowered to where the pane shows nothing past the map, the subject still on its centre;
- // one that has to come down goes down whatever the phase
- const Int width = TheDisplay->getWidth();
- const Int height = TheDisplay->getHeight();
- const Region2D map = mapRegion();
- Coord2D middle;
- middle.x = width * 0.5f;
- middle.y = height * 0.5f;
+ // measured once a logic frame, through the view itself: the circle's reach at the nearest and the
+ // farthest zoom gives the zoom that fits, and a fit is lowered to where the pane shows nothing past
+ // the map, the subject still on its centre; one that has to come down goes down whatever the phase
const Bool moves = frame != m_paneFitFrame;
+ if( m_paneFitValid && !moves )
+ return;
m_paneFitFrame = frame;
+ const Real farthest = m_paneBaseZoom * PANE_FIT_FARTHEST;
for( Int pane = 0; pane < m_paneCount; pane++ )
{
- const Real fitted = ObserverCamera_fitZoom( m_paneExtent[ pane ], m_paneCentres[ pane ], m_paneRadii[ pane ], width, height,
- m_ground, m_paneBaseZoom, m_paneBaseZoom * PANE_FIT_FARTHEST );
- // what the pane shows with them all in, the rays meeting in the middle
- Coord2D vertices[ 8 ];
- const Int vertexCount = ObserverCamera_panePolygon( m_paneRays, m_paneCount, pane, middle, width, height, vertices );
- const Real wanted = ObserverCamera_zoomInMap( subjects[ pane ], m_paneCentres[ pane ], vertices, vertexCount, width, height,
- m_ground, map, m_paneBaseZoom, fitted );
+ const Real reach = m_paneExtent[ pane ] * ( 1.0f + PANE_FIT_MARGIN );
+ const Real nearReach = groundReach( subjects[ pane ], m_paneCentres[ pane ], m_paneRadii[ pane ], m_paneBaseZoom );
+ const Real farReach = groundReach( subjects[ pane ], m_paneCentres[ pane ], m_paneRadii[ pane ], farthest );
+ const Real fitted = ObserverCamera_zoomForReach( m_paneBaseZoom, nearReach, farthest, farReach, reach, m_paneBaseZoom, farthest ); const Real wanted = zoomInMap( pane, subjects[ pane ], m_paneBaseZoom, fitted );
const Bool mapLowers = wanted < fitted && wanted < m_paneFit[ pane ];
if( !m_paneFitValid )
m_paneFit[ pane ] = wanted;
@@ -1543,7 +1488,7 @@ void ObserverCamera::stepPaneCameras( const ViewLocation &step, Real elapsedSeco
Coord2D glided;
glided.x = m_paneGlide[ pane ].getPosition().x;
glided.y = m_paneGlide[ pane ].getPosition().y;
- const Coord2D camera = paneLookPoint( pane, glided, pixel, zoom, &m_paneShifted[ pane ] );
+ const Coord2D camera = placePane( pane, glided, pixel, zoom );
m_paneView[ pane ].init( camera.x, camera.y, at.z, step.getAngle(), step.getPitch(), zoom );
}
}
@@ -1798,7 +1743,11 @@ Bool ObserverCamera::chooseTarget( const ViewLocation &current, ViewLocation *ta
place = m_panesLeftPlace;
else
m_panesLeftPlace = place;
- place = keepInMap( place, current );
+ // while there are panes each one keeps what it shows inside the map itself; the whole screen's
+ // corners, wider than pane 0 and at a zoom the panes do not draw at, pushed its subject off its
+ // circle and out of the picture
+ if( m_paneCount < 2 )
+ place = keepInMap( place, current );
target->init( place.x, place.y, at.z, current.getAngle(), current.getPitch(), current.getZoom() );
return TRUE;
}
@@ -1839,7 +1788,6 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
{
const UnsignedInt frame = TheGameLogic->getFrame();
const Bool introStarting = !m_introDone;
- measureCorners();
advancePanes( frame );
if( m_intro && introStarting )
pickIntroBases();
@@ -1878,6 +1826,7 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
Coord2D fromMiddle = { 0.0f, 0.0f };
if( panesZoom )
{
+ m_aimFrom = current;
fitPanes( TheGameLogic->getFrame() );
fromMiddle.x = ( m_paneCentres[ 0 ].x - TheDisplay->getWidth() * 0.5f ) * m_paneProgress;
fromMiddle.y = ( m_paneCentres[ 0 ].y - TheDisplay->getHeight() * 0.5f ) * m_paneProgress;
@@ -1915,8 +1864,11 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
Coord2D pixel;
pixel.x = TheDisplay->getWidth() * 0.5f + fromMiddle.x;
pixel.y = TheDisplay->getHeight() * 0.5f + fromMiddle.y;
- sitBack = paneLookPoint( 0, sitBack, pixel, step.getZoom(), &m_paneShifted[ 0 ] );
+ m_aimFrom = step;
+ sitBack = placePane( 0, sitBack, pixel, step.getZoom() );
}
+ // the pane cameras are placed by aiming the view, so before the view is put where it draws
+ stepPaneCameras( step, elapsed / MILLISECONDS_PER_SECOND );
m_mainOffset.x = step.getPosition().x - sitBack.x;
m_mainOffset.y = step.getPosition().y - sitBack.y;
ViewLocation placed;
@@ -1928,7 +1880,6 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
TheTacticalView->getPosition( &m_drivenTo );
m_driving = TRUE;
- stepPaneCameras( step, elapsed / MILLISECONDS_PER_SECOND );
if( TheGlobalData->m_directorRecord )
logPanes( TheGameLogic->getFrame() );
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
index 187574efd..1c8f3635a 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
@@ -226,9 +226,9 @@ public:
virtual void addScreenDistortion( const Coord3D *epicenter, const ScreenDistortionInfo &info );
virtual Real getFXPitch( void ) const { return m_FXPitch; } ///< returns the FX pitch angle
- /// buildCameraTransform scales the offset by zoom and then by 1 - groundLevel / (offset.z * zoom),
- /// which is zoom - groundLevel / offset.z, round a target at groundLevel
- virtual void getZoomPivot( Real *zoomAtPivot, Real *pivotHeight ) const { *zoomAtPivot = m_groundLevel / m_cameraOffset.z; *pivotHeight = m_groundLevel; }
+ /// the camera transform setCameraTransform builds, without moving the terrain's drawn window or
+ /// telling the radar
+ virtual void aimCamera( void );
virtual Bool setViewFilterMode(enum FilterModes filterMode); ///< Turns on viewport special effect (black & white mode)
virtual Bool setViewFilter(enum FilterTypes filter); ///< Turns on viewport special effect (black & white mode)
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
index 71ce95dca..17971626d 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
@@ -899,6 +899,33 @@ Bool W3DView::wantsIsometric( void ) const
//-------------------------------------------------------------------------------------------------
void W3DView::setCameraTransform( void )
+{
+ aimCamera();
+ if (m_freeCamera)
+ return;
+
+ if (TheTerrainRenderObject)
+ {
+ RefRenderObjListIterator *it = W3DDisplay::m_3DScene->createLightsIterator();
+ TheTerrainRenderObject->updateCenter(m_3DCamera, it);
+ if (it)
+ {
+ W3DDisplay::m_3DScene->destroyLightsIterator(it);
+ it = NULL;
+ }
+ }
+
+ // tell the radar its view box is stale, whatever it was that moved - it used to work that out
+ // itself by comparing the zoom and the angle, so panning left the box behind
+ if (TheRadar)
+ TheRadar->notifyViewChanged();
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The camera itself, built from the location now. The observer camera aims the view at a pane's
+ * place and asks it where things land before the draw does this for real. */
+//-------------------------------------------------------------------------------------------------
+void W3DView::aimCamera( void )
{
m_cameraHasMovedSinceRequest = true;
if (m_freeCamera)
@@ -917,10 +944,13 @@ void W3DView::setCameraTransform( void )
// 1200 was a number, not a distance: at the stock ceiling and pitch the far plane already cuts
// the terrain the game means to draw, and this fork zooms further out than the stock game did.
// Take the distance the terrain is actually drawn over instead, and open it with the height -
- // at twice the height you see twice as far.
+ // at twice the height you see twice as far. The height is the eye's own as well as the one the
+ // view settles towards: the observer camera sets the zoom outright with the settling held off,
+ // and a pane twice as high as the height said cut the far ground off in a black band.
static const Real VIEW_DEFAULT_MAX_HEIGHT_ABOVE_TERRAIN = 310.0f;
farZ = (WorldHeightMap::NORMAL_DRAW_WIDTH * 1.08f) * MAP_XY_FACTOR;
- const Real heightMultiplier = m_heightAboveGround / VIEW_DEFAULT_MAX_HEIGHT_ABOVE_TERRAIN;
+ const Real eyeHeight = m_cameraOffset.z * getZoom() - m_groundLevel;
+ const Real heightMultiplier = max(m_heightAboveGround, eyeHeight) / VIEW_DEFAULT_MAX_HEIGHT_ABOVE_TERRAIN;
if (heightMultiplier > 1.0f)
farZ *= heightMultiplier;
@@ -979,22 +1009,6 @@ void W3DView::setCameraTransform( void )
}
m_3DCamera->Set_Transform( cameraTransform );
-
- if (TheTerrainRenderObject)
- {
- RefRenderObjListIterator *it = W3DDisplay::m_3DScene->createLightsIterator();
- TheTerrainRenderObject->updateCenter(m_3DCamera, it);
- if (it)
- {
- W3DDisplay::m_3DScene->destroyLightsIterator(it);
- it = NULL;
- }
- }
-
- // tell the radar its view box is stale, whatever it was that moved - it used to work that out
- // itself by comparing the zoom and the angle, so panning left the box behind
- if (TheRadar)
- TheRadar->notifyViewChanged();
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_observer_camera.inc b/GeneralsMD/Code/Tests/test_observer_camera.inc
index 8130b884c..089962fee 100644
--- a/GeneralsMD/Code/Tests/test_observer_camera.inc
+++ b/GeneralsMD/Code/Tests/test_observer_camera.inc
@@ -467,139 +467,33 @@ TEST(a_subject_spreads_as_far_as_its_farthest_thing_within_reach)
TEST(a_pane_zooms_out_to_fit_its_subject_and_no_further_than_the_cap)
{
- ObserverCameraGround ground;
- const Coord2D atOne[ 4 ] = { { -400.0f, 400.0f }, { 400.0f, 400.0f }, { 300.0f, -200.0f }, { -300.0f, -200.0f } };
- for( Int corner = 0; corner < 4; corner++ )
- {
- ground.perZoom[ corner ] = atOne[ corner ];
- ground.shift[ corner ].x = ground.shift[ corner ].y = 0.0f;
- }
- ground.zoomAtPivot = 0.0f;
- const Coord2D centre = { 640.0f, 360.0f };
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( ground, 1.0f, corners );
- const Real reachAtOne = ObserverCamera_groundRadius( centre, 200.0f, 1280, 720, corners );
+ // the view measured a circle reaching 180 at zoom 1.2 and 420 at 2.4: the eye stands back in a
+ // straight line with the zoom from a pivot at 0.3, as W3D's camera does
+ const Real nearest = 1.2f;
+ const Real farthest = 2.4f;
// a subject that fits at the director's own zoom is not brought closer
- CHECK_NEAR( ObserverCamera_fitZoom( reachAtOne * 0.5f, centre, 200.0f, 1280, 720, ground, 1.0f, 2.0f ), 1.0f, 0.0001f );
- // one 1.5 times the circle's reach, with its 15 percent margin, needs 1.725 times as far out
- CHECK_NEAR( ObserverCamera_fitZoom( reachAtOne * 1.5f, centre, 200.0f, 1280, 720, ground, 1.0f, 3.0f ), 1.725f, 0.001f );
- CHECK_NEAR( ObserverCamera_fitZoom( reachAtOne * 5.0f, centre, 200.0f, 1280, 720, ground, 1.0f, 2.0f ), 2.0f, 0.001f );
- // a smaller circle needs the camera further out
- CHECK( ObserverCamera_fitZoom( reachAtOne, centre, 150.0f, 1280, 720, ground, 1.0f, 3.0f )
- > ObserverCamera_fitZoom( reachAtOne, centre, 200.0f, 1280, 720, ground, 1.0f, 3.0f ) );
-}
-
-/// the game's view as buildCameraTransform makes it: a pinhole looking down at 50 degrees on a target
-/// TARGET_HEIGHT above zero, its eye (zoom - ZOOM_AT_PIVOT) times 400 back from it
-static const Real TARGET_HEIGHT = 10.0f;
-static const Real ZOOM_AT_PIVOT = 0.3f;
-
-/// where a pixel of a 1280x720 picture falls on a plane at planeHeight for that view at zoom
-static Coord2D castPixel( Real x, Real y, Real zoom, Real planeHeight )
-{
- const Real distance = ( zoom - ZOOM_AT_PIVOT ) * 400.0f;
- const Real pitch = 50.0f * PI / 180.0f;
- const Real halfWide = 0.6f;
- const Real halfHigh = 0.6f * 720.0f / 1280.0f;
- const Real forwardY = cosf( pitch ), forwardZ = -sinf( pitch );
- const Real upY = sinf( pitch ), upZ = cosf( pitch );
- const Real right = ( 2.0f * x / 1280.0f - 1.0f ) * halfWide;
- const Real up = ( 1.0f - 2.0f * y / 720.0f ) * halfHigh;
- const Real eyeY = -distance * forwardY, eyeZ = TARGET_HEIGHT - distance * forwardZ;
- const Real rayX = right, rayY = forwardY + up * upY, rayZ = forwardZ + up * upZ;
- const Real along = ( planeHeight - eyeZ ) / rayZ;
- Coord2D ground;
- ground.x = rayX * along;
- ground.y = eyeY + rayY * along;
- return ground;
-}
-
-static void castCorners( Real zoom, Real planeHeight, Coord2D *corners )
-{
- corners[ 0 ] = castPixel( 0.0f, 0.0f, zoom, planeHeight );
- corners[ 1 ] = castPixel( 1280.0f, 0.0f, zoom, planeHeight );
- corners[ 2 ] = castPixel( 1280.0f, 720.0f, zoom, planeHeight );
- corners[ 3 ] = castPixel( 0.0f, 720.0f, zoom, planeHeight );
-}
-
-/// what measureCorners takes from the view at zoom over ground at groundHeight
-static ObserverCameraGround measureCast( Real zoom, Real groundHeight )
-{
- ObserverCameraGround ground;
- Coord2D onPivot[ 4 ];
- Coord2D onGround[ 4 ];
- castCorners( zoom, TARGET_HEIGHT, onPivot );
- castCorners( zoom, groundHeight, onGround );
- for( Int corner = 0; corner < 4; corner++ )
- {
- ground.perZoom[ corner ].x = onPivot[ corner ].x / ( zoom - ZOOM_AT_PIVOT );
- ground.perZoom[ corner ].y = onPivot[ corner ].y / ( zoom - ZOOM_AT_PIVOT );
- ground.shift[ corner ].x = onGround[ corner ].x - onPivot[ corner ].x;
- ground.shift[ corner ].y = onGround[ corner ].y - onPivot[ corner ].y;
- }
- ground.zoomAtPivot = ZOOM_AT_PIVOT;
- return ground;
-}
-
-TEST(corners_measured_at_one_zoom_say_where_they_fall_at_another)
-{
- // ground 110 above the camera's target, which is how the old scaling about the look point went
- // wrong: the top corners, nearly level, moved furthest
- const ObserverCameraGround ground = measureCast( 1.0f, 120.0f );
- const Real zooms[] = { 1.0f, 1.4f, 2.0f };
- for( Int index = 0; index < 3; index++ )
- {
- Coord2D cast[ 4 ];
- castCorners( zooms[ index ], 120.0f, cast );
- Coord2D predicted[ 4 ];
- ObserverCamera_cornersAtZoom( ground, zooms[ index ], predicted );
- for( Int corner = 0; corner < 4; corner++ )
- {
- CHECK_NEAR( predicted[ corner ].x, cast[ corner ].x, 0.5f );
- CHECK_NEAR( predicted[ corner ].y, cast[ corner ].y, 0.5f );
- }
- }
+ CHECK_NEAR( ObserverCamera_zoomForReach( nearest, 180.0f, farthest, 420.0f, 120.0f, nearest, farthest ), 1.2f, 0.0001f );
+ // 300 is half way along the line
+ CHECK_NEAR( ObserverCamera_zoomForReach( nearest, 180.0f, farthest, 420.0f, 300.0f, nearest, farthest ), 1.8f, 0.0001f );
+ // and one too wide for the cap gets the cap
+ CHECK_NEAR( ObserverCamera_zoomForReach( nearest, 180.0f, farthest, 420.0f, 900.0f, nearest, farthest ), 2.4f, 0.0001f );
}
-TEST(the_four_corners_project_every_pixel_onto_flat_ground_exactly)
+TEST(a_pane_whose_top_leaves_the_map_is_moved_in_by_the_top_alone)
{
- Coord2D corners[ 4 ];
- castCorners( 1.3f, 120.0f, corners );
- const Coord2D pixels[] = { { 900.0f, 200.0f }, { 150.0f, 650.0f }, { 640.0f, 360.0f }, { 1200.0f, 40.0f } };
- for( Int index = 0; index < 4; index++ )
- {
- const Coord2D cast = castPixel( pixels[ index ].x, pixels[ index ].y, 1.3f, 120.0f );
- const Coord2D projected = ObserverCamera_pixelToGround( pixels[ index ], 1280, 720, corners );
- CHECK_NEAR( projected.x, cast.x, 0.5f );
- CHECK_NEAR( projected.y, cast.y, 0.5f );
- const Coord2D back = ObserverCamera_groundToPixel( projected, 1280, 720, corners );
- CHECK_NEAR( back.x, pixels[ index ].x, 0.05f );
- CHECK_NEAR( back.y, pixels[ index ].y, 0.05f );
- }
-}
-
-TEST(a_subject_put_on_its_circle_centre_stays_there_at_every_zoom)
-{
- // the look point the pane cameras use, subject less the pixel's ground at that zoom, drawn back
- // through the real camera of that zoom, lands on the pixel it was put on
- const ObserverCameraGround ground = measureCast( 1.0f, 120.0f );
- const Coord2D subject = { 3000.0f, 2000.0f };
- const Coord2D centre = { 293.0f, 293.0f };
- const Real zooms[] = { 1.0f, 1.3f, 1.7f, 2.0f };
- for( Int index = 0; index < 4; index++ )
- {
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( ground, zooms[ index ], corners );
- const Coord2D look = ObserverCamera_lookPoint( subject, centre, 1280, 720, corners );
- Coord2D cast[ 4 ];
- castCorners( zooms[ index ], 120.0f, cast );
- Coord2D fromLook;
- fromLook.x = subject.x - look.x;
- fromLook.y = subject.y - look.y;
- const Coord2D drawn = ObserverCamera_groundToPixel( fromLook, 1280, 720, cast );
- CHECK_NEAR( drawn.x, centre.x, 0.5f );
- CHECK_NEAR( drawn.y, centre.y, 0.5f );
- }
+ // the ground under a tilted pane's corners: the top reaches three times as far as the bottom, so
+ // a look point 400 from the far edge shows 200 past it at the top while its middle is on the map
+ const Coord2D corners[ 4 ] = { { -900.0f, 600.0f }, { 900.0f, 600.0f }, { 300.0f, -200.0f }, { -300.0f, -200.0f } };
+ Region2D map;
+ map.lo.x = map.lo.y = 0.0f;
+ map.hi.x = map.hi.y = 3000.0f;
+ const Coord2D nearTop = { 1500.0f, 2600.0f };
+ const Coord2D kept = ObserverCamera_keepInMap( nearTop, corners, 4, map );
+ CHECK_NEAR( kept.x, 1500.0f, 0.001f );
+ CHECK_NEAR( kept.y, 2400.0f, 0.001f );
+ const Coord2D middle = { 1500.0f, 1500.0f };
+ const Coord2D free = ObserverCamera_keepInMap( middle, corners, 4, map );
+ CHECK_NEAR( free.y, 1500.0f, 0.001f );
}
TEST(a_pane_shows_its_wedge_cut_by_the_screen)
@@ -624,40 +518,6 @@ TEST(a_pane_shows_its_wedge_cut_by_the_screen)
CHECK_EQ( ObserverCamera_panePolygon( rays, 1, 0, middle, 1280, 720, vertices ), 4 );
}
-TEST(a_pane_comes_down_to_the_zoom_that_shows_no_ground_past_the_map)
-{
- const ObserverCameraGround ground = measureCast( 1.0f, 120.0f );
- Region2D map;
- map.lo.x = map.lo.y = 0.0f;
- map.hi.x = map.hi.y = 3000.0f;
- const Coord2D middlePixel = { 640.0f, 360.0f };
- Real rays[ OBSERVER_MOST_PANES ];
- Coord2D screen[ 8 ];
- const Int screenCount = ObserverCamera_panePolygon( rays, 1, 0, middlePixel, 1280, 720, screen );
-
- // in the middle of a big map the cap is free
- const Coord2D middle = { 1500.0f, 1500.0f };
- CHECK_NEAR( ObserverCamera_zoomInMap( middle, middlePixel, screen, screenCount, 1280, 720, ground, map, 1.0f, 1.5f ), 1.5f, 0.0001f );
-
- // near the far edge only the top of the screen, which reaches furthest, leaves the map: the
- // middle stays on it at any zoom, and the zoom comes down to where the top just touches the edge
- const Coord2D nearTop = { 1500.0f, 2550.0f };
- const Real kept = ObserverCamera_zoomInMap( nearTop, middlePixel, screen, screenCount, 1280, 720, ground, map, 1.0f, 3.0f );
- CHECK( kept > 1.0f && kept < 3.0f );
- Coord2D cast[ 4 ];
- castCorners( kept, 120.0f, cast );
- Coord2D corners[ 4 ];
- ObserverCamera_cornersAtZoom( ground, kept, corners );
- const Coord2D look = ObserverCamera_lookPoint( nearTop, middlePixel, 1280, 720, corners );
- const Real topEdge = look.y + max( cast[ 0 ].y, cast[ 1 ].y );
- CHECK_NEAR( topEdge, 3000.0f, 3.0f );
- CHECK( nearTop.y < 3000.0f );
-
- // and a subject that cannot be shown without the edge at any zoom gets the nearest
- const Coord2D pastEdge = { 10.0f, 1500.0f };
- CHECK_NEAR( ObserverCamera_zoomInMap( pastEdge, middlePixel, screen, screenCount, 1280, 720, ground, map, 1.0f, 2.0f ), 1.0f, 0.0001f );
-}
-
TEST(pane_lines_grow_with_the_picture_and_the_join_keeps_all_of_them)
{
CHECK_EQ( ObserverCamera_paneLineWidth( 720 ), 6 );