NCBI C++ ToolKit
glvbonode.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: glvbonode.cpp 43891 2019-09-16 13:50:00Z evgeniev $
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: Bob Falk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <gui/opengl/glvbonode.hpp>
34 #include <gui/opengl/glutils.hpp>
36 #include <gui/opengl/glresmgr.hpp>
37 
39 
41 {
42 }
43 
44 // Draw mode is one of: GL_{POINTS, LINES, LINE_STRIP, LINE_LOOP,
45 // TRIANGLES, TRIANGLE_STRIP TRIANGLE_FAN, QUADS, QUAD_STRIP}
46 CGlVboNode::CGlVboNode(GLenum draw_mode)
47 {
48  SetVBOGeom(draw_mode);
49 }
50 
52 {
53  m_VBOGeom.Reset();
54 }
55 
56 void CGlVboNode::SetVBOGeom(GLenum drawMode)
57 {
58  m_VBOGeom.Reset(CGlResMgr::Instance().CreateVboGeom(drawMode, ""));
59 }
60 
62 {
63  if (m_VBOGeom != NULL)
64  return m_VBOGeom->GetVertexFormat();
65  else NCBI_THROW(COpenGLException, eGenericError, "NULL VBO object");
66 }
67 
69 {
70  if (m_VBOGeom != NULL)
71  return m_VBOGeom->GetSecondaryFormat();
72  else NCBI_THROW(COpenGLException, eGenericError, "NULL VBO object");
73 }
74 
76 {
77  if (m_VBOGeom != NULL)
78  return m_VBOGeom->SetVertexBuffer2D(data);
79  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
80 }
81 
83 {
84  if (m_VBOGeom != NULL)
85  return m_VBOGeom->GetVertexBuffer2D(data);
86  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
87 }
88 
90 {
91  if (m_VBOGeom != NULL)
92  return m_VBOGeom->SetVertexBuffer3D(data);
93  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
94 }
95 
97 {
98  if (m_VBOGeom != NULL)
99  return m_VBOGeom->GetVertexBuffer3D(data);
100  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
101 }
102 
103 void CGlVboNode::SetColorBuffer(const vector<CRgbaColor>& data)
104 {
105  if (m_VBOGeom != NULL)
106  return m_VBOGeom->SetColorBuffer(data);
107  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
108 }
109 
110 void CGlVboNode::GetColorBuffer(vector<CRgbaColor>& data) const
111 {
112  if (m_VBOGeom != NULL)
113  return m_VBOGeom->GetColorBuffer(data);
114  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
115 }
116 
118 {
119  if (m_VBOGeom != NULL)
120  return m_VBOGeom->SetColorBufferUC(data);
121  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
122 }
123 
125 {
126  if (m_VBOGeom != NULL)
127  return m_VBOGeom->GetColorBufferUC(data);
128  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
129 }
130 
131 void CGlVboNode::SetTexCoordBuffer1D(const vector<float>& data)
132 {
133  if (m_VBOGeom != NULL)
134  return m_VBOGeom->SetTexCoordBuffer1D(data);
135  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
136 }
137 
138 void CGlVboNode::GetTexCoordBuffer1D(vector<float>& data) const
139 {
140  if (m_VBOGeom != NULL)
141  return m_VBOGeom->GetTexCoordBuffer1D(data);
142  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
143 }
144 
146 {
147  if (m_VBOGeom != NULL)
148  return m_VBOGeom->SetTexCoordBuffer(data);
149  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
150 }
151 
153 {
154  if (m_VBOGeom != NULL)
155  return m_VBOGeom->GetTexCoordBuffer(data);
156  else NCBI_THROW(COpenGLException, eGenericError, "NULL Texture Coord 1D Buffer");
157 }
158 
159 void CGlVboNode::Get2DVertexBuffer(vector<CVect2<float> >& verts) const
160 {
161  verts.clear();
162  vector<CVect3<float> > verts3d;
163 
164  switch (GetVertexFormat())
165  {
167  GetVertexBuffer2D(verts);
168  break;
170  GetVertexBuffer3D(verts3d);
171  verts.resize(verts3d.size());
172  for (size_t i = 0; i < verts3d.size(); ++i)
173  verts[i].Set(verts3d[i].X(), verts3d[i].Y());
174  break;
175  default:
176  break;
177  }
178 }
179 
180 bool CGlVboNode::GetColors(vector<CRgbaColor>& colors, bool is_greyscale) const
181 {
182  bool has_color = false;
184  if (colorFormat == IVboGeom::kSecondaryFormatColorFloat) {
185  has_color = true;
187  }
188  else if (colorFormat == IVboGeom::kSecondaryFormatColorUChar) {
189  has_color = true;
190  vector<CVect4<unsigned char> > colors_uc;
191  colors.clear();
192  GetColorBufferUC(colors_uc);
193  for (size_t i = 0; i < colors_uc.size(); ++i)
194  colors.push_back(CRgbaColor(colors_uc[i][0], colors_uc[i][1], colors_uc[i][2], colors_uc[i][3]));
195  }
196 
197  // If we are in grey scale mode, make sure all colors are greyscale.
198  if (has_color && is_greyscale) {
199  for (size_t i = 0; i < colors.size(); ++i) {
200  colors[i] = colors[i].GetGreyscale();
201  }
202  }
203 
204  return has_color;
205 }
206 
207 bool CGlVboNode::GetDefaultColor(CRgbaColor& default_color, bool is_greyscale) const
208 {
209  default_color = GetState().GetColor();
210 
211  if (is_greyscale) {
212  default_color = default_color.GetGreyscale();
213  }
214 
215  return GetState().ColorSet();
216 }
217 
219 {
220  if (m_VBOGeom != NULL) {
221  for (size_t i=0; i<m_Positions.size(); ++i) {
223  mat.Transpose();
224  m_VBOGeom->Render(mat.GetData());
225  }
226  }
227 }
228 
229 
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
static const Colors colors
Definition: cn3d_colors.cpp:50
char data[12]
Definition: iconv.c:80
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
void Transpose()
Definition: matrix4.hpp:347
const T * GetData() const
Definition: matrix4.hpp:103
void SetTexCoordBuffer(const vector< CVect2< float > > &data)
Definition: glvbonode.cpp:145
virtual ~CGlVboNode()
Delete all buffers.
Definition: glvbonode.cpp:51
void SetTexCoordBuffer1D(const vector< float > &data)
Definition: glvbonode.cpp:131
void SetVertexBuffer2D(const vector< CVect2< float > > &data)
Definition: glvbonode.cpp:75
void Get2DVertexBuffer(std::vector< CVect2< float > > &verts) const
Definition: glvbonode.cpp:159
bool GetDefaultColor(CRgbaColor &default_color, bool is_greyscale) const
Return default color from node in default_color.
Definition: glvbonode.cpp:207
static CGlResMgr & Instance()
Definition: glresmgr.cpp:59
void GetVertexBuffer3D(vector< CVect3< float > > &data) const
Definition: glvbonode.cpp:96
bool GetColors(std::vector< CRgbaColor > &colors, bool is_greyscale) const
Definition: glvbonode.cpp:180
void GetColorBuffer(vector< CRgbaColor > &data) const
Definition: glvbonode.cpp:110
ESecondaryFormat
Definition: ivbogeom.hpp:66
void GetColorBufferUC(vector< CVect4< unsigned char > > &data) const
Definition: glvbonode.cpp:124
CMatrix4< float > GetTransformedPosition(size_t idx)
return the position with rotation and pixel offset baked in
CIRef< IVboGeom > m_VBOGeom
subclass for vboset representing point, line, tri or quad geometry
Definition: glvbonode.hpp:127
vector< CMatrix4< float > > m_Positions
Set of positions (transformations) at which to render this node.
CGlState & GetState()
void SetVertexBuffer3D(const vector< CVect3< float > > &data)
Definition: glvbonode.cpp:89
void SetVBOGeom(IVboGeom *geom)
Set/get geometry object. These can be shared between nodes.
Definition: glvbonode.hpp:79
void GetTexCoordBuffer1D(vector< float > &data) const
Definition: glvbonode.cpp:138
IVboGeom::EVertexFormat GetVertexFormat() const
Definition: glvbonode.cpp:61
void GetTexCoordBuffer(vector< CVect2< float > > &data) const
Definition: glvbonode.cpp:152
CRgbaColor GetColor() const
Definition: glstate.hpp:259
void SetColorBufferUC(const vector< CVect4< unsigned char > > &data)
Definition: glvbonode.cpp:117
CGlVboNode()
Buffers are initially NULL and draw mode is not set.
Definition: glvbonode.cpp:40
IVboGeom::ESecondaryFormat GetSecondaryFormat() const
Definition: glvbonode.cpp:68
virtual void x_Render()
Render VBO(s)
Definition: glvbonode.cpp:218
void GetVertexBuffer2D(vector< CVect2< float > > &data) const
Definition: glvbonode.cpp:82
void SetColorBuffer(const vector< CRgbaColor > &data)
Definition: glvbonode.cpp:103
bool ColorSet() const
Definition: glstate.hpp:260
@ kVertexFormatVertex3D
Definition: ivbogeom.hpp:62
@ kVertexFormatVertex2D
Definition: ivbogeom.hpp:61
@ kSecondaryFormatColorUChar
Definition: ivbogeom.hpp:69
@ kSecondaryFormatColorFloat
Definition: ivbogeom.hpp:68
CRgbaColor GetGreyscale() const
Returns the greyscale equivalent of the current color.
Definition: rgba_color.cpp:481
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
Modified on Fri Sep 20 14:58:27 2024 by modify_doxy.py rev. 669887