summaryrefslogtreecommitdiffstats
path: root/tdescreensaver/kdesavers/wave.cpp
blob: 880219bf133221aebd46918ee3696c193861b854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//-----------------------------------------------------------------------------
//
// kwave - Waveing Flag Screen Saver for KDE 2
//
// Copyright (c)  Ian Reinhart Geiser 2001
//
#include <stdlib.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <kcolordialog.h>
#include <kbuttonbox.h>
#include <kcolorbutton.h>
#include <tdeglobal.h>
#include "wave.h"
#include "wave.moc"
#ifdef TQ_WS_MACX
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#else
#include <GL/glu.h>
#include <GL/gl.h>
#endif
#include <tqimage.h>
#include <kdebug.h>
#include <tqpainter.h>
#include <tqradiobutton.h>
#include <tqspinbox.h>
#include <kstandarddirs.h>
#include <math.h>
#include <tdemessagebox.h>
#ifndef GLU_NURBS_TEXTURE_COORD
#define GLU_NURBS_TEXTURE_COORD            100168
#endif
// libtdescreensaver interface

extern "C"
{
	KDE_EXPORT const char *kss_applicationName = "kwave.kss";
	KDE_EXPORT const char *kss_description = I18N_NOOP( "Bitmap Wave Screen Saver" );
	KDE_EXPORT const char *kss_version = "2.2.0";

	KDE_EXPORT KScreenSaver *kss_create( WId id )
	{
		return new KWaveSaver( id );
	}

	KDE_EXPORT TQDialog *kss_setup()
	{
		return new KWaveSetup();
	}
}

//-----------------------------------------------------------------------------
// dialog to setup screen saver parameters
//
KWaveSetup::KWaveSetup( TQWidget *parent, const char *name )
	: SetupUi( parent, name, TRUE )
{
	readSettings();

	preview->setFixedSize( 220, 170 );
	preview->setBackgroundColor( black );
	preview->show();    // otherwise saver does not get correct size
	saver = new KWaveSaver( preview->winId() );

	connect( PushButton1, TQT_SIGNAL( clicked() ), TQT_SLOT( slotOkPressed() ) );
	connect( PushButton2, TQT_SIGNAL( clicked() ), TQT_SLOT( reject() ) );
	connect( PushButton3, TQT_SIGNAL( clicked() ), TQT_SLOT( aboutPressed() ) );
	connect(  SpinBox1, TQT_SIGNAL( valueChanged(int)), saver, TQT_SLOT( updateSize(int)));
	connect( RadioButton1, TQT_SIGNAL( toggled(bool)), saver, TQT_SLOT( doStars(bool)));

}

KWaveSetup::~KWaveSetup( )
{
    delete saver;
}


// read settings from config file
void KWaveSetup::readSettings()
{
	TDEConfig *config = TDEGlobal::config();
	config->setGroup( "Settings" );

//	color = config->readColorEntry( "Color", &black );
}

// Ok pressed - save settings and exit
void KWaveSetup::slotOkPressed()
{
	TDEConfig *config = TDEGlobal::config();
	config->setGroup( "Settings" );

//	config->writeEntry( "Color", color );

	config->sync();

	accept();
}

void KWaveSetup::aboutPressed()
{
    KMessageBox::about(this,
        i18n("<h3>Bitmap Flag Screen Saver</h3>\n<p>Waving Flag Screen Saver for TDE</p>\nCopyright (c)  Ian Reinhart Geiser 2001"));
}
//-----------------------------------------------------------------------------


KWaveSaver::KWaveSaver( WId id ) : KScreenSaver( id )
{
	kdDebug() << "Blank" << endl;
	readSettings();

	timer = new TQTimer( this );
    	timer->start( 50, TRUE );
	setBackgroundColor( black );
        erase();
	wave = new Wave();
	embed(wave);
	wave->show();
	connect( timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(blank()) );;
}

KWaveSaver::~KWaveSaver()
{

}

// read configuration settings from config file
void KWaveSaver::readSettings()
{
	TDEConfig *config = TDEGlobal::config();
	config->setGroup( "Settings" );

//	color = config->readColorEntry( "Color", &black );
}

void KWaveSaver::blank()
{
	// Play wave

	wave->updateGL();
	timer->start( 100, TRUE );

}
Wave::Wave( TQWidget * parent, const char * name) : TQGLWidget (parent,name)
{
	pNurb = 0;

	nNumPoints = 4;
	index = 0;
}

Wave::~Wave()
{
	glDeleteTextures( 1, &texture[0] );
	gluDeleteNurbsRenderer(pNurb);
}

/** setup the GL enviroment */
void Wave::initializeGL ()
{

	kdDebug() << "InitGL" << endl;

	/* Load in the texture */
	if ( !LoadGLTextures( ) )
		exit(0);

	/* Enable Texture Mapping ( NEW ) */
	glEnable( GL_TEXTURE_2D );

	/* Enable smooth shading */
	glShadeModel( GL_SMOOTH );
	// Light values and coordinates
	GLfloat  specular[] = { 0.7f, 0.0f, 0.0f, 1.0f};
	GLfloat  shine[] = { 75.0f };

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f );

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	glEnable(GL_COLOR_MATERIAL);

	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, shine);

	glEnable(GL_AUTO_NORMAL);


	pNurb = gluNewNurbsRenderer();

	gluNurbsProperty(pNurb, GLU_SAMPLING_TOLERANCE, 25.0f);
		// Uncomment the next line and comment the one following to produce a
		// wire frame mesh.
	//gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
	gluNurbsProperty(pNurb, GLU_DISPLAY_MODE, (GLfloat)GLU_FILL);
	glEnable(GL_MAP2_TEXTURE_COORD_3);
    	glEnable(GL_MAP2_VERTEX_3);
	glEnable(GL_BLEND);

}
/** resize the gl view */
void Wave::resizeGL ( int w, int h)
{
	kdDebug() << "ResizeGL " << w << "," <<h<< endl;
        // Prevent a divide by zero
        if(h == 0)
                h = 1;

        // Set Viewport to window dimensions
    glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        // Perspective view
        gluPerspective (45.0f, (GLdouble)w/(GLdouble)h, 1.0, 40.0f);

        // Modelview matrix reset
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        // Viewing transformation, position for better view
    glTranslatef (0.0f, 0.0f, -20.0f);

}
/** paint the GL view */
void Wave::paintGL ()
{

	float ctrlPoints[4][4][3];

	index++;
	float Z[16];
	for( int i = 0; i < 16; i++)
	{
		Z[i] = 3.0 * sin(16*(3.141592654 * 2.0f) * (index+(i))/360);
	}

//	kdDebug() << "-----" << endl;
	int counter =15;
	for( int i = 0; i < 4; i++)
		for( int j = 0; j < 4;j++)
		{
			ctrlPoints[i][j][0] = float((5*i)-10);
			ctrlPoints[i][j][1] = float((3*j)-6);
			ctrlPoints[i][j][2] = Z[counter--];
//			kdDebug() << Z[counter] << endl;
		}
	// Knot sequence for the NURB
	float knots[8] = { 0, 0, 0, 0,  1, 1, 1, 1 };
        // Draw in Blu
        //glColor3ub(0,0,220);

        // Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Save the modelview matrix stack
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();

        // Rotate the mesh around to make it easier to see
        glRotatef(index/2, 1.0f,0.0f,0.0f);
	glRotatef(index/5, 0.0f,1.0f,0.0f);
	glRotatef(index/6, 0.0f,0.0f,1.0f);

        // Render the NURB
   	gluBeginSurface( pNurb );
		gluNurbsSurface( pNurb, 8, knots, 8, knots,
		4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_TEXTURE_COORD_3);
		//gluNurbsSurface( pNurb, 8, knots, 8, knots,
		//4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_NORMAL );
		gluNurbsSurface( pNurb, 8, knots, 8, knots,
		4*3, 3, &ctrlPoints[0][0][0], 4, 4, GL_MAP2_VERTEX_3 );
   	gluEndSurface( pNurb );

        // Restore the modelview matrix
        glPopMatrix();

	glFlush();
}

bool Wave::LoadGLTextures()
{
    /* Status indicator */
    bool Status = TRUE;

	TQImage buf; // = TQPixmap::grabWindow ( 0 ).convertToImage();
   kdDebug() << "Loading: " << locate("data", "tdescreensaver/image.png") << endl;
 if (buf.load( locate("data", "tdescreensaver/image.png") ) )

        {
                tex = convertToGLFormat(buf);  // flipped 32bit RGBA
                kdDebug() << "Texture loaded: " << tex.numBytes () << endl;
        }
        else
        {
                TQImage dummy( 64, 64, 64 );
                dummy.fill( TQt::white.rgb() );
                buf = dummy;
                tex = convertToGLFormat( buf );
        }
            /* Set the status to true */
            //Status = TRUE;
	glGenTextures(1, &texture[0]);   /* create three textures */
 	glBindTexture(GL_TEXTURE_2D, texture[0]);
	/* use linear filtering */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	/* actually generate the texture */
	glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
	GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
	kdDebug() << "Texture Loaded: " << tex.width() << "," << tex.height() << endl;


    return Status;
}