Esempio n. 1
0
void Camera::updateCameraMovement(double prevTime) {
	double currTime = glfwGetTime();
	float deltaTime = float(currTime - prevTime);

	glm::vec3 position = getCenter();
	glm::vec3 lookIn = getLookAt() - getCenter();
	glm::vec3 forward = glm::vec3(lookIn.x, 0, lookIn.z);

	bool moved = false;

	// Move forward
	if (glfwGetKey(m_window, GLFW_KEY_W) == GLFW_PRESS){
		position += forward * deltaTime * m_moveSpeed;
		moved = true;
	}
	// Move backward
	if (glfwGetKey(m_window, GLFW_KEY_S) == GLFW_PRESS){
		position -= forward * deltaTime * m_moveSpeed;
		moved = true;
	}
	// Strafe right
	if (glfwGetKey(m_window, GLFW_KEY_D) == GLFW_PRESS){
		position += getRight() * deltaTime * m_moveSpeed;
		moved = true;
	}
	// Strafe left
	if (glfwGetKey(m_window, GLFW_KEY_A) == GLFW_PRESS){
		position -= getRight() * deltaTime * m_moveSpeed;
		moved = true;
	}

	setCenter(position);
	setLookAt(getCenter() + lookIn);
}
Esempio n. 2
0
void Camera::updateCameraRotation() {

	double xPos, yPos;
	glfwGetCursorPos(m_window, &xPos, &yPos); // get current mouse position
	
	// if mouse still in window center, no need to recalculate view matrix
	if (xPos == m_windowWidth / 2 && yPos == m_windowHeight / 2) {
		return;
	}
	centerMouse();

	m_horizontalAngle += float(m_windowWidth / 2 - xPos) * m_mouseSpeed;
	m_verticalAngle += float(m_windowHeight / 2 - yPos) * m_mouseSpeed;

	glm::vec3 oldLookIn = getLookAt() - getCenter();
	glm::vec3 lookInHorizontal = glm::vec3(sin(m_horizontalAngle), oldLookIn.y, cos(m_horizontalAngle));
	glm::vec3 lookInVertical = glm::vec3(oldLookIn.x * cos(m_verticalAngle), sin(m_verticalAngle), oldLookIn.z * cos(m_verticalAngle));
	glm::vec3 newLookIn = glm::normalize(lookInHorizontal + lookInVertical);
	
	setLookAt(getCenter() + newLookIn);

	float rightHorizontalAngle = m_horizontalAngle - PI / 2.0f;
	setRight(glm::vec3(sin(rightHorizontalAngle), getRight().y, cos(rightHorizontalAngle)));

	setUp(glm::normalize(glm::cross(getRight(), newLookIn)));
}
Esempio n. 3
0
void FreeCamera::update(float dt) {
		if (is_mouse_down) {
			glm::vec2 delta = mouse_pos - click_pos;
			delta.x /= width;
			delta.y /= height;
			look_at.x += delta.x * 3;
			look_at.y -= delta.y * 3;
			click_pos = mouse_pos;
			look_at.y = glm::clamp(look_at.y, -1.4f, 1.4f);
		}
		glm::vec3 dir;
		if(keys_down[FORWARD]) {
			dir += getLookAt() - pos;
		}
		if(keys_down[BACK]) {
			dir -= getLookAt() - pos;
		}
		if(keys_down[LEFT]) {
			glm::vec3 left_vec = -glm::cross(getLookAt() - pos, glm::vec3(0,1,0));
			dir += glm::normalize(left_vec);
		}
		if(keys_down[RIGHT]) {
			glm::vec3 left_vec = -glm::cross(getLookAt() - pos, glm::vec3(0,1,0));
			dir -= glm::normalize(left_vec);
		}
		glm::vec3 t = dir;
		t *= 2 * acc * dt;
		cur_speed += t;
		float len = glm::length(cur_speed);
		if (len > 0.1)
			cur_speed /= len;
		len -= acc * dt;
		len = glm::clamp(len, 0.0f, max_speed);
		cur_speed *= len;
		glm::vec3 pos_inc = cur_speed;
		pos_inc *= dt;
		pos += pos_inc;		
		if (glm::length(pos) < min_limit) {
			pos /= glm::length(pos);
			pos *= min_limit;
		}
		if (glm::length(pos) > max_limit) {
			pos /= glm::length(pos);
			pos *= max_limit;
		}
}
void Camera::dragMouse( int x, int y )
{
	Vec3f mouseDelta   = Vec3f(x,y,0.0f) - mLastMousePosition;
	mLastMousePosition = Vec3f(x,y,0.0f);

	switch(mCurrentMouseAction)
	{
	case kActionTranslate:
		{
			calculateViewingTransformParameters();

			double xTrack =  -mouseDelta[0] * kMouseTranslationXSensitivity;
			double yTrack =  mouseDelta[1] * kMouseTranslationYSensitivity;

			Vec3f transXAxis = mUpVector ^ (mPosition - mLookAt);
			transXAxis /= sqrt((transXAxis*transXAxis));
			Vec3f transYAxis = (mPosition - mLookAt) ^ transXAxis;
			transYAxis /= sqrt((transYAxis*transYAxis));

			setLookAt(getLookAt() + transXAxis*xTrack + transYAxis*yTrack);
			
			break;
		}
	case kActionRotate:
		{
			float dAzimuth		=   -mouseDelta[0] * kMouseRotationSensitivity;
			float dElevation	=   mouseDelta[1] * kMouseRotationSensitivity;
			
			setAzimuth(getAzimuth() + dAzimuth);
			setElevation(getElevation() + dElevation);

			if (getAzimuth() > M_PI) 
				mAzimuth -= 2.0*M_PI;
			if (getElevation() > M_PI) 
				mElevation -= 2.0*M_PI;

			fprintf(stderr, "az %f, elev %f\n", mAzimuth, mElevation);

			break;
		}
	case kActionZoom:
		{
			float dDolly = -mouseDelta[1] * kMouseZoomSensitivity;
			setDolly(getDolly() + dDolly);
			break;
		}
	case kActionTwist:
		// Not implemented
	default:
		break;
	}

}
Esempio n. 5
0
  void tuiSaveCallback(FILE *fph)
  {
    if ( gTfrac )
    {
      BigFloat xleft,xright,ytop;
      tf_getFractalCoordinates(gTfrac,xleft,xright,ytop);
      char str1[2048];
      char str2[2048];
      char str3[2048];
      toString(xleft,str1);
      toString(xright,str2);
      toString(ytop,str3);

      fprintf(fph,"FractalCoordinates %s %s %s\r\n", str1, str2, str3 );

      NxF32 eye[3];
      NxF32 look[3];
      getLookAt(eye,look);
      fprintf(fph,"PsLookAt %0.9f %0.9f %0.9f  %0.9f %0.9f %0.9f\r\n", eye[0], eye[1], eye[2], look[0], look[1], look[2] );
    }
  }
Esempio n. 6
0
void Camera::updateViewMatrix() {
	setViewMatrix(glm::lookAt(getCenter(), getLookAt(), getUp()));
}
Esempio n. 7
0
void Camera::printDebugInfo() {
	fprintf(stderr, "%s, %s\n", glm::to_string(getCenter()).c_str(), glm::to_string(getLookAt()).c_str());
}
Esempio n. 8
0
glm::mat4x4 FreeCamera::ViewMatrix() const {
	glm::vec3 up = glm::vec3(0, 1, 0);
	return glm::lookAt(pos, getLookAt(), up);
}
Esempio n. 9
0
Glas::Vector3f TPSCamera::getPosition() const{
	auto direction = __impl__->attitude * Glas::Vector3f(0,0,1);
	return getLookAt() - direction.normalize() * __impl__->distance;
}
Esempio n. 10
0
void Camera::dragMouse( int x, int y )
{
	Vec3f mouseDelta   = Vec3f(x,y,0.0f) - mLastMousePosition;

	switch(mCurrentMouseAction)
	{
	case kActionTranslate:
		{
			// Determine mouse movement
			double xTrack =  -mouseDelta[0] * kMouseTranslationXSensitivity;
			double yTrack =  mouseDelta[1] * kMouseTranslationYSensitivity;

			// Recalculate stuff if needed
			if (m_bDirtyTransform)
			{
				updateTransform();
			}

			// Determine directions of motion in scene space
			Vec3f direction = m_vPosition - m_vLookAt;
			Vec3f transXAxis = m_vUpVector ^ direction;
			transXAxis /= sqrt((transXAxis*transXAxis));
			Vec3f transYAxis = direction ^ transXAxis;
			transYAxis /= sqrt((transYAxis*transYAxis));

			// Move the camera's look-at point
			setLookAt(getLookAt() + transXAxis*xTrack + transYAxis*yTrack);
			
			break;
		}
	case kActionRotate:
		{
			// Store the rotation in this quarternion
			float quat[4];

			// Get the mouse coordinates in a range between -1.0 and 1.0
			float viewHalfWidth = (m_iViewportWidth / 2.f), viewHalfHeight = (m_iViewportHeight / 2.f);
			float oldX = mLastMousePosition[0] * 2.f / (m_iViewportWidth - 1) - 1.f,
				  oldY = mLastMousePosition[1] * 2.f / (m_iViewportHeight - 1) - 1.f,
				  newX = x * 2.f / (m_iViewportWidth - 1) - 1.f,
				  newY = y * 2.f / (m_iViewportHeight - 1) - 1.f;

			// Get the quaternion to rotate around, from the trackball code.
			trackball( quat, oldX, -oldY, newX, -newY );

			// Add the new quaternion to the current one.
			float oldQuat[4];
			memcpy(oldQuat, m_fQuat, sizeof(float) * 4);
			//add_quats(oldQuat, quat, m_fQuat);
			add_quats(quat, oldQuat, m_fQuat);

			// Update the transform parameters.
			updateTransform();

			break;
		}
	case kActionZoom:
		{
			// Determine dolly movement.
			float fDollyDelta = -mouseDelta[1] * kMouseZoomSensitivity;

			// Add to dolly
			setDolly(getDolly() + fDollyDelta);

			updateTransform();

			break;
		}
	case kActionTwist:
		{
			break;
		}
	default:
		break;
	}

	mLastMousePosition = Vec3f(x,y,0.0f);

}