Problems centering image

Hello, I am trying to display an image centered in the window, but it ends up not being centered. Instead it’s at the top and too much to the left. The code will follow. I am testing with 512*512 .ppm-file.

#include <GL/glut.h>
#include <stdlib.h>
#include “readppm.h”

static char* image = NULL;
static int image_width = 0; /* width obtained from readppm() /
static int image_height = 0; /
height obtained from readppm() */
static int window_height = 600;
static int window_width = 800;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRasterPos2i((window_width - image_width)/2,(window_height-image_height)/2);
glDrawPixels(image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, image);
glutSwapBuffers();
}

void reshape(int height, int width)
{
window_height = height;
window_width = width;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, height, width);
}

void keyboard(unsigned char key, int x, int y)
{
/* 27 == ESC-key. */
if(key == 27 | | key == ‘q’ | | key == ‘Q’)
{
exit(EXIT_SUCCESS);
}
}

int main(int argc, char **argv)
{
image = readppm(“stars.ppm”, &image_height, &image_width);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(0,0);
glutCreateWindow(“image”);
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glClearColor(1.0, 1.0, 0.0, 1.0);
glutMainLoop();

return EXIT_SUCCESS;
}

Any ideas what’s wrong with my code?

Thanks in advance

// William Payne

[This message has been edited by William Payne (edited 05-20-2003).]

hello
there are some problem in your code:
in the display func, you should do:
if (window_width > image_width) x = ((window_width - image_width)/2;
else x = 0;
if (window_height > image_height) y = ((window_height - image_height)/2;
else y = 0;
glRasterPos2i(x, y);

in the reshape func you call:
glViewport(0, 0, height, width);
you should call
glViewport(0, 0, width, height);

hello yoyo and thanks for your reply. I tried your suggestions but it’s still not centered. Here’s the current code:

#include <GL/glut.h>
#include <stdlib.h>
#include “readppm.h”

static char* image = NULL;
static int image_width = 0; /* width obtained from readppm() /
static int image_height = 0; /
height obtained from readppm() */
static int window_height = 600;
static int window_width = 800;

void display()
{
int x = 0;
int y = 0;

if(window_width > image_width)
{
x = (window_width - image_width) / 2;
}
else
{
x = 0;
}
if (window_height > image_height)
{
y = (window_height - image_height) / 2;
}
else
{
y = 0;
}

glRasterPos2i(x, y);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRasterPos2i((window_width - image_width)/2,(window_height-image_height)/2);
glDrawPixels(image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, image);
glutSwapBuffers();
}

void reshape(int height, int width)
{
window_height = height;
window_width = width;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
}

void keyboard(unsigned char key, int x, int y)
{
/* 27 == ESC-key. */
if(key == 27 | | key == ‘q’ | | key == ‘Q’)
{
exit(EXIT_SUCCESS);
}
}

int main(int argc, char** argv)
{
image = readppm(“stars.ppm”, &image_height, &image_width);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(0,0);
glutCreateWindow(“image”);
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glClearColor(1.0, 1.0, 0.0, 1.0);
glutMainLoop();

return EXIT_SUCCESS;
}

Ideas?

You didn’t remove your previous glRasterPos.
Otherwise I don’t see the problem.

Step through the debugger and look at the values, simple as that.
As long as the image is smaller than your window, it should work. If it’s bigger it won’t.

glRasterPos outside the window will not draw anything and to center an image bigger than your window client area you need to use glRasterPos2i(0, 0) and adjust the glPixelStore values of GL_PACK_SKIP_PIXELS and GL_PACK_SKIP_ROWS instead.

Note that you have not requested a depth buffer with GLUT_DEPTH, the glEnable(GL_DEPTH_TEST) and the glClear(GL_DEPTH_BUFFER_BIT) make no sense.

[This message has been edited by Relic (edited 05-22-2003).]

Silly me, not removing the old call to glRasterPosi(). Anyway, that didn’t solve it because the two calls are the same since in my tests window width/height is always greater than bitmap width/height. Anyways, I found the problem. I had the arguments for reshape() switched…when I switched the names for the arguments in the parameter list for reshape(), it worked!

Thanks for your help