Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: how to calculate coordinates of rect by image dimension

  1. #1
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52

    how to calculate coordinates of rect by image dimension

    if i have an image 123x256 pixels
    if i draw a rect that have the same size as this image,how can i calculate the coordinates of vertices of this rect by modelview,projectio,viewport and image dimensions!

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,129
    Do you want the image to cover the whole viewport?

  3. #3
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    Assuming your window size is width and height, setup the viewport
    glViewport(0, 0, width, height);

    setup your ortho projection to match the viewport
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

    You can leave the modelview as is
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    and render your quad
    glColor4f(1.0, 1.0, 1.0, 1.0);
    glBegin(GL_QUADS);
    glVertex2f(0.0, 0.0);
    glVertex2f(123.0, 0.0);
    glVertex2f(123.0, 256.0);
    glVertex2f(0.0, 256.0);
    glEnd();
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  4. #4
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by thokra View Post
    Do you want the image to cover the whole viewport?
    No,i want the image cover my rect,and it's not distorted

  5. #5
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by V-man View Post
    Assuming your window size is width and height, setup the viewport
    glViewport(0, 0, width, height);

    setup your ortho projection to match the viewport
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

    You can leave the modelview as is
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    and render your quad
    glColor4f(1.0, 1.0, 1.0, 1.0);
    glBegin(GL_QUADS);
    glVertex2f(0.0, 0.0);
    glVertex2f(123.0, 0.0);
    glVertex2f(123.0, 256.0);
    glVertex2f(0.0, 256.0);
    glEnd();
    Can this method achieve?

  6. #6
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Is there anybody can help me out?

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Do you know how to render a texture (image) to a quad?

  8. #8
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by tonyo_au View Post
    Do you know how to render a texture (image) to a quad?
    Yes,i know this,but how to make the image not magnify and shrink

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,129
    If you transform the quad the image is mapped to using a perspective projection or an ortho projection with certain parameters there is no way you can keep it the same size on the screen. The image will theoretically still cover the whole quad but the quad itself will cover less pixel after projection.

    What exactly is your goal here? Do you want the image to always cover the an area equal to the image dimensions? Do you want somethng like a HUD?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •