uic4 port
[kdeartwork.git] / kscreensaver / kdesavers / wave.cpp
blob7e0f2aa3f8acce0f028ece86e2fc5970de140c9b
1 //-----------------------------------------------------------------------------
2 //
3 // kwave - Waveing Flag Screen Saver for KDE 2
4 //
5 // Copyright (c) Ian Reinhart Geiser 2001
6 //
7 #include <stdlib.h>
8 #include <qlabel.h>
9 #include <qlayout.h>
10 #include <kapplication.h>
11 #include <klocale.h>
12 #include <kconfig.h>
13 #include <kcolordialog.h>
14 #include <kbuttonbox.h>
15 #include <kcolorbutton.h>
16 #include <kglobal.h>
17 #include "wave.h"
18 #include "wave.moc"
19 #ifdef Q_WS_MACX
20 #include <OpenGL/glu.h>
21 #include <OpenGL/gl.h>
22 #else
23 #include <GL/glu.h>
24 #include <GL/gl.h>
25 #endif
26 #include <qimage.h>
27 #include <kdebug.h>
28 #include <qpainter.h>
29 #include <qradiobutton.h>
30 #include <qspinbox.h>
31 #include <kstandarddirs.h>
32 #include <math.h>
33 #include <kmessagebox.h>
34 #ifndef GLU_NURBS_TEXTURE_COORD
35 #define GLU_NURBS_TEXTURE_COORD 100168
36 #endif
37 // libkscreensaver interface
39 class KWaveSaverInterface : public KScreenSaverInterface
43 public:
44 virtual KAboutData* aboutData() {
45 return new KAboutData( "kwave.kss", I18N_NOOP( "Bitmap Wave Screen Saver" ), "2.2.0", I18N_NOOP( "Bitmap Wave Screen Saver" ) );
49 virtual KScreenSaver* create( WId id )
51 return new KWaveSaver( id );
54 virtual QDialog* setup()
56 return new KWaveSetup();
60 int main( int argc, char *argv[] )
62 KWaveSaverInterface kss;
63 return kScreenSaverMain( argc, argv, kss );
66 //-----------------------------------------------------------------------------
67 // dialog to setup screen saver parameters
69 KWaveSetup::KWaveSetup( QWidget *parent)
70 : QDialog( parent )
72 setupUi(this);
73 setModal(true);
74 readSettings();
76 preview->setFixedSize( 220, 170 );
77 preview->setBackgroundColor( Qt::black );
78 preview->show(); // otherwise saver does not get correct size
79 saver = new KWaveSaver( preview->winId() );
81 connect( PushButton1, SIGNAL( clicked() ), SLOT( slotOkPressed() ) );
82 connect( PushButton2, SIGNAL( clicked() ), SLOT( reject() ) );
83 connect( PushButton3, SIGNAL( clicked() ), SLOT( aboutPressed() ) );
84 connect( SpinBox1, SIGNAL( valueChanged(int)), saver, SLOT( updateSize(int)));
85 connect( RadioButton1, SIGNAL( toggled(bool)), saver, SLOT( doStars(bool)));
89 KWaveSetup::~KWaveSetup( )
91 delete saver;
95 // read settings from config file
96 void KWaveSetup::readSettings()
98 KConfig *config = KGlobal::config();
99 config->setGroup( "Settings" );
101 // color = config->readColorEntry( "Color", &black );
104 // Ok pressed - save settings and exit
105 void KWaveSetup::slotOkPressed()
107 KConfig *config = KGlobal::config();
108 config->setGroup( "Settings" );
110 // config->writeEntry( "Color", color );
112 config->sync();
114 accept();
117 void KWaveSetup::aboutPressed()
119 KMessageBox::about(this,
120 i18n("<h3>Bitmap Flag Screen Saver</h3>\n<p>Waving Flag Screen Saver for KDE</p>\nCopyright (c) Ian Reinhart Geiser 2001"));
122 //-----------------------------------------------------------------------------
125 KWaveSaver::KWaveSaver( WId id ) : KScreenSaver( id )
127 kDebug() << "Blank" << endl;
128 readSettings();
130 timer = new QTimer( this );
131 timer->start( 50, TRUE );
132 setBackgroundColor( Qt::black );
133 erase();
134 wave = new Wave();
135 embed(wave);
136 wave->show();
137 connect( timer, SIGNAL(timeout()), this, SLOT(blank()) );;
140 KWaveSaver::~KWaveSaver()
145 // read configuration settings from config file
146 void KWaveSaver::readSettings()
148 KConfig *config = KGlobal::config();
149 config->setGroup( "Settings" );
151 // color = config->readColorEntry( "Color", &black );
154 void KWaveSaver::blank()
156 // Play wave
158 wave->updateGL();
159 timer->start( 100, TRUE );
162 Wave::Wave( QWidget * parent, const char * name) : QGLWidget (parent,name)
164 pNurb = 0;
166 nNumPoints = 4;
167 index = 0;
170 Wave::~Wave()
172 glDeleteTextures( 1, &texture[0] );
173 gluDeleteNurbsRenderer(pNurb);
176 /** setup the GL enviroment */
177 void Wave::initializeGL ()
180 kDebug() << "InitGL" << endl;
182 /* Load in the texture */
183 if ( !LoadGLTextures( ) )
184 exit(0);
186 /* Enable Texture Mapping ( NEW ) */
187 glEnable( GL_TEXTURE_2D );
189 /* Enable smooth shading */
190 glShadeModel( GL_SMOOTH );
191 // Light values and coordinates
192 GLfloat specular[] = { 0.7f, 0.0f, 0.0f, 1.0f};
193 GLfloat shine[] = { 75.0f };
195 glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
197 glEnable(GL_LIGHTING);
198 glEnable(GL_LIGHT0);
200 glEnable(GL_COLOR_MATERIAL);
202 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
203 glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
204 glMaterialfv(GL_FRONT, GL_SHININESS, shine);
206 glEnable(GL_AUTO_NORMAL);
209 pNurb = gluNewNurbsRenderer();
211 gluNurbsProperty(pNurb, GLU_SAMPLING_TOLERANCE, 25.0f);
212 // Uncomment the next line and comment the one following to produce a
213 // wire frame mesh.
214 //gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
215 gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, (GLfloat)GLU_FILL);
216 glEnable(GL_MAP2_TEXTURE_COORD_3);
217 glEnable(GL_MAP2_VERTEX_3);
218 glEnable(GL_BLEND);
221 /** resize the gl view */
222 void Wave::resizeGL ( int w, int h)
224 kDebug() << "ResizeGL " << w << "," <<h<< endl;
225 // Prevent a divide by zero
226 if(h == 0)
227 h = 1;
229 // Set Viewport to window dimensions
230 glViewport(0, 0, w, h);
231 glMatrixMode(GL_PROJECTION);
232 glLoadIdentity();
234 // Perspective view
235 gluPerspective (45.0f, (GLdouble)w/(GLdouble)h, 1.0, 40.0f);
237 // Modelview matrix reset
238 glMatrixMode(GL_MODELVIEW);
239 glLoadIdentity();
241 // Viewing transformation, position for better view
242 glTranslatef (0.0f, 0.0f, -20.0f);
245 /** paint the GL view */
246 void Wave::paintGL ()
249 float ctrlPoints[4][4][3];
251 index++;
252 float Z[16];
253 for( int i = 0; i < 16; i++)
255 Z[i] = 3.0 * sin(16*(3.141592654 * 2.0f) * (index+(i))/360);
258 // kDebug() << "-----" << endl;
259 int counter =15;
260 for( int i = 0; i < 4; i++)
261 for( int j = 0; j < 4;j++)
263 ctrlPoints[i][j][0] = float((5*i)-10);
264 ctrlPoints[i][j][1] = float((3*j)-6);
265 ctrlPoints[i][j][2] = Z[counter--];
266 // kDebug() << Z[counter] << endl;
268 // Knot sequence for the NURB
269 float knots[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
270 // Draw in Blu
271 //glColor3ub(0,0,220);
273 // Clear the window with current clearing color
274 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
276 // Save the modelview matrix stack
277 glMatrixMode(GL_MODELVIEW);
278 glPushMatrix();
280 // Rotate the mesh around to make it easier to see
281 glRotatef(index/2, 1.0f,0.0f,0.0f);
282 glRotatef(index/5, 0.0f,1.0f,0.0f);
283 glRotatef(index/6, 0.0f,0.0f,1.0f);
285 // Render the NURB
286 gluBeginSurface( pNurb );
287 gluNurbsSurface( pNurb, 8, knots, 8, knots,
288 4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_TEXTURE_COORD_3);
289 //gluNurbsSurface( pNurb, 8, knots, 8, knots,
290 //4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_NORMAL );
291 gluNurbsSurface( pNurb, 8, knots, 8, knots,
292 4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3 );
293 gluEndSurface( pNurb );
295 // Restore the modelview matrix
296 glPopMatrix();
298 glFlush();
301 bool Wave::LoadGLTextures()
303 /* Status indicator */
304 bool Status = TRUE;
306 QImage buf; // = QPixmap::grabWindow ( 0 ).convertToImage();
307 kDebug() << "Loading: " << KStandardDirs::locate("data", "kscreensaver/image.png") << endl;
308 if (buf.load( KStandardDirs::locate("data", "kscreensaver/image.png") ) )
311 tex = convertToGLFormat(buf); // flipped 32bit RGBA
312 kDebug() << "Texture loaded: " << tex.numBytes () << endl;
314 else
316 QImage dummy( 64, 64, 64 );
317 dummy.fill( Qt::white );
318 buf = dummy;
319 tex = convertToGLFormat( buf );
321 /* Set the status to true */
322 //Status = TRUE;
323 glGenTextures(1, &texture[0]); /* create three textures */
324 glBindTexture(GL_TEXTURE_2D, texture[0]);
325 /* use linear filtering */
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
329 /* actually generate the texture */
330 glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
331 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
332 kDebug() << "Texture Loaded: " << tex.width() << "," << tex.height() << endl;
335 return Status;