Drawing a cube in a glOrtho() view?
-
07-06-2021 - |
题
I'm developing an 4 split viewer in OpenGL. One view is a perspective view, the other ones are for orthogonal projections (front, left, top).
I wrote a method that draws a cube from (-1,-1,-1) to (1,1,1) and it works well in the perspective view, but i can't get it to work in my views that use glOrtho(...).
Right now I set it up like this:
glOrtho(0, width, height, 0, 0, 1);
The funny thing is, that the view doesn't show the cube at all. I implemented a border for the view in OpenGL which gets shown correctly though.
I'm using Qt and wrote a derived class of QGLWidget.
解决方案
glOrtho
define 6 clipping planes in the view space after the model transform
and the view transform
. So you should specify the parameters of glOrtho in the Eye Space
, not the Screen Space
. in your case, it should be glOrtho(-width/(float)height, width/height, -1.0f, 1.0f, -1.0f, 1.0f)
which assumes the eye position
is in (0.0, 0.0, 0.0)
and your viewport
is width
by height
. the NearVal
parameter of glOrtho
should be negative if the near plane is behind the viewer(the eye position). You may want to man glOrtho. the 4th Chapter of Cg tuturial
can give you a detailed information about the transformation of different coordinate spaces i.e. Object Space
, World Space
, Eye Space
, Clip space
, Normalized Device Space
and Window Space
.