KiCad PCB EDA Suite
opengl_utils.cpp File Reference
#include "../../common_ogl/openGL_includes.h"
#include "opengl_utils.h"
#include <trigo.h>
#include <wx/debug.h>

Go to the source code of this file.

Macros

#define RADPERDEG   0.0174533f
 

Functions

void DrawRoundArrow (SFVEC3F aPosition, SFVEC3F aTargetPos, float aSize)
 Draw a round arrow. More...
 
void DrawBoundingBox (const BBOX_3D &aBBox)
 Draw the bounding box lines. More...
 
void DrawHalfOpenCylinder (unsigned int aNrSidesPerCircle)
 Draw a half open cylinder with diameter 1.0f and height 1.0f. More...
 
void DrawSegment (const ROUND_SEGMENT_2D &aSegment, unsigned int aNrSidesPerCircle)
 Draw a thick line segment with rounded ends. More...
 

Macro Definition Documentation

◆ RADPERDEG

#define RADPERDEG   0.0174533f

Definition at line 30 of file opengl_utils.cpp.

Function Documentation

◆ DrawBoundingBox()

void DrawBoundingBox ( const BBOX_3D aBBox)

Draw the bounding box lines.

Parameters
aBBoxis the box to draw.

Definition at line 102 of file opengl_utils.cpp.

103{
104 wxASSERT( aBBox.IsInitialized() );
105
106 glBegin( GL_LINE_LOOP );
107 glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Min().z );
108 glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Min().z );
109 glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Min().z );
110 glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Min().z );
111 glEnd();
112
113 glBegin( GL_LINE_LOOP );
114 glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Max().z );
115 glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Max().z );
116 glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Max().z );
117 glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Max().z );
118 glEnd();
119
120 glBegin( GL_LINE_STRIP );
121 glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Min().z );
122 glVertex3f( aBBox.Min().x, aBBox.Min().y, aBBox.Max().z );
123 glEnd();
124
125 glBegin( GL_LINE_STRIP );
126 glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Min().z );
127 glVertex3f( aBBox.Max().x, aBBox.Min().y, aBBox.Max().z );
128 glEnd();
129
130 glBegin( GL_LINE_STRIP );
131 glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Min().z );
132 glVertex3f( aBBox.Max().x, aBBox.Max().y, aBBox.Max().z );
133 glEnd();
134
135 glBegin( GL_LINE_STRIP );
136 glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Min().z );
137 glVertex3f( aBBox.Min().x, aBBox.Max().y, aBBox.Max().z );
138 glEnd();
139}
const SFVEC3F & Min() const
Return the minimum vertex pointer.
Definition: bbox_3d.h:191
const SFVEC3F & Max() const
Return the maximum vertex pointer.
Definition: bbox_3d.h:198
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_3d.cpp:88

References BBOX_3D::IsInitialized(), BBOX_3D::Max(), and BBOX_3D::Min().

◆ DrawHalfOpenCylinder()

void DrawHalfOpenCylinder ( unsigned int  aNrSidesPerCircle)

Draw a half open cylinder with diameter 1.0f and height 1.0f.

The bottom center is at (0,0,0) and top center is at (0,0,1).

Parameters
aNrSidesPerCircleis the number of segments to approximate a circle.

Definition at line 142 of file opengl_utils.cpp.

143{
144 if( aNrSidesPerCircle > 1 )
145 {
146 const float radius = 0.5f;
147 const EDA_ANGLE delta = ANGLE_360 / aNrSidesPerCircle;
148
149 // Generate bottom
150 glNormal3f( 0.0f, 0.0f,-1.0f );
151 glBegin( GL_TRIANGLE_FAN );
152 glVertex3f( 0.0, 0.0, 0.0 ); // This is the V0 of the FAN
153
154 for( EDA_ANGLE ii = ANGLE_0; ii < ANGLE_180; ii += delta )
155 {
156 SFVEC2D corner = SFVEC2D( 0.0, radius );
157 RotatePoint( &corner.x, &corner.y, ii );
158 glVertex3f( static_cast<GLfloat>( corner.x ), static_cast<GLfloat>( corner.y ), 0.0 );
159 }
160
161 glVertex3d( 0.0, -radius, 0.0 );
162 glEnd();
163
164 // Generate top
165 glNormal3f( 0.0f, 0.0f, 1.0f );
166 glBegin( GL_TRIANGLE_FAN );
167 glVertex3f( 0.0, 0.0, 1.0 ); // This is the V0 of the FAN
168
169 for( EDA_ANGLE ii = ANGLE_180; ii > ANGLE_0; ii -= delta )
170 {
171 SFVEC2D corner = SFVEC2D( 0.0, radius );
172
173 RotatePoint( &corner.x, &corner.y, ii );
174 glVertex3f( static_cast<GLfloat>( corner.x ), static_cast<GLfloat>( corner.y ), 1.0 );
175 }
176
177 glVertex3f( 0.0, radius, 1.0 );
178 glEnd();
179
180 // Generate contours
181 glBegin( GL_QUAD_STRIP );
182
183 for( EDA_ANGLE ii = ANGLE_180; ii > ANGLE_0; ii -= delta )
184 {
185 SFVEC2D corner = SFVEC2D( 0.0, radius );
186
187 RotatePoint( &corner.x, &corner.y, ii );
188 glNormal3f( static_cast<GLfloat>( corner.x * 2.0f ),
189 static_cast<GLfloat>( corner.y * 2.0f ), 0.0f );
190 glVertex3f( static_cast<GLfloat>( corner.x ), static_cast<GLfloat>( corner.y ), 1.0f );
191 glVertex3f( static_cast<GLfloat>( corner.x ), static_cast<GLfloat>( corner.y ), 0.0f );
192 }
193
194 glNormal3f( 0.0, 1.0f, 0.0f );
195 glVertex3d( 0.0, radius, 1.0 );
196 glVertex3d( 0.0, radius, 0.0 );
197 glEnd();
198 }
199}
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:433
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:435
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:429
constexpr int delta
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
glm::dvec2 SFVEC2D
Definition: xv3d_types.h:43

References ANGLE_0, ANGLE_180, ANGLE_360, delta, and RotatePoint().

Referenced by DrawSegment().

◆ DrawRoundArrow()

void DrawRoundArrow ( SFVEC3F  aPosition,
SFVEC3F  aTargetPos,
float  aSize 
)

Draw a round arrow.

Parameters
aPositionis the start position of the arrow.
aTargetPosis the end position of the arrow.
aSizeis the diameter of the arrow.

Definition at line 32 of file opengl_utils.cpp.

33{
34 wxASSERT( aSize > 0.0f );
35
36 SFVEC3F vec = aTargetPos - aPosition;
37 float length = glm::length( vec );
38
39 GLUquadricObj *quadObj;
40
41 glPushMatrix ();
42
43 glTranslatef( aPosition.x, aPosition.y, aPosition.z );
44
45 if( ( vec.x != 0.0f ) || ( vec.y != 0.0f ) )
46 {
47 glRotatef( atan2( vec.y, vec.x ) / RADPERDEG, 0.0f, 0.0f, 1.0f );
48 glRotatef( atan2( sqrt( vec.x * vec.x + vec.y * vec.y ), vec.z ) / RADPERDEG,
49 0.0f, 1.0f, 0.0f );
50
51 }
52 else if( vec.z < 0.0f )
53 {
54 glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );
55 }
56
57 glTranslatef( 0.0f, 0.0f, length - 4.0f * aSize );
58
59 quadObj = gluNewQuadric();
60 gluQuadricDrawStyle( quadObj, GLU_FILL );
61 gluQuadricNormals( quadObj, GLU_SMOOTH );
62 gluCylinder( quadObj, 2.0 * aSize, 0.0, 4.0 * aSize, 12, 1 );
63 gluDeleteQuadric( quadObj );
64
65 quadObj = gluNewQuadric();
66 gluQuadricDrawStyle( quadObj, GLU_FILL );
67 gluQuadricNormals( quadObj, GLU_SMOOTH );
68 gluDisk( quadObj, 0.0, 2.0 * aSize, 12, 1 );
69 gluDeleteQuadric( quadObj );
70
71 quadObj = gluNewQuadric();
72 gluQuadricDrawStyle( quadObj, GLU_FILL );
73 gluQuadricNormals( quadObj, GLU_SMOOTH );
74 gluDisk( quadObj, 0.0, aSize, 12, 1 );
75 gluDeleteQuadric( quadObj );
76
77
78 quadObj = gluNewQuadric();
79 gluQuadricDrawStyle( quadObj, GLU_FILL );
80 gluQuadricNormals( quadObj, GLU_SMOOTH );
81 gluSphere( quadObj, aSize, 24, 24 );
82 gluDeleteQuadric( quadObj );
83
84 glTranslatef( 0.0f , 0.0f ,-length + 4.0f * aSize );
85
86 quadObj = gluNewQuadric();
87 gluQuadricDrawStyle( quadObj, GLU_FILL );
88 gluQuadricNormals( quadObj, GLU_SMOOTH );
89 gluCylinder( quadObj, aSize, aSize, length - 4.0 * aSize, 12, 1 );
90 gluDeleteQuadric( quadObj );
91
92 quadObj = gluNewQuadric();
93 gluQuadricDrawStyle( quadObj, GLU_FILL );
94 gluQuadricNormals( quadObj, GLU_SMOOTH );
95 gluDisk( quadObj, 0.0, aSize, 12, 1 );
96 gluDeleteQuadric( quadObj );
97
98 glPopMatrix ();
99}
#define RADPERDEG
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

References RADPERDEG.

Referenced by EDA_3D_MODEL_VIEWER::OnPaint(), and RENDER_3D_OPENGL::render3dArrows().

◆ DrawSegment()

void DrawSegment ( const ROUND_SEGMENT_2D aSegment,
unsigned int  aNrSidesPerCircle 
)

Draw a thick line segment with rounded ends.

Parameters
aSegmentis the thick segment to draw
aNrSidesPerCircleis the number of segments to approximate the circle used to draw the rounded ends of the segment.

Definition at line 202 of file opengl_utils.cpp.

203{
204 glPushMatrix();
205
206 const SFVEC2F& start = aSegment.GetStart();
207 const SFVEC2F& end_minus_start = aSegment.GetEnd_minus_Start();
208 const float radius = aSegment.GetRadius();
209 const float width = aSegment.GetWidth();
210 const float length = aSegment.GetLength();
211
212 glTranslatef( start.x, start.y, 0.0f );
213
214 if( ( end_minus_start.x != 0.0f ) || ( end_minus_start.y != 0.0f ) )
215 {
216 glRotatef( atan2( end_minus_start.y, end_minus_start.x ) / RADPERDEG, 0.0f, 0.0f, 1.0f );
217 }
218
219 glPushMatrix();
220 glTranslatef( length, 0.0, 0.0f );
221 glScalef( width, width, 1.0f );
222 DrawHalfOpenCylinder( aNrSidesPerCircle );
223 glPopMatrix();
224
225 glBegin( GL_QUADS );
226 glNormal3f( 0.0,-1.0, 0.0 );
227 glVertex3f( length,-radius, 1.0 );
228 glVertex3f( 0.0, -radius, 1.0 );
229 glVertex3f( 0.0, -radius, 0.0 );
230 glVertex3f( length,-radius, 0.0 );
231 glEnd();
232
233 glBegin( GL_QUADS );
234 glNormal3f( 0.0, 1.0, 0.0 );
235 glVertex3f( length, radius, 0.0 );
236 glVertex3f( 0.0, radius, 0.0 );
237 glVertex3f( 0.0, radius, 1.0 );
238 glVertex3f( length, radius, 1.0 );
239 glEnd();
240
241 glBegin( GL_QUADS );
242 glNormal3f( 0.0, 0.0, 1.0 );
243 glVertex3f( length, radius, 1.0 );
244 glVertex3f( 0.0, radius, 1.0 );
245 glVertex3f( 0.0, -radius, 1.0 );
246 glVertex3f( length,-radius, 1.0 );
247 glEnd();
248
249 glBegin( GL_QUADS );
250 glNormal3f( 0.0, 0.0,-1.0 );
251 glVertex3f( length,-radius, 0.0 );
252 glVertex3f( 0.0, -radius, 0.0 );
253 glVertex3f( 0.0, radius, 0.0 );
254 glVertex3f( length, radius, 0.0 );
255 glEnd();
256
257 glScalef( width, width, 1.0f );
258 glRotatef( 180, 0.0, 0.0, 1.0 );
259 DrawHalfOpenCylinder( aNrSidesPerCircle );
260
261 glPopMatrix ();
262}
float GetRadius() const
float GetWidth() const
float GetLength() const
const SFVEC2F & GetStart() const
const SFVEC2F & GetEnd_minus_Start() const
void DrawHalfOpenCylinder(unsigned int aNrSidesPerCircle)
Draw a half open cylinder with diameter 1.0f and height 1.0f.
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42

References DrawHalfOpenCylinder(), ROUND_SEGMENT_2D::GetEnd_minus_Start(), ROUND_SEGMENT_2D::GetLength(), ROUND_SEGMENT_2D::GetRadius(), ROUND_SEGMENT_2D::GetStart(), ROUND_SEGMENT_2D::GetWidth(), and RADPERDEG.