William Payne
05-20-2003, 06:23 PM
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).]
#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).]