Any ideas why this doesn’t work? I am drawing a scene just fine and I want to be able to attach to any object in it.
First I clear the scene and apply the viewport transformation. Second I setup the projection transformation. Then I setup the viewing transformation and render my scene graph.
Problem is that it doesnt matter what the attach point coordinates are, I am always looking at the origin.
Any clues?
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Render this scene
void MScene::render()
{
glPushAttrib(GL_TRANSFORM_BIT);
// Clear the scene and apply the viewport transformation.
glClearColor(
m_BkgColor.red(),
m_BkgColor.green(),
m_BkgColor.blue(),
m_BkgColor.alpha()
);
clear();
MCamera* camera = m_CameraPtr.get(); // Get pointer to camera
camera->applyViewportTransformation(); // Viewport transformation
// Setup the projection transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
camera->applyProjectionTransformation();
// Setup the viewing transformation and render the scene
MAttachedNavigator* navigator = m_NavigatorPtr.get(); // Get pointer to the navigator
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
navigator->setupViewingTransform(); // Viewing transformation
ActionTree(p_SceneGraph); // Render the scene
glPopMatrix();
// Done
glPopAttrib();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Apply the viewport transformation for this camera.
void MCamera::applyViewportTransformation() const
{
glViewport(
viewPortXOrigin(),viewPortYOrigin(),viewPortWidth(),viewPortHeight()
);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Apply the viewport transformation for this camera.
void MCamera::applyProjectionTransformation() const
{
gluPerspective(fovY(),viewPort()->aspectRatio(),nearPlane(),farPlane());
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup the viewing transform.
void MAttachedNavigator::setupViewingTransform()
{
MDoubleVector3D cameraPos(attachment().attachPoint());
cameraPos += m_CameraOffset;
glTranslated(0,0,-cameraPos.magnitude());
glRotated(-m_CameraRotation.x(),0,0,1);
glRotated(-m_CameraRotation.y(),1,0,0);
glRotated(-m_CameraRotation.z(),0,0,1);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Retrieve the current attachment
const MAttachable& MAttachedNavigator::attachment() const
{
return m_CurrentAttachment;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Retrieve the coordinates of this attachment
const MDoubleVector3D& MAttachable::attachPoint() const
{
return m_AttachPointCoordinates;
}