glViewport() unclear..

Hi all, I’m kind of new to OpenGL so please bear with me if you will. And excuse my stupidity if it anything I say seem to be such. I’m working on a program where I want to draw a grid in a glut window. However, I want to limit the viewing portion of the grid. I am using this to initialize my world window and viewport:

glMatrixMode ( GL_PROJECTION );
gluOrtho2D ( 0, 50, 0, 50 );
glViewport ( 10, 10, 500, 500 );

a note: my glut window is 600x600

now the problem is no matter how I change the glViewport function my viewing area still takes up the whole glut window(which basically fills a 50x50 square grid on the entire window.) but what I want is for this grid to be drawn within what my viewport had defined. so any clue where I’m doing it wrong? You help is much appreciated…

glMatrixMode ( GL_PROJECTION );
gluOrtho2D ( 0, 50, 0, 50 );
glViewport ( 10, 10, 500, 500 );

ok what you are doing here is saying

start viewport bottom left at 10,10 and size along the x axis is 500(width), also size along the y axis is 500(length)

now see the problem… you set ortho projection to use coordinates 0,0(bottomleft) -> 50,50(topright)

ok so when you now call your viewport you are trying to set a viewport of dimensions basically (500500) in an area of size only (5050)

ortho parameters go like this
left,right,top,bottom (clipping planes)

while viewport uses these parameters
start_x, start_y, length_x, length_y

to create a viewport that is 10 units in size in both directions

viewport(100,100,10,10); //start viewport at 100,100… size 10 lengtnx and lengthy

Well that’s exactly what I want to do. I want to create a world window that is just 50x50. And have the viewport to take care whatever that is needed to expand this 50x50 window into a 500x500 window. The problem is my actual GLUT window is 600x600 and I want a drawing area of 500x500 within this 600x600 window. But it just keep drawing under the area (or using the viewport) that is set by defult, namely the actual 600x600 window… any idea why?

In the code you have posted I don’t see any obvious problem. It should work as you describe. Therefore the error must exist elsewhere in your code. Are you certain you aren’t resetting the viewport at some other point in time? Are you also certain that the viewport is successfully set in the resize function? Check for gl error codes to see if glViewport failed.

glViewport in resize function? But i don’t resize my window though. Hm… I’ve been looking at this for quite awhile and I still don’t see the error codes… but here’s my program.

#include <iostream.h>
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>

void setWindow ( float left, float right, float bottom, float top );
void myInit ();
void myDisplay();

//******** Global Variables ********
int Size;

void main ( int argc, char** argv )
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( 600, 600 );
glutInitWindowPosition ( 100, 150);
glutCreateWindow ( “Grid” );
myInit ();
glutDisplayFunc ( myDisplay );
glutMainLoop ();

void setWindow ( float left, float right, float bottom, float top )
glMatrixMode ( GL_PROJECTION );
gluOrtho2D ( left, right, bottom, top );

void myInit ( )
glClearColor ( 1.0, 1.0, 1.0, 0.0 );
glColor3f ( 0.0f, 0.0f, 0.0f );
glPointSize ( 2.0 );
cout << "Enter the size (10-50): “;
cin >> Size;
if ( Size < 10 | | Size > 50 )
cout << "
Size rested to 10!” << endl;
Size = 10;
setWindow ( 0, Size, 0, Size );
glViewport ( 100, 100, 10, 10 );

void myDisplay()
int i = 0;
glBegin ( GL_LINES );
for (i; i < Size; i++)
glVertex2f ( i, 0 );
glVertex2f ( i, Size );
i = 0;
for (i; i < Size; i++)
glVertex2f ( 0, i );
glVertex2f ( Size, i );
glEnd ();
glFlush ();


or you can download it here…

Ok, so now its obvious why it isn’t working. The viewport is being reset because you haven’t specified a resize function. At least define a dummy resize function so glut doesn’t call its default resize (reshape) function in which it resets the viewport. You should define more than a dummy resize function, if you properly want to handle resizing, otherwise disable window resizing.

ok I think I got it. Thanks!