glarcball.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:5k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: glarcball.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 20:50:20  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: glarcball.cpp,v 1000.1 2004/06/01 20:50:20 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE
  13.  *               National Center for Biotechnology Information
  14.  *
  15.  *  This software / database is a "United States Government Work" under the
  16.  *  terms of the United States Copyright Act.  It was written as part of
  17.  *  the author's official duties as a United States Government employee and
  18.  *  thus cannot be copyrighted.  This software / database is freely available
  19.  *  to the public for use. The National Library of Medicine and the U.S.
  20.  *  Government have not placed any restriction on its use or reproduction.
  21.  *
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy
  23.  *  and reliability of the software and data, the NLM and the U.S.
  24.  *  Government do not and cannot warrant the performance or results that
  25.  *  may be obtained by using this software or data. The NLM and the U.S.
  26.  *  Government disclaim all warranties, express or implied, including
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.
  29.  *
  30.  *  Please cite the author in any work or product based on this material.
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Authors:  Mike DiCuccio
  35.  *
  36.  * File Description:
  37.  *
  38.  */
  39. #include <ncbi_pch.hpp>
  40. #include <gui/opengl.h>
  41. #include <gui/opengl/glarcball.hpp>
  42. #include <math.h>
  43. BEGIN_NCBI_SCOPE
  44. //
  45. // default ctor
  46. CGlArcBall::CGlArcBall()
  47.     : m_IsDragging(false),
  48.       m_ScreenX(100),
  49.       m_ScreenY(100),
  50.       m_MouseX(0),
  51.       m_MouseY(0),
  52.       m_Center(0.0, 0.0, 0.0, 0.0),
  53.       m_Radius(5.0),
  54.       m_QuatNow(0.0, 0.0, 0.0, 1.0),
  55.       m_QuatDown(0.0, 0.0, 0.0, 1.0),
  56.       m_QuatDrag(0.0, 0.0, 0.0, 1.0)
  57. {
  58.     m_MatNow.Identity();
  59. }
  60. //
  61. // conversion ctor
  62. CGlArcBall::CGlArcBall(const TVect& center, float radius)
  63.     : m_IsDragging(false),
  64.       m_ScreenX(100),
  65.       m_ScreenY(100),
  66.       m_MouseX(0),
  67.       m_MouseY(0),
  68.       m_Center(0.0, 0.0, 0.0, 0.0),
  69.       m_Radius(1.0),
  70.       m_QuatNow(0.0, 0.0, 0.0, 1.0),
  71.       m_QuatDown(0.0, 0.0, 0.0, 1.0),
  72.       m_QuatDrag(0.0, 0.0, 0.0, 1.0)
  73. {
  74.     m_MatNow.Identity();
  75.     Place(center, radius);
  76. }
  77. //
  78. // dtor
  79. CGlArcBall::~CGlArcBall()
  80. {
  81. }
  82. //
  83. // place the arc world with a point of rotation and a radius
  84. void CGlArcBall::Place(const TVect& center, float radius)
  85. {
  86.     m_Center = center;
  87.     m_Radius = radius;
  88. }
  89. //
  90. // place the arc world with a point of rotation, keeping the current radius
  91. void CGlArcBall::Place(const TVect& center)
  92. {
  93.     m_Center = center;
  94. }
  95. //
  96. // begin a drag
  97. void CGlArcBall::BeginDrag()
  98. {
  99.     m_IsDragging = true;
  100.     m_DragFrom = x_ToSphere(m_MouseX, m_MouseY);
  101. }
  102. //
  103. // end a drag
  104. void CGlArcBall::EndDrag()
  105. {
  106.     m_IsDragging = false;
  107.     m_QuatDown = m_QuatNow;
  108. }
  109. //
  110. //update the arc ball with a given position
  111. void CGlArcBall::Update(int x, int y)
  112. {
  113.     m_MouseX =  2.0f * (float(x) / float(m_ScreenX)) - 1.0f;
  114.     m_MouseY = -2.0f * (float(y) / float(m_ScreenY)) + 1.0f;
  115.     if (m_IsDragging)
  116.     {
  117.         CVect4<float> to = x_ToSphere(m_MouseX, m_MouseY);
  118.         // set the dragging quat
  119.         m_QuatDrag.X() = m_DragFrom.Y() * to.Z() - m_DragFrom.Z() * to.Y();
  120.         m_QuatDrag.Y() = m_DragFrom.Z() * to.X() - m_DragFrom.X() * to.Z();
  121.         m_QuatDrag.Z() = m_DragFrom.X() * to.Y() - m_DragFrom.Y() * to.X();
  122.         m_QuatDrag.W() = m_DragFrom.X() * to.X() +
  123.             m_DragFrom.Y() * to.Y() +
  124.             m_DragFrom.Z() * to.Z();
  125.         // set the current quat
  126.         m_QuatNow = m_QuatDrag * m_QuatDown;
  127.         // convert to a matrix for OpenGL
  128.         m_QuatNow.Conjugate().ToMatrix(m_MatNow);
  129.     }
  130. }
  131. //
  132. // convert 2d screen coordinates to 3d sphere coordinates
  133. CVect4<float> CGlArcBall::x_ToSphere(float x, float y)
  134. {
  135.     CVect4<float> result;
  136.     result.X() = (x - m_Center.X()) / m_Radius;
  137.     result.Y() = (y - m_Center.Y()) / m_Radius;
  138.     float mag = result.X() * result.X() + result.Y() * result.Y();
  139.     if (mag > 1.0)
  140.     {
  141.         float scale = 1.0f / ::sqrt(mag);
  142.         result.X() *= scale;
  143.         result.Y() *= scale;
  144.         result.Z() = 0.0;
  145.     }
  146.     else
  147.     {
  148.         result.Z() = (float)::sqrt(1.0 - mag);
  149.     }
  150.     result.W() = 0.0;
  151.     return result;
  152. }
  153. //
  154. // apply the matrix using OpenGL
  155. void CGlArcBall::ApplyGL() const
  156. {
  157.     glMultMatrixf(m_MatNow.GetData());
  158. }
  159. END_NCBI_SCOPE
  160. /*
  161.  * ===========================================================================
  162.  * $Log: glarcball.cpp,v $
  163.  * Revision 1000.1  2004/06/01 20:50:20  gouriano
  164.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
  165.  *
  166.  * Revision 1.3  2004/05/21 22:27:44  gorelenk
  167.  * Added PCH ncbi_pch.hpp
  168.  *
  169.  * Revision 1.2  2004/05/03 13:03:15  dicuccio
  170.  * Fixed compiler warnings on MSVC7
  171.  *
  172.  * Revision 1.1  2003/06/09 19:22:13  dicuccio
  173.  * Initial revision
  174.  *
  175.  * ===========================================================================
  176.  */