Skip to content

Commit 5232089

Browse files
authored
Fix Camera canvas resize and rename initXXX to configureXXX (#3249)
1 parent dc61fb1 commit 5232089

27 files changed

Lines changed: 519 additions & 402 deletions

File tree

axmol/2d/NodeGrid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ Camera* NodeGrid::getGridCamera()
8484
{
8585
if (!_gridCamera)
8686
{
87-
_gridCamera = Camera::createOrthographicView(_director->getCanvasSize(), -1024, 1024);
87+
_gridCamera = Camera::create(CameraMode::Ortho);
8888
AX_SAFE_RETAIN(_gridCamera);
8989
}
9090
else
9191
{
92-
_gridCamera->initOrthographicView(_director->getCanvasSize(), -1024, 1024);
92+
_gridCamera->configureOrthographicView(_director->getCanvasSize(), -1024, 1024);
9393
}
9494

9595
auto visitingCamera = Camera::getVisitingCamera();

axmol/2d/Transition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ void TransitionCrossFade::onEnter()
11261126
Vec2 size = _director->getCanvasSize();
11271127
LayerColor* layer = LayerColor::create(color);
11281128

1129-
auto camera = Camera::createOrthographicView(_director->getCanvasSize(), -1024, 1024);
1129+
auto camera = Camera::create(CameraMode::Ortho);
11301130

11311131
// create the first render texture for inScene
11321132
RenderTexture* inTexture =

axmol/2d/TransitionProgress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void TransitionProgress::onEnter()
8080
sprite->setAnchorPoint(Vec2(0.5f, 0.5f));
8181

8282
// render outScene to its texturebuffer
83-
auto camera = Camera::createOrthographicView(_director->getCanvasSize(), -1024, 1024);
83+
auto camera = Camera::create(CameraMode::Ortho);
8484

8585
{
8686
RefPtr<RenderTexturePass> pass(RenderTexturePass::obtain(texture), tlx::adopt_object);

axmol/base/Director.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ void Director::setCanvasSize(const Vec2& canvasSize)
371371

372372
updateOverlayCamera();
373373
updateOffscreenCamera();
374+
375+
if (_runningScene)
376+
{
377+
if (auto* camera = _runningScene->getDefaultCamera())
378+
camera->onCanvasSizeChanged(canvasSize);
379+
}
374380
}
375381

376382
TextureCache* Director::getTextureCache() const
@@ -404,7 +410,7 @@ Camera* Director::getOverlayCamera()
404410
{
405411
if (!_overlayCamera)
406412
{
407-
_overlayCamera = Camera::createOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
413+
_overlayCamera = Camera::create(CameraMode::Ortho);
408414
_overlayCamera->retain();
409415
_overlayCamera->setCameraFlag(CameraFlag::DEFAULT);
410416
_overlayCamera->setDepth(127);
@@ -417,7 +423,7 @@ Camera* Director::getOffscreenCamera()
417423
{
418424
if (!_offscreenCamera)
419425
{
420-
_offscreenCamera = Camera::createOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
426+
_offscreenCamera = Camera::create(CameraMode::Ortho);
421427
_offscreenCamera->retain();
422428
_offscreenCamera->setCameraFlag(CameraFlag::DEFAULT);
423429
_offscreenCamera->setDepth(0);
@@ -427,18 +433,18 @@ Camera* Director::getOffscreenCamera()
427433

428434
void Director::updateOverlayCamera()
429435
{
430-
if (_canvasSizeInPoints.width <= 0 || _canvasSizeInPoints.height <= 0 || !_offscreenCamera)
436+
if (_canvasSizeInPoints.width <= 0 || _canvasSizeInPoints.height <= 0 || !_overlayCamera)
431437
return;
432438

433-
_overlayCamera->initOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
439+
_overlayCamera->configureOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
434440
}
435441

436442
void Director::updateOffscreenCamera()
437443
{
438444
if (_canvasSizeInPoints.width <= 0 || _canvasSizeInPoints.height <= 0 || !_offscreenCamera)
439445
return;
440446

441-
_offscreenCamera->initOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
447+
_offscreenCamera->configureOrthographicView(_canvasSizeInPoints, -1024.0f, 1024.0f);
442448
}
443449

444450
void Director::setNextDeltaTimeZero(bool nextDeltaTimeZero)

axmol/base/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ enum class RenderScaleMode
299299

300300
enum class CameraMode
301301
{
302+
None,
302303
Ortho, ///< Pure orthographic projection, camera at (w/2, h/2, 0) looking -Z
303304
Perspective, ///< Pure perspective projection, user positions camera; default at (0, 1.5, 5) looking at origin
304305
Classic, ///< Calibrated perspective at (w/2, h/2, zEye) looking at center; z=0 has no distortion

axmol/base/Utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ void captureNode(Node* startNode, std::function<void(RefPtr<Image>)> imageCallba
159159

160160
RefPtr<RenderTexturePass> rtxPass(RenderTexturePass::obtain(rtx), tlx::adopt_object);
161161

162-
Camera* camera = Camera::createOrthographicView(size, -1024.f, 1024.f);
162+
auto camera = Camera::create();
163+
camera->configureOrthographicView(size, -1024.f, 1024.f);
163164

164165
rtxPass->begin(camera);
165166
rtxPass->clearAll();

axmol/scene/Camera.cpp

Lines changed: 91 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -42,62 +42,44 @@ Viewport Camera::_defaultViewport;
4242

4343
// start static methods
4444

45+
Camera* Camera::create()
46+
{
47+
auto ret = new Camera();
48+
ret->autorelease();
49+
return ret;
50+
}
51+
4552
Camera* Camera::create(CameraMode mode)
4653
{
47-
auto& size = Director::getInstance()->getCanvasSize();
54+
auto& canvasSize = Director::getInstance()->getCanvasSize();
4855
switch (mode)
4956
{
5057
case CameraMode::Ortho:
5158
{
52-
auto cam = Camera::createOrthographicView(size, -1024.0f, 1024.0f);
59+
auto cam = Camera::create();
60+
cam->configureOrthographicView(canvasSize, -1024.0f, 1024.0f);
5361
return cam;
5462
}
5563
case CameraMode::Perspective:
5664
{
57-
auto cam = Camera::createPerspective(60.0f, size.width / size.height, 0.3f, 1000.0f);
65+
auto cam = Camera::create();
66+
cam->configurePerspective(60.0f, canvasSize.width / canvasSize.height, 0.3f, 1000.0f);
5867
cam->setPosition3D(Vec3(0.0f, 1.5f, 5.0f));
5968
cam->lookAt(Vec3(0, 0, 0));
6069
return cam;
6170
}
6271
case CameraMode::Classic:
6372
{
64-
Camera* camera = new Camera();
65-
camera->initClassic();
66-
camera->autorelease();
67-
return camera;
73+
auto cam = Camera::create();
74+
cam->configureClassicView(canvasSize);
75+
cam->setDepth(0);
76+
return cam;
6877
}
6978
}
7079
AXASSERT(false, "Invalid CameraMode");
7180
return nullptr;
7281
}
7382

74-
Camera* Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
75-
{
76-
auto ret = new Camera();
77-
ret->_cameraMode = CameraMode::Perspective;
78-
ret->initPerspective(fieldOfView, aspectRatio, nearPlane, farPlane);
79-
ret->autorelease();
80-
return ret;
81-
}
82-
83-
Camera* Camera::createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
84-
{
85-
auto ret = new Camera();
86-
ret->_cameraMode = CameraMode::Ortho;
87-
ret->initOrthographic(zoomX, zoomY, nearPlane, farPlane);
88-
ret->autorelease();
89-
return ret;
90-
}
91-
92-
Camera* Camera::createOrthographicView(const Vec2& size, float nearPlane, float farPlane)
93-
{
94-
auto ret = new Camera();
95-
ret->_cameraMode = CameraMode::Ortho;
96-
ret->initOrthographicView(size, nearPlane, farPlane);
97-
ret->autorelease();
98-
return ret;
99-
}
100-
10183
Camera* Camera::getDefaultCamera()
10284
{
10385
// camera nullptr scene init fix #690
@@ -128,15 +110,7 @@ void Camera::setDefaultViewport(const Viewport& vp)
128110
// end static methods
129111

130112
Camera::Camera()
131-
: _eyeZdistance(1)
132-
, _zoomFactor(1)
133-
, _nearPlane(-1024)
134-
, _farPlane(1024)
135-
, _zoomFactorNearPlane(10)
136-
, _zoomFactorFarPlane(1024)
137-
{
138-
// minggo comment
139-
// _frustum.setClipZ(true);
113+
{
140114
_renderView = _director->getRenderView();
141115
}
142116

@@ -224,88 +198,107 @@ void Camera::setAdditionalProjection(const Mat4& mat)
224198
getViewProjectionMatrix();
225199
}
226200

227-
void Camera::initClassic()
201+
void Camera::updateProjection()
202+
{
203+
switch (_cameraMode)
204+
{
205+
case CameraMode::Ortho:
206+
Mat4::createOrthographic(_zoom[0] * _zoomFactor, _zoom[1] * _zoomFactor, _nearPlane, _farPlane, &_projection);
207+
break;
208+
209+
case CameraMode::Perspective:
210+
case CameraMode::Classic:
211+
Mat4::createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane, &_projection);
212+
break;
213+
case CameraMode::None:
214+
break;
215+
}
216+
217+
_viewProjectionDirty = true;
218+
_frustumDirty = true;
219+
}
220+
221+
void Camera::configureClassicView(const Vec2& canvasSize)
228222
{
229-
// Classic mode only - calibrated perspective
230-
auto& size = _director->getCanvasSize();
231-
float zeye = _director->getZEye();
232-
_fieldOfView = 60.0F;
233-
_nearPlane = 0.5F;
234-
_farPlane = zeye + size.height / 2.0f;
235-
initPerspective(_fieldOfView, (float)size.width / size.height, _nearPlane, _farPlane);
236-
Vec3 eye(size.width / 2.0f, size.height / 2.0f, zeye), center(size.width / 2.0f, size.height / 2.0f, 0.0f),
237-
up(0.0f, 1.0f, 0.0f);
238-
setPosition3D(eye);
239-
lookAt(center, up);
240-
_eyeZdistance = eye.z;
223+
const float zeye = _director->getZEye();
224+
const float farPlane = zeye + canvasSize.height * 0.5f;
241225

242-
setDepth(0);
226+
configureClassic(canvasSize.width / canvasSize.height, 0.5f, farPlane);
243227

244-
if (_zoomFactor != 1.0F)
228+
setPosition3D(Vec3(canvasSize.width * 0.5f, canvasSize.height * 0.5f, zeye));
229+
lookAt(Vec3(canvasSize.width * 0.5f, canvasSize.height * 0.5f, 0.0f), Vec3::yAxis);
230+
_eyeZdistance = zeye;
231+
232+
if (_zoomFactor != 1.0f)
245233
applyZoom();
246234
}
247235

248-
void Camera::updateTransform()
236+
void Camera::onCanvasSizeChanged(const Vec2& canvasSize)
249237
{
250-
auto& size = _director->getCanvasSize();
238+
if (canvasSize.width <= 0.0f || canvasSize.height <= 0.0f)
239+
return;
240+
251241
switch (_cameraMode)
252242
{
243+
case CameraMode::Classic:
244+
configureClassicView(canvasSize);
245+
break;
246+
253247
case CameraMode::Ortho:
254-
{
255-
initOrthographicView(size, _nearPlane, _farPlane);
248+
configureOrthographicView(canvasSize, _nearPlane, _farPlane);
256249
break;
257-
}
250+
258251
case CameraMode::Perspective:
259-
{
260-
initPerspective(_fieldOfView, (float)size.width / size.height, _nearPlane, _farPlane);
252+
_aspectRatio = canvasSize.width / canvasSize.height;
253+
updateProjection();
261254
break;
262-
}
263-
case CameraMode::Classic:
264-
{
265-
float zeye = _director->getZEye();
266-
initPerspective(_fieldOfView, (float)size.width / size.height, _nearPlane, _farPlane);
267-
Vec3 eye(size.width / 2.0f, size.height / 2.0f, zeye), center(size.width / 2.0f, size.height / 2.0f, 0.0f),
268-
up(0.0f, 1.0f, 0.0f);
269-
_eyeZdistance = eye.z;
255+
case CameraMode::None:
270256
break;
271257
}
272-
}
273258
}
274259

275-
bool Camera::initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
260+
void Camera::configureClassic(float aspectRatio, float nearPlane, float farPlane)
276261
{
262+
_cameraMode = CameraMode::Classic;
263+
_fieldOfView = 60.0F;
264+
_aspectRatio = aspectRatio;
265+
_nearPlane = nearPlane;
266+
_farPlane = farPlane;
267+
_zoomFactorNearPlane = _nearPlane;
268+
_zoomFactorFarPlane = _farPlane;
269+
updateProjection();
270+
}
271+
272+
bool Camera::configurePerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
273+
{
274+
_cameraMode = CameraMode::Perspective;
277275
_fieldOfView = fieldOfView;
276+
_aspectRatio = aspectRatio;
278277
_nearPlane = nearPlane;
279278
_farPlane = farPlane;
280279

281-
if (_zoomFactorFarPlane == 1024)
282-
_zoomFactorFarPlane = farPlane;
283-
if (_zoomFactorNearPlane == 10)
284-
_zoomFactorNearPlane = nearPlane;
285-
286-
Mat4::createPerspective(_fieldOfView, aspectRatio, _nearPlane, _farPlane, &_projection);
287-
_viewProjectionDirty = true;
288-
_frustumDirty = true;
280+
_zoomFactorNearPlane = nearPlane;
281+
_zoomFactorFarPlane = farPlane;
289282

283+
updateProjection();
290284
return true;
291285
}
292286

293-
bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
287+
bool Camera::configureOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
294288
{
295-
_zoom[0] = zoomX;
296-
_zoom[1] = zoomY;
297-
_nearPlane = nearPlane;
298-
_farPlane = farPlane;
299-
Mat4::createOrthographic(_zoom[0] * _zoomFactor, _zoom[1] * _zoomFactor, _nearPlane, _farPlane, &_projection);
300-
_viewProjectionDirty = true;
301-
_frustumDirty = true;
289+
_cameraMode = CameraMode::Ortho;
290+
_zoom[0] = zoomX;
291+
_zoom[1] = zoomY;
292+
_nearPlane = nearPlane;
293+
_farPlane = farPlane;
302294

295+
updateProjection();
303296
return true;
304297
}
305298

306-
bool Camera::initOrthographicView(const Vec2& size, float nearPlane, float farPlane)
299+
bool Camera::configureOrthographicView(const Vec2& size, float nearPlane, float farPlane)
307300
{
308-
initOrthographic(size.width, size.height, nearPlane, farPlane);
301+
configureOrthographic(size.width, size.height, nearPlane, farPlane);
309302
setPosition3D(Vec3(size.width / 2.0F, size.height / 2.0F, 0.0F));
310303
setRotation3D(Vec3(0.0F, 0.0F, 0.0F));
311304
return true;
@@ -450,14 +443,14 @@ void Camera::applyZoom()
450443
if (_zoomFactorFarPlane * _zoomFactor > _farPlane)
451444
{
452445
_farPlane = _zoomFactorFarPlane * _zoomFactor;
453-
Camera::updateTransform();
446+
updateProjection();
454447
}
455448

456449
// Push the near plane closer the more we zoom in.
457450
if (_zoomFactorNearPlane * _zoomFactor < _nearPlane)
458451
{
459452
_nearPlane = _zoomFactorNearPlane * _zoomFactor;
460-
Camera::updateTransform();
453+
updateProjection();
461454
}
462455

463456
this->setPositionZ(_eyeZdistance * _zoomFactor);
@@ -545,19 +538,19 @@ int Camera::getRenderOrder() const
545538
void Camera::setFOV(float fieldOfView)
546539
{
547540
_fieldOfView = fieldOfView;
548-
Camera::updateTransform();
541+
updateProjection();
549542
}
550543

551544
void Camera::setFarPlane(float farPlane)
552545
{
553546
_farPlane = farPlane;
554-
Camera::updateTransform();
547+
updateProjection();
555548
}
556549

557550
void Camera::setNearPlane(float nearPlane)
558551
{
559552
_nearPlane = nearPlane;
560-
Camera::updateTransform();
553+
updateProjection();
561554
}
562555

563556
void Camera::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)

0 commit comments

Comments
 (0)