glGetIntegerv(GL_VIEWPORT, Pi)

Started by
5 comments, last by Thazager 12 years, 10 months ago
Two problems today making me irritated.

/***************************/

The first problem is that im going to make a small billiard-game. Yet i dont have information of what resolution theyll run my game in. It will probably be 1280-800 or the other one (whatever it is, cant fínd it in my papers). I use fullscreen with this guy:
glutFullScreen();

Then i try to get the resolution of the window using the following guy:
glGetIntegerv(GL_VIEWPORT, Pi);

Pi[2] and Pi[3] is suppose to be the width and the height. But they return 300, 300 pixels. If i set glutInitWindowSize(x, y); before running glutFullScreen; i get x, y from glGetIntegerv();

When i run glGetIntegerv anywhere else in the program after my initialization-commands (Edit: initalization = everything before glutMainLoop()) i get the correct size of the screen, but while setting up the screen i get everything but the correct size. Ive tried to run my "glutDisplayFunc"ion and my "glutReshapeFunc"ion. But it wont make any difference. So how do i get the size of my screen before i start the program?


/***************************/

My second problem is the geometry of my program that fails.

I put my camera at [0,0,8] and I set my upvector towards [0,1,0]. It is looking down on the billiard table which lies on the (z=0)-plane. I setup my viewport in the following manner:
gluPerspective(60, width/height, 1, 30);
this should meant that (if i understood this stuff correctly) i can see the points [-x, -y, 0] and [x, y, 0] on my billiard-table-plane where
x = 8 sin(30)*width/height = 4*width/height
y = 8 sin(30) = 4

This is true for all resolutions if i only multiply x and y with approximately 1.14

Who is 1.14 and where is this guy coming from? Or have i misunderstood everything? (Edit: I try to hit the balls with the mouse, therefore i need to know where on the billiard-table i have the mouse.)

[Edited by - slutbit on June 10, 2010 9:48:07 AM]
Advertisement
1: glGetIntegerv(GL_VIEWPORT, Pi) returns the parameter passed to glViewport, not the size of the window or the screen. The default viewport, however, is the size of the frame buffer at the time the rendering context is created. So if you create a 300x300 window, then glGetIntegerv(GL_VIEWPORT, Pi) will return the vector [0, 0, 300, 300], and that will not change until you call glViewport.

If you call glutFullScreen, the window is resized, but unless you respond to the reshape callback and set the viewport there, the viewport will not change. Consequently, the values returned by glGetIntegerv(GL_VIEWPORT, Pi) won't change either if you change the size of the window without responding to the reshape callback.

You cannot use OpenGL to get the size of the screen. You need to use the windowing framework, GLUT in your case. If you need it before the application starts (the OpenGL part of it, at least), you need to look somewhere else than at OpenGL.

2: width and height are probably integers, so watch out for integer divisions. 8 sin(30)*width/height will return the correct value, but 4*width/height won't. Other than that common problem, is the eye-point set to (0,0,0), or some other vector so that the viewpoint is pointing along the negative Z-axis?
Quote:Original post by Brother Bob
...If you call glutFullScreen, the window is resized, but unless you respond to the reshape callback and set the viewport there, the viewport will not change...


1. Thanks for answering Bob :)
How do i respond the reshape callback? The reshape function takes width and height as indata so i dont really understand what you are trying to say. Directly after having called glutMainLoop(); i can get the correct resolution, but not until then. That mean that i have to put a big part of my initalization in the idle()-function, which feels a bit stupid :P

2. Me being stupid, not understanding simple geometry and trigonometry. I multiplied by sin(30) instead of tan(30), which means i end up a factor 1/cos(30)=1.155 wrong.
You respond to it by registering a callback, and GLUT will tell you the size of the window by calling the function. When calling glutFullScreen, the size of the window, which is the screen resolution, is passed to the reshape function.
void reshape(int w, int h){    glViewport(0, 0, w, h);}int main(){    ...    glutReshapeFunc(reshape);    ...}

Quote:Original post by Brother Bob
You respond to it by registering a callback, and GLUT will tell you the size of the window by calling the function. When calling glutFullScreen, the size of the window, which is the screen resolution, is passed to the reshape function.
void reshape(int w, int h){    glViewport(0, 0, w, h);}int main(){    ...    glutReshapeFunc(reshape);    ...}


Sorry, think i dont understand you. How should i call a function whos inparameters is the width and the height?
By calling glutReshapeFunc i give information about which function that should be called when resizing the window. This is done, but that does not mean the function is called within any function before i start the glutMainLoop().

I can answer you NO, the screen resolution is not passed to the reshape function when calling glutFullScreen. This is done somewhere else after glutMainLoop() is already started. Make your own attempt to set a printf("hello\n"); inside the reshape function and you'll see.
Quote:Original post by slutbit
Sorry, think i dont understand you. How should i call a function whos inparameters is the width and the height?
By calling glutReshapeFunc i give information about which function that should be called when resizing the window. This is done, but that does not mean the function is called within any function before i start the glutMainLoop().

As I said, OpenGL will not help you find the screen resolution. You need to look into your windowing library for that, and it has nothing to do with OpenGL. The reshape function will tell you the size of the window when it has been resized, and if that is not what you want, you need to look into another solution.

Perhaps glutGet with GLUT_SCREEN_WIDTH/HEIGHT is what you want.

Quote:Original post by slutbit
I can answer you NO, the screen resolution is not passed to the reshape function when calling glutFullScreen. This is done somewhere else after glutMainLoop() is already started. Make your own attempt to set a printf("hello\n"); inside the reshape function and you'll see.

Tried it; the reshape function is called with the desktop resolution, as expected.
To get screen size at the time of needing them:

windowWidth = GetSystemMetrics(SM_CXSCREEN);
windowHeight = GetSystemMetrics(SM_CYSCREEN);

This topic is closed to new replies.

Advertisement