glmtransform.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:7k
- /*
- * ===========================================================================
- * PRODUCTION $Log: glmtransform.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 21:13:41 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: glmtransform.cpp,v 1000.1 2004/06/01 21:13:41 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio, Vladimir Tereshkov
- *
- * File Description:
- * Arbitrary OpenGL Mouse transformations class based on CGlArcBall with addition of pan and zoom
- */
- #include <ncbi_pch.hpp>
- #include "glmtransform.hpp"
- #include <gui/opengl.h>
- #include <math.h>
- BEGIN_NCBI_SCOPE
- //
- // default ctor
- CGlMTransform::CGlMTransform()
- : m_IsDragging(false),
- m_ScreenX(100),
- m_ScreenY(100),
- m_MouseX(0),
- m_MouseY(0),
- m_Center(0.0, 0.0, 0.0, 0.0),
- m_Radius(5.0),
- m_QuatNow(0.0, 0.0, 0.0, 1.0),
- m_QuatDown(0.0, 0.0, 0.0, 1.0),
- m_QuatDrag(0.0, 0.0, 0.0, 1.0),
- m_Translate(0, 0, 0),
- m_TranslateDown(0, 0, 0)
- {
- }
- //
- // conversion ctor
- CGlMTransform::CGlMTransform(const TVect& center, float radius)
- : m_IsDragging(false),
- m_ScreenX(100),
- m_ScreenY(100),
- m_MouseX(0),
- m_MouseY(0),
- m_Center(0.0, 0.0, 0.0, 0.0),
- m_Radius(1.0),
- m_QuatNow(0.0, 0.0, 0.0, 1.0),
- m_QuatDown(0.0, 0.0, 0.0, 1.0),
- m_QuatDrag(0.0, 0.0, 0.0, 1.0),
- m_Translate(0, 0, 0),
- m_TranslateDown(0, 0, 0)
- {
- m_MatNow.Identity();
- Place(center, radius);
- }
- //
- // dtor
- CGlMTransform::~CGlMTransform()
- {
- }
- //
- // place the arc world with a point of rotation and a radius
- void CGlMTransform::Place(const TVect& center, float radius)
- {
- m_Center = center;
- m_Radius = radius;
- }
- //
- // place the arc world with a point of rotation, keeping the current radius
- void CGlMTransform::Place(const TVect& center)
- {
- m_Center = center;
- }
- //
- // begin a drag
- void CGlMTransform::BeginDrag()
- {
- m_IsDragging = true;
- m_DragFrom = x_ToSphere(m_MouseX, m_MouseY);
- }
- //
- // end a drag
- void CGlMTransform::EndDrag()
- {
- m_IsDragging = false;
- m_QuatDown = m_QuatNow;
- m_TranslateDown = m_Translate;
- }
- //
- //update the arc ball with a given position
- void CGlMTransform::Update(int x, int y)
- {
- m_MouseX = 2.0 * (float(x) / float(m_ScreenX)) - 1.0;
- m_MouseY = -2.0 * (float(y) / float(m_ScreenY)) + 1.0;
- if (m_IsDragging)
- {
- CVect4<float> to = x_ToSphere(m_MouseX, m_MouseY);
- switch (m_Mode){
- case ePan:
- m_Translate[0] = to.X() - m_DragFrom.X();
- m_Translate[1] = to.Y() - m_DragFrom.Y();
- m_Translate[2] = 0;
- m_Translate+=m_TranslateDown;
- break;
- case eZoom:
- m_Translate[0]=0;
- m_Translate[1]=0;
- m_Translate[2] = 2 * (to.Y() - m_DragFrom.Y());
- m_Translate+=m_TranslateDown;
- break;
- case eRotate:
- // set the dragging quat
- m_QuatDrag.X() = m_DragFrom.Y() * to.Z() - m_DragFrom.Z() * to.Y();
- m_QuatDrag.Y() = m_DragFrom.Z() * to.X() - m_DragFrom.X() * to.Z();
- m_QuatDrag.Z() = m_DragFrom.X() * to.Y() - m_DragFrom.Y() * to.X();
- m_QuatDrag.W() = m_DragFrom.X() * to.X() +
- m_DragFrom.Y() * to.Y() +
- m_DragFrom.Z() * to.Z();
-
- // set the current quat
- m_QuatNow = m_QuatDrag * m_QuatDown;
- // convert to a matrix for OpenGL
- m_QuatNow.Conjugate().ToMatrix(m_MatNow);
- break;
- }
- }
- }
- //
- // convert 2d screen coordinates to 3d sphere coordinates
- CVect4<float> CGlMTransform::x_ToSphere(float x, float y)
- {
- CVect4<float> result;
- result.X() = (x - m_Center.X())/ m_Radius;
- result.Y() = (y - m_Center.Y())/ m_Radius;
- if (m_Mode == eRotate){
- float mag = result.X() * result.X() + result.Y() * result.Y();
- if (mag > 1.0)
- {
- float scale = 1.0 / ::sqrt(mag);
- result.X() *= scale;
- result.Y() *= scale;
- result.Z() = 0.0;
- }
- else
- {
- result.Z() = ::sqrt(1.0 - mag);
- }
- result.W() = 0.0;
- }
- return result;
- }
- //
- // apply the matrix using OpenGL
- void CGlMTransform::ApplyGL(EMode mode) const
- {
- switch (mode){
- case eRotate:
- glMultMatrixf(m_MatNow.GetData());
- break;
- case eTranslate:
- glTranslatef(m_Translate[0], m_Translate[1], m_Translate[2]);
- break;
- case eAll:
- glTranslatef(m_Translate[0], m_Translate[1], m_Translate[2]);
- glMultMatrixf(m_MatNow.GetData());
- break;
- case ePan:
- glTranslatef(m_Translate[0], m_Translate[1], 0);
- break;
- case eZoom:
- glTranslatef(0, 0, m_Translate[2]);
- break;
- }
- }
- CGlMTransform::CGlMTransform(const CGlMTransform&src)
- {
- m_MatNow = src.GetMatrix();
- m_Translate = src.GetTranslation();
- }
- CGlMTransform& CGlMTransform::operator= (const CGlMTransform & src)
- {
- m_MatNow = src.GetMatrix();
- m_Translate = src.GetTranslation();
- return *this;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: glmtransform.cpp,v $
- * Revision 1000.1 2004/06/01 21:13:41 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
- *
- * Revision 1.3 2004/05/21 22:27:55 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.2 2004/01/28 15:51:10 tereshko
- * Code clean-up
- *
- * Revision 1.1 2004/01/05 16:20:48 tereshko
- * Initial revision
- *
- * ===========================================================================
- */