@@ -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->configurePerspective (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->configureOrthographic (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->configureOrthographicView (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
@@ -244,19 +218,13 @@ void Camera::updateProjection()
244218
245219void Camera::configureClassicView (const Vec2& canvasSize)
246220{
247- const float zeye = _director->getZEye ();
221+ const float zeye = _director->getZEye ();
222+ const float farPlane = zeye + canvasSize.height * 0 .5f ;
248223
249- _aspectRatio = canvasSize.width / canvasSize.height ;
250- _farPlane = zeye + canvasSize.height * 0 .5f ;
251- _zoomFactorFarPlane = _farPlane;
224+ configureClassic (canvasSize.width / canvasSize.height , 0 .5f , farPlane);
252225
253- updateProjection ();
254-
255- const Vec3 eye (canvasSize.width * 0 .5f , canvasSize.height * 0 .5f , zeye);
256- const Vec3 center (canvasSize.width * 0 .5f , canvasSize.height * 0 .5f , 0 .0f );
257-
258- setPosition3D (eye);
259- lookAt (center, Vec3::yAxis);
226+ setPosition3D (Vec3 (canvasSize.width * 0 .5f , canvasSize.height * 0 .5f , zeye));
227+ lookAt (Vec3 (canvasSize.width * 0 .5f , canvasSize.height * 0 .5f , 0 .0f ), Vec3::yAxis);
260228 _eyeZdistance = zeye;
261229
262230 if (_zoomFactor != 1 .0f )
@@ -285,42 +253,40 @@ void Camera::onCanvasSizeChanged(const Vec2& canvasSize)
285253 }
286254}
287255
288- void Camera::initClassic ()
289- {
290- _cameraMode = CameraMode::Classic;
291- _fieldOfView = 60 .0F ;
292- _nearPlane = 0 .5F ;
293- configureClassicView (_director->getCanvasSize ());
294- setDepth (0 );
295- }
296-
297- void Camera::updateTransform ()
256+ void Camera::configureClassic (float aspectRatio, float nearPlane, float farPlane)
298257{
258+ _cameraMode = CameraMode::Classic;
259+ _fieldOfView = 60 .0F ;
260+ _aspectRatio = aspectRatio;
261+ _nearPlane = nearPlane;
262+ _farPlane = farPlane;
263+ _zoomFactorNearPlane = _nearPlane;
264+ _zoomFactorFarPlane = _farPlane;
299265 updateProjection ();
300266}
301267
302268bool Camera::configurePerspective (float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
303269{
270+ _cameraMode = CameraMode::Perspective;
304271 _fieldOfView = fieldOfView;
305272 _aspectRatio = aspectRatio;
306273 _nearPlane = nearPlane;
307274 _farPlane = farPlane;
308275
309- if (_zoomFactorFarPlane == 1024 )
310- _zoomFactorFarPlane = farPlane;
311- if (_zoomFactorNearPlane == 10 )
312- _zoomFactorNearPlane = nearPlane;
276+ _zoomFactorNearPlane = nearPlane;
277+ _zoomFactorFarPlane = farPlane;
313278
314279 updateProjection ();
315280 return true ;
316281}
317282
318283bool Camera::configureOrthographic (float zoomX, float zoomY, float nearPlane, float farPlane)
319284{
320- _zoom[0 ] = zoomX;
321- _zoom[1 ] = zoomY;
322- _nearPlane = nearPlane;
323- _farPlane = farPlane;
285+ _cameraMode = CameraMode::Ortho;
286+ _zoom[0 ] = zoomX;
287+ _zoom[1 ] = zoomY;
288+ _nearPlane = nearPlane;
289+ _farPlane = farPlane;
324290
325291 updateProjection ();
326292 return true ;
0 commit comments