kick
|
00001 // 00002 // Created by morten on 24/05/14. 00003 // 00004 00005 #include "kick/scene/camera_perspective.h" 00006 #include "kick/core/engine.h" 00007 #include "kick/scene/transform.h" 00008 #include <iostream> 00009 00010 using namespace glm; 00011 using namespace std; 00012 00013 namespace kick { 00014 00015 CameraPerspective::CameraPerspective(GameObject *gameObject) 00016 : Camera(gameObject), 00017 viewportListener{Engine::context()->contextSurfaceSize.createListener([&](ivec2 viewport) { 00018 resetProjectionMatrix(); 00019 })} 00020 { 00021 resetProjectionMatrix(); 00022 } 00023 00024 float CameraPerspective::near() const { 00025 return mNear; 00026 } 00027 00028 void CameraPerspective::setNear(float near) { 00029 CameraPerspective::mNear = near; 00030 resetProjectionMatrix(); 00031 } 00032 00033 float CameraPerspective::far() const { 00034 return mFar; 00035 } 00036 00037 void CameraPerspective::setFar(float far) { 00038 CameraPerspective::mFar = far; 00039 resetProjectionMatrix(); 00040 } 00041 00042 float CameraPerspective::fieldOfViewY() const { 00043 return mFieldOfViewY; 00044 } 00045 00046 void CameraPerspective::setFieldOfViewY(float fieldOfViewY) { 00047 CameraPerspective::mFieldOfViewY = fieldOfViewY; 00048 resetProjectionMatrix(); 00049 } 00050 00051 void CameraPerspective::set(float near, float far, float fieldOfView) { 00052 this->mNear = near; 00053 this->mFar = far; 00054 this->mFieldOfViewY = fieldOfView; 00055 resetProjectionMatrix(); 00056 } 00057 00058 void CameraPerspective::update(ivec2 viewportDimension) { 00059 vec2 dim = mNormalizedViewportDim * (vec2)viewportDimension; 00060 float aspect = dim.x/dim.y; 00061 mProjectionMatrix = perspective(mFieldOfViewY, aspect, mNear, mFar); 00062 } 00063 00064 void CameraPerspective::resetProjectionMatrix() { 00065 ivec2 viewportDimension = Engine::context()->getContextSurfaceDim(); 00066 update(viewportDimension); 00067 } 00068 00069 float CameraPerspective::fieldOfViewX() const { 00070 ivec2 screenSize = Engine::context()->getContextSurfaceDim(); 00071 vec2 viewport = ((vec2 )screenSize)*mNormalizedViewportDim; 00072 float aspect = viewport.x / viewport.y; 00073 return mFieldOfViewY * aspect; 00074 } 00075 00076 00077 }