Search code examples
c++openglviewportglut

Can't make glViewport() to work


I'm starting to work in a 2D game, but I don't know the way I'm supposed to use viewPort(). This is my Init() code:

void init(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(100, 100, 800, 600);
    gluOrtho2D(0, 800, 600, 0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.0, 0.0, 0.0, 0.0);
}

What I want to know is that, after drawing an image to the window, how can I look only at a part of it, and not the whole world?


Solution

  • Everything you did there belongs into the drawing code. Loading textures and shaders, setting up framebuffer objects would be initialization, but you're not doing that up there.

    Also glClear must come after glClearColor.

    Last but not least, the viewport does not affect clear operations (you have to use scissor testing for clear operations to be limited to a certain rectangle).