72 glEnable( GL_COLOR_MATERIAL );
73 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
80 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
81 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
83 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
84 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
85 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
113 glDisable( GL_CULL_FACE );
117 glClear( GL_DEPTH_BUFFER_BIT );
119 glMatrixMode( GL_PROJECTION );
124 glMatrixMode( GL_MODELVIEW );
127 glm::mat4 TranslationMatrix = glm::translate( glm::mat4( 1.0f ),
SFVEC3F( 0.0f, 0.0f, -(
m_arrowSize * 2.75f ) ) );
128 glm::mat4 ViewMatrix = TranslationMatrix * aCameraRotationMatrix;
129 glLoadMatrixf( glm::value_ptr( ViewMatrix ) );
133 auto drawBillboardCircle =
134 [](
const glm::vec3& aCenter,
float aRadius,
const glm::vec3& aColor,
135 const glm::vec3& aCamRight,
const glm::vec3& aCamUp,
int aSegments = 64 )
137 float thickness = aRadius * 0.4f;
138 glColor3f( aColor.r, aColor.g, aColor.b );
140 glBegin( GL_TRIANGLE_STRIP );
141 for(
int i = 0; i <= aSegments; ++i )
143 float angle = 2.0f * glm::pi<float>() * i / aSegments;
144 glm::vec3 dir = cos( angle ) * aCamRight + sin( angle ) * aCamUp;
146 glm::vec3 outer = aCenter + dir * ( aRadius + thickness * 0.5f );
147 glm::vec3 inner = aCenter + dir * ( aRadius - thickness * 0.5f );
149 glVertex3f( outer.x, outer.y, outer.z );
150 glVertex3f( inner.x, inner.y, inner.z );
155 glm::vec3 camRight( aCameraRotationMatrix[0][0], aCameraRotationMatrix[1][0], aCameraRotationMatrix[2][0] );
156 glm::vec3 camUp( aCameraRotationMatrix[0][1], aCameraRotationMatrix[1][1], aCameraRotationMatrix[2][1] );
158 glEnable( GL_BLEND );
159 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
163 glColor4f( sphere.m_color.r, sphere.m_color.g, sphere.m_color.b, 0.3f );
165 glTranslatef( sphere.m_position.x, sphere.m_position.y, sphere.m_position.z );
168 gluSphere(
m_quadric, sphere.m_radius, 32, 32 );
172 drawBillboardCircle( sphere.m_position, sphere.m_radius, sphere.m_color, camRight, camUp );
177 glDisable( GL_DEPTH_TEST );
178 glDisable( GL_LIGHTING );
180 std::array<std::string, 6> labels = {
"X",
"",
"Y",
"",
"Z",
"" };
185 glm::vec3 offset = camForward * 0.02f;
187 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
190 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
191 const glm::vec3& aCamUp )
193 glColor3f( aColor.r, aColor.g, aColor.b );
196 float h = aSize * 0.5f;
199 glm::vec3 dir1 = ( -aCamRight + aCamUp ) * h;
200 glm::vec3 dir2 = ( -aCamRight - aCamUp ) * h;
203 glVertex3f( ( aPos - dir1 ).x, ( aPos - dir1 ).y, ( aPos - dir1 ).z );
204 glVertex3f( ( aPos + dir1 ).x, ( aPos + dir1 ).y, ( aPos + dir1 ).z );
206 glVertex3f( ( aPos - dir2 ).x, ( aPos - dir2 ).y, ( aPos - dir2 ).z );
207 glVertex3f( ( aPos + dir2 ).x, ( aPos + dir2 ).y, ( aPos + dir2 ).z );
212 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
213 const glm::vec3& aCamUp )
215 glColor3f( aColor.r, aColor.g, aColor.b );
218 float h = aSize * 0.5f;
221 glm::vec3 topLeft = aPos + aCamUp * h - aCamRight * h;
222 glm::vec3 topRight = aPos + aCamUp * h + aCamRight * h;
223 glm::vec3 bottom = aPos - aCamUp * h;
226 glVertex3f( topLeft.x, topLeft.y, topLeft.z );
227 glVertex3f( aPos.x, aPos.y, aPos.z );
229 glVertex3f( topRight.x, topRight.y, topRight.z );
230 glVertex3f( aPos.x, aPos.y, aPos.z );
232 glVertex3f( aPos.x, aPos.y, aPos.z );
233 glVertex3f( bottom.x, bottom.y, bottom.z );
238 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
239 const glm::vec3& aCamUp )
241 glColor3f( aColor.r, aColor.g, aColor.b );
244 float h = aSize * 0.5f;
247 glm::vec3 topLeft = aPos + aCamUp * h - aCamRight * h;
248 glm::vec3 topRight = aPos + aCamUp * h + aCamRight * h;
249 glm::vec3 bottomLeft = aPos - aCamUp * h - aCamRight * h;
250 glm::vec3 bottomRight = aPos - aCamUp * h + aCamRight * h;
252 glBegin( GL_LINE_STRIP );
253 glVertex3f( topLeft.x, topLeft.y, topLeft.z );
254 glVertex3f( topRight.x, topRight.y, topRight.z );
255 glVertex3f( bottomLeft.x, bottomLeft.y, bottomLeft.z );
256 glVertex3f( bottomRight.x, bottomRight.y, bottomRight.z );
260 for(
size_t i = 0; i <
m_spheres.size(); ++i )
262 if( labels[i].
empty() )
265 glm::vec3 textPos =
m_spheres[i].m_position + offset;
266 const std::string& label = labels[i];
270 drawX( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
272 else if( label ==
"Y" )
274 drawY( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
276 else if( label ==
"Z" )
278 drawZ( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
282 glEnable( GL_LIGHTING );
283 glEnable( GL_DEPTH_TEST );
289 glColor3f( 0.9f, 0.0f, 0.0f );
290 glVertex3f( 0.0f, 0.0f, 0.0f );
293 glColor3f( 0.0f, 0.9f, 0.0f );
294 glVertex3f( 0.0f, 0.0f, 0.0f );
297 glColor3f( 0.0f, 0.0f, 0.9f );
298 glVertex3f( 0.0f, 0.0f, 0.0f );
303 glEnable( GL_CULL_FACE );
324 glm::mat4 TranslationMatrix = glm::translate( glm::mat4( 1.0f ),
SFVEC3F( 0.0f, 0.0f, -(
m_arrowSize * 2.75f ) ) );
325 glm::mat4 ViewMatrix = TranslationMatrix * aCameraRotationMatrix;
327 glm::mat4 proj = glm::perspective( glm::radians( fov ), 1.0f, 0.001f, 2.0f *
RANGE_SCALE_3D );
328 glm::mat4 invVP = glm::inverse( proj * ViewMatrix );
333 glm::vec4 rayStartWorld = invVP * rayStartNDC;
334 rayStartWorld /= rayStartWorld.w;
336 glm::vec4 rayEndWorld = invVP * rayEndNDC;
337 rayEndWorld /= rayEndWorld.w;
339 glm::vec3 rayOrigin = glm::vec3( rayStartWorld );
340 glm::vec3 rayDirection = glm::normalize( glm::vec3( rayEndWorld - rayStartWorld ) );
342 auto intersectDist = [](
const glm::vec3& aRayOrigin,
const glm::vec3& aRayDir,
const glm::vec3& aSphereCenter,
343 float aRadius ) ->
float
345 glm::vec3
L = aSphereCenter - aRayOrigin;
346 float tca = glm::dot(
L, aRayDir );
347 float d2 = glm::dot(
L,
L ) - tca * tca;
349 if( d2 > aRadius * aRadius )
355 int clickedIndex = -1;
356 float closestDist = std::numeric_limits<float>::max();
359 for(
size_t i = 0; i <
m_spheres.size(); ++i )
362 float dist = intersectDist( rayOrigin, rayDirection, sphere.m_position, sphere.m_radius );
364 if( dist >= 0.0f && dist < closestDist )
367 clickedIndex =
static_cast<int>( i );
372 for(
size_t i = 0; i <
m_spheres.size(); ++i )
374 if(
static_cast<int>( i ) == clickedIndex )
375 m_spheres[i].m_color = { 1.0f, 1.0f, 1.0f };