KiCad PCB EDA Suite
Loading...
Searching...
No Matches
3d_spheres_gizmo.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 2025, Damjan Prerad <[email protected]>
5 * Copyright (C) The 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
25#include <kicad_gl/kiglu.h> // Must be included first
26#include <glm/geometric.hpp>
27#include <limits>
28
29#include "3d_spheres_gizmo.h"
30
31
33{
34 if( m_quadric )
35 {
36 gluDeleteQuadric( m_quadric );
37 m_quadric = nullptr;
38 }
39}
40
41
42SPHERES_GIZMO::SPHERES_GIZMO( int aGizmoPosX, int aGizmoPosY )
43{
44 m_gizmoPosX = aGizmoPosX;
45 m_gizmoPosY = aGizmoPosY;
46
47 m_quadric = gluNewQuadric();
48 gluQuadricNormals( m_quadric, GLU_SMOOTH );
49}
50
51
52void SPHERES_GIZMO::setViewport( int ax, int ay, int aWidth, int aHeight )
53{
54 m_viewportX = ax;
55 m_viewportY = ay;
56 m_viewportW = aWidth;
57 m_viewportH = aHeight;
58}
59
60
61std::tuple<int, int, int, int> SPHERES_GIZMO::getViewport() const
62{
63 return std::make_tuple( m_viewportX, m_viewportY, m_viewportW, m_viewportH );
64}
65
66
67void SPHERES_GIZMO::setGizmoPosition( int ax, int ay )
68{
69 m_gizmoPosX = ax;
70 m_gizmoPosY = ay;
71}
72
73
75{
76 glEnable( GL_COLOR_MATERIAL );
77 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
78
79 const SFVEC4F ambient = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
80 const SFVEC4F diffuse = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
81 const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
82 const SFVEC4F specular = SFVEC4F( 0.1f, 0.1f, 0.1f, 1.0f );
83
84 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
85 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
86
87 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
88 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
89 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
90}
91
92void SPHERES_GIZMO::handleMouseInput( int aMouseX, int aMouseY )
93{
94 int smallViewportW = m_viewportH / 8;
95 int smallViewportH = m_viewportH / 8;
96
97 bool inside = ( aMouseX >= m_gizmoPosX && aMouseX <= m_gizmoPosX + smallViewportW && aMouseY >= m_gizmoPosY
98 && aMouseY <= m_gizmoPosY + smallViewportH );
99
100 if( inside )
101 {
102 m_ndcX = 2.0f * static_cast<float>( aMouseX - m_gizmoPosX ) / smallViewportW - 1.0f;
103 m_ndcY = 2.0f * static_cast<float>( aMouseY - m_gizmoPosY ) / smallViewportH - 1.0f;
104 }
105 else
106 {
107 m_ndcX = -1.0f;
108 m_ndcY = -1.0f;
109 }
110}
111
112
113void SPHERES_GIZMO::render3dSpheresGizmo( glm::mat4 aCameraRotationMatrix )
114{
115 float fov = 60.0f;
116
117 glDisable( GL_CULL_FACE );
118
119 // Set up a square viewport (Y x Y)
120 glViewport( m_gizmoPosX, m_gizmoPosY, m_viewportH / 8, m_viewportH / 8 );
121 glClear( GL_DEPTH_BUFFER_BIT );
122
123 glMatrixMode( GL_PROJECTION );
124 glLoadIdentity();
125
126 gluPerspective( fov, 1.0f, 0.001f, 2.0f * RANGE_SCALE_3D );
127
128 glMatrixMode( GL_MODELVIEW );
129 glLoadIdentity();
130
131 glm::mat4 TranslationMatrix = glm::translate( glm::mat4( 1.0f ), SFVEC3F( 0.0f, 0.0f, -( m_arrowSize * 2.75f ) ) );
132 glm::mat4 ViewMatrix = TranslationMatrix * aCameraRotationMatrix;
133 glLoadMatrixf( glm::value_ptr( ViewMatrix ) );
134
136
137 auto drawBillboardCircle =
138 []( const glm::vec3& aCenter, float aRadius, const glm::vec3& aColor,
139 const glm::vec3& aCamRight, const glm::vec3& aCamUp, int aSegments = 64 )
140 {
141 float thickness = aRadius * 0.4f;
142 glColor3f( aColor.r, aColor.g, aColor.b );
143
144 glBegin( GL_TRIANGLE_STRIP );
145 for( int i = 0; i <= aSegments; ++i )
146 {
147 float angle = 2.0f * glm::pi<float>() * i / aSegments;
148 glm::vec3 dir = cos( angle ) * aCamRight + sin( angle ) * aCamUp;
149
150 glm::vec3 outer = aCenter + dir * ( aRadius + thickness * 0.5f );
151 glm::vec3 inner = aCenter + dir * ( aRadius - thickness * 0.5f );
152
153 glVertex3f( outer.x, outer.y, outer.z );
154 glVertex3f( inner.x, inner.y, inner.z );
155 }
156 glEnd();
157 };
158
159 glm::vec3 camRight( aCameraRotationMatrix[0][0], aCameraRotationMatrix[1][0], aCameraRotationMatrix[2][0] );
160 glm::vec3 camUp( aCameraRotationMatrix[0][1], aCameraRotationMatrix[1][1], aCameraRotationMatrix[2][1] );
161
162 glEnable( GL_BLEND );
163 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
164
165 for( const auto& sphere : m_spheres )
166 {
167 glColor4f( sphere.m_color.r, sphere.m_color.g, sphere.m_color.b, 0.3f );
168 glPushMatrix();
169 glTranslatef( sphere.m_position.x, sphere.m_position.y, sphere.m_position.z );
170 if( m_quadric )
171 {
172 gluSphere( m_quadric, sphere.m_radius, 32, 32 );
173 }
174 glPopMatrix();
175
176 drawBillboardCircle( sphere.m_position, sphere.m_radius, sphere.m_color, camRight, camUp );
177 }
178
179 // Draw sphere labels
180
181 glDisable( GL_DEPTH_TEST );
182 glDisable( GL_LIGHTING );
183
184 std::array<std::string, 6> labels = { "X", "", "Y", "", "Z", "" };
185
186 // View direction (camera looks along negative Z in view space)
187 // So we offset a little toward the camera to avoid z-fighting
188 glm::vec3 camForward( m_cameraRotationMatrix[0][2], m_cameraRotationMatrix[1][2], m_cameraRotationMatrix[2][2] );
189 glm::vec3 offset = camForward * 0.02f;
190
191 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
192
193 auto drawX =
194 []( const glm::vec3& aPos, float aSize, const glm::vec3& aColor, const glm::vec3& aCamRight,
195 const glm::vec3& aCamUp )
196 {
197 glColor3f( aColor.r, aColor.g, aColor.b );
198 glLineWidth( 3.0f );
199
200 float h = aSize * 0.5f;
201
202 // Define two diagonal line directions in camera-facing plane
203 glm::vec3 dir1 = ( -aCamRight + aCamUp ) * h; // one diagonal
204 glm::vec3 dir2 = ( -aCamRight - aCamUp ) * h; // other diagonal
205
206 glBegin( GL_LINES );
207 glVertex3f( ( aPos - dir1 ).x, ( aPos - dir1 ).y, ( aPos - dir1 ).z );
208 glVertex3f( ( aPos + dir1 ).x, ( aPos + dir1 ).y, ( aPos + dir1 ).z );
209
210 glVertex3f( ( aPos - dir2 ).x, ( aPos - dir2 ).y, ( aPos - dir2 ).z );
211 glVertex3f( ( aPos + dir2 ).x, ( aPos + dir2 ).y, ( aPos + dir2 ).z );
212 glEnd();
213 };
214
215 auto drawY =
216 []( const glm::vec3& aPos, float aSize, const glm::vec3& aColor, const glm::vec3& aCamRight,
217 const glm::vec3& aCamUp )
218 {
219 glColor3f( aColor.r, aColor.g, aColor.b );
220 glLineWidth( 3.0f );
221
222 float h = aSize * 0.5f;
223
224 // Top-left and top-right in screen plane
225 glm::vec3 topLeft = aPos + aCamUp * h - aCamRight * h;
226 glm::vec3 topRight = aPos + aCamUp * h + aCamRight * h;
227 glm::vec3 bottom = aPos - aCamUp * h;
228
229 glBegin( GL_LINES );
230 glVertex3f( topLeft.x, topLeft.y, topLeft.z );
231 glVertex3f( aPos.x, aPos.y, aPos.z );
232
233 glVertex3f( topRight.x, topRight.y, topRight.z );
234 glVertex3f( aPos.x, aPos.y, aPos.z );
235
236 glVertex3f( aPos.x, aPos.y, aPos.z );
237 glVertex3f( bottom.x, bottom.y, bottom.z );
238 glEnd();
239 };
240
241 auto drawZ =
242 []( const glm::vec3& aPos, float aSize, const glm::vec3& aColor, const glm::vec3& aCamRight,
243 const glm::vec3& aCamUp )
244 {
245 glColor3f( aColor.r, aColor.g, aColor.b );
246 glLineWidth( 3.0f );
247
248 float h = aSize * 0.5f;
249
250 // Define corners in screen plane relative to camera
251 glm::vec3 topLeft = aPos + aCamUp * h - aCamRight * h;
252 glm::vec3 topRight = aPos + aCamUp * h + aCamRight * h;
253 glm::vec3 bottomLeft = aPos - aCamUp * h - aCamRight * h;
254 glm::vec3 bottomRight = aPos - aCamUp * h + aCamRight * h;
255
256 glBegin( GL_LINE_STRIP );
257 glVertex3f( topLeft.x, topLeft.y, topLeft.z );
258 glVertex3f( topRight.x, topRight.y, topRight.z );
259 glVertex3f( bottomLeft.x, bottomLeft.y, bottomLeft.z );
260 glVertex3f( bottomRight.x, bottomRight.y, bottomRight.z );
261 glEnd();
262 };
263
264 for( size_t i = 0; i < m_spheres.size(); ++i )
265 {
266 if( labels[i].empty() )
267 continue;
268
269 glm::vec3 textPos = m_spheres[i].m_position + offset;
270 const std::string& label = labels[i];
271
272 if( label == "X" )
273 {
274 drawX( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
275 }
276 else if( label == "Y" )
277 {
278 drawY( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
279 }
280 else if( label == "Z" )
281 {
282 drawZ( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
283 }
284 }
285
286 glEnable( GL_LIGHTING );
287 glEnable( GL_DEPTH_TEST );
288
289 // Draw lines only to the positive axis spheres
290 glLineWidth( 2.0f );
291 glBegin( GL_LINES );
292
293 glColor3f( 0.9f, 0.0f, 0.0f ); // X+
294 glVertex3f( 0.0f, 0.0f, 0.0f );
295 glVertex3f( m_arrowSize, 0.0f, 0.0f );
296
297 glColor3f( 0.0f, 0.9f, 0.0f ); // Y+
298 glVertex3f( 0.0f, 0.0f, 0.0f );
299 glVertex3f( 0.0f, m_arrowSize, 0.0f );
300
301 glColor3f( 0.0f, 0.0f, 0.9f ); // Z+
302 glVertex3f( 0.0f, 0.0f, 0.0f );
303 glVertex3f( 0.0f, 0.0f, m_arrowSize );
304
305 glEnd();
306
307 glEnable( GL_CULL_FACE );
308}
309
314
321
322
323void SPHERES_GIZMO::updateSelection( glm::mat4 aCameraRotationMatrix )
324{
325 m_cameraRotationMatrix = aCameraRotationMatrix;
326
327 float fov = 60.0f;
328 glm::mat4 TranslationMatrix = glm::translate( glm::mat4( 1.0f ), SFVEC3F( 0.0f, 0.0f, -( m_arrowSize * 2.75f ) ) );
329 glm::mat4 ViewMatrix = TranslationMatrix * aCameraRotationMatrix;
330
331 glm::mat4 proj = glm::perspective( glm::radians( fov ), 1.0f, 0.001f, 2.0f * RANGE_SCALE_3D );
332 glm::mat4 invVP = glm::inverse( proj * ViewMatrix );
333
334 glm::vec4 rayStartNDC( m_ndcX, m_ndcY, -1.0f, 1.0f );
335 glm::vec4 rayEndNDC( m_ndcX, m_ndcY, 1.0f, 1.0f );
336
337 glm::vec4 rayStartWorld = invVP * rayStartNDC;
338 rayStartWorld /= rayStartWorld.w;
339
340 glm::vec4 rayEndWorld = invVP * rayEndNDC;
341 rayEndWorld /= rayEndWorld.w;
342
343 glm::vec3 rayOrigin = glm::vec3( rayStartWorld );
344 glm::vec3 rayDirection = glm::normalize( glm::vec3( rayEndWorld - rayStartWorld ) );
345
346 auto intersectDist = []( const glm::vec3& aRayOrigin, const glm::vec3& aRayDir, const glm::vec3& aSphereCenter,
347 float aRadius ) -> float
348 {
349 glm::vec3 L = aSphereCenter - aRayOrigin;
350 float tca = glm::dot( L, aRayDir );
351 float d2 = glm::dot( L, L ) - tca * tca;
352
353 if( d2 > aRadius * aRadius )
354 return -1.0f;
355
356 return tca;
357 };
358
359 int clickedIndex = -1;
360 float closestDist = std::numeric_limits<float>::max();
362
363 for( size_t i = 0; i < m_spheres.size(); ++i )
364 {
365 const auto& sphere = m_spheres[i];
366 float dist = intersectDist( rayOrigin, rayDirection, sphere.m_position, sphere.m_radius );
367
368 if( dist >= 0.0f && dist < closestDist )
369 {
370 closestDist = dist;
371 clickedIndex = static_cast<int>( i );
373 }
374 }
375
376 for( size_t i = 0; i < m_spheres.size(); ++i )
377 {
378 if( static_cast<int>( i ) == clickedIndex )
379 m_spheres[i].m_color = { 1.0f, 1.0f, 1.0f };
380 else
381 m_spheres[i].m_color = m_spheres[i].m_originalColor;
382 }
383}
#define RANGE_SCALE_3D
This defines the range that all coord will have to be rendered.
GizmoSphereSelection
Enum to indicate which sphere (direction) is selected.
void updateSelection(glm::mat4 aCameraRotationMatrix)
void handleMouseInput(int aMouseX, int aMouseY)
void render3dSpheresGizmo(glm::mat4 aCameraRotationMatrix)
GLUquadric * m_quadric
const float m_arrowSize
std::array< GizmoSphere, 6 > m_spheres
List of all directional gizmo spheres.
GizmoSphereSelection getSelectedGizmoSphere() const
std::tuple< int, int, int, int > getViewport() const
void setViewport(int ax, int ay, int aWidth, int aHeight)
glm::mat4 m_cameraRotationMatrix
GizmoSphereSelection m_selectedGizmoSphere
SPHERES_GIZMO(int aGizmoPosX, int aGizmoPosY)
void resetSelectedGizmoSphere()
void setGizmoPosition(int ax, int ay)
static bool empty(const wxTextEntryBase *aCtrl)
glm::vec3 SFVEC3F
Definition xv3d_types.h:44
glm::vec4 SFVEC4F
Definition xv3d_types.h:46