KiCad PCB EDA Suite
ogl_legacy_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include "../../common_ogl/openGL_includes.h"
32 #include "ogl_legacy_utils.h"
33 #include <trigo.h>
34 #include <wx/debug.h> // For the wxASSERT
35 
36 #define RADPERDEG 0.0174533
37 
38 void OGL_draw_arrow( SFVEC3F aPosition, SFVEC3F aTargetPos, float aSize )
39 {
40  wxASSERT( aSize > 0.0f );
41 
42  SFVEC3F vec = (aTargetPos - aPosition);
43  float length = glm::length( vec );
44 
45  GLUquadricObj *quadObj;
46 
47  glPushMatrix ();
48 
49  glTranslatef( aPosition.x, aPosition.y, aPosition.z );
50 
51  if( ( vec.x != 0.0f ) || ( vec.y != 0.0f ) )
52  {
53  glRotatef( atan2( vec.y, vec.x ) / RADPERDEG, 0.0f, 0.0f, 1.0f );
54  glRotatef( atan2( sqrt( vec.x * vec.x + vec.y * vec.y ), vec.z ) / RADPERDEG,
55  0.0f,
56  1.0f,
57  0.0f );
58 
59  } else if( vec.z < 0.0f )
60  {
61  glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );
62  }
63 
64  glTranslatef( 0.0f, 0.0f, length - 4.0f * aSize );
65 
66  quadObj = gluNewQuadric();
67  gluQuadricDrawStyle( quadObj, GLU_FILL );
68  gluQuadricNormals( quadObj, GLU_SMOOTH );
69  gluCylinder( quadObj, 2.0 * aSize, 0.0, 4.0 * aSize, 12, 1 );
70  gluDeleteQuadric( quadObj );
71 
72  quadObj = gluNewQuadric();
73  gluQuadricDrawStyle( quadObj, GLU_FILL );
74  gluQuadricNormals( quadObj, GLU_SMOOTH );
75  gluDisk( quadObj, 0.0, 2.0 * aSize, 12, 1 );
76  gluDeleteQuadric( quadObj );
77 
78  quadObj = gluNewQuadric();
79  gluQuadricDrawStyle( quadObj, GLU_FILL );
80  gluQuadricNormals( quadObj, GLU_SMOOTH );
81  gluDisk( quadObj, 0.0, aSize, 12, 1 );
82  gluDeleteQuadric( quadObj );
83 
84 
85  quadObj = gluNewQuadric();
86  gluQuadricDrawStyle( quadObj, GLU_FILL );
87  gluQuadricNormals( quadObj, GLU_SMOOTH );
88  gluSphere( quadObj, aSize, 24, 24 );
89  gluDeleteQuadric( quadObj );
90 
91  glTranslatef( 0.0f , 0.0f ,-length + 4.0f * aSize );
92 
93  quadObj = gluNewQuadric();
94  gluQuadricDrawStyle( quadObj, GLU_FILL );
95  gluQuadricNormals( quadObj, GLU_SMOOTH );
96  gluCylinder( quadObj, aSize, aSize, length - 4.0 * aSize, 12, 1 );
97  gluDeleteQuadric( quadObj );
98 /*
99 
100  quadObj = gluNewQuadric();
101  gluQuadricDrawStyle( quadObj, GLU_FILL );
102  gluQuadricNormals( quadObj, GLU_SMOOTH );
103  gluSphere( quadObj, aSize, 24, 24 );
104  gluDeleteQuadric( quadObj );
105 */
106 
107  quadObj = gluNewQuadric();
108  gluQuadricDrawStyle( quadObj, GLU_FILL );
109  gluQuadricNormals( quadObj, GLU_SMOOTH );
110  gluDisk( quadObj, 0.0, aSize, 12, 1 );
111  gluDeleteQuadric( quadObj );
112 
113  glPopMatrix ();
114 }
115 
116 
117 void OGL_draw_bbox( const CBBOX &aBBox )
118 {
119  wxASSERT( aBBox.IsInitialized() );
120 
121  glBegin( GL_LINE_LOOP );
122  glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Min().z );
123  glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Min().z );
124  glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Min().z );
125  glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Min().z );
126  glEnd();
127 
128  glBegin( GL_LINE_LOOP );
129  glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Max().z );
130  glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Max().z );
131  glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Max().z );
132  glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Max().z );
133  glEnd();
134 
135  glBegin( GL_LINE_STRIP );
136  glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Min().z );
137  glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Max().z );
138  glEnd();
139 
140  glBegin( GL_LINE_STRIP );
141  glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Min().z );
142  glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Max().z );
143  glEnd();
144 
145  glBegin( GL_LINE_STRIP );
146  glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Min().z );
147  glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Max().z );
148  glEnd();
149 
150  glBegin( GL_LINE_STRIP );
151  glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Min().z );
152  glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Max().z );
153  glEnd();
154 }
155 
156 
157 void OGL_draw_half_open_cylinder( unsigned int aNrSidesPerCircle )
158 {
159  if( aNrSidesPerCircle > 1 )
160  {
161  const float radius = 0.5f;
162  const int delta = 3600 / aNrSidesPerCircle;
163 
164  // Generate bottom
165  glNormal3f( 0.0f, 0.0f,-1.0f );
166  glBegin( GL_TRIANGLE_FAN );
167  glVertex3f( 0.0, 0.0, 0.0 ); // This is the V0 of the FAN
168 
169  for( int ii = 0; ii < 1800; ii += delta )
170  {
171  SFVEC2D corner = SFVEC2D( 0.0, radius );
172  RotatePoint( &corner.x, &corner.y, ii );
173  glVertex3f( corner.x, corner.y, 0.0 );
174  }
175 
176  glVertex3d( 0.0, -radius, 0.0 );
177  glEnd();
178 
179  // Generate top
180  glNormal3f( 0.0f, 0.0f, 1.0f );
181  glBegin( GL_TRIANGLE_FAN );
182  glVertex3f( 0.0, 0.0, 1.0 ); // This is the V0 of the FAN
183 
184  for( int ii = 1800; ii > 0; ii -= delta )
185  {
186  SFVEC2D corner = SFVEC2D( 0.0, radius );
187 
188  RotatePoint( &corner.x, &corner.y, ii );
189  glVertex3f( corner.x, corner.y, 1.0 );
190  }
191 
192  glVertex3f( 0.0, radius, 1.0 );
193  glEnd();
194 
195  // Generate contours
196  glBegin( GL_QUAD_STRIP );
197 
198  for( int ii = 1800; ii > 0; ii -= delta )
199  {
200  SFVEC2D corner = SFVEC2D( 0.0, radius );
201 
202  RotatePoint( &corner.x, &corner.y, ii );
203  glNormal3f( corner.x * 2.0f, corner.y * 2.0f, 0.0f );
204  glVertex3f( corner.x, corner.y, 1.0 );
205  glVertex3f( corner.x, corner.y, 0.0 );
206  }
207 
208  glNormal3f( 0.0, 1.0f, 0.0f );
209  glVertex3d( 0.0, radius, 1.0 );
210  glVertex3d( 0.0, radius, 0.0 );
211  glEnd();
212  }
213 }
214 
215 
216 void OGL_Draw_segment( const CROUNDSEGMENT2D &aSegment,
217  unsigned int aNrSidesPerCircle )
218 {
219  glPushMatrix();
220 
221  const SFVEC2F start = aSegment.GetStart();
222  const SFVEC2F end_minus_start = aSegment.GetEnd_minus_Start();
223  const float radius = aSegment.GetRadius();
224  const float width = aSegment.GetWidth();
225  const float length = aSegment.GetLength();
226 
227  glTranslatef( start.x, start.y, 0.0f );
228 
229  if( ( end_minus_start.x != 0.0f ) || ( end_minus_start.y != 0.0f ) )
230  {
231  glRotatef( atan2( end_minus_start.y, end_minus_start.x ) / RADPERDEG,
232  0.0f,
233  0.0f,
234  1.0f );
235  }
236 
237  glPushMatrix();
238  glTranslatef( length, 0.0, 0.0f );
239  glScalef( width, width, 1.0f );
240  OGL_draw_half_open_cylinder( aNrSidesPerCircle );
241  glPopMatrix();
242 
243  glBegin( GL_QUADS );
244  glNormal3f( 0.0,-1.0, 0.0 );
245  glVertex3f( length,-radius, 1.0 );
246  glVertex3f( 0.0, -radius, 1.0 );
247  glVertex3f( 0.0, -radius, 0.0 );
248  glVertex3f( length,-radius, 0.0 );
249  glEnd();
250 
251  glBegin( GL_QUADS );
252  glNormal3f( 0.0, 1.0, 0.0 );
253  glVertex3f( length, radius, 0.0 );
254  glVertex3f( 0.0, radius, 0.0 );
255  glVertex3f( 0.0, radius, 1.0 );
256  glVertex3f( length, radius, 1.0 );
257  glEnd();
258 
259  glBegin( GL_QUADS );
260  glNormal3f( 0.0, 0.0, 1.0 );
261  glVertex3f( length, radius, 1.0 );
262  glVertex3f( 0.0, radius, 1.0 );
263  glVertex3f( 0.0, -radius, 1.0 );
264  glVertex3f( length,-radius, 1.0 );
265  glEnd();
266 
267  glBegin( GL_QUADS );
268  glNormal3f( 0.0, 0.0,-1.0 );
269  glVertex3f( length,-radius, 0.0 );
270  glVertex3f( 0.0, -radius, 0.0 );
271  glVertex3f( 0.0, radius, 0.0 );
272  glVertex3f( length, radius, 0.0 );
273  glEnd();
274 
275  glScalef( width, width, 1.0f );
276  glRotatef( 180, 0.0, 0.0, 1.0 );
277  OGL_draw_half_open_cylinder( aNrSidesPerCircle );
278 
279  glPopMatrix ();
280 }
const SFVEC2F & GetStart() const
float GetWidth() const
const SFVEC3F & Min() const
Function Min return the minimun vertex pointer.
Definition: cbbox.h:205
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox.cpp:87
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
float GetLength() const
static const int delta[8][2]
Definition: solve.cpp:112
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
float GetRadius() const
const SFVEC3F & Max() const
Function Max return the maximum vertex pointer.
Definition: cbbox.h:212
void OGL_draw_half_open_cylinder(unsigned int aNrSidesPerCircle)
OGL_draw_half_open_cylinder - draws an open half cylinder with diameter 1.0f and Height 1...
void OGL_Draw_segment(const CROUNDSEGMENT2D &aSegment, unsigned int aNrSidesPerCircle)
OGL_Draw_segment.
glm::dvec2 SFVEC2D
Definition: xv3d_types.h:46
const SFVEC2F & GetEnd_minus_Start() const
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void OGL_draw_bbox(const CBBOX &aBBox)
OGL_draw_bbox - draw the bounding box lines.
#define RADPERDEG
Class CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40
void OGL_draw_arrow(SFVEC3F aPosition, SFVEC3F aTargetPos, float aSize)
OGL_draw_arrow - draw a round arrow.