我有一些代码沿 x、y 和 z 轴绘制一条线。我的问题是这些线被剪裁,因此它们在原点附近不可见:
这听起来像是一个远裁剪平面问题,但我给 gluPerspective 提供了 zFar=50,这应该足够了。让它更大似乎没有帮助。还有什么可能导致剪辑?
这是我的代码:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;
public class Test {
static int width = 300, height = 200;
public static void main(String[] _) throws Exception {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
glClear(GL_COLOR_BUFFER_BIT);
// projection matrix
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
GLU.gluPerspective(50, width / (float) height, .1f, 50);
// modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLU.gluLookAt(
.8f, .8f, .8f,
0, 0, 0,
0, 1, 0);
// draw a line for each axis
glBegin(GL_LINES);
// x axis in red
glColor3f(1, 0, 0);
glVertex3i(0, 0, 0);
glVertex3i(10, 0, 0);
// y axis in green
glColor3f(0, 1, 0);
glVertex3i(0, 0, 0);
glVertex3i(0, 10, 0);
// z axis in blue
glColor3f(0, 0, 1);
glVertex3i(0, 0, 0);
glVertex3i(0, 0, 10);
glEnd();
Display.update();
// wait for a close event
while (!Display.isCloseRequested()) {
Thread.sleep(20);
Display.processMessages();
}
Display.destroy();
}
}
更新- 删除glLoadIdentity();
后glMatrixMode(GL_MODELVIEW);
给出了预期的结果,但我不明白为什么。默认的模型视图矩阵不是单位矩阵吗?
更新- 我编写了相同代码的 C 版本,它可以按需要工作。为什么有区别?