glViewport()

Hi Nexusone,
Thank you very much for your help. I got it according to your ways.
Best regards,
Hui Zhang

[This message has been edited by huizhang (edited 09-24-2002).]

How does it not work exactly?

It’s a little odd doing that in the Init function. Usually that should be done in the resize. You also are setting glViewport to a size of 500x300, but your window is initially 300x400.

First I think that you maybe are thinking in 2D for seting up your world.

The view port is the physical size of your window. Starting at the top left hand of the window to the lower right hand side.
Don’t get confused with your Windows desktop X/Y position, even though you tell it to put your window at 100, 100. Any graphics drawn in the openGL window start at 0,0 to 300,400, not 100+300, 100+400.

Viewport tell openGL the size space to render your 3D scene to.

// example reshape function which is called by windows, when then window has been re-sized.

glutReshapeFunc(reshape); // place in main function

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h); Tell openGL the new size of our window in pixels.
}

Now your ortho view is the area in which opengl is going to render, anything not within the bounds of the ortho setting will not be drawn. For ortho2D you set your top/bottom points, but it also sets you front(near) and back(far) to -1, 1.
Anything drawn with a Z axis outside of -1 to 1 will not be drawn.

If you are want to work in pixels then the ortho should look like this gluOrtho2D(0,0,300,400); // Which is the size of your window.

But ortho view could also look something like this: gluOrtho2D(-10,10,10,-10) and would still fill the screeen.

Originally posted by huizhang:
[b]Hi,I am a new student of OpenGL. Thank you for your help.
My program like the following. I don’t know the reason that the glViewport() doesn’t work.

#include <GL/glut.h>
//#include <GL/glu.h>
#include <GL/gl.h>

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(200.0,200.0);
glVertex2f(200.0,300.0);
glVertex2f(300.0,300.0);
glVertex2f(300.0,200.0);

glEnd();
glFlush();
}

void init()
{
glClearColor(1.0, 0.0, 1.0, 0.0);

glColor3f(0.0,1.0,1.0);

glViewport(0,0,500,300);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(100,400,200,600);

}

int main(int argc, char** argv)
{

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(300,400);
glutInitWindowPosition(100,100);
glutCreateWindow(“Hello, world!”);
glutDisplayFunc(display);

init();

glutMainLoop();
return 0;
}

[/b]