Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Help with a visual effect

  1. #1
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Help with a visual effect

    Hi, I want to know what's the simple way to obain this visual effect: a rectangle (for example) that rotates 180 degrees from the Y axis:
    Qt Code:
    1. Coordinate system
    2. |Y
    3. |
    4. |
    5. |
    6. |_________________ X
    7. /
    8. /
    9. /
    10. / Z
    To copy to clipboard, switch view to plain text mode 

    All I want is to emulate the effect of a page (of a book) turning around
    I think that I could use openGL but I don't hve much experinece with it, maybe it can be easily achieved with QPainter
    Thanks in advance.
    Last edited by SkripT; 4th May 2006 at 17:39.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    If you have a point (x, y, z) and you rotate it around Y axis by a degrees, you'll get (x', y', z') where:
    x' = x cos a + z sin a,
    y' = y,
    z' = -x sin a + z cos a.

    Second thing you will need is a projection. In this case it probably will be enough if you use:
    xp = x - z
    yp = y + z
    (where xp, and yp are the painter coordinates).

    The algorithm:
    • translate the painter so that the (0, 0) point is in the middle of the top edge of the book,
    • x1 = width/2, y1 = 0, z1 = 0, (upper corner of the page)
    • x2 = width/2, y2 = height, z2 = 0, (lower corner of the page)
    • for a = 0, 1, ..., 180:
      • calculate new x1, y1, z1, x2, y2 and z2
      • draw a (0, 0)--(x1-z1, y1-z1)--(x2-z2, y2-z2)--(0, height) rectangle

    Note that in fact x1 == x2, z1 == z2 and both y1 and y2 are constant.

  3. The following user says thank you to jacek for this useful post:

    SkripT (4th May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Thanks a lot jacek, good knowledge of maths

    EDIT: taking a look at this alogrithm carefully, I think that it will paint the rectangle with the correct size in each degree, the problem is that I want to paint an image (texture) on this rectangle and I want that it "deforms" with each degree. Is it still possible with QPainter?
    Last edited by SkripT; 4th May 2006 at 18:52.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    Quote Originally Posted by SkripT
    I want that it "deforms" with each degree. Is it still possible with QPainter?
    Try QPainter::setMatrix() or QImage::transform().

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with a visual effect

    Wouldn't it be enough to transform the painter matrix to do it all?

    Hmm... Looks like QMatrix is just 2x2 in size...

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    Quote Originally Posted by wysota
    Looks like QMatrix is just 2x2 in size...
    No, it's 3x3, but the last column is [0;0;1]. Anyway, it will be enough, since you only have to shear and scale the painter (or image).

  8. #7
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Thanks a lot jacek and wysota for your replies. My question is: so, using QMatrix is enought to do all the rotations/shear/scale that I need applied on the image or the rectangle to simulate the effect that I need? QMatrix seems to work only in 2D and this effect has a 3D component I think....

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    Quote Originally Posted by SkripT
    QMatrix is enought to do all the rotations/shear/scale that I need applied on the image or the rectangle to simulate the effect that I need?
    Theoretically, yes.
    Quote Originally Posted by SkripT
    QMatrix seems to work only in 2D and this effect has a 3D component I think....
    Do you have a 3D monitor?

  10. #9
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Quote Originally Posted by jacek
    Theoretically, yes.

    Do you have a 3D monitor?
    Not, but now I know what to request for christmas .

    I will try with QMatrix it and I comment the results

  11. #10
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Hi again, I've been reading carefully and testing what the docs explains from QMatrix class and I don't know how could I use it to obtain the effect that I want . I know that I should call QMatrix::shear and QMatrix::scale in some way because, as the docs says, "...Rotation is achieved by carefully setting both the shearing factors and the scaling factors" but I don't know how
    It's clear that I'm not a math expert... Could you please guide me a little bit in how could I configure the QMatrix to get the respective "transformated" rectangle for each angle rotated? Thanks again.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

  13. The following 2 users say thank you to wysota for this useful post:

    SkripT (5th May 2006), superteny (27th May 2009)

  14. #12
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Thanks a lot wysota. Now I know that to rotate from the Y-axis, I have to use the following matrix:
    Qt Code:
    1. Matrix:
    2. cosa 0 -sina 0
    3. 0 1 0 0
    4. sina 0 cosa 0
    5. 0 0 0 1
    To copy to clipboard, switch view to plain text mode 
    Now the problem is that QMatrix works with 3x3 matrixs How can I transfrom this matrix in terms of QMatrix?
    Last edited by SkripT; 5th May 2006 at 09:31.

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with a visual effect

    Jacek has always been much better at maths (especially matrices) than me, so he'll probably correct me but I think using ( cosa, -sina, sina, cosa) could do the trick. Try it, maybe it works, but you should wait for Jacek's reply anyway.

  16. #14
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Ok thanks anyway wysota. I think that, as I comented, QMatrix uses only 3x3 matrix because they work in 2D...

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with a visual effect

    Yes, but you should be able to achieve the same result with a smaller matrix. The equation will just be more complicated (or use more than one QMatrix).

  18. #16
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Hi again, I attach the code that I use, trying as wysota comments. The problem is that the rotation is done in 2D, so if I try to rotate an image 90º from the Y axis, the result is not a vertical line or even a null image
    Qt Code:
    1. #include <QtGui>
    2. #include <math.h>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QImage imatge("../test.jpg");
    10.  
    11. double radians = 90 * 3.14159265 / 180; //rotation of 90º
    12.  
    13. imatge = imatge.transformed(QMatrix(cos(radians),-sin(radians),sin(radians),cos(radians),0,0));
    14. imatge.save("../transformated.jpg", "jpg");
    15.  
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I think that's not as easy as that code
    Last edited by SkripT; 5th May 2006 at 11:27.

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    Let's see... you need a matrix that for given angle a will transform the (0, 0)--(width/2, 0)--(width/2, height)--(0, heigth) rectangle into (0, 0)--(x1-z1, y1+z1)--(x2-z2, y2+z2)--(0, height).

    QMatrix works like this:
    x' = m11*x + m21*y + dx
    y' = m22*y + m12*x + dy

    Since we need to map (0,0) to (0,0), dx = dy = 0.

    (0, height) must be mapped to (0, height), so:
    0 = m11*0 + m21*height + 0
    height = m22*height + m12*0 + 0,
    thus:
    m21 = 0 and m22 = 1.

    (width/2, 0) must be mapped to (x1-z1, y1+z1), where x1, y1, z1 are coordinates of a (width/2,0,0) point rotated around Y axis by a degrees, so:
    x1 = width/2 cos a
    y1 = 0
    z1 = -width/2 sin a

    x1 - z1 = width/2 ( sin a + cos a )
    y1 + z1 = -width/2 sin a

    width/2 (sin a + cos a ) = m11 * width/2
    -width/2 sin a = 0 + m12* width/2

    which gives us: m11 = sin a + cos a and m12 = - sin a

    Whole matrix:
    Qt Code:
    1. / sin a + cos a - sin a 0 \
    2. | 0 1 0 |
    3. \ 0 0 1 /
    To copy to clipboard, switch view to plain text mode 

  20. The following user says thank you to jacek for this useful post:

    SkripT (5th May 2006)

  21. #18
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Wow jacek, you're a machine in maths Now it give better results, but the rotation in 90º is not a "slim" image . Here's the code:
    Qt Code:
    1. #include <QtGui>
    2. #include <math.h>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QImage imatge("../proba.jpg");
    10.  
    11. double radians = 90 * 3.14159265 / 180;
    12.  
    13. imatge = imatge.transformed(QMatrix(sin(radians)+cos(radians),-sin(radians),0,1,0,0));
    14. imatge.save("../transformada.jpg", "jpg");
    15.  
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

  22. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a visual effect

    With this you'll get a better result:
    Qt Code:
    1. / 0.5 sin a + cos a - 0.5 sin a 0 \
    2. | 0 1 0 |
    3. \ 0 0 1 /
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  23. The following user says thank you to jacek for this useful post:

    SkripT (5th May 2006)

  24. #20
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with a visual effect

    Really good example jacek, thanks a lot

Similar Threads

  1. Help for 3D like Visual Effect.
    By AmolShinde_8 in forum Qt Programming
    Replies: 0
    Last Post: 6th October 2008, 15:25
  2. Qt 4.4.1 Compile Error with MS Visual C++ 2008 Express SP1
    By BrainB0ne in forum Installation and Deployment
    Replies: 3
    Last Post: 19th August 2008, 15:49
  3. Qt configure with msvc.net
    By jivanr in forum Installation and Deployment
    Replies: 1
    Last Post: 11th June 2007, 08:17
  4. Compile App using OpenGL and Visual Studios 2003
    By Rayven in forum General Programming
    Replies: 3
    Last Post: 26th April 2007, 15:43
  5. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.