Results 1 to 10 of 19

Thread: 2D perspective correct texturing ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    @Sesilla
    While here on PGD we usually don't treat weak or broad questions as plague like they are commonly treated on Stack Overflow it is still good if you can provide more information in your question about what kind of an answer do you expect as this helps us understand of what do you need.

    Also sometimes it might be better to provide description of that final results do you expect instead of asking about specific approach. Why? This way you might get answer with multiple possible approaches so you can then chose the one that suits you best.

  2. #2
    @SilverWarior
    Thanks for your suggestions!

    Thi is my quad:




    This is a texture:



    And this the result with possibilities to "deform" the scale or tile of the texture:



    Thanks in advance
    Sesilla
    Attached Images Attached Images

  3. #3
    Do you wan't the texture to be projected on your quad so that it looks like rotated plane (texture rotates and scales with the quad) or just use your quad as mask to show only part of the texture as it is done in your image example?

  4. #4
    The first one! The texture projected like pseudo 3d!

    Sesilla

  5. #5
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    If you're in OpenGL then you're providing texture coordinates for the quad. Perspective correction is automatic. You provide coordinates between 0.0 and 1.0 for each of your vertices. To produce an image like you've posted you have to scale the texture coordinates in relation to your vertex coordinates, or in much older versions of GL you can use automatic texture coordinate generation which in 2D will produce the same result.

    The solution for 'perspective' in 2D as you desire is to simply leave your texture coordinates at each corner of the texture while altering the vertices of the quad.

    Input the vertex coordinates that would give you a quad like in the image you have provided and in counter-clockwise order (OpenGL defaults to counter-clockwise winding to determine front facing polygons) set your texture coordinates as (x:0,y:0) (x:0,y:1) (x:1,y:1) (x:1,x:0)

    As long as you keep your texture coordinates the same, you can draw your (convex) quad at any combination of vertices and openGL will 'skew' the texture automatically.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  6. #6
    Hi phibermon,

    with this code:
    Code:
    glBegin(GL_QUADS);
            
    glTexCoord2f(0, 0);
    
    glVertex2f  (500, 200);
    
    glTexCoord2f(0, 1);
    glVertex2f  (600,  200);
    
    glTexCoord2f(1, 1);
    glVertex2f  (1000,  700);
    
    glTexCoord2f(1, 0);
    glVertex2f  (50, 700);

    this is the rendering:



    Browsing the web i found the Opengl glTexCoord4f function, but it is not clear about use it.

    Thanks
    Sesilla
    Attached Images Attached Images

  7. #7
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Your vertices are in clockwise order and your texture coordinates are in counter-clockwise order, the GPU actually draws two triangles and not a quad and because you have opposite winding, the two triangles are getting different relative texture coordinates. Always draw two triangles instead of one quad so this fact is always present in your mind and such issues become more obvious. It's better for all kinds of technical reasons that you don't need to learn if you don't want to. try the following instead :

    Code:
    glBegin(GL_TRIANGLES);
            
    glTexCoord2f(0, 0);
    glVertex2f(500, 200);
    
    glTexCoord2f(0, 1);
    glVertex2f(50, 700);
    
    glTexCoord2f(1, 1);
    glVertex2f  (1000,  700);
    
    
    glTexCoord2f(1, 1);
    glVertex2f  (1000,  700);
    
    glTexCoord2f(1, 0);
    glVertex2f  (600,  200);
    
    
    glTexCoord2f(0, 0);
    glVertex2f(500, 200);
    
    glEnd();
    Counter-clockwise always for coordinates. OpenGL and other API's like Direct3D use what is called the 'winding order' to determine if a one sided polygon is facing you (the 'camera') ( one sided polygon = default OpenGL state - simply meaning that it will be invisible if viewed from the other side. This implies that you can render two-sided polygons that are visible from both sides and this is true. This would seem preferable as you don't have to care about winding but in reality it slows down rendering as discarding polygons that can't be seen is one of the main principals behind rendering things more quickly. In 2D this isn't really an issue as you'll never be 'looking' from the other side of the polygon but it's best practice and should you move to 3D you'll already be doing things correctly)
    Last edited by phibermon; 06-03-2015 at 08:32 PM.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

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
  •