PDA

View Full Version : Confused with Viewport

ToddAtWSU
10-17-2005, 06:52 PM
I am confused with something that is occuring in my viewport. Here is my code and I will explain my confusion after the code:

glViewport( (GLsizei) ( width * 5 / 7 ), (GLsizei) ( 0 ), (GLsizei) ( width * 2 / 7 ), (GLsizei) ( height ) );
glOrtho( (GLfloat) ( width * 5 / 7 ), (GLfloat) width, 0.0, (GLfloat) height, -2.0, 2.0 );
glColor3f( 0.0, 0.0, 0.0 );
glBegin( GL_LINES );
glVertex2f( ( width * 4 / 7 ), -height / 2 );
glVertex2f( ( width * 4 / 7 ), 3 * height / 2 );
glVertex2f( ( width * 5 / 7 ), ( 0 ) );
glVertex2f( ( width * 5 / 7 ), ( height ) );
glVertex2f( ( width * 6 / 7 ), ( 0 ) );
glVertex2f( ( width * 6 / 7 ), ( height ) );
glEnd( );

I am trying to draw 3 lines in the left 2/7 of the screen, which is its own viewport as can be seen. The first line I want to run down the entire length of the screen on the left edge of the viewport. I would have thought my x-coord would be (width * 5 /7 ) but I have to use 4/7 to get it to work. Then I want the line to run the entire width so I thought it would go from 0 to height, but instead it runs from -height/2 to 3*height/2. And the last 2 lines I want to go down the middle half of the viewport so I would have thought from 3*height/4 to height/4 but no, its from 0 to height. height is equal glutGet( GLUT_WINDOW_HEIGHT ); and width is equal to glutGet( GLUT_WINDOW_WIDTH );. For some reason height prints out as 0 and width as 1082081280 even though I set the window initially to a width of 700 and height of 500. So this may be a problem more than my code thinking. Any advice on why this doesn't work the way I think it should would be apprectiated! Thanks!

memfr0b
10-18-2005, 12:28 AM
I suppose this is caused by your width and height being bogus. Make sure you call glutGet when the window width and height are valid, after glutInit and glutCreateWindow.

The rest of your assumptions sound right. The left-most coordinate is width*5/7, the right-most is width. The y axis goes from 0 (bottom) to height (top), so a vertical line through the bottom half of the viewport would have the vertices (x, 0) and (x, height/2).

ToddAtWSU
10-18-2005, 02:41 AM
That is what confuses me. Here is the order I make my function calls in main:

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(700, 500);
glutInitWindowPosition ( ( 0 ), ( 0 ) );
glutCreateWindow("CEG 476 - Project 3");
init();

And the first 2 lines of init is where I call glutGet( ). I even set width to be 700 in my global definition and height to be 500....but to no avail it still gets the bogus value. So I don't know where these are coming from. I just print out the height and width values when I click anywhere on the screen with my mouse right now.

memfr0b
10-18-2005, 06:55 AM
It might be that glutGet only works after you enter glutMainLoop. Or you might have shadowed your width and height variables in your display function.

ToddAtWSU
10-18-2005, 06:58 AM
What do you mean by shadowed? The only places where I change my global variables for height and width are in the init function and resize function so if the screen size changes I can shrink or enlarge the objects properly. I have even specifically said in init that width = 700 and height = 500 and when I print out the values they print as 0 and 1+ billion. So I know something is getting messed up, but where I do not know. Thanks for your continued help. I used this same technique in my last project and it worked well. Thanks!

jide
10-18-2005, 07:27 AM
Ensure the variables you use inside glutGet are of good types. Otherwise, that might explains why you have the wrong values.
A second approach is to use your own variables for the size just because you stipple glutInitWindowSize. So something like that might help:

int width = 700;
int height= 500;

//...

glutInitWindowSize (width,height);

// then simply use width and height in your program.Also remember that the maths you do are done with integers the most of time so you might have truncated results.

Hope that helps.

ToddAtWSU
10-18-2005, 07:51 AM
I might have made the width and height as floats. Would this cause a problem. I cannot see my code right off hand, but will be able to get back to it in an hour or two. But do width and height have to be ints instead of possibly being floats? As far as the truncating, I think that is why I made them floats, but it really isn't that big of a deal because it just has to reside in a general area and using fractions seems like the easiest way to be able to properly redraw the objects when they are resized. Thanks!

ToddAtWSU
10-18-2005, 09:30 AM
By changing the values for height and width from float to ints fixed my problem. That is crazy what will happen when they are the wrong type! Thanks!

ToddAtWSU
10-18-2005, 10:40 AM
Well, I thought I had my problem fixed, but the only problem I fixed was the invalid height and width values. Can anyone look at this snippet of code to see if they can find something wrong? I want to draw a line along the left edge of the viewport to make a distinct division of the 2 viewports. Here is the code that I would think would allow this to work. My right viewport is the right 2/7 of the window and the left viewport is the left 5/7 of the window.

glViewport( (GLsizei) ( width * 5 / 7 ), (GLsizei) ( 0 ), (GLsizei) ( width * 2 / 7 ), (GLsizei) ( height ) );
glOrtho( ( width * 5 / 7 ), ( width ), ( 0 ), ( height ), ( -1.0 ), ( 1.0 ) );
glColor3f( 0.0, 0.0, 0.0 );
glBegin( GL_LINES );
glVertex2f( (GLfloat) ( width * 5 / 7 ), (GLfloat) ( height ) );
glVertex2f( (GLfloat) ( width * 5 / 7 ), (GLfloat) ( 0 ) );
glEnd( );
But this is what I actually have to use to get the line to appear properly:

glViewport( (GLsizei) ( width * 5 / 7 ), (GLsizei) ( 0 ), (GLsizei) ( width * 2 / 7 ), (GLsizei) ( height ) );
glOrtho( ( width * 5 / 7 ), ( width ), ( 0 ), ( height ), ( -1.0 ), ( 1.0 ) );
glColor3f( 0.0, 0.0, 0.0 );
glBegin( GL_LINES );
glVertex2f( ( width * 4 / 7 ), -height / 2 );
glVertex2f( ( width * 4 / 7 ), 3 * height / 2 );
glEnd( );
Thanks for your help because I am confused why this is. Like I said before, my width is set to 700 and the height to 500 and I have it set up to print the x and y coordinates whenever I click on the screen and when I click on the line supposedly drawn at width * 4 / 7 the x value says 500. I am not worried about having truncation errors with my code, although with the defualt values, this shouldn't be a problem. Thanks again!

memfr0b
10-18-2005, 11:52 AM
The first snippet works perfectly for me. Do you, by chance, have the other matrix not set to identity?

ToddAtWSU
10-18-2005, 02:03 PM
I was forgetting to load my ortho correctly for the first part, which threw everything else out of whack. Now my code appears to be working well. Thank you all for your help...without you I would not have gotten this stuff fixed. Thank you all so much!!!

-Of course I will be back on here if I get stuck again...can you tell I'm very new with OpenGL yet! :)