Back to index

kdeartwork  4.3.2
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes
RotationGLWidget Class Reference

GL widget class for the KRotation screen saver. More...

#include <rotation.h>

Collaboration diagram for RotationGLWidget:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 RotationGLWidget (QWidget *parent, const KRotationSaver &saver)
 Constructor of KRotation's GL widget.

Protected Member Functions

virtual void paintGL ()
 Called if scenery (GL view) must be updated.
virtual void resizeGL (int w, int h)
 Called if gl widget was resized.
virtual void initializeGL ()
 Setup the GL environment.

Private Member Functions

void myGlArrow (GLfloat total_length, GLfloat head_length, GLfloat base_width, GLfloat head_width)
 Draw 3D arrow.
void draw_traces (void)
 Draw the traces in the GL area.

Private Attributes

GLfloat m_eyeR
 Eye position distance from coordinate zero point.
GLfloat m_eyeTheta
 Eye position theta angle from z axis.
GLfloat m_eyePhi
 Eye position phi angle (longitude)
Vector3d m_boxSize
 Box size.
GLuint m_fixedAxses
 GL object list of fixed coordinate systems axses.
GLuint m_bodyAxses
 GL object list of rotating coordinate systems axses.
GLfloat m_lightR
 Light position distance from coordinate zero point.
GLfloat m_lightTheta
 Light position theta angle from z axis.
GLfloat m_lightPhi
 Light position phi angle (longitude)
GLfloat m_bodyAxsesLength
 Length of the rotating coordinate system axses.
GLfloat m_fixedAxsesLength
 Length of the fixed coordinate system axses.
const KRotationSaverm_saver
 reference to screen saver

Detailed Description

GL widget class for the KRotation screen saver.

Class implements QGLWidget to display the KRotation screen saver.

Definition at line 106 of file rotation.h.


Constructor & Destructor Documentation

RotationGLWidget::RotationGLWidget ( QWidget *  parent,
const KRotationSaver saver 
)

Constructor of KRotation's GL widget.

Parameters:
parentparent widget, passed to QGLWidget's constructor
omegacurrent rotation vector
etrace data
J3 vector with momenta of inertia with respect to the 3 figure axes.

Definition at line 158 of file rotation.cpp.

   : QGLWidget(parent),
     m_eyeR(25),
     m_eyeTheta(1),
     m_eyePhi(M_PI*0.25),
     m_boxSize(Vector3d::Ones()),
     m_fixedAxses(0),
     m_bodyAxses(0),
     m_lightR(10),
     m_lightTheta(M_PI/4),
     m_lightPhi(0),
     m_bodyAxsesLength(6),
     m_fixedAxsesLength(8),
     m_saver(saver)
{
   /* Set the box sizes from the momenta of inertia.  J is the 3 vector with
    * momenta of inertia with respect to the 3 figure axes. */

   const Vector3d& J = m_saver.J();

   /* the default values must be valid so that w,h,d are real! */
   const GLfloat x2 = 6.0*(-J[0] + J[1] + J[2]);
   const GLfloat y2 = 6.0*( J[0] - J[1] + J[2]);
   const GLfloat z2 = 6.0*( J[0] + J[1] - J[2]);

   if ((x2>=0) && (y2>=0) && (z2>=0))
   {
      m_boxSize = Vector3d(std::sqrt(x2), std::sqrt(y2), std::sqrt(z2));
   }
   else
   {
      kError() << "parameter error";
   }
}

Here is the call graph for this function:


Member Function Documentation

void RotationGLWidget::draw_traces ( void  ) [private]

Draw the traces in the GL area.

Definition at line 285 of file rotation.cpp.

{
   const std::deque<Matrix3d>&e = m_saver.e();

   // traces must contain at least 2 elements
   if (e.size() <= 1)
   {
      return;
   }

   glPushMatrix();
   glScalef(m_bodyAxsesLength, m_bodyAxsesLength, m_bodyAxsesLength);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   for (int j=0; j<3; ++j)
   {
      if (m_saver.traceFlag(j))
      {
         // emission colour
         GLfloat em[4] = {0,0,0,1};
         em[j] = 1; // set either red, green, blue emission colour

         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, em);
         glColor4fv(em);

         // set iterator of the tail part
         std::deque<Matrix3d>::const_iterator eit  = e.begin();
         std::deque<Matrix3d>::const_iterator tail =
            e.begin() +
            static_cast<std::deque<Matrix3d>::difference_type>
            (0.9 * e.size());

         glBegin(GL_LINES);
         for (; eit < e.end()-1; ++eit)
         {
            glVertex3f((*eit)(0,j), (*eit)(1,j), (*eit)(2,j));
            // decrease transparency for tail section
            if (eit > tail)
            {
               em[3] =
                  static_cast<GLfloat>
                  (1.0 - double(eit-tail)/(0.1 * e.size()));
            }
            glColor4fv(em);
            glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, em);
            glVertex3f((*(eit+1))(0,j), (*(eit+1))(1,j), (*(eit+1))(2,j));
         }
         glEnd();
      }
   }

   glDisable(GL_BLEND);

   glPopMatrix();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void RotationGLWidget::initializeGL ( void  ) [protected, virtual]

Setup the GL environment.

Definition at line 197 of file rotation.cpp.

{
   qglClearColor(QColor(Qt::black)); // set color to clear the background

   glClearDepth(1);             // depth buffer setup
   glEnable(GL_DEPTH_TEST);     // depth testing
   glDepthFunc(GL_LEQUAL);      // type of depth test

   glShadeModel(GL_SMOOTH);     // smooth color shading in poygons

   // nice perspective calculation
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

   // set up the light
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   // set position of light0
   GLfloat lightPos[4]=
      {m_lightR * std::sin(m_lightTheta) * std::sin(m_lightPhi),
       m_lightR * std::sin(m_lightTheta) * std::cos(m_lightPhi),
       m_lightR * std::cos(m_lightTheta), 1.};
   glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

   // enable setting the material colour by glColor()
   glEnable(GL_COLOR_MATERIAL);

   // set up display lists

   if (m_fixedAxses == 0)
   {
      m_fixedAxses = glGenLists(1); // list to be returned
   }
   glNewList(m_fixedAxses, GL_COMPILE);

   // fixed coordinate system axes

   glPushMatrix();
   glLoadIdentity();

   // z-axis, blue
   qglColor(QColor(Qt::blue));
   myGlArrow(m_fixedAxsesLength, 0.5f, 0.03f, 0.1f);

   // x-axis, red
   qglColor(QColor(Qt::red));
   glRotatef(90, 0, 1, 0);

   myGlArrow(m_fixedAxsesLength, 0.5f, 0.03f, 0.1f);

   // y-axis, green
   qglColor(QColor(Qt::green));
   glLoadIdentity();
   glRotatef(-90, 1, 0, 0);
   myGlArrow(m_fixedAxsesLength, 0.5f, 0.03f, 0.1f);

   glPopMatrix();
   glEndList();
   // end of axes object list


   // box and box-axses
   if (m_bodyAxses == 0)
   {
      m_bodyAxses = glGenLists(1); // list to be returned
   }
   glNewList(m_bodyAxses, GL_COMPILE);

   // z-axis, blue
   qglColor(QColor(Qt::blue));
   myGlArrow(m_bodyAxsesLength, 0.5f, 0.03f, 0.1f);

   // x-axis, red
   qglColor(QColor(Qt::red));
   glPushMatrix();
   glRotatef(90, 0, 1, 0);
   myGlArrow(m_bodyAxsesLength, 0.5f, 0.03f, 0.1f);
   glPopMatrix();

   // y-axis, green
   qglColor(QColor(Qt::green));
   glPushMatrix();
   glRotatef(-90, 1, 0, 0);
   myGlArrow(m_bodyAxsesLength, 0.5f, 0.03f, 0.1f);
   glPopMatrix();

   glEndList();
}

Here is the call graph for this function:

void RotationGLWidget::myGlArrow ( GLfloat  total_length,
GLfloat  head_length,
GLfloat  base_width,
GLfloat  head_width 
) [private]

Draw 3D arrow.

Parameters:
total_lengthtotal length of arrow
head_lengthlength of arrow head (cone)
base_widthwidth of arrow base
head_widthwidth of arrow head (cone)

The arrow is drawn from the coordinates zero point along th z direction. The cone's tip is located at (0,0,total_length).

Definition at line 466 of file rotation.cpp.

{
   GLUquadricObj* const quadAx = gluNewQuadric();
   glPushMatrix();
   gluCylinder(
      quadAx, base_width, base_width,
      total_length - head_length, 10, 1);
   glTranslatef(0, 0, total_length - head_length);
   gluCylinder(quadAx, head_width, 0, head_length, 10, 1);
   glPopMatrix();
   gluDeleteQuadric(quadAx);
}

Here is the caller graph for this function:

void RotationGLWidget::paintGL ( void  ) [protected, virtual]

Called if scenery (GL view) must be updated.

Definition at line 343 of file rotation.cpp.

{
   // clear color and depth buffer
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

   glMatrixMode(GL_MODELVIEW);           // select modelview matrix

   glLoadIdentity();
   GLfloat const em[] = {0,0,0,1};
   glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, em);

   glPushMatrix();
   // calculate the transform which rotates the unit z vector onto omega
   glLoadMatrixd(
      Transform<double,3>(
         Quaternion<double>()
         .setFromTwoVectors(Vector3d::UnitZ(), m_saver.omega())
         .toRotationMatrix())
      .data());
   // draw the white omega arrow
   qglColor(QColor(Qt::white));
   myGlArrow(7, .5f, .1f, 0.2f);
   glPopMatrix();

   // draw the fixed axes
   glCallList(m_fixedAxses);


   // create transformation/rotation matrix from the body and its unit axes
   glPushMatrix();
   glLoadMatrixd(
      Transform<double,3>(m_saver.e().front())
      .data());

   // draw the body unit axis
   glCallList(m_bodyAxses);

   // scaling from a cube to the rotating body
   glScalef(m_boxSize[0]/2, m_boxSize[1]/2, m_boxSize[2]/2);

   // paint box
   glBegin(GL_QUADS);
   // front (z)
   qglColor(QColor(Qt::blue));
   glNormal3f( 0,0,1);
   glVertex3f( 1,  1,  1);
   glVertex3f(-1,  1,  1);
   glVertex3f(-1, -1,  1);
   glVertex3f( 1, -1,  1);
   // back (-z)
   glNormal3f( 0,0,-1);
   glVertex3f( 1,  1, -1);
   glVertex3f(-1,  1, -1);
   glVertex3f(-1, -1, -1);
   glVertex3f( 1, -1, -1);
   // top (y)
   qglColor(QColor(Qt::green));
   glNormal3f( 0,1,0);
   glVertex3f( 1,  1,  1);
   glVertex3f( 1,  1, -1);
   glVertex3f(-1,  1, -1);
   glVertex3f(-1,  1,  1);
   // bottom (-y)
   glNormal3f( 0,-1,0);
   glVertex3f( 1, -1,  1);

   glVertex3f( 1, -1, -1);
   glVertex3f(-1, -1, -1);
   glVertex3f(-1, -1,  1);
   // left (-x)
   qglColor(QColor(Qt::red));
   glNormal3f( -1,0,0);
   glVertex3f(-1,  1,  1);
   glVertex3f(-1,  1, -1);
   glVertex3f(-1, -1, -1);
   glVertex3f(-1, -1,  1);
   // right (x)
   glNormal3f( 1,0,0);
   glVertex3f( 1,  1,  1);
   glVertex3f( 1,  1, -1);
   glVertex3f( 1, -1, -1);
   glVertex3f( 1, -1,  1);
   glEnd();

   glPopMatrix();

   // draw the traces
   draw_traces();

   glFlush();
}

Here is the call graph for this function:

void RotationGLWidget::resizeGL ( int  w,
int  h 
) [protected, virtual]

Called if gl widget was resized.

Method makes adjustments for new perspective

Definition at line 435 of file rotation.cpp.

{
   kDebug() << "w=" << w << ", h=" << h << "\n";

   // prevent division by zero
   if (h == 0)
   {
      return;
   }

   // set the new view port
   glViewport(0, 0, (GLint)w, (GLint)h);

   // set up projection matrix
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   // Perspective view
   gluPerspective(40.0f, (GLdouble)w/(GLdouble)h, 1.0, 100.0f);

   // Viewing transformation, position for better view
   // Theta is polar angle 0<Theta<Pi
   gluLookAt(
      m_eyeR * std::sin(m_eyeTheta) * std::sin(m_eyePhi),
      m_eyeR * std::sin(m_eyeTheta) * std::cos(m_eyePhi),
      m_eyeR * std::cos(m_eyeTheta),
      0,0,0,
      0,0,1);
}

Member Data Documentation

GL object list of rotating coordinate systems axses.

Definition at line 159 of file rotation.h.

Length of the rotating coordinate system axses.

Definition at line 168 of file rotation.h.

Vector3d RotationGLWidget::m_boxSize [private]

Box size.

Definition at line 155 of file rotation.h.

GLfloat RotationGLWidget::m_eyePhi [private]

Eye position phi angle (longitude)

Definition at line 153 of file rotation.h.

GLfloat RotationGLWidget::m_eyeR [private]

Eye position distance from coordinate zero point.

Definition at line 149 of file rotation.h.

GLfloat RotationGLWidget::m_eyeTheta [private]

Eye position theta angle from z axis.

Definition at line 151 of file rotation.h.

GL object list of fixed coordinate systems axses.

Definition at line 157 of file rotation.h.

Length of the fixed coordinate system axses.

Definition at line 170 of file rotation.h.

GLfloat RotationGLWidget::m_lightPhi [private]

Light position phi angle (longitude)

Definition at line 165 of file rotation.h.

GLfloat RotationGLWidget::m_lightR [private]

Light position distance from coordinate zero point.

Definition at line 161 of file rotation.h.

GLfloat RotationGLWidget::m_lightTheta [private]

Light position theta angle from z axis.

Definition at line 163 of file rotation.h.

reference to screen saver

Definition at line 173 of file rotation.h.


The documentation for this class was generated from the following files: