Orfeo Toolbox  3.16
otbGluPolygonDrawingHelper.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #include <iostream>
19 
21 #include "otbMacro.h"
22 
23 // This is defined in windows only, and it is needed for FunctionPointerType
24 // to be properly defined.
25 
26 // There are function prototype conflits under cygwin between standard w32 API
27 // and standard C ones
28 #ifndef CALLBACK
29 #if defined(_WINDOWS) || defined(__CYGWIN__)
30 #define CALLBACK __stdcall
31 #else
32 #define CALLBACK
33 #endif
34 #endif
35 
36 extern "C"
37 {
38 typedef GLvoid (CALLBACK * FunctionPointerType)();
39 
40 void CALLBACK CombineCallback(GLdouble coords[3], GLdouble * /*data*/[4], GLfloat /*weights*/[4], GLdouble **dataOut)
41 {
42  GLdouble * vertex = new GLdouble[3];
43  vertex[0] = coords[0];
44  vertex[1] = coords[1];
45  vertex[2] = coords[2];
46  *dataOut = vertex;
47 }
48 
49 void CALLBACK ErrorCallback(GLenum errorCode)
50 {
51  const GLubyte * estring = gluErrorString(errorCode);
52  std::cout << "Glu Tesselation error: " << estring << std::endl;
53 }
54 
55 void CALLBACK BeginCallback(GLenum prim)
56 {
57  glBegin(prim);
58 }
60 {
61  glEnd();
62 }
63 
64 void CALLBACK VertexCallback(void * data)
65 {
66  glVertex3dv((GLdouble*) data);
67 }
68 
69 } // end extern C
70 
71 namespace otb
72 {
73 
75 {
76  // Instantiation of the tesselator object
77  m_PointVector.clear();
78  m_GluTesselator = gluNewTess();
79 
80  // Color
81  m_Color[0] = 0;
82  m_Color[1] = 0;
83  m_Color[2] = 0;
84  m_Color[3] = 0;
85 
86  // Setting up the tesselator callbacks
87  gluTessCallback(m_GluTesselator, GLU_TESS_BEGIN, (FunctionPointerType) BeginCallback);
88  gluTessCallback(m_GluTesselator, GLU_TESS_END, (FunctionPointerType) EndCallback);
89  gluTessCallback(m_GluTesselator, GLU_TESS_ERROR, (FunctionPointerType) ErrorCallback);
90  gluTessCallback(m_GluTesselator, GLU_TESS_VERTEX, (FunctionPointerType) VertexCallback);
91  gluTessCallback(m_GluTesselator, GLU_TESS_COMBINE, (FunctionPointerType) CombineCallback);
92 }
93 
95 {
96  gluDeleteTess(m_GluTesselator);
97 }
98 
99 void GluPolygonDrawingHelper::SetWindingRule(GLdouble windingRule)
100 {
101  gluTessProperty(m_GluTesselator, GLU_TESS_WINDING_RULE, windingRule);
102 }
103 
104 void GluPolygonDrawingHelper::SetBoundaryOnly(GLdouble boundaryOnly)
105 {
106  gluTessProperty(m_GluTesselator, GLU_TESS_BOUNDARY_ONLY, boundaryOnly);
107 }
108 
109 void GluPolygonDrawingHelper::Color3d(double r, double g, double b)
110 {
111  m_Color[0] = r;
112  m_Color[1] = g;
113  m_Color[2] = b;
114  m_Color[3] = 1.;
115 }
116 
117 void GluPolygonDrawingHelper::Color4d(double r, double g, double b, double alpha)
118 {
119  m_Color[0] = r;
120  m_Color[1] = g;
121  m_Color[2] = b;
122  m_Color[3] = alpha;
123 }
124 
125 void GluPolygonDrawingHelper::Vertex2d(double x, double y)
126 {
127  PointType p;
128  p[0] = x;
129  p[1] = y;
130  p[2] = 0;
131  m_PointVector.push_back(p);
132 
133 }
134 
135 void GluPolygonDrawingHelper::Vertex3d(double x, double y, double z)
136 {
137  PointType p;
138  p[0] = x;
139  p[1] = y;
140  p[2] = z;
141  m_PointVector.push_back(p);
142 }
143 
145 {
146  // Set up a malloced space for the polygon data.
147  GLdouble ** data = new GLdouble *[m_PointVector.size()];
148  unsigned int i = 0;
149  PointVectorType::iterator it;
150  for (it = m_PointVector.begin(); it != m_PointVector.end(); ++i, ++it)
151  {
152  data[i] = new GLdouble[3];
153  data[i][0] = (*it)[0];
154  data[i][1] = (*it)[1];
155  data[i][2] = (*it)[2];
156  }
157  // Set the color
158  glColor4d(m_Color[0], m_Color[1], m_Color[2], m_Color[3]);
159  gluTessBeginPolygon(m_GluTesselator, NULL);
160  gluTessBeginContour(m_GluTesselator);
161 
162  for (i = 0; i < m_PointVector.size(); ++i)
163  {
164  gluTessVertex(m_GluTesselator, data[i], data[i]);
165  }
166  gluTessEndContour(m_GluTesselator);
167  gluTessEndPolygon(m_GluTesselator);
168 
169  for (i = 0; i < m_PointVector.size(); ++i)
170  {
171  delete[] data[i];
172  }
173  delete[] data;
174 
175  m_PointVector.clear();
176 }
177 } // end namespace otb

Generated at Sun Feb 3 2013 00:25:08 for Orfeo Toolbox with doxygen 1.8.1.1