PDA

View Full Version : getting the window size



davico
03-22-2009, 03:01 AM
Hallo,

I would like to get the size (width, height) of the window. I think I should use glutGet(GLUT_WINDOW_X), but this function it always return the same (wrong) values even when I resize the window. This is the code I am using:




#include <GL/glut.h>
#include <stdio.h>


// dimensions

float _flagW;
float _flagH;

int winW;
int winH;

void init (void)
{
// set default values
_flagW = 100.0;
_flagH = _flagW / 2;
}

void getWinDimensions()
{
winW = glutGet(GLUT_WINDOW_X);
winH = glutGet(GLUT_WINDOW_Y);
}

void draw()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

getWinDimensions();

printf("%d\n",winW);


glOrtho(0.0, 200.0, 0.0, 200.0, -1.0, 1.0);

glEnable(GL_POLYGON_SMOOTH);



glColor3f(1.0,0.0,0.0);

// cross: vertical
glBegin(GL_POLYGON);
glVertex2i(0 , 0);
glVertex2i(_flagW , 0);
glVertex2i(_flagW, _flagH);
glVertex2i(0, _flagH);
glEnd();

glFlush();
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
init();
glutInitWindowSize(500,500);
glutCreateWindow("GB flag");
glutDisplayFunc(draw);
glutMainLoop();
}

enunes
03-22-2009, 03:11 AM
Hi,

in Glut, when you specify a reshape function, it takes two parameters that are internally passed (width anr height). This reshape function gets called again whenever the window gets resized and with the width and height updated.

You can take those and store in your global variables, something like:


void reshape (int w, int h) {
winW = w;
winH = h;

//other stuff to call when window gets resized, such as:
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60.0, ratio, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

and this to your main body:


glutReshapeFunc (reshape);

davico
03-22-2009, 03:30 AM
Thanks enunes. It works.