简体   繁体   English

2D到3D地图OpenGL

[英]2D to 3D map OpenGL

I am trying to make a simple game engine but I got stuck at a point when I tried to map a 2D mouse coordinate to a 3D coordinate in my world. 我试图制作一个简单的游戏引擎,但是当我尝试将2D鼠标坐标映射到我的世界中的3D坐标时,我陷入了困境。 The basic game has a plane that serves as the ground as it is going to be (hopefully with time) an RTS gameengine. 基本游戏有一个飞机作为地面,因为它将成为RTS游戏引擎(希望随着时间的流逝)。

My problem is that I can't really come up with anything useful. 我的问题是我真的无法提出任何有用的东西。 The plane is located at the 0,-100,-300 points and is about 1000x1000 in size. 该平面位于0,-100,-300点,大小约为1000x1000。 My camera is at 0,0,0 and the scene is rotated at 60 degreesto give the impression of a "bird eye" cam. 我的相机位于0,0,0,场景旋转了60度,给人以“鸟眼”凸轮的感觉。

I was thinking about the trigonometric equations, using that I know the height of my camera and the angle and possibly calculating the distance will give me the right coords but this is just an idea. 我在考虑三角方程,据此我知道我的相机的高度和角度,并可能计算距离会给我正确的坐标,但这只是一个主意。

Can somebody please give me some advice? 有人可以给我一些建议吗?

You can do it with a simple ray casting. 您可以通过简单的射线投射来实现。

First, using gluUnProject , you can obtain the 3D world coordinates m corresponding to the 2D window coordinates of the mouse pointer. 首先,使用gluUnProject ,可以获得与鼠标指针的2D窗口坐标相对应的3D世界坐标m

Given the camera position e = (0, 0, 0) , you can compute the mouse ray direction r = m - e . 给定相机位置e = (0, 0, 0) ,您可以计算鼠标光线方向r = m - e

Now, given a point p on the plane and the plane normal n , you can compute the intersection of the mouse ray with the plane . 现在,在平面上给定点p和平面法线n ,您可以计算鼠标射线与平面的交点

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

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