示例#1
0
void Camera::setModelviewMatrix() {
    GLfloat matrix[16];
    Vector3 eyeVector(eye.x, eye.y, eye.z);

    matrix[0] = u.x;
    matrix[4] = u.y;
    matrix[8] = u.z;
    matrix[12] = -eyeVector.dot(u);
    matrix[1] = v.x;
    matrix[5] = v.y;
    matrix[9] = v.z;
    matrix[13] = -eyeVector.dot(v);
    matrix[2] = n.x;
    matrix[6] = n.y;
    matrix[10] = n.z;
    matrix[14] = -eyeVector.dot(n);
    matrix[3] = 0;
    matrix[7] = 0;
    matrix[11] = 0;
    matrix[15] = 1;

    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(matrix);
}
	void PinholeCamera::update(const GameTimer & timer)
	{
		RayTraceCamera::update(timer);
		XMFLOAT3 movementAmount = { 0.0f, 0.0f, 0.0f };

		if (mKeyboard != nullptr)
		{
			if (mKeyboard->isKeyDown(DIK_W))
			{
				movementAmount.y = 1.0f;
			}
			if (mKeyboard->isKeyDown(DIK_S))
			{
				movementAmount.y = -1.0f;
			}
			if (mKeyboard->isKeyDown(DIK_A))
			{
				movementAmount.x = -1.0f;
			}
			if (mKeyboard->isKeyDown(DIK_D))
			{
				movementAmount.x = 1.0f;
			}


			if (mKeyboard->isKeyDown(DIK_R))
			{
				movementAmount.z = -1.0f ;
			}
			if (mKeyboard->isKeyDown(DIK_F))
			{
				movementAmount.z = 1.0f;
			}

			if (mKeyboard->isKeyDown(DIK_Q))
			{
				mViewPlaneDistance -= 10.0f * timer.elapsedGameTime();
			}
			if (mKeyboard->isKeyDown(DIK_E))
			{
				mViewPlaneDistance += 10.0f * timer.elapsedGameTime();
			}
		}

		float rotationAmount = 0;
		if ((mMouse != nullptr))
		{
			if ((mMouse->isButtonDown(MouseButtons::MouseButtonsLeft)))
			{

				LPDIMOUSESTATE mouseState = mMouse->currentState();
				rotationAmount = -mouseState->lX ;
				//rotationAmount.y = -mouseState->lY * mMouseSensitivity;
			}
		}
		//XMStoreFloat3(&mUp,XMVector3Transform(upVector(), XMMatrixRotationZ(rotationAmount/180.0f * 3.14)));
		XMVECTOR position = eyeVector();
		XMVECTOR movement = XMLoadFloat3(&movementAmount) * mMovementRate * timer.elapsedGameTime();

		/*XMVECTOR strafe = right * XMVectorGetX(movement);
		position += strafe;

		XMVECTOR forward = XMLoadFloat3(&mDirection) * XMVectorGetY(movement);
		position += forward;*/

		XMStoreFloat3(&mPosition, position + movement);
		
	}