开发者

OpenGL Orthographic Projection Clipping

Assuming I use Orhographic Projection, and have a reshape function like this:

void reshape(f32 width, f32 height){
    aspect = width/height;
    glViewport(0, 0, width, height);
    // guaranted 960x640 HUD canvas
    if(640*aspect>=960){
        ortho.x = 640*aspect;
        ortho.y = 640;
    }else{
        ortho.x = 960;
        ortho.y = 960/aspect;
    }
    glOrtho(0, ortho.x, ortho.y, 0, -1.0f, 1.0f);
}

How can I make sure, that all vertices >ortho.x or >ortho.y (normally offscreen) are didn't drawn? Because if I scale the windows to something with a bigger aspect ratio than 1.5f (960/640) I see the objects, that schouldn't be full visible (because the viewport is so big like the window). Is there something like a clipping pane in开发者_如何转开发 orthographic projection?


What you want is to use [glScissor][1] to ensure that the rendered area never goes beyond a certain size. glScissor takes a rectangle in window coordinates (remember: window coordinates have the origin at the bottom-left). The scissor test prevents the generation of fragments outside of this area.

To activate the scissor test, you must use glEnable(GL_SCISSOR). Unless you do that, the above call won't actually do anything.


Use constant values for the limit parameters of glOrtho, but use glViewport and glScissor (enable with glEnable(GL_SCISSOR_TEST)) to limit rendering to a sub-portion of your window.

BTW: You should set the projection and viewport in the rendering function. Doing it in the reshape handler makes not much sense. In any serious OpenGL application you'll switch projection modes several times during a full render, so just do it that way from the very beginning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜