Back to index

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

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

#include <pendulum.h>

List of all members.

Public Member Functions

 PendulumGLWidget (QWidget *parent=0)
 Constructor of KPendulum's GL widget.
 ~PendulumGLWidget (void)
 Destructor of KPendulum's GL widget.
void setEyePhi (double phi)
 Set phi angle of viewpoint.
void setAngles (const double &q1, const double &q2)
 Set angles of pendulum configuration.
void setMasses (const double &m1, const double &m2)
 Set masses of pendulum configuration.
void setLengths (const double &l1, const double &l2)
 Set lengths of pendulum configuration.
void setBarColor (const QColor &c)
 set color of the bars
QColor barColor (void) const
 get color of the bars
void setM1Color (const QColor &c)
 set color of mass 1
QColor m1Color (void) const
 get color of mass 1
void setM2Color (const QColor &c)
 set color of mass 2
QColor m2Color (void) const
 get color of mass 2

Protected Member Functions

virtual void paintGL ()
 paint the GL view
virtual void resizeGL (int w, int h)
 resize the gl view
virtual void initializeGL ()
 setup the GL environment

Private Attributes

GLfloat m_eyeR
 Eye position distance from coordinate zero point.
double m_eyeTheta
 Eye position theta angle from z axis in sterad.
double m_eyePhi
 Eye position phi angle (longitude) in sterad.
GLfloat m_lightR
 Light position distance from coordinate zero point.
double m_lightTheta
 Light position theta angle from z axis in sterad.
double m_lightPhi
 Light position phi angle (longitude) in sterad.
GLfloat m_ang1
GLfloat m_ang2
GLfloat m_sqrtm1
GLfloat m_sqrtm2
GLfloat m_l1
GLfloat m_l2
GLUquadricObj *const m_quadM1
 Pointer to a quadric object used in the rendering function paintGL()
QColor m_barColor
 color of the pendulum bars
QColor m_m1Color
 color of the 1.
QColor m_m2Color
 color of the 2.

Detailed Description

GL widget class for the KPendulum screen saver.

Class implements QGLWidget to display the KPendulum screen saver.

Definition at line 93 of file pendulum.h.


Constructor & Destructor Documentation

PendulumGLWidget::PendulumGLWidget ( QWidget *  parent = 0)

Constructor of KPendulum's GL widget.

Parameters:
parentparent widget, passed to QGLWidget's constructor

Definition at line 151 of file pendulum.cpp.

   : QGLWidget(parent),
     m_eyeR(30),                  // eye coordinates (polar)
     m_eyeTheta(M_PI*0.45),
     m_eyePhi(0),
     m_lightR(m_eyeR),              // light coordinates (polar)
     m_lightTheta(M_PI*0.25),
     m_lightPhi(M_PI*0.25),
     m_quadM1(gluNewQuadric()),
     m_barColor(KPendulumSaver::sm_barColorDefault),
     m_m1Color(KPendulumSaver::sm_m1ColorDefault),
     m_m2Color(KPendulumSaver::sm_m2ColorDefault)
{
}

Destructor of KPendulum's GL widget.

Definition at line 166 of file pendulum.cpp.

{
   gluDeleteQuadric(m_quadM1);
}

Member Function Documentation

QColor PendulumGLWidget::barColor ( void  ) const [inline]

get color of the bars

Returns:
color

Definition at line 127 of file pendulum.h.

{return m_barColor;}

Here is the caller graph for this function:

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

setup the GL environment

Definition at line 234 of file pendulum.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);
   glEnable(GL_LIGHT1);

   glMatrixMode(GL_MODELVIEW);           // select modelview matrix
   glLoadIdentity();
   // 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),
       0};
   glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
   // set position of light1
   lightPos[0] = m_lightR * std::sin(m_lightTheta) * std::sin(m_lightPhi+M_PI);
   lightPos[1] = m_lightR * std::sin(m_lightTheta) * std::cos(m_lightPhi+M_PI);
   glLightfv(GL_LIGHT1, GL_POSITION, lightPos);

   // only for lights #>0
   GLfloat spec[] = {1,1,1,1};
   glLightfv(GL_LIGHT1, GL_SPECULAR, spec);
   glLightfv(GL_LIGHT1, GL_DIFFUSE, spec);

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

   GLfloat emi[4] = {.13, .13, .13, 1};
   glMaterialfv(GL_FRONT, GL_EMISSION, emi);
}
QColor PendulumGLWidget::m1Color ( void  ) const [inline]

get color of mass 1

Returns:
color

Definition at line 133 of file pendulum.h.

{return m_m1Color;}

Here is the caller graph for this function:

QColor PendulumGLWidget::m2Color ( void  ) const [inline]

get color of mass 2

Returns:
color

Definition at line 139 of file pendulum.h.

{return m_m2Color;}

Here is the caller graph for this function:

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

paint the GL view

Definition at line 278 of file pendulum.cpp.

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

   glMatrixMode(GL_MODELVIEW);           // select modelview matrix

   glLoadIdentity();

   const GLfloat width     = 2.0;
   const GLfloat masswidth = 1.0;
   const int noOfSlices    = 20;

   // top axis, left (x>0)
   glTranslatef(0.5*width, 0, 0);
   glRotatef(90, 0, 1, 0);
   qglColor(m_barColor);
   gluCylinder(m_quadM1, 0.2, 0.2, 5, 10, 1);
   gluSphere(m_quadM1, 0.2, 10, 10);
   // top axis, right
   glLoadIdentity();
   glTranslatef(-0.5*width, 0, 0);
   glRotatef(-90, 0, 1, 0);
   gluCylinder(m_quadM1, 0.2, 0.2, 5, 10, 1);
   gluSphere(m_quadM1, 0.2, 10, 10);
   // 1. part, left
   glLoadIdentity();
   glRotatef(m_ang1, 1, 0, 0);
   glPushMatrix();
   glTranslatef(0.5*width, 0, -m_l1);
   gluCylinder(m_quadM1, 0.2, 0.2, m_l1, 10, 1);
   glPopMatrix();

   // 1. part, right
   glPushMatrix();
   glTranslatef(-0.5*width, 0, -m_l1);
   gluCylinder(m_quadM1, 0.2, 0.2, m_l1, 10, 1);
   // 1. part, bottom
   glRotatef(90, 0, 1, 0);
   gluSphere(m_quadM1, 0.2, 10, 10); // bottom corner 1
   gluCylinder(m_quadM1, 0.2, 0.2, width, 10, 1); // connection
   glTranslatef(0, 0, 0.5*(width-masswidth));
   qglColor(m_m1Color);
   gluCylinder(m_quadM1, m_sqrtm1, m_sqrtm1, masswidth, noOfSlices, 1); // mass 1
   gluQuadricOrientation(m_quadM1, GLU_INSIDE);
   gluDisk(m_quadM1, 0, m_sqrtm1, noOfSlices, 1); // bottom of mass
   gluQuadricOrientation(m_quadM1, GLU_OUTSIDE);
   glTranslatef(0, 0, masswidth);
   gluDisk(m_quadM1, 0, m_sqrtm1, noOfSlices, 1); // top of mass

   glTranslatef(0, 0, 0.5*(width-masswidth));
   qglColor(m_barColor);
   gluSphere(m_quadM1, 0.2, 10, 10); // bottom corner 2
   glPopMatrix();

   // 2. pendulum bar
   glLoadIdentity();
   glTranslatef(0, m_l1*std::sin(m_ang1*M_PI/180.), -m_l1*std::cos(m_ang1*M_PI/180.));
   glRotatef(m_ang2, 1, 0, 0);
   glTranslatef(0, 0, -m_l2);
   qglColor(m_barColor);
   gluCylinder(m_quadM1, 0.2, 0.2, m_l2, 10, 1);

   // mass 2
   glRotatef(90, 0, 1, 0);
   glTranslatef(0, 0, -0.5*masswidth);
   qglColor(m_m2Color);
   gluCylinder(m_quadM1, m_sqrtm2, m_sqrtm2, masswidth, noOfSlices, 1);
   gluQuadricOrientation(m_quadM1, GLU_INSIDE);
   gluDisk(m_quadM1, 0, m_sqrtm2, noOfSlices, 1); // bottom of mass
   gluQuadricOrientation(m_quadM1, GLU_OUTSIDE);
   glTranslatef(0, 0, masswidth);
   gluDisk(m_quadM1, 0, m_sqrtm2, noOfSlices, 1); // top of mass

   glFlush();
}
void PendulumGLWidget::resizeGL ( int  w,
int  h 
) [protected, virtual]

resize the gl view

Definition at line 355 of file pendulum.cpp.

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

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

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

   // set up projection matrix
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   // Perspective view
   gluPerspective(
      40.0f,
      static_cast<GLdouble>(w)/static_cast<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);
}

Here is the caller graph for this function:

void PendulumGLWidget::setAngles ( const double &  q1,
const double &  q2 
)

Set angles of pendulum configuration.

Parameters:
q1angle of 1. pendulum in sterad
q2angle of 2. pendulum in sterad

Definition at line 191 of file pendulum.cpp.

{
   m_ang1 = static_cast<GLfloat>(q1*180./M_PI);
   m_ang2 = static_cast<GLfloat>(q2*180./M_PI);
}

Here is the caller graph for this function:

void PendulumGLWidget::setBarColor ( const QColor &  c)

set color of the bars

Parameters:
ccolor

Definition at line 209 of file pendulum.cpp.

{
   if (c.isValid())
   {
      m_barColor = c;
   }
}

Here is the caller graph for this function:

void PendulumGLWidget::setEyePhi ( double  phi)

Set phi angle of viewpoint.

Parameters:
phiangle in sterad

Definition at line 171 of file pendulum.cpp.

{
   m_eyePhi = phi;
   while (m_eyePhi < 0)
   {
      m_eyePhi += 2.*M_PI;
   }
   while (m_eyePhi > 2*M_PI)
   {
      m_eyePhi -= 2.*M_PI;
   }

   // get the view port
   GLint vp[4];
   glGetIntegerv(GL_VIEWPORT, vp);

   // calc new perspective, a resize event is simulated here
   resizeGL(static_cast<int>(vp[2]), static_cast<int>(vp[3]));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PendulumGLWidget::setLengths ( const double &  l1,
const double &  l2 
)

Set lengths of pendulum configuration.

Parameters:
l1length of 1. pendulum
l2length of 2. pendulum

Definition at line 203 of file pendulum.cpp.

{
   m_l1 = static_cast<GLfloat>(l1);
   m_l2 = static_cast<GLfloat>(l2);
}

Here is the caller graph for this function:

void PendulumGLWidget::setM1Color ( const QColor &  c)

set color of mass 1

Parameters:
ccolor

Definition at line 217 of file pendulum.cpp.

{
   if (c.isValid())
   {
      m_m1Color = c;
   }
}

Here is the caller graph for this function:

void PendulumGLWidget::setM2Color ( const QColor &  c)

set color of mass 2

Parameters:
ccolor

Definition at line 224 of file pendulum.cpp.

{
   if (c.isValid())
   {
      m_m2Color = c;
   }
}

Here is the caller graph for this function:

void PendulumGLWidget::setMasses ( const double &  m1,
const double &  m2 
)

Set masses of pendulum configuration.

Parameters:
m1mass of 1. pendulum
m2mass of 2. pendulum

Definition at line 197 of file pendulum.cpp.

{
   m_sqrtm1 = static_cast<GLfloat>(std::sqrt(m1));
   m_sqrtm2 = static_cast<GLfloat>(std::sqrt(m2));
}

Here is the caller graph for this function:


Member Data Documentation

GLfloat PendulumGLWidget::m_ang1 [private]
  1. pendulum's angle, degree

Definition at line 164 of file pendulum.h.

GLfloat PendulumGLWidget::m_ang2 [private]
  1. pendulum's angle, degree

Definition at line 166 of file pendulum.h.

QColor PendulumGLWidget::m_barColor [private]

color of the pendulum bars

Definition at line 182 of file pendulum.h.

double PendulumGLWidget::m_eyePhi [private]

Eye position phi angle (longitude) in sterad.

Definition at line 155 of file pendulum.h.

GLfloat PendulumGLWidget::m_eyeR [private]

Eye position distance from coordinate zero point.

Definition at line 151 of file pendulum.h.

double PendulumGLWidget::m_eyeTheta [private]

Eye position theta angle from z axis in sterad.

Definition at line 153 of file pendulum.h.

GLfloat PendulumGLWidget::m_l1 [private]
  1. pendulum's length

Definition at line 174 of file pendulum.h.

GLfloat PendulumGLWidget::m_l2 [private]
  1. pendulum's length

Definition at line 176 of file pendulum.h.

double PendulumGLWidget::m_lightPhi [private]

Light position phi angle (longitude) in sterad.

Definition at line 161 of file pendulum.h.

GLfloat PendulumGLWidget::m_lightR [private]

Light position distance from coordinate zero point.

Definition at line 157 of file pendulum.h.

Light position theta angle from z axis in sterad.

Definition at line 159 of file pendulum.h.

QColor PendulumGLWidget::m_m1Color [private]

color of the 1.

mass

Definition at line 184 of file pendulum.h.

QColor PendulumGLWidget::m_m2Color [private]

color of the 2.

mass

Definition at line 186 of file pendulum.h.

GLUquadricObj* const PendulumGLWidget::m_quadM1 [private]

Pointer to a quadric object used in the rendering function paintGL()

Definition at line 179 of file pendulum.h.

GLfloat PendulumGLWidget::m_sqrtm1 [private]
  1. pendulum's square root of mass

Definition at line 169 of file pendulum.h.

GLfloat PendulumGLWidget::m_sqrtm2 [private]
  1. pendulum's square root of mass

Definition at line 171 of file pendulum.h.


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