1 /***************************************************************************
2 * Copyright (C) 2007 by Mark Kretschmann <kretschmann@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #define DEBUG_PREFIX "CoverBling"
23 #include "CoverBling.h"
28 #include <KStandardDirs>
30 #define TEXTURE_SIZE QSize( 256, 256 )
33 CoverBling::CoverBling( QWidget
* parent
)
34 : QGLWidget( QGLFormat(QGL::DepthBuffer
|QGL::SampleBuffers
|QGL::AlphaChannel
|QGL::DoubleBuffer
), parent
)
36 , m_zOffset( M_PI
/ 2 )
40 setFixedHeight( 200 );
42 m_coverPaths
<< "amarok/images/album_cover_1.jpg";
43 m_coverPaths
<< "amarok/images/album_cover_2.jpg";
44 m_coverPaths
<< "amarok/images/album_cover_3.jpg";
45 m_coverPaths
<< "amarok/images/album_cover_4.jpg";
46 m_coverPaths
<< "amarok/images/album_cover_5.jpg";
47 m_coverPaths
<< "amarok/images/album_cover_6.jpg";
48 m_coverPaths
<< "amarok/images/album_cover_7.jpg";
49 m_coverPaths
<< "amarok/images/album_cover_8.jpg";
50 m_coverPaths
<< "amarok/images/album_cover_9.jpg";
52 QTimer
* timer
= new QTimer( this );
53 connect( timer
, SIGNAL( timeout() ), this, SLOT( updateGL() ) );
54 timer
->start( 20 ); //50fps
58 CoverBling::initializeGL() //reimplemented
62 //generate all textures
63 foreach( QString path
, m_coverPaths
) {
64 QImage
image( KStandardDirs().findResource( "data", path
) );
65 image
= image
.scaled( TEXTURE_SIZE
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
66 m_textureIds
<< bindTexture( image
);
69 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
70 glShadeModel(GL_SMOOTH
);
71 qglClearColor( Qt::black
);
72 glEnable( GL_MULTISAMPLE
); //enable anti aliasing
73 glEnable( GL_DEPTH_TEST
);
76 //Display list for drawing a textured rectangle
77 m_texturedRectList
= glGenLists( 1 );
78 glNewList( m_texturedRectList
, GL_COMPILE
);
80 glTexCoord2f (0.0, 0.0);
81 glColor3f( 1.0, 1.0, 1.0 );
82 glVertex3f (-1.0, -1.0, -1.0);
83 glTexCoord2f (1.0, 0.0);
84 glColor3f( 0.1, 0.1, 0.1 );
85 glVertex3f (1.0, -1.0, -1.0);
86 glTexCoord2f (1.0, 1.0);
87 glColor3f( 0.1, 0.1, 0.1 );
88 glVertex3f (1.0, 1.0, -1.0);
89 glTexCoord2f (0.0, 1.0);
90 glColor3f( 1.0, 1.0, 1.0 );
91 glVertex3f (-1.0, 1.0, -1.0);
93 //glDisable( GL_DEPTH_TEST );
96 //Display list for drawing reflection of the textured rectangle
97 m_texturedRectReflectedList
= glGenLists( 1 );
98 glNewList( m_texturedRectReflectedList
, GL_COMPILE
);
99 glTranslatef( 0.0, -2.0, 0.0 );
100 glScalef( 1.0, -1.0, 1.0 );
102 glEnable( GL_BLEND
);
103 //glBlendFunc( GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR );
104 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
107 glTexCoord2f (0.0, 0.0);
108 glColor4f( 1.0, 1.0, 1.0, 0.3 );
109 glVertex3f (-1.0, -1.0, -1.0);
110 glColor4f( 1.0, 1.0, 1.0, 0.3 - 0.15 );
111 glTexCoord2f (1.0, 0.0);
112 glVertex3f (1.0, -1.0, -1.0);
113 glColor4f( 1.0, 1.0, 1.0, 0.02 - 0.15 );
114 glTexCoord2f (1.0, 1.0);
115 glVertex3f (1.0, 1.0, -1.0);
116 glColor4f( 1.0, 1.0, 1.0, 0.02 );
117 glTexCoord2f (0.0, 1.0);
118 glVertex3f (-1.0, 1.0, -1.0);
121 glDisable( GL_BLEND
);
126 CoverBling::resizeGL( int width
, int height
) //reimplemented
130 glViewport( 0, 0, (GLint
)width
, (GLint
)height
);
131 glMatrixMode(GL_PROJECTION
);
133 //glFrustum( -0.5f, 0.5f, -0.5f, 0.5f, 0.3f, 4.5f );
135 glMatrixMode(GL_MODELVIEW
);
139 CoverBling::setPerspective()
141 gluPerspective( 30, (double)width() / height(), 1.0, 20.0 );
145 CoverBling::paintGL() //reimplemented
147 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
149 const QPoint mousePos
= mapFromGlobal( QCursor::pos() );
150 draw( objectAtPosition( mousePos
) );
154 CoverBling::draw( GLuint selected
)
156 GLuint objectName
= 1;
158 glMatrixMode( GL_MODELVIEW
);
160 glRotatef( 10, 1.0, 0.0, 0.0 ); //Rotate whole scene around X axis; simulates camera tilt
161 glScalef( 1.0, 1.0, 6.0 );
164 //glBegin( GL_POLYGON );
165 // glColor3f( 0.0, 0.0, 0.5 );
166 // glVertex3f (-3.0, -1.0, -2.0);
167 // glColor3f( 1.0, 0.0, 0.5 );
168 // glVertex3f (3.0, -1.0, -2.0);
169 // glColor3f( 0.0, 1.0, 0.5 );
170 // glVertex3f (3.0, -1.0, 2.0);
171 // glColor3f( 0.0, 0.0, 1.5 );
172 // glVertex3f (-3.0, -1.0, 2.0);
175 glColor3f( 1.0, 1.0, 1.0 ); //reset color
176 glEnable( GL_TEXTURE_2D
);
178 float xoffset
= -5.5;
179 float yoffset
= -0.6;
180 float zoffset
= -1.1;
182 foreach( GLuint id
, m_textureIds
) {
183 glBindTexture( GL_TEXTURE_2D
, id
);
185 const float xsin
= sin( xoffset
);
186 const float zsin
= sin( zoffset
);
189 glTranslatef( xoffset
, yoffset
, zoffset
);
190 glRotatef( 8, 0.0, 1.0, 0.0 );
193 if( objectName
== selected
)
194 glColor3f( 1.0, 0.0, 0.0 );
195 glLoadName( objectName
++ );
196 glCallList( m_texturedRectList
);
197 glColor4f( 1.0, 1.0, 1.0, 1.0 );
199 //draw reflection on the ground
202 glCallList( m_texturedRectReflectedList
);
205 glColor4f( 1.0, 1.0, 1.0, 1.0 );
208 glDisable( GL_TEXTURE_2D
);
212 CoverBling::objectAtPosition( const QPoint
& pos
)
214 // this is the same as in every OpenGL picking example
215 const int MaxSize
= 512; // see below for an explanation on the buffer content
216 GLuint buffer
[MaxSize
];
219 glGetIntegerv(GL_VIEWPORT
, viewport
);
220 glSelectBuffer(MaxSize
, buffer
);
222 glRenderMode(GL_SELECT
);
227 glMatrixMode(GL_PROJECTION
);
230 gluPickMatrix((GLdouble
)pos
.x(), (GLdouble
)(viewport
[3] - pos
.y()), 5.0, 5.0, viewport
);
233 glMatrixMode(GL_PROJECTION
);
236 const int hits
= glRenderMode( GL_RENDER
);
240 //determine object with the lowest Z value
241 uint hitZValue
= UINT_MAX
;
243 for( int i
= 0; i
< hits
; i
++ ) {
244 if( buffer
[(i
*4)+1] < hitZValue
) {
245 hit
= buffer
[(i
*4)+3];
246 hitZValue
= buffer
[(i
*4)+1];
250 // return the name of the clicked surface
255 #include "CoverBling.moc"