【发布时间】:2014-08-13 05:06:17
【问题描述】:
正如标题所示,我正在尝试使用 LWJGL 同时渲染两个 3D 对象,但是,只有第二个正在渲染。我进行了一些搜索,并在每次渲染之前和之后添加了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()),因为我真的不知道旋转如何成为问题。两个对象分别渲染就好了,它们只是不会同时渲染。它们有两种不同的尺寸,一种比另一种大。但是,无论它们的顺序如何,都只会呈现第二个。
【问题讨论】:
-
如果您尝试手动渲染两个对象(没有循环),它是否有效?
-
不,它仍然只渲染最后一个要渲染的对象。
-
但它们的位置不同/大小不同?
-
它们的位置、大小和颜色明显不同。