# How to setup glOrtho and Viewport [duplicate]

When setting up a window I set 6 variables in my render class

int windowWidth;    // the width of the window
int windowHeight;   //the height of the window
int dimL;           // the coordinate of the left side of the display
int dimR;           // the coordinate of the right side of the display
int dimB;           // the coordinate of the top side of the display
int dimT;           //the coordinate of the bottom side of the display


In my initiation routine, i set the displayMode like this:

Display.setDisplayMode(new DisplayMode(windowWidth, windowHeight));

I set glOrtho like this:

GL11.glOrtho(dimL, dimR, dimB, dimT, 1, -1);


now my question is:

Using these variables how can i configure glViewport to have the area defined by the dimX variables fill the display?

thanks!

It might help if you knew that the viewport defines the region (in terms of window-space coordinates) where your scene is projected to. If you had no projection matrix at all, then the coordinates (-1,-1) would map to the lower-left corner of your viewport and the coordinates (1,1) would map to the top-right. We call that coordinate space "Normalized Device Coordinates" and they make illustrating how the viewport mapping works a lot easier.

The glViewport (...) call is effectively to establish the window-space coordinates of the bottom-left and top-right corners. Using the above-mentioned information, you should come up with this:

glViewport (dimL, dimB,  dimR - dimL, dimT - dimB)

// First two parameters are the window-space origin (bottom-left)
// The remaining two are the width and height of the viewport


### Here is an illustration of the relation between projection and viewport discussed above:

For more details see the following article.

Keep in mind that the viewport only defines the region that geometry projects to. When you clear the color, depth and stencil buffers it will clear your entire window's framebuffer. If you only want to clear the region defined by dimX, then you will need to use a scissor rectangle in addition.

• I don't think this is right: glViewport (dimL, dimB, dimR - dimL, dimT - dimB). Those dimL, dimB, dimR, and dimT values are the values being passed to glOrtho() (according to the code provided by the OP); they don't necessarily have anything to do with the window dimensions. Dec 22 '14 at 1:46
• @TrevorPowell: They can be used for both. If you want to match your projection to your viewport so that you project 1:1 with pixel coordinates (e.g. not stretched) you will usually match the dimensions of the orthographic projection with the viewport. The way I see it, there's no other way to interpret "coordinate of the left side of the display" in conjunction with filling just the "area defined by the dimX variables". Dec 22 '14 at 2:15
• Your glViewport call won't work if dimL doesn't equal zero, or if dimR doesn't equal windowWidth. Similarly, dimB must be zero, and dimT must equal windowHeight. I don't think the question implied that any of those conditions were necessarily the case. Dec 22 '14 at 2:23
• @TrevorPowell: How do you figure? There is no requirement that glViewport have an origin at 0,0 or that the bounds of the viewport fill the entire screen. The only requirements glViewport introduces is that the width and height are >= 0. Dec 22 '14 at 2:26
• Using these variables how can i configure glViewport to have the area defined by the dimX variables fill the display? The origin needs to be at 0,0 and the viewport needs to fill the entire window. That was the question being asked, as I read it. Dec 22 '14 at 2:28

glViewport() specifies the part of the window to which OpenGL will draw (in pixels).

glOrtho() specifies the projection matrix to use inside the viewport.

To make the coordinates specified by glOrtho() fill the whole window, you would call glViewport(0,0,windowWidth,windowHeight);.

Or alternately, not call glViewport() at all, since OpenGL's default viewport already fills the whole window.

• The OP doesn't want to fill the entire window, just the area defined by the dimX variables - dimL, dimR, dimB and dimT define the region to fill and they are not related to the size of the window (though is presumably smaller in area than the window). Dec 22 '14 at 2:38
• @AndonM.Coleman The precise question was how to "fill the display". I think it's pretty clear that they weren't asking how to fill only a small portion of a window. Dec 22 '14 at 14:07
• If you read the comments you would come to a different conclusion. There are 4 variables defining the dimensions of "display" and the dimensions of "display" are independent from the two variables defining the characteristics of the window (windowWidth and windowHeight) - "display" and " window" are two different things. I don't know how you can possibly interpret the question the way you are, because the viewport already works the way you want unless you touch it manually. Dec 22 '14 at 15:14
• @AndonM.Coleman These arguments are unhelpful. I think your interpretation of the question is problematic. Particularly since the question was how to make the area defined by those variables fill the display. That question makes no sense if those two things are, in fact, the same thing, as you claim. Unless the original asker comes back to explain their intent, we're going to have to agree to disagree over the likely intent of this confusingly phrased question. Dec 23 '14 at 5:01
• The area defined by those variables is the display. It's kind of like the client region in most window systems. You have the window and then a smaller rectangle in window coordinates that you want to render into. But I agree, if you were having trouble interpreting the question you should have left a comment for the OP and not down-voted my answer. Dec 23 '14 at 11:35