正如标题所示,我试图使用LWJGL同时渲染两个三维对象,但是,只有第二个渲染。我做了一些搜索,并在每次渲染之前和之后添加了glPushMatrix()
和glPopMatrix()
。另外,我试图使用ArrayList动态地而不是静态地渲染每个对象。
实际渲染代码,每帧调用一次:
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//this is done dynamically
for (int i = 0; i < MainProgram.renderer.parts.size(); i++) {
GL11.glPushMatrix();
BasePart p = MainProgram.renderer.parts.get(i);
glTranslatef(p.Position.X+MainProgram.CurrentCamera.CoordinateFrame.X, p.Position.Y-MainProgram.CurrentCamera.CoordinateFrame.Y, -MainProgram.zoom+p.Position.Z-MainProgram.CurrentCamera.CoordinateFrame.Z);
if (MainProgram.renderer.rbd == true) {
p.Rotation.Y += MainProgram.renderer.ydif;
p.Rotation.X += MainProgram.renderer.xdif;
}
glRotatef(p.Rotation.X, 0f, 1f, 0f);
glRotatef(-p.Rotation.Y, 1f, 0f, 0f);
glBegin(GL_QUADS);
glColor3f(p.BrickColor.r, p.BrickColor.g, p.BrickColor.b); //green
glVertex3f(p.Size.X, p.Size.Y, -p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y, -p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(-p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(-p.Size.X, -p.Size.Y, -p.Size.Z);
glVertex3f(p.Size.X, -p.Size.Y, -p.Size.Z);
glVertex3f(p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(-p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X,-p.Size.Y,-p.Size.Z);
glVertex3f(-p.Size.X,-p.Size.Y,-p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y,-p.Size.Z);
glVertex3f(p.Size.X, p.Size.Y,-p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(-p.Size.X, p.Size.Y, -p.Size.Z);
glVertex3f(-p.Size.X, -p.Size.Y, -p.Size.Z);
glVertex3f(-p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, p.Size.Y, -p.Size.Z);
glVertex3f(p.Size.X, p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, -p.Size.Y, p.Size.Z);
glVertex3f(p.Size.X, -p.Size.Y, -p.Size.Z);
glEnd();
GL11.glPopMatrix();
}
Size,Position,Rotation和CoordinateFrame都是具有X,Y和Z值的类。 MainProgram.renderer.parts
是一个ArrayList,用于保存要呈现的对象。我怀疑问题出在实际渲染中的某个位置(glBegin()
到glEnd()
),因为我并不真正看到轮换可能是一个问题。两个对象分别渲染得很好,它们不会同时渲染。它们有两种不同的尺寸,一种比另一种尺寸大。然而,无论它们处于什么顺序,只有第二个呈现。
答案 0 :(得分:0)
我解决了这个问题:每次我想画画时我都在清理缓冲区。我现在已经清除了它,它起作用了。