Java - GluProject 如何处理旋转

标签 java glut

我正在尝试在我的游戏中使用 gluProject。我找到了一种使用它的方法,但我不知道如何处理旋转。

这是我编写的代码:

public static Vector2 getScreenCoordinates(float x, float y, float z, int height, int width)
{
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);

    GL11.glGetFloat(2982, modelview);
    GL11.glGetFloat(2983, projection);
    GL11.glGetInteger(2978, viewport);

    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);
    if (result)
    {
        float screen_x = screen_coords.get(0);
        float screen_y = screen_coords.get(1);
        float scrren_z = screen_coords.get(2);

        screen_y -= height / 2;
        screen_y = -screen_y;
        screen_y += height / 2;

        return new Vector2(screen_x, screen_y);
    }
    else
    {
         System.out.printf("Failed to convert 3D coords to 2D screen coords");
         return null;
    }
}

因此,xyz 是我的 3D map 中的坐标。 heightwidth 是我的窗口大小。

如何修改它来处理旋转(偏航和俯仰)?

谢谢。

<小时/>

这是我的新代码:

public Vector2 worldToScreen(double x, double y, double z, int yaw, int pitch)
{   
    // Initialize the result buffer
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);

    // Init the OpenGL buffers
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);

    // Add the rotation
    GL11.glRotatef(yaw, 0, 0, 1);
    GL11.glRotatef(pitch, 1, 0, 0);

    // Get the OpenGL data
    GL11.glGetFloat(2982, modelview);
    GL11.glGetFloat(2983, projection);
    GL11.glGetInteger(2978, viewport);

    // Remove the rotation
    GL11.glRotatef(-yaw, 0, 0, 1);
    GL11.glRotatef(-pitch, 1, 0, 0);

    // Calculate the screen position
    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);

    if (result)
    {
        if ( (screen_coords.get(0) < -1.0f) || (screen_coords.get(0) > 1.0f) || (screen_coords.get(1) < -1.0f) || (screen_coords.get(1) > 1.0f) )
            return null;

        int window_half_width = getWidth() / 2;
        int window_half_height = getHeight() / 2;

        int screen_x = window_half_width + (window_half_width * -screen_coords.get(0));
        int screen_y = window_half_height + (window_half_height * screen_coords.get(1));

        System.out.printf("(Full Screen / No bounds) [" +x+ ", " +y+ ", " +z+ "] gives [" +screen_coords.get(0)+ ", " +screen_coords.get(1)+ "]");
        System.out.printf("(Every Time) [" +x+ ", " +y+ ", " +z+ "] gives [" +screen_x+ ", " +screen_y+ "]");
        return new Vector2(screen_x, screen_y);
    }
    else
    {
        System.out.printf("Failed to convert 3D coords to 2D screen coords");
        return null;
    }
}

正确吗?

最佳答案

偏航是绕 y 轴(指向上方)的旋转,俯仰是绕 x 轴的旋转。

GL11.glRotatef(pitch, 1, 0, 0);
GL11.glRotatef(yaw, 0, 1, 0);

推送/弹出投影矩阵也可能会更快,并且您应该处于正确的矩阵模式。 (模型 View 矩阵和投影矩阵都必须正确)。

GL11.glPushMatrix();
// Camera rotations
GL11.glPopMatrix();

但是为什么需要撤消相机转换是值得怀疑的。您可能应该在每个帧或视口(viewport)中应用一次它们。使用该 View 渲染场景,然后获取投影坐标,然后更改为 2D 渲染模式。

更新

如果您使用相机的滚动、俯仰和偏航来表示它们在 OpenGL 坐标系中的实际含义(相对于相机而言),我会更喜欢它。关键是,只要正确设置相机模型 View 和投影矩阵,就可以投影该点。您一定已经在某处设置了相机。您将能够看到这是否正确,因为您可以看到结果。应用该转换,您就会知道它是正确的。

这是正确的吗?有可能?您必须应用所有转换。与您在游戏中看到的摄像机 View 完全相同的转换。这包括所有平移和旋转。您会知道您是否正确,因为您应该已经在游戏中应用这些转换。

但是,你需要考虑的是,事先的相机投影矩阵是什么?如果您不知道,请调用 glLoadIdentity() 将其清除,然后调用您的相机转换逻辑。您需要一个准确的模型 View 和投影矩阵 - 与您在游戏中使用的相机设置完全相同。如果您的模型 View 或投影矩阵处于错误状态,它将无法工作。

关于Java - GluProject 如何处理旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078711/

相关文章:

java - React - 如何将我想要的组件拾取到另一个模块中

java-如何使用一些用户定义的参数部署远程计算机上的 jar 文件

java - 使用 Maven 命令行将 Java/动态 Web 项目转换为 Maven 项目

c++ - CreateDC() 导致 glutInit() 失败?

c++ - OpenGl - 纹理 glutSolidSphere?

c++ - 获取当前的旋转和位置坐标

java - 如何以 "catch-all-types"的方式扩展 jackson 属性(property)发现?

java - PHP 类中可公开访问的对象

c++ - opengl中如何使用子窗口

c++ - 如何使用 openGL 在窗口的左上角创建菜单