AppSrc/CDrawMagGlass.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CDrawMagGlass.h"
       
    19 #include "TextureLoader.h"
       
    20 #include "DrawUtility.h"
       
    21 #include "ImagicConsts.h"
       
    22 
       
    23 
       
    24 GLfixed CDrawMagGlass::iMagGlassVertices[VERTICES_PER_LINE*VERTICES_PER_LINE*3];
       
    25 GLfixed CDrawMagGlass::iMagGlassTex[VERTICES_PER_LINE*VERTICES_PER_LINE*2];
       
    26     
       
    27 const TInt CDrawMagGlass::iMagGlassTriCount=(VERTICES_PER_LINE-1)*(VERTICES_PER_LINE-1)*2;
       
    28 GLushort CDrawMagGlass::iMagGlassIndices[CDrawMagGlass::iMagGlassTriCount*3];
       
    29 
       
    30 
       
    31 
       
    32 // Texture coordinate data
       
    33 const GLfixed CDrawMagGlass::iGlobalTexCoords[] =
       
    34    {
       
    35    //bitmap has to be flipped over
       
    36    0,       1<<16,
       
    37    1<<16,   1<<16,
       
    38    0,       0,
       
    39    1<<16,   0
       
    40    };
       
    41 
       
    42 CDrawMagGlass::CDrawMagGlass()
       
    43     {
       
    44     // No implementation required
       
    45     }
       
    46 
       
    47 CDrawMagGlass::~CDrawMagGlass()
       
    48     {
       
    49     }
       
    50 
       
    51 CDrawMagGlass* CDrawMagGlass::NewLC(CImagicContainerBrowser* aContainer, CDrawOneByOne* aDrawOneByOne)
       
    52     {
       
    53     CDrawMagGlass* self = new (ELeave) CDrawMagGlass();
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL(aContainer,aDrawOneByOne);
       
    56     return self;
       
    57     }
       
    58 
       
    59 CDrawMagGlass* CDrawMagGlass::NewL(CImagicContainerBrowser* aContainer, CDrawOneByOne* aDrawOneByOne)
       
    60     {
       
    61     CDrawMagGlass* self = CDrawMagGlass::NewLC(aContainer,aDrawOneByOne);
       
    62     CleanupStack::Pop(); // self;
       
    63     return self;
       
    64     }
       
    65 
       
    66 void CDrawMagGlass::ConstructL(CImagicContainerBrowser* aContainer, CDrawOneByOne* aDrawOneByOne)
       
    67     {
       
    68     iContainer = aContainer;
       
    69     iDrawOneByOne = aDrawOneByOne;
       
    70     }
       
    71 
       
    72 TReal CDrawMagGlass::GetMagGlassZoomFactor()
       
    73     {
       
    74     DP1_IMAGIC(_L("CDrawMagGlass::GetMagGlassZoomFactor - magGlass: %f"), iMagGlassZoomFactor);
       
    75     return iMagGlassZoomFactor;
       
    76     }
       
    77 
       
    78 /*----------------------------------------------------------------------*/
       
    79 // Interpolates given value into target value with step
       
    80 //
       
    81 void CDrawMagGlass::Interpolate(float &aValue, const float aTarget, const float aStep)
       
    82     {
       
    83     //DP0_IMAGIC(_L("CImagicContainerBrowser::Interpolate"));
       
    84     // Calculate new value
       
    85     float diff = aTarget-aValue;
       
    86     aValue += diff * aStep * 0.2/*iTimeDiff*/ * 30; 
       
    87     //float timediff = Min(0.1f, iTimeDiff); // so max value of timediff is 100tick (100ms)
       
    88     //aValue += diff * aStep * timediff * 30; 
       
    89     
       
    90     // Check that value is in range
       
    91     if (aValue > aTarget && diff > 0)
       
    92         aValue = aTarget;
       
    93     if (aValue < aTarget && diff < 0)
       
    94         aValue = aTarget;
       
    95     }
       
    96 
       
    97 void CDrawMagGlass::InitDrawMagGlass()
       
    98     {
       
    99     iMagGlassZoomFactor = 1;
       
   100     
       
   101     TInt pos=0;
       
   102     for (TInt y=0; y<VERTICES_PER_LINE-1; y++)
       
   103         {
       
   104         TInt startvert=y*VERTICES_PER_LINE;
       
   105         for (TInt x=0; x<VERTICES_PER_LINE-1; x++, pos+=6)
       
   106             {
       
   107             // First triangle
       
   108             iMagGlassIndices[pos+0]=startvert+x;
       
   109             iMagGlassIndices[pos+1]=startvert+x+1;
       
   110             iMagGlassIndices[pos+2]=startvert+x+VERTICES_PER_LINE;
       
   111             
       
   112             // Second triangle
       
   113             iMagGlassIndices[pos+3]=startvert+x+VERTICES_PER_LINE;
       
   114             iMagGlassIndices[pos+4]=startvert+x+1;
       
   115             iMagGlassIndices[pos+5]=startvert+x+VERTICES_PER_LINE+1;
       
   116             }
       
   117         }
       
   118     }
       
   119     
       
   120 void CDrawMagGlass::DrawMagGlass(const TSize &aScreenPhysicalSize, TReal aImageAspectRatio)
       
   121     {
       
   122     TInt pos=0;
       
   123     
       
   124 #ifdef __WINS__ 
       
   125     // Fake touhcpoint for emulator
       
   126     iContainer->iLastTouchPoint.iX=320/2;
       
   127     iContainer->iLastTouchPoint.iY=240/2;
       
   128 #endif
       
   129         
       
   130     // Calculate finger position on screen
       
   131     // First calculate position on 0-1 range
       
   132     GLfixed xFinger;
       
   133     GLfixed yFinger;
       
   134     if(iContainer->GetScreenOrientation())
       
   135         {
       
   136         const TInt fingerOffset = 100;
       
   137         xFinger=iContainer->iLastTouchPoint.iX*(1<<16) / aScreenPhysicalSize.iWidth;
       
   138         yFinger=(iContainer->iLastTouchPoint.iY-fingerOffset)*(1<<16) / aScreenPhysicalSize.iHeight;
       
   139         }
       
   140     else
       
   141         {
       
   142         const TInt fingerOffset = 100;
       
   143         xFinger=(iContainer->iLastTouchPoint.iX-fingerOffset)*(1<<16) / aScreenPhysicalSize.iWidth;
       
   144         yFinger=(iContainer->iLastTouchPoint.iY)*(1<<16) / aScreenPhysicalSize.iHeight;
       
   145         }
       
   146     
       
   147     // Move center to match OpenGL center
       
   148     xFinger=-(0.5*(1<<16)-xFinger);
       
   149     yFinger=0.5*(1<<16)-yFinger;
       
   150     // Then scale it to opengl window size
       
   151     /*xFinger=xFinger*iContainer->iDrawOnebyOneW*2;
       
   152     yFinger=yFinger*iContainer->iDrawOnebyOneH*2;*/
       
   153     xFinger=xFinger*iDrawOneByOne->GetDrawOneByOneWidth()*2;
       
   154     yFinger=yFinger*iDrawOneByOne->GetDrawOneByOneHeight()*2;
       
   155         
       
   156     
       
   157     CImageData* data=iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   158     
       
   159     if((data->GetOrientation() == 0 || data->GetOrientation() == 180) && !iContainer->GetScreenOrientation())
       
   160         {
       
   161         GLfixed tmpX=xFinger;
       
   162         GLfixed tmpY=yFinger;
       
   163         xFinger = tmpY;
       
   164         yFinger = tmpX * -1;
       
   165         }
       
   166     else if(data->GetOrientation() == 0)
       
   167         ;
       
   168     else if((data->GetOrientation() == 90 || data->GetOrientation() == 270) && iContainer->GetScreenOrientation())
       
   169         {
       
   170         GLfixed tmpX=xFinger;
       
   171         GLfixed tmpY=yFinger;
       
   172         xFinger = tmpY;
       
   173         yFinger = tmpX * -1;
       
   174         }
       
   175     else if((data->GetOrientation() == 90 || data->GetOrientation() == 270) && !iContainer->GetScreenOrientation())
       
   176         {
       
   177         xFinger = xFinger * -1;
       
   178         yFinger = yFinger * -1;
       
   179         }
       
   180     
       
   181     
       
   182     //Interpolate(iMagGlassZoomFactor, 1.8, 0.05);
       
   183     //const float step=0.3; 
       
   184     //const float step=0.2;
       
   185     /*
       
   186     if (iMagGlassZoomFactor < CDrawOneByOne::KMaxMagGlassZoomFactor-step)
       
   187         //iDrawOneByOne->IncMagGlassZoomFactor(step);
       
   188         iMagGlassZoomFactor += step;
       
   189     else
       
   190         //iDrawOneByOne->SetMagGlassZoomFactor(CDrawOneByOne::KMaxMagGlassZoomFactor);
       
   191         iMagGlassZoomFactor = CDrawOneByOne::KMaxMagGlassZoomFactor;
       
   192     */
       
   193     Interpolate(iMagGlassZoomFactor, CDrawOneByOne::KMaxMagGlassZoomFactor, 0.1);
       
   194     DP1_IMAGIC(_L("CDrawMagGlass::DrawMagGlass - magGlass: %f"), iMagGlassZoomFactor);
       
   195     
       
   196     /*CImageData* data=iIEngine->GetImageData(iCurrentIndex, iImagicAppUi->GetUIDrawMode());
       
   197     if(data->GetOrientation() == 90 || data->GetOrientation() == 270)
       
   198         aImageAspectRatio = 1/aImageAspectRatio;*/
       
   199     
       
   200     // Create vertices
       
   201     pos=0;
       
   202     TInt texPos=0;
       
   203     for (TInt y=0; y<VERTICES_PER_LINE; y++)
       
   204         for (TInt x=0; x<VERTICES_PER_LINE; x++,pos+=3,texPos+=2)
       
   205             {
       
   206             // Calculate new vertex coordinates
       
   207             // First, calculate center
       
   208             GLfixed xCoord= x;
       
   209             GLfixed yCoord=-y;
       
   210             GLfixed zCoord= 0;
       
   211             // Then convert to fixed point
       
   212             xCoord*=(1<<16)/(VERTICES_PER_LINE-1);
       
   213             yCoord*=(1<<16)/(VERTICES_PER_LINE-1);
       
   214             // shift 0.5 to locate image center at (0,0) 
       
   215             xCoord -= (1<<15);
       
   216             yCoord += (1<<15);
       
   217             
       
   218             // Set aspect ratio
       
   219             if (aImageAspectRatio > 1)
       
   220                 yCoord *= (1/aImageAspectRatio);
       
   221             else
       
   222                 xCoord *= (1*aImageAspectRatio);
       
   223             
       
   224             
       
   225             // Zoom ball sizes
       
   226             const float ballSize=0.35;      // Zoom ball size
       
   227             const float zoomSize=0.2;     // Sharp zoom size, has to be at least 0.1 smaller bigger than ball size
       
   228             // Calculate squared values
       
   229             const GLfixed ballSizeSquared=(ballSize*(1<<8))*(ballSize*(1<<8));
       
   230             const GLfixed zoomSizeSquared=(zoomSize*(1<<8))*(zoomSize*(1<<8));
       
   231             
       
   232             // Calculate distance to finger with both axis
       
   233             GLfixed xDist=xFinger-xCoord;
       
   234             GLfixed yDist=yFinger-yCoord;
       
   235             // Calculate squared distance with phytagoras
       
   236             GLfixed dist=(xDist/(1<<8))*(xDist/(1<<8)) + (yDist/(1<<8))*(yDist/(1<<8));
       
   237             
       
   238             // Are we close enough
       
   239             if (dist < ballSizeSquared)
       
   240                 {
       
   241                 // Zoom factor
       
   242                 //const float iMagGlassZoomFactor=1.8;
       
   243                 
       
   244                 // Determine proper zoom for this vertex
       
   245                 //float zoom=iContainer->iMagGlassZoomFactor;
       
   246                 float zoom=iMagGlassZoomFactor;
       
   247                 
       
   248                 if (dist > zoomSizeSquared)
       
   249                     {
       
   250                     // We are between sharp zoom and ball edge
       
   251                     // Calculat escale factor for zoom
       
   252                     float scale=((float)(dist-zoomSizeSquared))/(ballSizeSquared-zoomSizeSquared);
       
   253                     zoom=(zoom-1)*(1.0-scale)+1;
       
   254                     }
       
   255                 
       
   256                 // Calculate new axis distances with zoom
       
   257                 xDist*=zoom;
       
   258                 yDist*=zoom;
       
   259                 // Calculate new coordinates based on distances
       
   260                 xCoord=xFinger-xDist;
       
   261                 yCoord=yFinger-yDist;
       
   262                 zCoord=128;     // Set coordinate a bit above of original picture
       
   263                 }
       
   264             
       
   265             // Store vertex data
       
   266             iMagGlassVertices[pos+0]=xCoord;
       
   267             iMagGlassVertices[pos+1]=yCoord;
       
   268             iMagGlassVertices[pos+2]=zCoord;
       
   269             
       
   270             // Set texture coodrinates
       
   271             iMagGlassTex[texPos+0]=x*(1<<16)/(VERTICES_PER_LINE-1);
       
   272             iMagGlassTex[texPos+1]=y*(1<<16)/(VERTICES_PER_LINE-1);
       
   273             }
       
   274     
       
   275     glEnable(GL_DEPTH_TEST);
       
   276     glEnable(GL_CULL_FACE);
       
   277     glCullFace(GL_FRONT);
       
   278     
       
   279     // Set vertex and texture coordinates and draw
       
   280     glVertexPointer(3, GL_FIXED, 0, iMagGlassVertices);
       
   281     glTexCoordPointer(2, GL_FIXED, 0, iMagGlassTex);
       
   282     glDrawElements(GL_TRIANGLES,iMagGlassTriCount*3,GL_UNSIGNED_SHORT,iMagGlassIndices);
       
   283     //glDrawElements(GL_LINES,triCount*3,GL_UNSIGNED_SHORT,indices);
       
   284     
       
   285     glDisable(GL_CULL_FACE);
       
   286     glDisable(GL_DEPTH_TEST);
       
   287     }
       
   288