Back to index

kdeartwork  4.3.2
wave.cpp
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // kwave - Waveing Flag Screen Saver for KDE
00004 //
00005 // Copyright (c)  Ian Reinhart Geiser 2001
00006 //
00007 #include <stdlib.h>
00008 #include <qlabel.h>
00009 #include <qlayout.h>
00010 #include <kapplication.h>
00011 #include <klocale.h>
00012 #include <kconfig.h>
00013 #include <kcolordialog.h>
00014 #include <kcolorbutton.h>
00015 #include <kglobal.h>
00016 #include "wave.h"
00017 #include "wave.moc"
00018 #ifdef Q_WS_MACX
00019 #include <OpenGL/glu.h>
00020 #include <OpenGL/gl.h>
00021 #else
00022 #include <GL/glu.h>
00023 #include <GL/gl.h>
00024 #endif
00025 #include <qimage.h>
00026 #include <kdebug.h>
00027 #include <qpainter.h>
00028 #include <qradiobutton.h>
00029 #include <qspinbox.h>
00030 #include <kstandarddirs.h>
00031 #include <math.h>
00032 #include <kmessagebox.h>
00033 #ifndef GLU_NURBS_TEXTURE_COORD
00034 #define GLU_NURBS_TEXTURE_COORD            100168
00035 #endif
00036 // libkscreensaver interface
00037 
00038 class KWaveSaverInterface : public KScreenSaverInterface
00039 {
00040 
00041 
00042 public:
00043     virtual KAboutData* aboutData() {
00044         return new KAboutData( "kwave.kss", "klock", ki18n( "Bitmap Wave Screen Saver" ), "2.2.0", ki18n( "Bitmap Wave Screen Saver" ) );
00045     }
00046 
00047 
00048        virtual KScreenSaver* create( WId id )
00049        {
00050               return new KWaveSaver( id );
00051        }
00052 
00053        virtual QDialog* setup()
00054        {
00055               return new KWaveSetup();
00056        }
00057 };
00058 
00059 int main( int argc, char *argv[] )
00060 {
00061     KWaveSaverInterface kss;
00062     return kScreenSaverMain( argc, argv, kss );
00063 }
00064 
00065 //-----------------------------------------------------------------------------
00066 // dialog to setup screen saver parameters
00067 //
00068 KWaveSetup::KWaveSetup( QWidget *parent)
00069        : QDialog( parent )
00070 {
00071        setupUi(this);
00072        setModal(true);
00073        readSettings();
00074 
00075        preview->setFixedSize( 220, 170 );
00076 
00077         QPalette palette;
00078         palette.setColor( preview->backgroundRole(), Qt::black );
00079         preview->setPalette( palette );
00080        preview->setAutoFillBackground(true);
00081 
00082        preview->show();    // otherwise saver does not get correct size
00083        saver = new KWaveSaver( preview->winId() );
00084 
00085        connect( PushButton1, SIGNAL( clicked() ), SLOT( slotOkPressed() ) );
00086        connect( PushButton2, SIGNAL( clicked() ), SLOT( reject() ) );
00087        connect( PushButton3, SIGNAL( clicked() ), SLOT( aboutPressed() ) );
00088        connect(  SpinBox1, SIGNAL( valueChanged(int)), saver, SLOT( updateSize(int)));
00089        connect( RadioButton1, SIGNAL( toggled(bool)), saver, SLOT( doStars(bool)));
00090 
00091 }
00092 
00093 KWaveSetup::~KWaveSetup( )
00094 {
00095     delete saver;
00096 }
00097 
00098 
00099 // read settings from config file
00100 void KWaveSetup::readSettings()
00101 {
00102        KConfigGroup config(KGlobal::config(), "Settings");
00103 
00104 //     color = group.configColorEntry( "Color", &black );
00105 }
00106 
00107 // Ok pressed - save settings and exit
00108 void KWaveSetup::slotOkPressed()
00109 {
00110        KConfigGroup config(KGlobal::config(), "Settings");
00111 
00112 //     group.writeEntry( "Color", color );
00113 
00114        config.sync();
00115 
00116        accept();
00117 }
00118 
00119 void KWaveSetup::aboutPressed()
00120 {
00121     KMessageBox::about(this,
00122         i18n("<h3>Bitmap Flag Screen Saver</h3>\n<p>Waving Flag Screen Saver for KDE</p>\nCopyright (c)  Ian Reinhart Geiser 2001"));
00123 }
00124 //-----------------------------------------------------------------------------
00125 
00126 
00127 KWaveSaver::KWaveSaver( WId id ) : KScreenSaver( id )
00128 {
00129        kDebug() << "Blank";
00130        readSettings();
00131 
00132        timer = new QTimer( this );
00133         timer->setSingleShot( true );
00134        timer->start( 50 );
00135         {
00136             QPalette palette;
00137             palette.setColor( backgroundRole(), Qt::black );
00138             setPalette( palette );
00139            setAutoFillBackground(true);
00140         }
00141        wave = new Wave();
00142        embed(wave);
00143        wave->show();
00144        connect( timer, SIGNAL(timeout()), this, SLOT(blank()) );;
00145 }
00146 
00147 KWaveSaver::~KWaveSaver()
00148 {
00149 
00150 }
00151 
00152 // read configuration settings from config file
00153 void KWaveSaver::readSettings()
00154 {
00155        KConfigGroup config(KGlobal::config(), "Settings");
00156 
00157 //     color = group.configColorEntry( "Color", &black );
00158 }
00159 
00160 void KWaveSaver::blank()
00161 {
00162        // Play wave
00163 
00164        wave->updateGL();
00165        timer->start( 100 );
00166 
00167 }
00168 Wave::Wave( QWidget * parent ) : QGLWidget (parent)
00169 {
00170        pNurb = 0;
00171 
00172        nNumPoints = 4;
00173        index = 0;
00174 }
00175 
00176 Wave::~Wave()
00177 {
00178        glDeleteTextures( 1, &texture[0] );
00179        gluDeleteNurbsRenderer(pNurb);
00180 }
00181 
00183 void Wave::initializeGL ()
00184 {
00185 
00186        kDebug() << "InitGL";
00187 
00188        /* Load in the texture */
00189        if ( !LoadGLTextures( ) )
00190               exit(0);
00191 
00192        /* Enable Texture Mapping ( NEW ) */
00193        glEnable( GL_TEXTURE_2D );
00194 
00195        /* Enable smooth shading */
00196        glShadeModel( GL_SMOOTH );
00197        // Light values and coordinates
00198        GLfloat  specular[] = { 0.7f, 0.0f, 0.0f, 1.0f};
00199        GLfloat  shine[] = { 75.0f };
00200 
00201        glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
00202 
00203        glEnable(GL_LIGHTING);
00204        glEnable(GL_LIGHT0);
00205 
00206        glEnable(GL_COLOR_MATERIAL);
00207 
00208        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00209        glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
00210        glMaterialfv(GL_FRONT, GL_SHININESS, shine);
00211 
00212        glEnable(GL_AUTO_NORMAL);
00213 
00214 
00215        pNurb = gluNewNurbsRenderer();
00216 
00217        gluNurbsProperty(pNurb, GLU_SAMPLING_TOLERANCE, 25.0f);
00218               // Uncomment the next line and comment the one following to produce a
00219               // wire frame mesh.
00220        //gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
00221        gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, (GLfloat)GLU_FILL);
00222        glEnable(GL_MAP2_TEXTURE_COORD_3);
00223        glEnable(GL_MAP2_VERTEX_3);
00224        glEnable(GL_BLEND);
00225 
00226 }
00228 void Wave::resizeGL ( int w, int h)
00229 {
00230        kDebug() << "ResizeGL " << w << "," <<h;
00231         // Prevent a divide by zero
00232         if(h == 0)
00233                 h = 1;
00234 
00235         // Set Viewport to window dimensions
00236     glViewport(0, 0, w, h);
00237         glMatrixMode(GL_PROJECTION);
00238         glLoadIdentity();
00239 
00240         // Perspective view
00241         gluPerspective (45.0f, (GLdouble)w/(GLdouble)h, 1.0, 40.0f);
00242 
00243         // Modelview matrix reset
00244         glMatrixMode(GL_MODELVIEW);
00245         glLoadIdentity();
00246 
00247         // Viewing transformation, position for better view
00248     glTranslatef (0.0f, 0.0f, -20.0f);
00249 
00250 }
00252 void Wave::paintGL ()
00253 {
00254 
00255        float ctrlPoints[4][4][3];
00256 
00257        index++;
00258        float Z[16];
00259        for( int i = 0; i < 16; i++)
00260        {
00261               Z[i] = 3.0 * sin(16*(3.141592654 * 2.0f) * (index+(i))/360);
00262        }
00263 
00264 //     kDebug() << "-----";
00265        int counter =15;
00266        for( int i = 0; i < 4; i++)
00267               for( int j = 0; j < 4;j++)
00268               {
00269                      ctrlPoints[i][j][0] = float((5*i)-10);
00270                      ctrlPoints[i][j][1] = float((3*j)-6);
00271                      ctrlPoints[i][j][2] = Z[counter--];
00272 //                   kDebug() << Z[counter];
00273               }
00274        // Knot sequence for the NURB
00275        float knots[8] = { 0, 0, 0, 0,  1, 1, 1, 1 };
00276         // Draw in Blu
00277         //glColor3ub(0,0,220);
00278 
00279         // Clear the window with current clearing color
00280         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00281 
00282         // Save the modelview matrix stack
00283         glMatrixMode(GL_MODELVIEW);
00284         glPushMatrix();
00285 
00286         // Rotate the mesh around to make it easier to see
00287         glRotatef(index/2, 1.0f,0.0f,0.0f);
00288        glRotatef(index/5, 0.0f,1.0f,0.0f);
00289        glRotatef(index/6, 0.0f,0.0f,1.0f);
00290 
00291         // Render the NURB
00292        gluBeginSurface( pNurb );
00293               gluNurbsSurface( pNurb, 8, knots, 8, knots,
00294               4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_TEXTURE_COORD_3);
00295               //gluNurbsSurface( pNurb, 8, knots, 8, knots,
00296               //4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_NORMAL );
00297               gluNurbsSurface( pNurb, 8, knots, 8, knots,
00298               4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3 );
00299        gluEndSurface( pNurb );
00300 
00301         // Restore the modelview matrix
00302         glPopMatrix();
00303 
00304        glFlush();
00305 }
00306 
00307 bool Wave::LoadGLTextures()
00308 {
00309     /* Status indicator */
00310     bool Status = true;
00311 
00312        QImage buf; // = QPixmap::grabWindow ( 0 ).convertToImage();
00313    kDebug() << "Loading: " << KStandardDirs::locate("data", "kscreensaver/image.png");
00314  if (buf.load( KStandardDirs::locate("data", "kscreensaver/image.png") ) )
00315 
00316         {
00317                 tex = convertToGLFormat(buf);  // flipped 32bit RGBA
00318                 kDebug() << "Texture loaded: " << tex.numBytes ();
00319         }
00320         else
00321         {
00322                 QImage dummy( 64, 64, QImage::Format_RGB32 );
00323                 dummy.fill( Qt::white );
00324                 buf = dummy;
00325                 tex = convertToGLFormat( buf );
00326         }
00327             /* Set the status to true */
00328             //Status = true;
00329        glGenTextures(1, &texture[0]);   /* create three textures */
00330        glBindTexture(GL_TEXTURE_2D, texture[0]);
00331        /* use linear filtering */
00332        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00333        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00334        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00335        /* actually generate the texture */
00336        glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
00337        GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
00338        kDebug() << "Texture Loaded: " << tex.width() << "," << tex.height();
00339 
00340 
00341     return Status;
00342 }