本节内容是在第一人称漫游上完成的,请先了解上文中第一人称漫游的实现.

这一节讲下第三人称漫游是如何实现,第三人称,简单来说,就是在你后面会跟着一台摄像机顺着你拍摄。

先看一下失败的尝试。这个方法是把人定在摄像机方向的前面,结合前面第一人称漫游的实现,如果只是前后左右移动,人和摄像机是一起的,这样是不用改动,关键是原来以摄像机为原点旋转,而这个我们要以人为原点来旋转,先来看下水平左右的的旋转实现,如下图:

Opengl绘制我们的小屋(四)第三人称漫游-LMLPHP

根据上面关系,我们主要代码如下,大致过程如下,人左右旋转,然后得到摄像机的新位置,摄像机再调整方向,根据摄像机的方向与人的距离设定人的位置。

 type ThreeCamera() =
let camera = new Camera()
let mutable toEye = .f
member this.Eye with get() = camera.Eye
member this.Target with get() = camera.Target
member this.Location
with get() =
let mutable tv = camera.Target-camera.Eye
tv.Normalize()
camera.Eye + tv * toEye
member this.ToEye with get() = toEye and set value = toEye <- value
member this.Transelt (x,y,z) =
camera.Transelt(x,y,z)
//左右对中心转
member this.RightAndLeft x =
let origin = this.Location
let oe = camera.Eye - origin
let length = oe.Length
let oex = Vector2(oe.X,oe.Z)
let oez = Vector2(-oe.Z,oe.X)
let sinLR =length * float32 (Math.Sin(x))
let cosLR =length * float32 (Math.Cos(x))
//得到摄像机新位置
let newEye = oex * cosLR + oez * sinLR + Vector2(origin.X,origin.Z)
camera.Eye <- Vector3(newEye.X,camera.Eye.Y,newEye.Y)
//重新调整摄像机的方向
camera.XAngle <- camera.XAngle + x

左右旋转在正常的速度下,能得到正常的效果,后面我试着改变人与摄像机的长度,或者快速左右旋转,都会造成人的位置移动,想了一久都没想到是什么原因,如果有知道可能原因,麻烦告知一下.谢谢了.

此路不通后,我想了一种思路,说起来很简单,第一人称是以摄像机为球心,摄像机的方向为球面坐标系.如果以第三人称来看,人应该是球心,摄像机所在位置可以看做是球面,只需要换算一下球心与球面的算法就可以,如下所示.

     type Camera() =
let mutable origin = Vector3.Zero
let mutable length = .
let mutable yangle = .
let mutable xangle= Math.PI/.
let mutable bThree = true
do
if not bThree then xangle <- Math.PI/. else xangle <- 1.5*Math.PI
member this.IsThree
with get() = bThree
and set value =
//if value then this.Origin <- this.Target
xangle <- xangle + Math.PI
bThree <- value
member this.Eye
with get() =
let mutable eye = this.Origin
if bThree then eye <- this.Direction
eye
member this.People
with get() =
let mutable people = this.Origin
if not bThree then people <- this.Direction
people
member this.Target
with get() =
let mutable target = this.Direction
if bThree then target <- this.Origin
target
member this.Origin
with get() = origin
and set value = origin <- value
member this.Length
with get() = length
and set value =
if value < . then length <- 0.1
length <- value
member this.YAngle
with get() = yangle
and set value =
if value > Math.PI/. then yangle <- Math.PI/.
elif value < -Math.PI/. then yangle <- -Math.PI/.
else yangle <- value
member this.XAngle
with get() = xangle
and set value =
if value > .* Math.PI then xangle <- value - .* Math.PI
elif value < . then xangle <- value + . * Math.PI
else xangle <- value
member this.PeopleAngle
with get()=
let mutable angle = this.XAngle + Math.PI/.
angle
member this.Direction
with get() =
let mutable len = .
if bThree then len <- length
let xyLength = Math.Cos(this.YAngle) * len
let x:float =float origin.X + xyLength * Math.Cos(this.XAngle)
let y:float =float origin.Y + len * Math.Sin(this.YAngle)
let z:float =float origin.Z + xyLength * Math.Sin(this.XAngle)
Vector3(float32 x,float32 y,float32 z)
member this.Transelt (x,y,z) =
let sinX = Math.Sin(this.XAngle)
let cosX = Math.Cos(this.XAngle)
let mutable xstep = x * sinX + z * cosX
let mutable zstep = z * sinX - x * cosX
if bThree then
xstep <- -xstep
zstep <- -zstep
let x1 = float origin.X + xstep
let y1 = float origin.Y + y
let z1 = float origin.Z + zstep
printfn "angle:%f, sinx:%f, cosx:%f" this.XAngle sinX cosX
printfn "x:%f, y:%f, z:%f" x1 y1 z1
origin <- new Vector3(float32 x1,float32 y1,float32 z1)
member this.Rotate (x,y) =
let xa = this.XAngle + x
let mutable ystep = y
if bThree then ystep <- -y
let ya =this.YAngle + ystep
this.YAngle <- ya
this.XAngle <- xa

xangle与yangle分别是指人或者摄像机当前的偏移量,上文中讲第一人称有讲。IsThree表示是在第三人称漫游情况下,这里面会计算球心Origin与球面Direction,在第一人称时,摄像机位置Eye是origin,摄像机方向Target是Direction,因为摄像机是方向向量,所以length此时固定为1来算,此时改变此值没什么意义,而在第三人称时,人是Origin,而摄像机Eye是Direction,此时length表示人与摄像机的距离,会造成视角内人物放大与放小的效果。

这段代码可以很好的工作,并且很容易就实现第一人称与第三人称的切换,需要注意的是,以第三人称漫游时,上下旋转和前后左右走动与第一人称是相反的,大家可以自己想像一下,在第三人称时,人向上,对应摄像机的在球面是向下动的,同理,前后左右走动也是一样,只有左右旋转时,第一人称与第三人称方向一致。

效果图,灯光还没设置完整,里面有点暗。

Opengl绘制我们的小屋(四)第三人称漫游-LMLPHP

原来的附件在我的笔记本上HD5650可以运行,我发现在我老婆的电脑上,用的是650TI,会出现相关内存不能为读的问题,经查找,发现问题是在设置顶点数据//GL.VertexPointer(3,VertexPointerType.Float,0,IntPtr.Zero)这句代码有问题,现改为GL.InterleavedArrays(InterleavedArrayFormat.V3f,0,IntPtr.Zero),也可以运行,具体原因不知,有那么清楚,可以帮忙说明下。

下面给出实现第一人称与第三人称切换版的附件。

新增加快捷键V切换一三人称漫游,小键盘上的+与—分别是增加与缩小人与摄像机的距离。

一三人称小室切换版。

05-17 01:39