简体   繁体   English

Java中的透视3D投影

[英]Perspective 3D Projection in Java

I'm working on creating a simple 3D rendering engine in Java. 我正在努力用Java创建一个简单的3D渲染引擎。 I've messed about and found a few different ways of doing perspective projection, but the only one I got partly working had weird stretching effects the further away from the centre of the screen the object was moved, making it look very unrealistic. 我已经搞砸了并且发现了一些不同的透视投影方法,但是我唯一一个部分工作的方法是奇怪的拉伸效果,离屏幕中心越远,物体移动,使它看起来非常不现实。 Basically, I want a method (however simple or complicated it needs to be) that takes a 3D point to be projected and the 3D point and rotation (possibly?) of the 'camera' as arguments, and returns the position on the screen that that point should drawn at. 基本上,我想要一个方法(无论多么简单或复杂)需要投射一个3D点,并将'相机'的3D点和旋转(可能?)作为参数,并返回屏幕上的位置这一点应该被吸引到。 I don't care how long/short/simple/complicated this method is. 我不关心这种方法有多长/短/简单/复杂。 I just want it to generate the same kind of perspective you see in a modern 3D first person shooters or any other game, and I know I may have to use matrix multiplication for this. 我只是想让它产生你在现代3D第一人称射击游戏或任何其他游戏中看到的相同类型的视角,我知道我可能必须使用矩阵乘法。 I don't really want to use OpenGL or any other libraries because I'd quite like to do this as a learning exercise and roll my own, if it's possible. 我真的不想使用OpenGL或任何其他库,因为我非常希望这样做是一个学习练习,如果可能的话,我会自己动手。

Any help would be appreciated quite a lot :) Thanks, again - James 任何帮助将非常感激:)谢谢,再次 - 詹姆斯

Update: To show what I mean by the 'stretching effects' here are some screen shots of a demo I've put together. 更新:为了展示我对'拉伸效果'的意思,这里有一些我放在一起的演示的屏幕截图。 Here a cube (40x40x10) centered at the coords (-20,-20,-5) is drawn with the only projection method I've got working at all (code below). 在这里,一个以coords(-20,-20,-5)为中心的立方体(40x40x10)是用我唯一可用的投影方法绘制的(下面的代码)。 The three screens show the camera at (0, 0, 50) in the first screenshot then moved in the X dimension to show the effect in the other two. 三个屏幕在第一个屏幕截图中显示相机在(0,0,50),然后在X维度中移动以显示其他两个中的效果。

相机在(0,0,50)相机在X坐标中移动了一下相机移动得更远

Projection code I'm using: 我正在使用的投影代码:

public static Point projectPointC(Vector3 point, Vector3 camera) {
    Vector3 a = point;
    Vector3 c = camera;
    Point b = new Point();

    b.x = (int) Math.round((a.x * c.z - c.x * a.z) / (c.z - a.z));
    b.y = (int) Math.round((a.y * c.z - c.y * a.z) / (c.z - a.z));

    return b;
}

You really can't do this without getting stuck in to the maths. 如果不加入数学,你真的不能这样做。 There are loads of resources on the web that explain matrix multiplication, homogeneous coordinates, perspective projections etc. It's a big topic and there's no point repeating any of the required calculations here. 网络上有大量的资源可以解释矩阵乘法,齐次坐标,透视投影等。这是一个很大的主题,并且没有必要重复任何所需的计算。

Here is a possible starting point for your learning: 这是您学习的可能起点:

http://en.wikipedia.org/wiki/Transformation_matrix http://en.wikipedia.org/wiki/Transformation_matrix

It's almost impossible to say what's wrong with your current approach - but one possibility based on your explanation that it looks odd as the object moves away from the centre is that your field of view is too wide. 几乎不可能说出你当前的方法有什么问题 - 但是根据你的解释,当一个物体离开中心时看起来很奇怪的一种可能性就是你的视野太宽了。 This results in a kind of fish-eye lens distortion where too much of the world view is squashed in to the edge of the screen. 这导致一种鱼眼镜头失真,其中太多的世界视图被压扁到屏幕的边缘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM