Camera::Camera(void) : needsUpdate(true) { // zAxis should be (0, 0, -1) eye = vec3(0, 0, 5); lookAt = vec3(0, 0, 4); CalculateViewMatrix(); }
// ----------------------------------------------------------------- void ModuleCamera3D::Move(const vec3 &Movement) { Position += Movement; Reference += Movement; CalculateViewMatrix(); }
// ----------------------------------------------------------------- void ModuleCamera3D::LookAt( const vec3 &Spot) { Reference = Spot; Z = normalize(Position - Reference); X = normalize(cross(vec3(0.0f, 1.0f, 0.0f), Z)); Y = cross(Z, X); CalculateViewMatrix(); }
void Camera::Rotate(glm::vec3 axis, float degrees,bool localSpace) { glm::quat rotation = glm::angleAxis(glm::radians(degrees), glm::normalize(axis)); if(localSpace) orientation = rotation * orientation; else orientation = orientation * rotation; CalculateLocalVectors(); CalculateViewMatrix(); }
ModuleCamera3D::ModuleCamera3D(Application* app, bool start_enabled) : Module(app, start_enabled) { CalculateViewMatrix(); X = vec3(1.0f, 0.0f, 0.0f); Y = vec3(0.0f, 1.0f, 0.0f); Z = vec3(0.0f, 0.0f, 1.0f); Position = vec3(0.0f, 0.0f, 5.0f); Reference = vec3(0.0f, 0.0f, 0.0f); }
void CCameraKernel::SetViewMatrix(D3DXMATRIX* pMatrix /* = NULL */) { if (pMatrix) m_matView = *pMatrix; else CalculateViewMatrix(&m_matView); m_pDevice->SetTransform(D3DTS_VIEW, &m_matView); m_vRightVector = D3DXVECTOR3(m_matView._11, m_matView._12, m_matView._13); m_vUpVector = D3DXVECTOR3(m_matView._21, m_matView._22, m_matView._23); m_vLookVector = D3DXVECTOR3(m_matView._31, m_matView._32, m_matView._33); }
void Camera::Update(float dt) { static float st = 0.f; st = dt; //this->Rotate(0, 0.01f, 0); //eye += vec3(0, 0.01, 0); //lookAt += vec3(0, 0.01, 0); //needsUpdate = true; if (needsUpdate) { CalculateViewMatrix(); } }
VOID CameraClass::SetViewMatrix(D3DXMATRIX * pMatrix) { if (pMatrix) { m_matView = *pMatrix; } else { CalculateViewMatrix(&m_matView); } m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView); m_vRightVector = D3DXVECTOR3(m_matView._11, m_matView._12, m_matView._13); m_vUpVector = D3DXVECTOR3(m_matView._21, m_matView._22, m_matView._23); m_vLookVector = D3DXVECTOR3(m_matView._31, m_matView._32, m_matView._33); }
// ----------------------------------------------------------------- void ModuleCamera3D::Look(const vec3 &Position, const vec3 &Reference, bool RotateAroundReference) { this->Position = Position; this->Reference = Reference; Z = normalize(Position - Reference); X = normalize(cross(vec3(0.0f, 1.0f, 0.0f), Z)); Y = cross(Z, X); if(!RotateAroundReference) { this->Reference = this->Position; this->Position += Z * 0.05f; } CalculateViewMatrix(); }
void Camera::Rotate(float x, float y, float z) { // We want to move LookAt Point appropriately. // First update matrix if eye position or lookAt point have changed if (needsUpdate) { CalculateViewMatrix(); } // Calculate rotation matrix for this rotation mat4 m = glm::mat4_cast(glm::quat(vec3(x, y, z))); Vec4 newZ = m * Vec4(zAxis.x, zAxis.y, zAxis.z, 0.0f); zAxis = Vec3(newZ.x, newZ.y, newZ.z) ; lookAt = eye + zAxis; right = Cross(zAxis, up); needsUpdate = true; }
void Camera::Update(float dt) { bool recalculateViewProjectionMatrix = mRecalculateViewMatrix || mRecalculateProjectionMatrix; if(mRecalculateViewMatrix) { CalculateViewMatrix(); } if(mRecalculateProjectionMatrix) { CalculateProjectionMatrix(); } if(recalculateViewProjectionMatrix) { XMMATRIX view = XMLoadFloat4x4(&mViewMatrix); XMMATRIX proj = XMLoadFloat4x4(&mProjectionMatrix); XMMATRIX viewProj = view * proj; XMStoreFloat4x4(&mViewProjectionMatrix, viewProj); } }
void Camera::Translate(glm::vec3 direction, float distance) { position += direction * distance; CalculateViewMatrix(); }
Camera::Camera(float width, float height, glm::vec3 initialPosition, glm::vec3 initialRotation, float fov, float nearClip, float farClip) :Transform(initialPosition, initialRotation), camFOV(fov), camFarClip(farClip), camNearClip(nearClip) { SetViewportSize(width, height); CalculateViewMatrix(); }
// ----------------------------------------------------------------- update_status ModuleCamera3D::Update() { // OnKeys WASD keys ----------------------------------- // TODO 3: Make the camera go up/down when pressing R (up) F(down) if (App->input->GetKey(SDL_SCANCODE_R) == KEY_REPEAT) { App->camera->Move({ 0, 1.0f, 0 }); } if (App->input->GetKey(SDL_SCANCODE_F) == KEY_REPEAT) { App->camera->Move({ 0, -1.0f, 0 }); } // TODO 4: Make the camera go forward (w) and backward with (s) // Note that the vectors X/Y/Z contain the current axis of the camera // you can read them to modify Position if (App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) { App->camera->Move({ 0, 0, -1.0f }); } if (App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) { App->camera->Move({ 0, 0, 1.0f }); } // TODO 5: Make the camera go left (a) and right with (d) // Note that the vectors X/Y/Z contain the current axis of the camera // you can read them to modify Position if (App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) { App->camera->Move({ -1.0f, 0, 0 }); //App->camera->Reference.x += 0; //App->camera->Reference.y += 0; //App->camera->Reference.z += -50.0f; } if (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) { App->camera->Move({ 1.0f, 0, 0 }); } // Mouse motion ---------------- if(App->input->GetMouseButton(SDL_BUTTON_RIGHT) == KEY_REPEAT) { int dx = -App->input->GetMouseXMotion(); int dy = -App->input->GetMouseYMotion(); // TODO (Homework): Rotate the camera with the mouse //LookAt({ dx, dy, 0 }); } // Recalculate matrix ------------- CalculateViewMatrix(); return UPDATE_CONTINUE; }
// ----------------------------------------------------------------- update_status ModuleCamera3D::Update() { // OnKeys WASD keys ----------------------------------- // TODO 3: Make the camera go up/down when pressing R (up) F(down) if (App->input->GetKey(SDL_SCANCODE_R) == KEY_REPEAT) { Move(vec3(0.0f, 0.1f, 0.0f)); } if (App->input->GetKey(SDL_SCANCODE_F) == KEY_REPEAT) { Move(vec3(0.0f, -0.1f, 0.0f)); } /* TODO 4: Make the camera go forward (w) and backward with (s) Note that the vectors X/Y/Z contain the current axis of the camera you can read them to modify Position*/ if (App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) { Move(vec3(0.0f, 0.0f, -0.1f)); } if (App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) { Move(vec3(0.0f, 0.0f, 0.1f)); } // TODO 5: Make the camera go left (a) and right with (d) // Note that the vectors X/Y/Z contain the current axis of the camera // you can read them to modify Position if (App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) { Move(vec3(-0.1f, 0.0f, 0.0f)); } if (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) { Move(vec3(0.1f, 0.0f, 0.0f)); } if (App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT){ LookAt(vec3(1.0f, 1.0f, 1.0f)); } if (App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT){ Y -= 0.1f; } /*y += 0.1f; z += 0.1f; x += 0.1f;*/ // Mouse motion ---------------- if(App->input->GetMouseButton(SDL_BUTTON_RIGHT) == KEY_REPEAT) { int dx = -App->input->GetMouseXMotion(); int dy = -App->input->GetMouseYMotion(); // TODO (Homework): Rotate the camera with the mouse } // Recalculate matrix ------------- CalculateViewMatrix(); return UPDATE_CONTINUE; }