NCBI C++ ToolKit
opengl_renderer.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: opengl_renderer.hpp 33815 2007-05-04 17:18:18Z kazimird $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Paul Thiessen
27 *
28 * File Description:
29 * Classes to hold the OpenGL rendering engine
30 *
31 * ===========================================================================
32 */
33 
34 #ifndef CN3D_OPENGL_RENDERER__HPP
35 #define CN3D_OPENGL_RENDERER__HPP
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbiobj.hpp>
39 
40 #include <list>
41 #include <map>
42 #include <vector>
43 #include <string>
44 
47 
48 #include "vector_math.hpp"
49 #include "style_manager.hpp"
50 
51 class wxFont;
52 
53 // do not include GL headers here, so that other modules can more easily
54 // access this without potential name conflicts; use these instead by default,
55 // but will be set to proper types in opengl_renderer.cpp
56 #ifndef GL_ENUM_TYPE
57 #define GL_ENUM_TYPE int
58 #define GL_INT_TYPE int
59 #define GL_DOUBLE_TYPE double
60 #endif
61 
62 
63 BEGIN_SCOPE(Cn3D)
64 
65 class StructureSet;
66 class AtomStyle;
67 class BondStyle;
68 class HelixStyle;
69 class StrandStyle;
70 class Cn3DGLCanvas;
71 
73 {
74 public:
75  OpenGLRenderer(Cn3DGLCanvas *parentGLCanvas);
76 
77  // public data
78  static const unsigned int NO_NAME;
79 
80  // public methods
81 
82  // calls once-only OpenGL initialization stuff (should be called after
83  // the rendering context is established and the renderer made current)
84  void Init(void) const;
85 
86  // tells the renderer that new camera settings need to be applied - should also
87  // be called after window resize
88  void NewView(double eyeTranslateToAngleDegrees = 0.0) const;
89 
90  // get the name
91  bool GetSelected(int x, int y, unsigned int *name);
92 
93  // reset camera to full-view state
94  void ResetCamera(void);
95 
96  // called to change view (according to mouse movements)
97  enum eViewAdjust {
98  eXYRotateHV, // rotate about X,Y axes according to horiz. & vert. movement
99  eZRotateH, // rotate in plane (about Z) according to horiz. movement
100  eXYTranslateHV, // translate in X,Y according to horiz. & vert. movement
101  eZoomH, // zoom in/out with horiz. movement
102  eZoomHHVV, // zoom according to (H1,V1),(H2,V2) box
103  eZoomIn, // zoom in
104  eZoomOut, // zoom out
105  eCenterCamera // reset camera to look at origin
106  };
107  void ChangeView(eViewAdjust control, int dX = 0, int dY = 0, int X2 = 0, int Y2 = 0);
108 
109  // center the view on the given viewCenter point, and zoom the view according to radius
110  void CenterView(const Vector& viewCenter, double radius);
111 
112  // draws the display lists
113  void Display(void);
114 
115  // tells the renderer what structure(s) it's to draw
116  void AttachStructureSet(StructureSet *targetStructureSet);
117  void ComputeBestView(void);
118 
119  // constructs the structure display lists (but doesn't draw them)
120  void Construct(void);
121 
122  // push the global view matrix, then apply transformation (e.g., for structure alignment)
123  void PushMatrix(const Matrix* xform);
124  // pop matrix
125  void PopMatrix(void);
126 
127  // display list management
128  static const unsigned int NO_LIST, FIRST_LIST, FONT_BASE;
129  void StartDisplayList(unsigned int list);
130  void EndDisplayList(void);
131 
132  // frame management
133  void ShowAllFrames(void);
134  void ShowFirstFrame(void);
135  void ShowLastFrame(void);
136  void ShowNextFrame(void);
137  void ShowPreviousFrame(void);
138  void ShowFrameNumber(int frame);
139 
140  // drawing methods
141  void DrawAtom(const Vector& site, const AtomStyle& atomStyle);
142  void DrawBond(const Vector& site1, const Vector& site2, const BondStyle& style,
143  const Vector *site0, const Vector* site3);
144  void DrawHelix(const Vector& Nterm, const Vector& Cterm, const HelixStyle& helixStyle);
145  void DrawStrand(const Vector& Nterm, const Vector& Cterm,
146  const Vector& unitNormal, const StrandStyle& strandStyle);
147  void DrawLabel(const std::string& text, const Vector& center, const Vector& color);
148 
149  // load/save camera angle from/to asn data
150  bool SaveToASNViewSettings(ncbi::objects::CCn3d_user_annotations *annotations);
151  bool LoadFromASNViewSettings(const ncbi::objects::CCn3d_user_annotations& annotations);
152  bool HasASNViewSettings(void) const { return !initialViewFromASN.Empty(); }
153 
154  // restore to saved view settings
155  void RestoreSavedView(void);
156 
157  // set font used by OpenGL to the wxFont associated with the glCanvas
158  bool SetGLFont(int firstChar, int nChars, int fontBase);
159  const wxFont& GetGLFont(void) const;
160 
161  // stereo
162  void EnableStereo(bool enableStereo);
163 
164  double GetRotateSpeed(void) const { return rotateSpeed; }
165 
166  void RecreateQuadric(void) const;
167 
168 private:
169 
172 
174  eResetCache, // reset cached values (no call to glMaterial)
175  eSetCacheValues, // set values in cache, but don't call glMaterial
176  eUseCachedValues, // set color with cached values
177  eSetColorIfDifferent // set color IFF values are different from cached values (+ set cache to new values)
178  };
179  void SetColor(EColorAction action, GL_ENUM_TYPE = 0,
180  GL_DOUBLE_TYPE red = 0.0, GL_DOUBLE_TYPE green = 0.0, GL_DOUBLE_TYPE blue = 0.0, GL_DOUBLE_TYPE alpha = 1.0);
181 
182  // only defined #if USE_MY_GLU_QUADS (in opengl_renderer.cpp)
183  void MyGluDisk(GL_DOUBLE_TYPE innerRadius, GL_DOUBLE_TYPE outerRadius, GL_INT_TYPE slices, GL_INT_TYPE loops);
184  void MyGluCylinder(GL_DOUBLE_TYPE baseRadius, GL_DOUBLE_TYPE topRadius,
185  GL_DOUBLE_TYPE height, GL_INT_TYPE slices, GL_INT_TYPE stacks);
186  void MyGluSphere(GL_DOUBLE_TYPE radius, GL_INT_TYPE slices, GL_INT_TYPE stacks);
187 
188  void DrawHalfBond(const Vector& site1, const Vector& midpoint,
189  StyleManager::eDisplayStyle style, double radius, bool cap1, bool cap2);
190  void DrawHalfWorm(const Vector *p0, const Vector& p1,
191  const Vector& p2, const Vector *p3, double radius, bool cap1, bool cap2, double tension);
192 
193  void ConstructLogo(void);
194 
195  // camera data
200  ncbi::CRef < ncbi::objects::CCn3d_view_settings > initialViewFromASN;
201 
202  // controls for view changes
203  double rotateSpeed;
204 
205  // misc rendering stuff
208  unsigned int currentFrame;
209  std::vector < bool > displayListEmpty;
210  bool IsFrameEmpty(unsigned int frame) const;
211  unsigned int currentDisplayList;
212 
213  // controls for stereo
214  bool stereoOn;
215 
216  // stuff for rendering transparent spheres (done during Display())
217 public:
218  typedef struct {
220  unsigned int name;
221  double radius, alpha;
222  } SphereInfo;
223 private:
224  typedef std::list < SphereInfo > SphereList;
225  typedef std::map < unsigned int, SphereList > SphereMap;
227  void AddTransparentSphere(const Vector& color, unsigned int name,
228  const Vector& site, double radius, double alpha);
229  void ClearTransparentSpheresForList(unsigned int list)
230  {
231  SphereMap::iterator i = transparentSphereMap.find(list);
232  if (i != transparentSphereMap.end())
233  transparentSphereMap.erase(i);
234  }
235 
236  class SpherePtr
237  {
238  public:
239  Vector siteGL; // atom site in GL coordinates
241  const SphereInfo *ptr;
242  friend bool operator < (const SpherePtr& a, const SpherePtr& b)
243  { return (a.distanceFromCamera < b.distanceFromCamera); }
244  };
245  typedef std::list < SpherePtr > SpherePtrList;
247  void AddTransparentSpheresForList(unsigned int list);
248  void RenderTransparentSpheres(void);
249 };
250 
251 END_SCOPE(Cn3D)
252 
253 #endif // CN3D_OPENGL_RENDERER__HPP
User-defined methods of the data storage class.
User-defined methods of the data storage class.
friend bool operator<(const SpherePtr &a, const SpherePtr &b)
void MyGluSphere(double radius, int slices, int stacks)
std::list< SpherePtr > SpherePtrList
void ConstructLogo(void)
bool GetSelected(int x, int y, unsigned int *name)
std::vector< bool > displayListEmpty
void PopMatrix(void)
void ShowPreviousFrame(void)
void DrawHalfBond(const Vector &site1, const Vector &midpoint, StyleManager::eDisplayStyle style, double radius, bool cap1, bool cap2)
void CenterView(const Vector &viewCenter, double radius)
void DrawAtom(const Vector &site, const AtomStyle &atomStyle)
void ResetCamera(void)
SpherePtrList transparentSpheresToRender
void RecreateQuadric(void) const
void AddTransparentSphere(const Vector &color, unsigned int name, const Vector &site, double radius, double alpha)
std::map< unsigned int, SphereList > SphereMap
void AddTransparentSpheresForList(unsigned int list)
void ShowAllFrames(void)
void ShowLastFrame(void)
void SetColor(EColorAction action, int=0, double red=0.0, double green=0.0, double blue=0.0, double alpha=1.0)
void StartDisplayList(unsigned int list)
const wxFont & GetGLFont(void) const
void MyGluDisk(double innerRadius, double outerRadius, int slices, int loops)
SphereMap transparentSphereMap
void AttachStructureSet(StructureSet *targetStructureSet)
void Construct(void)
double viewMatrix[16]
void EndDisplayList(void)
void MyGluCylinder(double baseRadius, double topRadius, double height, int slices, int stacks)
Cn3DGLCanvas * glCanvas
void ShowFirstFrame(void)
void DrawBond(const Vector &site1, const Vector &site2, const BondStyle &style, const Vector *site0, const Vector *site3)
double GetRotateSpeed(void) const
void DrawHalfWorm(const Vector *p0, const Vector &p1, const Vector &p2, const Vector *p3, double radius, bool cap1, bool cap2, double tension)
ncbi::CRef< ncbi::objects::CCn3d_view_settings > initialViewFromASN
void ChangeView(eViewAdjust control, int dX=0, int dY=0, int X2=0, int Y2=0)
bool LoadFromASNViewSettings(const ncbi::objects::CCn3d_user_annotations &annotations)
void PushMatrix(const Matrix *xform)
bool SetGLFont(int firstChar, int nChars, int fontBase)
void RestoreSavedView(void)
void ShowFrameNumber(int frame)
void Init(void) const
void EnableStereo(bool enableStereo)
void DrawHelix(const Vector &Nterm, const Vector &Cterm, const HelixStyle &helixStyle)
void ClearTransparentSpheresForList(unsigned int list)
bool IsFrameEmpty(unsigned int frame) const
void RenderTransparentSpheres(void)
void NewView(double eyeTranslateToAngleDegrees=0.0) const
bool HasASNViewSettings(void) const
static const unsigned int NO_NAME
void ComputeBestView(void)
bool SaveToASNViewSettings(ncbi::objects::CCn3d_user_annotations *annotations)
StructureSet * structureSet
static const unsigned int FIRST_LIST
unsigned int currentFrame
std::list< SphereInfo > SphereList
void DrawStrand(const Vector &Nterm, const Vector &Cterm, const Vector &unitNormal, const StrandStyle &strandStyle)
void DrawLabel(const std::string &text, const Vector &center, const Vector &color)
static const unsigned int NO_LIST
unsigned int currentDisplayList
void ShowNextFrame(void)
static const unsigned int FONT_BASE
OpenGLRenderer(Cn3DGLCanvas *parentGLCanvas)
Include a standard set of the NCBI C++ Toolkit most basic headers.
string
Definition: cgiapp.hpp:690
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
n background color
int i
static void text(MDB_val *v)
Definition: mdb_dump.c:62
unsigned int a
Definition: ncbi_localip.c:102
ESERV_Site site
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
#define GL_DOUBLE_TYPE
#define GL_ENUM_TYPE
#define GL_INT_TYPE
Modified on Fri Sep 20 14:57:44 2024 by modify_doxy.py rev. 669887