Temporarily disable showing the mediabrowser as jefferais commit caused Amarok to...
[amarok.git] / src / context / CoverBling.cpp
blob02eef9cdafe0d92752e79f4df2828b7c5b9b3e7e
1 /***************************************************************************
2 * Copyright (C) 2007 by Mark Kretschmann <kretschmann@kde.org> *
3 * *
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. *
8 * *
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. *
13 * *
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"
22 #include "debug.h"
23 #include "CoverBling.h"
25 #include <math.h>
27 #include <QtOpenGL>
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 )
35 , m_xOffset( 0.0 )
36 , m_zOffset( M_PI / 2 )
38 DEBUG_BLOCK
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
57 void
58 CoverBling::initializeGL() //reimplemented
60 DEBUG_BLOCK
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 );
74 glDepthMask( true );
76 //Display list for drawing a textured rectangle
77 m_texturedRectList = glGenLists( 1 );
78 glNewList( m_texturedRectList, GL_COMPILE );
79 glBegin (GL_QUADS);
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);
92 glEnd ();
93 //glDisable( GL_DEPTH_TEST );
94 glEndList();
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 );
106 glBegin (GL_QUADS);
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);
119 glEnd ();
121 glDisable( GL_BLEND );
122 glEndList();
125 void
126 CoverBling::resizeGL( int width, int height ) //reimplemented
128 DEBUG_BLOCK
130 glViewport( 0, 0, (GLint)width, (GLint)height );
131 glMatrixMode(GL_PROJECTION);
132 glLoadIdentity();
133 //glFrustum( -0.5f, 0.5f, -0.5f, 0.5f, 0.3f, 4.5f );
134 setPerspective();
135 glMatrixMode(GL_MODELVIEW);
138 void
139 CoverBling::setPerspective()
141 gluPerspective( 30, (double)width() / height(), 1.0, 20.0 );
144 void
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 ) );
153 void
154 CoverBling::draw( GLuint selected )
156 GLuint objectName = 1;
158 glMatrixMode( GL_MODELVIEW );
159 glLoadIdentity();
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 );
163 //draw the ground
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);
173 //glEnd();
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 );
184 glPushMatrix();
185 const float xsin = sin( xoffset );
186 const float zsin = sin( zoffset );
187 xoffset += 1.0;
188 zoffset += 0.1;
189 glTranslatef( xoffset, yoffset, zoffset );
190 glRotatef( 8, 0.0, 1.0, 0.0 );
192 //draw the cover
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
200 glLoadName( 0 );
201 glPushMatrix();
202 glCallList( m_texturedRectReflectedList );
203 glPopMatrix();
204 glPopMatrix();
205 glColor4f( 1.0, 1.0, 1.0, 1.0 );
208 glDisable( GL_TEXTURE_2D);
211 GLuint
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];
217 GLint viewport[4];
219 glGetIntegerv(GL_VIEWPORT, viewport);
220 glSelectBuffer(MaxSize, buffer);
221 // enter select mode
222 glRenderMode(GL_SELECT);
224 glInitNames();
225 glPushName(0);
227 glMatrixMode(GL_PROJECTION);
228 glPushMatrix();
229 glLoadIdentity();
230 gluPickMatrix((GLdouble)pos.x(), (GLdouble)(viewport[3] - pos.y()), 5.0, 5.0, viewport);
231 setPerspective();
232 draw();
233 glMatrixMode(GL_PROJECTION);
234 glPopMatrix();
236 const int hits = glRenderMode( GL_RENDER );
237 if ( !hits )
238 return 0;
240 //determine object with the lowest Z value
241 uint hitZValue = UINT_MAX;
242 uint hit = 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
251 return hit;
255 #include "CoverBling.moc"