Jogl坐标加倍和负

时间:2013-05-01 22:48:23

标签: java opengl coordinates lwjgl jogl

我是OpenGl的新手,我遇到了一个非常令人沮丧的问题。我有一个显示器,在那个显示器上我正在绘制一个3D立方体,并使用“投影”来查看它。在我的渲染循环中,我已经要求它使用投影并绘制立方体。我还将它的立方体的x,y和z坐标设置为camrea的x,y和z(即:当相机处于(1,2,3)时,立方体将被绘制为(1) ,2,3)。我的问题是,当它在相机的位置“绘制”立方体时,它与相机的实际位置完全相同(即相机处于(10,0,1)并且立方体绘制时)至于(-20,0,-2)。这让我更奇怪的是,我把它打印出相机和立方体的坐标,那些是完全相同的。所以基本上,内部的坐标是同样,但在视觉上,立方体很明显不在相机的位置。

以下是渲染循环前相机的代码:

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(this.fov, this.aspectRatio, this.nearClip, this.farClip);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glTranslated(this.x, this.y, this.z);

这是在渲染循环中调用的相机代码:(camera.useView();)

    GL11.glRotatef(this.rx, 1.0f, 0.0f, 0.0f);
    GL11.glRotatef(this.ry, 0.0f, 1.0f, 0.0f);
    GL11.glRotatef(this.rz, 0.0f, 0.0f, 1.0f);
    GL11.glTranslatef(this.x, this.y, this.z);

这是我用于绘制/渲染多维数据集的代码:(cube.render();)

    GL11.glPushMatrix();
    GL11.glRotatef(rx, 1.0f, 0.0f, 0.0f);
    GL11.glRotatef(ry, 0.0f, 1.0f, 0.0f);
    GL11.glRotatef(rz, 0.0f, 0.0f, 1.0f);
    GL11.glTranslatef(this.x, this.y, this.z);
    GL11.glColor4f(r, g, b, a);

    GL11.glBegin(GL11.GL_QUADS);
    //FRONT FACE
    GL11.glVertex3d(x, y, z + depth);
    GL11.glVertex3d(x, y + height, z + depth);
    GL11.glVertex3d(x + width, y + height, z + depth);
    GL11.glVertex3d(x + width, y, z + depth);

    //BACK FACE
    GL11.glVertex3d(x, y, z);
    GL11.glVertex3d(x, y + height, z);
    GL11.glVertex3d(x + width, y + height, z);
    GL11.glVertex3d(x + width, y, z);

    //BOTTOM FACE
    GL11.glVertex3d(x, y, z);
    GL11.glVertex3d(x, y, z + depth);
    GL11.glVertex3d(x, y + height, z + depth);
    GL11.glVertex3d(x, y + height, z);

    //TOP FACE
    GL11.glVertex3d(x + width, y, z);
    GL11.glVertex3d(x + width, y, z + depth);
    GL11.glVertex3d(x + width, y + height, z + depth);
    GL11.glVertex3d(x + width, y + height, z);

    //LEFT FACE
    GL11.glVertex3d(x, y, z);
    GL11.glVertex3d(x + width, y, z);
    GL11.glVertex3d(x + width, y, z + depth);
    GL11.glVertex3d(x, y, z + depth);

    //RIGHT FACE
    GL11.glVertex3d(x, y + height, z);
    GL11.glVertex3d(x + width, y + height, z);
    GL11.glVertex3d(x + width, y + height, z + depth);
    GL11.glVertex3d(x, y + height, z + depth);

    GL11.glEnd();
    GL11.glPopMatrix();

为了提供更多信息,这是我的渲染循环:

    private void render()
{
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();


    camera.useView(); 


    if (camera.getX() > cube.getX())
    {
        cube.setX(cube.getX() + 0.03f);
    }
    if (camera.getX() < cube.getX())
    {
        cube.setX(cube.getX() - 0.03f);
    }

    if (camera.getZ() > cube.getZ())
    {
        cube.setZ(cube.getZ() + 0.03f);
    }
    if (camera.getZ() < cube.getZ())
    {
        cube.setZ(cube.getZ() - 0.03f);
    }


    cube.render();

}

如果需要任何其他信息或说明,请告知我们。

0 个答案:

没有答案