Compile GL screensaver
[kdeartwork.git] / kscreensaver / kdesavers / wave.cpp
blob68a63496a03042c4d498df95a4b04f9d6ca4799c
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, const char *name )
70 : SetupUi( parent, name, TRUE )
72 readSettings();
74 preview->setFixedSize( 220, 170 );
75 preview->setBackgroundColor( Qt::black );
76 preview->show(); // otherwise saver does not get correct size
77 saver = new KWaveSaver( preview->winId() );
79 connect( PushButton1, SIGNAL( clicked() ), SLOT( slotOkPressed() ) );
80 connect( PushButton2, SIGNAL( clicked() ), SLOT( reject() ) );
81 connect( PushButton3, SIGNAL( clicked() ), SLOT( aboutPressed() ) );
82 connect( SpinBox1, SIGNAL( valueChanged(int)), saver, SLOT( updateSize(int)));
83 connect( RadioButton1, SIGNAL( toggled(bool)), saver, SLOT( doStars(bool)));
87 KWaveSetup::~KWaveSetup( )
89 delete saver;
93 // read settings from config file
94 void KWaveSetup::readSettings()
96 KConfig *config = KGlobal::config();
97 config->setGroup( "Settings" );
99 // color = config->readColorEntry( "Color", &black );
102 // Ok pressed - save settings and exit
103 void KWaveSetup::slotOkPressed()
105 KConfig *config = KGlobal::config();
106 config->setGroup( "Settings" );
108 // config->writeEntry( "Color", color );
110 config->sync();
112 accept();
115 void KWaveSetup::aboutPressed()
117 KMessageBox::about(this,
118 i18n("<h3>Bitmap Flag Screen Saver</h3>\n<p>Waving Flag Screen Saver for KDE</p>\nCopyright (c) Ian Reinhart Geiser 2001"));
120 //-----------------------------------------------------------------------------
123 KWaveSaver::KWaveSaver( WId id ) : KScreenSaver( id )
125 kDebug() << "Blank" << endl;
126 readSettings();
128 timer = new QTimer( this );
129 timer->start( 50, TRUE );
130 setBackgroundColor( Qt::black );
131 erase();
132 wave = new Wave();
133 embed(wave);
134 wave->show();
135 connect( timer, SIGNAL(timeout()), this, SLOT(blank()) );;
138 KWaveSaver::~KWaveSaver()
143 // read configuration settings from config file
144 void KWaveSaver::readSettings()
146 KConfig *config = KGlobal::config();
147 config->setGroup( "Settings" );
149 // color = config->readColorEntry( "Color", &black );
152 void KWaveSaver::blank()
154 // Play wave
156 wave->updateGL();
157 timer->start( 100, TRUE );
160 Wave::Wave( QWidget * parent, const char * name) : QGLWidget (parent,name)
162 pNurb = 0;
164 nNumPoints = 4;
165 index = 0;
168 Wave::~Wave()
170 glDeleteTextures( 1, &texture[0] );
171 gluDeleteNurbsRenderer(pNurb);
174 /** setup the GL enviroment */
175 void Wave::initializeGL ()
178 kDebug() << "InitGL" << endl;
180 /* Load in the texture */
181 if ( !LoadGLTextures( ) )
182 exit(0);
184 /* Enable Texture Mapping ( NEW ) */
185 glEnable( GL_TEXTURE_2D );
187 /* Enable smooth shading */
188 glShadeModel( GL_SMOOTH );
189 // Light values and coordinates
190 GLfloat specular[] = { 0.7f, 0.0f, 0.0f, 1.0f};
191 GLfloat shine[] = { 75.0f };
193 glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
195 glEnable(GL_LIGHTING);
196 glEnable(GL_LIGHT0);
198 glEnable(GL_COLOR_MATERIAL);
200 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
201 glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
202 glMaterialfv(GL_FRONT, GL_SHININESS, shine);
204 glEnable(GL_AUTO_NORMAL);
207 pNurb = gluNewNurbsRenderer();
209 gluNurbsProperty(pNurb, GLU_SAMPLING_TOLERANCE, 25.0f);
210 // Uncomment the next line and comment the one following to produce a
211 // wire frame mesh.
212 //gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
213 gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, (GLfloat)GLU_FILL);
214 glEnable(GL_MAP2_TEXTURE_COORD_3);
215 glEnable(GL_MAP2_VERTEX_3);
216 glEnable(GL_BLEND);
219 /** resize the gl view */
220 void Wave::resizeGL ( int w, int h)
222 kDebug() << "ResizeGL " << w << "," <<h<< endl;
223 // Prevent a divide by zero
224 if(h == 0)
225 h = 1;
227 // Set Viewport to window dimensions
228 glViewport(0, 0, w, h);
229 glMatrixMode(GL_PROJECTION);
230 glLoadIdentity();
232 // Perspective view
233 gluPerspective (45.0f, (GLdouble)w/(GLdouble)h, 1.0, 40.0f);
235 // Modelview matrix reset
236 glMatrixMode(GL_MODELVIEW);
237 glLoadIdentity();
239 // Viewing transformation, position for better view
240 glTranslatef (0.0f, 0.0f, -20.0f);
243 /** paint the GL view */
244 void Wave::paintGL ()
247 float ctrlPoints[4][4][3];
249 index++;
250 float Z[16];
251 for( int i = 0; i < 16; i++)
253 Z[i] = 3.0 * sin(16*(3.141592654 * 2.0f) * (index+(i))/360);
256 // kDebug() << "-----" << endl;
257 int counter =15;
258 for( int i = 0; i < 4; i++)
259 for( int j = 0; j < 4;j++)
261 ctrlPoints[i][j][0] = float((5*i)-10);
262 ctrlPoints[i][j][1] = float((3*j)-6);
263 ctrlPoints[i][j][2] = Z[counter--];
264 // kDebug() << Z[counter] << endl;
266 // Knot sequence for the NURB
267 float knots[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
268 // Draw in Blu
269 //glColor3ub(0,0,220);
271 // Clear the window with current clearing color
272 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
274 // Save the modelview matrix stack
275 glMatrixMode(GL_MODELVIEW);
276 glPushMatrix();
278 // Rotate the mesh around to make it easier to see
279 glRotatef(index/2, 1.0f,0.0f,0.0f);
280 glRotatef(index/5, 0.0f,1.0f,0.0f);
281 glRotatef(index/6, 0.0f,0.0f,1.0f);
283 // Render the NURB
284 gluBeginSurface( pNurb );
285 gluNurbsSurface( pNurb, 8, knots, 8, knots,
286 4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_TEXTURE_COORD_3);
287 //gluNurbsSurface( pNurb, 8, knots, 8, knots,
288 //4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_NORMAL );
289 gluNurbsSurface( pNurb, 8, knots, 8, knots,
290 4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3 );
291 gluEndSurface( pNurb );
293 // Restore the modelview matrix
294 glPopMatrix();
296 glFlush();
299 bool Wave::LoadGLTextures()
301 /* Status indicator */
302 bool Status = TRUE;
304 QImage buf; // = QPixmap::grabWindow ( 0 ).convertToImage();
305 kDebug() << "Loading: " << KStandardDirs::locate("data", "kscreensaver/image.png") << endl;
306 if (buf.load( KStandardDirs::locate("data", "kscreensaver/image.png") ) )
309 tex = convertToGLFormat(buf); // flipped 32bit RGBA
310 kDebug() << "Texture loaded: " << tex.numBytes () << endl;
312 else
314 QImage dummy( 64, 64, 64 );
315 dummy.fill( Qt::white );
316 buf = dummy;
317 tex = convertToGLFormat( buf );
319 /* Set the status to true */
320 //Status = TRUE;
321 glGenTextures(1, &texture[0]); /* create three textures */
322 glBindTexture(GL_TEXTURE_2D, texture[0]);
323 /* use linear filtering */
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
326 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
327 /* actually generate the texture */
328 glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
329 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
330 kDebug() << "Texture Loaded: " << tex.width() << "," << tex.height() << endl;
333 return Status;