Up to now I have used glDrawPixels but since my image is a large geotiff one, I'm getting some horrible results for example:
this is the code used for zoom extent:

void COpenGLControl::ZoomFullExtent()
    GLint iViewport[4];
    glGetIntegerv(GL_VIEWPORT, iViewport);
    glPixelZoom((GLfloat) iViewport[2] / (GLfloat)width,
                (GLfloat) iViewport[3] / (GLfloat)height);

Since the image is a square one, it's not a good result.

This is the code used for zoom in:

void COpenGLControl::FixedZoomIn()
    glPixelZoom(2.0f, 2.0f);

but it does zoom in operation just one time and when I again click on the button the zoom in is not done I think it is because that when I call COpenGLControl::FixedZoomIn for an instance of the class COpenGLControl infact the openGL window is zoommed not the contents in it.

I have heard about texture mapping and how to zoom by changing z of the camera point.

How would I customize the class COpenGLControl for my purpose?

  • \$\begingroup\$ Your image link was missing from your post. \$\endgroup\$
    – House
    Aug 10 '13 at 16:17
  • \$\begingroup\$ it is because I couldn't submit the image, I'm a new member to this site and encountered the error you need at least 10 reputations to submit an image \$\endgroup\$ Aug 10 '13 at 16:23
  • \$\begingroup\$ The OpenGL Pixel functions are very outdated, just draw a rectangle with the texture and size the rectangle depending on the the Zoom. \$\endgroup\$
    – API-Beast
    Aug 10 '13 at 16:37

The reason glPixelZoom doesn't work as you expect is that calls to it aren't cumulative; the second call overwrites the first. See the manual at https://www.opengl.org/sdk/docs/man2/xhtml/glPixelZoom.xml

Instead of using glDrawPixels, you should draw the image as a texture mapped square.

Set up the texture object once:

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
    image_w, image_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);

Draw it on the screen each frame, by first setting up an orthogonal projection to map (0, 0) to the top left corner of the screen, and (screen_w, screen_h) to the bottom right corner.

glViewport(0, 0, screen_w, screen_h);

glOrtho(0, screen_w, screen_h, 0, -1, 1);


And then draw a textured square (using a triangle strip in this example):


glBindTexture(GL_TEXTURE_2D, tex);

x0 = 0; // top left corner of image
y0 = 0;
x1 = x0 + image_w * zoom; // bottom right corner of image
y1 = y0 + image_h * zoom;

    glTexCoord2f(0, 1); glVertex2f(x0, y1);
    glTexCoord2f(0, 0); glVertex2f(x0, y0);
    glTexCoord2f(1, 1); glVertex2f(x1, y1);
    glTexCoord2f(1, 0); glVertex2f(x1, y0);
  • \$\begingroup\$ well thanks @ccxvii but a question rose up for me. why should I use use GL_TRIANGLE_STRIP as the parameter for glBegin and for example why not use GL_QUADS as the parameter? \$\endgroup\$ Aug 12 '13 at 12:53
  • \$\begingroup\$ Hmmm, I don't think the right way to zoom into an image is to make the image grow. If for example you draw any overlay on it, you will also have to move all these points. I believe the right way is to modify the viewport (reduce it to magnify) \$\endgroup\$
    – Antonio
    Dec 1 '17 at 19:36
  • \$\begingroup\$ @Antonio: Zooming the image and zooming the view are not the same thing. You may very well want to scale the image but not other elements on the screen, in which case you should draw a bigger rectangle rather than zooming in the camera. \$\endgroup\$
    – ccxvii
    Dec 12 '17 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.