@@ -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+
4552Camera* 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-
10183Camera* 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
130112Camera::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
545538void Camera::setFOV (float fieldOfView)
546539{
547540 _fieldOfView = fieldOfView;
548- Camera::updateTransform ();
541+ updateProjection ();
549542}
550543
551544void Camera::setFarPlane (float farPlane)
552545{
553546 _farPlane = farPlane;
554- Camera::updateTransform ();
547+ updateProjection ();
555548}
556549
557550void Camera::setNearPlane (float nearPlane)
558551{
559552 _nearPlane = nearPlane;
560- Camera::updateTransform ();
553+ updateProjection ();
561554}
562555
563556void Camera::visit (Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
0 commit comments