Nokia N97 SDK
Example Applications Guide

Slideshowrenderers.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Slideshowrenderers.cpp
00004  *  Part of     : OpenGLEx / Slideshow
00005  *
00006  *  Copyright (c) 2005-2006 Nokia Corporation.
00007  *  This material, including documentation and any related
00008  *  computer programs, is protected by copyright controlled by
00009  *  Nokia Corporation.
00010  * ==============================================================================
00011  */
00012 
00013 // INCLUDE FILES
00014 
00015 #include "Slideshowrenderers.h"
00016 #include <e32math.h>
00017 
00018 // ----------------------------------------------------------------------------
00019 // Static background renderer
00020 // ----------------------------------------------------------------------------
00021 
00022 void CStaticRenderer::Render(const TTexture& texture, TBool filter)
00023     {
00024     GLfixed tw = 1 << 16;
00025     GLfixed th = 1 << 16;
00026     GLfixed texCoords[] =
00027         {
00028         0,  th,
00029         tw, th,
00030         0,  0,
00031         tw, 0,
00032         };
00033     GLfixed vertices[] =
00034         {
00035         0,       0,
00036         1 << 16, 0,
00037         0,       1 << 16,
00038         1 << 16, 1 << 16,
00039         };
00040 
00041     // Set orthogonal projection
00042     glMatrixMode(GL_PROJECTION);
00043     glPushMatrix();
00044     glLoadIdentity();
00045     glOrthof(0, 1, 0, 1, -1, 1);
00046     glMatrixMode(GL_MODELVIEW);
00047     glLoadIdentity();
00048 
00049     // Set texture parameters
00050     glBindTexture(GL_TEXTURE_2D, texture.iID);
00051     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter ? GL_LINEAR : GL_NEAREST);
00052     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter ? GL_LINEAR : GL_NEAREST);
00053 
00054     // Draw the textured quad
00055     glEnableClientState(GL_VERTEX_ARRAY);
00056     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00057     glTexCoordPointer( 2, GL_FIXED, 0, texCoords );
00058     glVertexPointer( 2, GL_FIXED, 0, vertices );
00059     glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
00060 
00061     glMatrixMode( GL_PROJECTION );
00062     glPopMatrix();
00063     glMatrixMode( GL_MODELVIEW);
00064     }
00065 
00066 // ----------------------------------------------------------------------------
00067 // Mask transition renderer
00068 // ----------------------------------------------------------------------------
00069 
00070 CMaskTransition::CMaskTransition(const TTexture& mask, TSweepStyle sweepStyle):
00071     iSweepStyle(sweepStyle), iMask(mask)
00072     {
00073     }
00074 
00075 void CMaskTransition::Render(const TTexture& texture, float ratio)
00076     {
00077     GLfixed tw = 1 << 16;
00078     GLfixed th = 1 << 16;
00079     GLfixed texCoords[] =
00080         {
00081         0,  th,
00082         tw, th,
00083         0,  0,
00084         tw, 0,
00085         };
00086     GLfixed vertices[] =
00087         {
00088         0,       0,
00089         1 << 16, 0,
00090         0,       1 << 16,
00091         1 << 16, 1 << 16,
00092         };
00093 
00094     // Set orthogonal projection
00095     glMatrixMode(GL_PROJECTION);
00096     glPushMatrix();
00097     glLoadIdentity();
00098     glOrthof(0, 1, 0, 1, -1, 1);
00099     glMatrixMode(GL_MODELVIEW);
00100     glLoadIdentity();
00101 
00102     // Set up blending
00103     glEnable(GL_BLEND);
00104     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00105 
00106     // Assign the mask texture to the first texture unit
00107     glActiveTexture(GL_TEXTURE0);
00108     glEnable(GL_TEXTURE_2D);
00109     glBindTexture(GL_TEXTURE_2D, iMask.iID);
00110     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00111     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00112     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00113     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00114 
00115     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00116     glTexCoordPointer( 2, GL_FIXED, 0, texCoords );
00117 
00118     // Transform the texture matrix according to the selected
00119     // sweep style. The transformation is animated based on the
00120     // ratio parameter.
00121     glMatrixMode(GL_TEXTURE);
00122     glLoadIdentity();
00123 
00124     switch (iSweepStyle)
00125         {
00126         case ESweepCurved:
00127             ratio = 1 - ratio;
00128             glScalef(.5, .5, .5);
00129             glTranslatef(0 * sin(2 * ratio * PI) + .5, 1 * cos(ratio * PI) + .5, 0);
00130             glRotatef(22.5 * sin(ratio * PI), 0, 0, 1);
00131             break;
00132         case ESweepCurved2:
00133             ratio = 1 - ratio;
00134             ratio += 0.5;
00135             glScalef(.5, .5, .5);
00136             glTranslatef(0 * cos(2 * ratio * PI) + .5, 1 * sin(ratio * PI) + .5, 0);
00137             glRotatef(22.5 * cos(ratio * PI), 0, 0, 1);
00138             break;
00139         case ESweepHorizontal:
00140             glRotatef(90, 0, 0, 1);
00141             glTranslatef(2 * ratio - 1, -1, 0);
00142             break;
00143         case ESweepVertical:
00144             glTranslatef(0, 2 * ratio - 1, 0);
00145             break;
00146         }
00147 
00148     glMatrixMode(GL_MODELVIEW);
00149 
00150     // Assign the picture to the second texture unit
00151     glActiveTexture(GL_TEXTURE1);
00152     glEnable(GL_TEXTURE_2D);
00153     glBindTexture(GL_TEXTURE_2D, texture.iID);
00154     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00155     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00156 
00157     // Set full transparency as the texture environment color
00158     GLfloat c[] = {1, 1, 1, 0};
00159     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, c);
00160     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
00161 
00162     /*
00163      * Set up the texture environment combiner. Interpolation
00164      * combining is used here, so that the output is a blend
00165      * of the picture and full transparency based on the mask
00166      * texture. The blending equation is:
00167      *
00168      * output = image * mask + transparent * (1 - mask)
00169      */
00170     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
00171     glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);         // picture
00172     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
00173     glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);        // transparent white
00174     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
00175     glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_PREVIOUS);        // mask
00176     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
00177 
00178     // Set the same combiner for the alpha channel
00179     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
00180     glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);         // picture
00181     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
00182     glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_CONSTANT);        // transparent white
00183     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
00184     glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_ALPHA, GL_PREVIOUS);        // mask
00185     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
00186 
00187     /*
00188      * Set up vertex pointers for the second texture unit. Note
00189      * That glClientActiveTexture() is used instead of
00190      * glActiveTexture(), because vertex and texture coordinate
00191      * pointers are client side features.
00192      */
00193     glClientActiveTexture(GL_TEXTURE1);
00194     glEnableClientState(GL_VERTEX_ARRAY);
00195     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00196     glTexCoordPointer( 2, GL_FIXED, 0, texCoords );
00197     glVertexPointer( 2, GL_FIXED, 0, vertices );
00198 
00199     // Draw the textured quad
00200     glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
00201     glColor4f(1, 1, 1, 1);
00202 
00203     // Reset the state
00204     glDisable(GL_BLEND);
00205     glDisable(GL_TEXTURE_2D);
00206     glMatrixMode( GL_PROJECTION );
00207     glPopMatrix();
00208     glActiveTexture(GL_TEXTURE0);
00209     glClientActiveTexture(GL_TEXTURE0);
00210     glMatrixMode(GL_TEXTURE);
00211     glLoadIdentity();
00212     glMatrixMode( GL_MODELVIEW);
00213     }
00214 
00215 // ----------------------------------------------------------------------------
00216 // Whisk transition renderer
00217 // ----------------------------------------------------------------------------
00218 
00219 CWhiskTransition::CWhiskTransition(TDirection dir):
00220     iDir(dir)
00221     {
00222     }
00223 
00224 void CWhiskTransition::Render(const TTexture& texture, float ratio)
00225     {
00226     glBindTexture(GL_TEXTURE_2D, texture.iID);
00227     GLfixed tw = 1 << 16;
00228     GLfixed th = 1 << 16;
00229     GLfixed texCoords[] =
00230         {
00231         0,  th,
00232         tw, th,
00233         0,  0,
00234         tw, 0,
00235         };
00236     GLfixed vertices[] =
00237         {
00238         0,       0,
00239         1 << 16, 0,
00240         0,       1 << 16,
00241         1 << 16, 1 << 16,
00242         };
00243 
00244     // Set orthogonal projection
00245     glMatrixMode(GL_PROJECTION);
00246     glPushMatrix();
00247     glLoadIdentity();
00248     glOrthof(0, 1, 0, 1, -1, 1);
00249     glMatrixMode(GL_MODELVIEW);
00250     glLoadIdentity();
00251 
00252     // Move the picture
00253     glTranslatef(((iDir == ELeft) ? -1 : 1) * ratio * ratio, 0, 0);
00254 
00255     // Set texture parameters
00256     glBindTexture(GL_TEXTURE_2D, texture.iID);
00257     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00258     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00259 
00260     // Set up blending
00261     glEnable(GL_BLEND);
00262     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00263     glColor4f(1, 1, 1, 1 - ratio);
00264 
00265     // Draw the textured quad
00266     glEnableClientState(GL_VERTEX_ARRAY);
00267     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00268     glTexCoordPointer( 2, GL_FIXED, 0, texCoords );
00269     glVertexPointer( 2, GL_FIXED, 0, vertices );
00270     glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
00271 
00272     // Reset the state
00273     glDisable(GL_BLEND);
00274     glColor4f(1, 1, 1, 1);
00275     glMatrixMode( GL_PROJECTION );
00276     glPopMatrix();
00277     glMatrixMode( GL_MODELVIEW);
00278     }
00279 
00280 // ----------------------------------------------------------------------------
00281 // Curtain transition renderer
00282 // ----------------------------------------------------------------------------
00283 
00284 CCurtainTransition::CCurtainTransition(TDirection dir):
00285     iDir(dir)
00286     {
00287     }
00288 
00289 void CCurtainTransition::Render(const TTexture& texture, float ratio)
00290     {
00291     glBindTexture(GL_TEXTURE_2D, texture.iID);
00292     const TInt resolution = 8;
00293     GLfixed vertices[resolution * resolution * 2];
00294     GLfixed texCoords[resolution * resolution * 2];
00295     GLshort indices[resolution * (resolution - 1) * 2 + (resolution - 2) * 4];
00296 
00297     // Build triangle strip indices
00298     TInt x, y;
00299     GLshort *i = indices;
00300 
00301     for (y = 0; y < resolution - 1; y++)
00302         {
00303         for (x = 0; x < resolution; x++)
00304             {
00305             *i++ = y * resolution + x;
00306             *i++ = (y + 1) * resolution + x;
00307             }
00308 
00309         // Insert degenerate triangles to join triangle strips
00310         if (y < resolution - 1)
00311             {
00312             *i = *(i - 1); i++;
00313             *i = *(i + 1) = (y + 1) * resolution; i += 2;
00314             *i++ = (y + 2) * resolution;
00315             }
00316         }
00317 
00318     // Calculate vertex positions
00319     for (y = 0; y < resolution; y++)
00320         {
00321         for (x = 0; x < resolution; x++)
00322             {
00323             // Calculate a grid position for this vertex and displace it using a sum of sines, that converges
00324             // to zero as the animation advances.
00325             GLfloat u = x / (GLfloat)(resolution - 1);
00326             GLfloat v = y / (GLfloat)(resolution - 1);
00327             GLint d = (iDir == ELeft) ? -1 : 1;
00328             vertices[(y * resolution + x) * 2 + 0] = (-1 << 16) + x * (2 << 16) / (resolution - 1) +
00329                 (GLint)((1 << 16) * (sin(u * 5 + ratio * 4) + sin(v * 7 + ratio * 3 + texture.iID) + 8 * d * (1 - v * v)) * ratio * ratio / 4);
00330             vertices[(y * resolution + x) * 2 + 1] = (-1 << 16) + y * (2 << 16) / (resolution - 1) +
00331                 (GLint)((1 << 16) * (sin(u * 7 + ratio * 3) + sin(v * 5 + ratio * 4 + texture.iID)) * ratio * ratio / 4);
00332             texCoords[(y * resolution + x) * 2 + 0] = x * (1 << 16) / (resolution - 1);
00333             texCoords[(y * resolution + x) * 2 + 1] = (resolution - y - 1) * (1 << 16) / (resolution - 1);
00334             }
00335         }
00336 
00337     // Set orthogonal projection
00338     glMatrixMode(GL_PROJECTION);
00339     glPushMatrix();
00340     glLoadIdentity();
00341     glFrustumf(-1, 1, -1, 1, 1, 2);
00342     glMatrixMode(GL_MODELVIEW);
00343     glLoadIdentity();
00344 
00345     // Move the picture
00346     glTranslatef(0, 0, -1);
00347 
00348     // Set texture parameters
00349     glBindTexture(GL_TEXTURE_2D, texture.iID);
00350     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00351     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00352 
00353     // Set up blending
00354     glEnable(GL_BLEND);
00355     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00356     glColor4f(1, 1, 1, 1 - ratio);
00357 
00358     // Draw the textured grid
00359     glEnableClientState(GL_VERTEX_ARRAY);
00360     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00361     glTexCoordPointer( 2, GL_FIXED, 0, texCoords );
00362     glVertexPointer( 2, GL_FIXED, 0, vertices );
00363     glDrawElements( GL_TRIANGLE_STRIP, sizeof(indices) / sizeof(indices[0]), GL_UNSIGNED_SHORT, indices );
00364 
00365     // Reset the state
00366     glDisable(GL_BLEND);
00367     glColor4f(1, 1, 1, 1);
00368     glMatrixMode( GL_PROJECTION );
00369     glPopMatrix();
00370     glMatrixMode( GL_MODELVIEW);
00371     }

© Nokia 2009

Back to top