76 glEnable( GL_COLOR_MATERIAL );
77 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
84 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
85 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 96.0f );
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 );
117 glDisable( GL_CULL_FACE );
121 glClear( GL_DEPTH_BUFFER_BIT );
123 glMatrixMode( GL_PROJECTION );
128 glMatrixMode( GL_MODELVIEW );
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 ) );
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 )
141 float thickness = aRadius * 0.4f;
142 glColor3f( aColor.r, aColor.g, aColor.b );
144 glBegin( GL_TRIANGLE_STRIP );
145 for(
int i = 0; i <= aSegments; ++i )
147 float angle = 2.0f * glm::pi<float>() * i / aSegments;
148 glm::vec3 dir = cos( angle ) * aCamRight + sin( angle ) * aCamUp;
150 glm::vec3 outer = aCenter + dir * ( aRadius + thickness * 0.5f );
151 glm::vec3 inner = aCenter + dir * ( aRadius - thickness * 0.5f );
153 glVertex3f( outer.x, outer.y, outer.z );
154 glVertex3f( inner.x, inner.y, inner.z );
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] );
162 glEnable( GL_BLEND );
163 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
167 glColor4f( sphere.m_color.r, sphere.m_color.g, sphere.m_color.b, 0.3f );
169 glTranslatef( sphere.m_position.x, sphere.m_position.y, sphere.m_position.z );
172 gluSphere(
m_quadric, sphere.m_radius, 32, 32 );
176 drawBillboardCircle( sphere.m_position, sphere.m_radius, sphere.m_color, camRight, camUp );
181 glDisable( GL_DEPTH_TEST );
182 glDisable( GL_LIGHTING );
184 std::array<std::string, 6> labels = {
"X",
"",
"Y",
"",
"Z",
"" };
189 glm::vec3 offset = camForward * 0.02f;
191 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
194 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
195 const glm::vec3& aCamUp )
197 glColor3f( aColor.r, aColor.g, aColor.b );
200 float h = aSize * 0.5f;
203 glm::vec3 dir1 = ( -aCamRight + aCamUp ) * h;
204 glm::vec3 dir2 = ( -aCamRight - aCamUp ) * h;
207 glVertex3f( ( aPos - dir1 ).x, ( aPos - dir1 ).y, ( aPos - dir1 ).z );
208 glVertex3f( ( aPos + dir1 ).x, ( aPos + dir1 ).y, ( aPos + dir1 ).z );
210 glVertex3f( ( aPos - dir2 ).x, ( aPos - dir2 ).y, ( aPos - dir2 ).z );
211 glVertex3f( ( aPos + dir2 ).x, ( aPos + dir2 ).y, ( aPos + dir2 ).z );
216 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
217 const glm::vec3& aCamUp )
219 glColor3f( aColor.r, aColor.g, aColor.b );
222 float h = aSize * 0.5f;
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;
230 glVertex3f( topLeft.x, topLeft.y, topLeft.z );
231 glVertex3f( aPos.x, aPos.y, aPos.z );
233 glVertex3f( topRight.x, topRight.y, topRight.z );
234 glVertex3f( aPos.x, aPos.y, aPos.z );
236 glVertex3f( aPos.x, aPos.y, aPos.z );
237 glVertex3f( bottom.x, bottom.y, bottom.z );
242 [](
const glm::vec3& aPos,
float aSize,
const glm::vec3& aColor,
const glm::vec3& aCamRight,
243 const glm::vec3& aCamUp )
245 glColor3f( aColor.r, aColor.g, aColor.b );
248 float h = aSize * 0.5f;
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;
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 );
264 for(
size_t i = 0; i <
m_spheres.size(); ++i )
266 if( labels[i].
empty() )
269 glm::vec3 textPos =
m_spheres[i].m_position + offset;
270 const std::string& label = labels[i];
274 drawX( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
276 else if( label ==
"Y" )
278 drawY( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
280 else if( label ==
"Z" )
282 drawZ( textPos, 0.30f, glm::vec3( 0.0f ), camRight, camUp );
286 glEnable( GL_LIGHTING );
287 glEnable( GL_DEPTH_TEST );
293 glColor3f( 0.9f, 0.0f, 0.0f );
294 glVertex3f( 0.0f, 0.0f, 0.0f );
297 glColor3f( 0.0f, 0.9f, 0.0f );
298 glVertex3f( 0.0f, 0.0f, 0.0f );
301 glColor3f( 0.0f, 0.0f, 0.9f );
302 glVertex3f( 0.0f, 0.0f, 0.0f );
307 glEnable( GL_CULL_FACE );
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;
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 );
337 glm::vec4 rayStartWorld = invVP * rayStartNDC;
338 rayStartWorld /= rayStartWorld.w;
340 glm::vec4 rayEndWorld = invVP * rayEndNDC;
341 rayEndWorld /= rayEndWorld.w;
343 glm::vec3 rayOrigin = glm::vec3( rayStartWorld );
344 glm::vec3 rayDirection = glm::normalize( glm::vec3( rayEndWorld - rayStartWorld ) );
346 auto intersectDist = [](
const glm::vec3& aRayOrigin,
const glm::vec3& aRayDir,
const glm::vec3& aSphereCenter,
347 float aRadius ) ->
float
349 glm::vec3
L = aSphereCenter - aRayOrigin;
350 float tca = glm::dot(
L, aRayDir );
351 float d2 = glm::dot(
L,
L ) - tca * tca;
353 if( d2 > aRadius * aRadius )
359 int clickedIndex = -1;
360 float closestDist = std::numeric_limits<float>::max();
363 for(
size_t i = 0; i <
m_spheres.size(); ++i )
366 float dist = intersectDist( rayOrigin, rayDirection, sphere.m_position, sphere.m_radius );
368 if( dist >= 0.0f && dist < closestDist )
371 clickedIndex =
static_cast<int>( i );
376 for(
size_t i = 0; i <
m_spheres.size(); ++i )
378 if(
static_cast<int>( i ) == clickedIndex )
379 m_spheres[i].m_color = { 1.0f, 1.0f, 1.0f };