glc_turntablemover.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Copyright (C) 2009 Laurent Bauer
00006  http://glc-lib.sourceforge.net
00007 
00008  GLC-lib is free software; you can redistribute it and/or modify
00009  it under the terms of the GNU Lesser General Public License as published by
00010  the Free Software Foundation; either version 3 of the License, or
00011  (at your option) any later version.
00012 
00013  GLC-lib is distributed in the hope that it will be useful,
00014  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  GNU Lesser General Public License for more details.
00017 
00018  You should have received a copy of the GNU Lesser General Public License
00019  along with GLC-lib; if not, write to the Free Software
00020  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 
00022  *****************************************************************************/
00023 
00024 #include "glc_turntablemover.h"
00025 #include "glc_viewport.h"
00026 
00027 // Default constructor
00028 GLC_TurnTableMover::GLC_TurnTableMover(GLC_Viewport* pViewport, const QList<GLC_RepMover*>& repsList)
00029 : GLC_Mover(pViewport, repsList)
00030 , m_Sign(1.0)
00031 {
00032 
00033 }
00034 
00035 
00036 // Copy constructor
00037 GLC_TurnTableMover::GLC_TurnTableMover(const GLC_TurnTableMover& mover)
00038 : GLC_Mover(mover)
00039 , m_Sign(mover.m_Sign)
00040 {
00041 }
00042 
00043 
00044 GLC_TurnTableMover::~GLC_TurnTableMover()
00045 {
00046 }
00047 
00048 
00050 // Get Functions
00052 
00053 // Return a clone of the mover
00054 GLC_Mover* GLC_TurnTableMover::clone() const
00055 {
00056         return new GLC_TurnTableMover(*this);
00057 }
00058 
00059 
00061 // Set Functions
00063 
00064 // Initialized the mover
00065 void GLC_TurnTableMover::init(const GLC_UserInput& userInput)
00066 {
00067         GLC_Mover::m_PreviousVector.setVect(static_cast<double>(userInput.x()), static_cast<double>(userInput.y()),0.0);
00068         GLC_Camera* pCamera= GLC_Mover::m_pViewport->cameraHandle();
00069         // Calculate angle sign
00070         m_Sign= pCamera->defaultUpVector() * pCamera->upVector();
00071         if (m_Sign == 0)
00072         {
00073                 m_Sign= 1;
00074         }
00075         else
00076         {
00077                 m_Sign= m_Sign / fabs(m_Sign);
00078         }
00079 
00080         pCamera->setUpCam(pCamera->defaultUpVector() * m_Sign);
00081 }
00082 
00083 
00084 bool GLC_TurnTableMover::move(const GLC_UserInput& userInput)
00085 {
00086         GLC_Camera* pCamera= GLC_Mover::m_pViewport->cameraHandle();
00087         // Turn table rotation
00088         const double rotSpeed= 2.3;
00089         const double width= static_cast<double> ( GLC_Mover::m_pViewport->viewVSize() );
00090         const double height= static_cast<double> ( GLC_Mover::m_pViewport->viewHSize() );
00091 
00092         const double alpha = -((static_cast<double>(userInput.x()) - GLC_Mover::m_PreviousVector.x()) / width) * rotSpeed;
00093         const double beta = ((static_cast<double>(userInput.y()) - GLC_Mover::m_PreviousVector.y()) / height) * rotSpeed;
00094 
00095         // Rotation around the screen vertical axis
00096         pCamera->rotateAroundTarget(pCamera->defaultUpVector(), alpha * m_Sign);
00097 
00098         // Rotation around the screen horizontal axis
00099         GLC_Vector3d incidentVector= -pCamera->forward();
00100         GLC_Vector3d rightVector= incidentVector ^ pCamera->upVector();
00101         if (!rightVector.isNull())
00102         {
00103                 pCamera->rotateAroundTarget(rightVector, beta);
00104         }
00105 
00106         m_PreviousVector.setVect(static_cast<double>(userInput.x()), static_cast<double>(userInput.y()), 0.0);
00107 
00108         return true;
00109 }

SourceForge.net Logo

©2005-2011 Laurent Ribon