OpenGL 几何知识

一 、引言

为什么要学习几何相关知识?因为在OpenGL中所有可视对象都是2D/3D几何图形,对几何图形的移动/缩放/旋转;拉伸/扭曲等;图形从一个场景转换到另一个场景中,都需要借助向量和矩阵的运算。

二 、坐标系以及变换

事实上,我们所谓的3D在显示屏上并非真正的3D,因为我们的显示屏都是2D的,所以,在计算机内部创建的3D图形都会投影到2D屏幕中,在此期间,就会涉及到坐标系变换。应用程序设置的顶点坐标,经过一系列场景变换后(一般在顶点坐标系完成),得到标准的设备坐标【-1.0,1.0】,然后送入光栅化器,最终得到屏幕的2D坐标点(像素点)。光栅化OpenGL无法控制,故这里我们只讨论场景变换。

用户顶点坐标
场景变换
标准设备坐标
光栅化
屏幕坐标

从用户顶点坐标到屏幕坐标一般会设计以下五个坐标空间。

  • 局部空间(Local Space,或者称为物体空间(Object Space))
  • 世界空间(World Space)
  • 观察空间(View Space,或者称为视觉空间(Eye Space))
  • 裁剪空间(Clip Space)
  • 屏幕空间(Screen Space)
    借用LearnOpenGL中的图说明一下:
    在这里插入图片描述
    1.局部空间中的物体的位置都是局部原点,大小针对物体本身。
    2.局部坐标通过模型变换矩阵,转到世界坐标空间。世界空间中,所有物体的坐标原点都相同,都是世界坐标系原点。
    3.世界坐标通过视图变换矩阵转移到观察空间。世界空间中包含所有物体,观察者的远近和方向共同决定视口中的画面。
    4.观察空间中的点通过投影变换矩阵转移到裁剪空间,投影变换会创建一个平截头体(观察箱),观察箱内的坐标会被映射到标准化设备坐标系中。在裁剪空间中,坐标点都位于[-1.0,1.0]之间,超出范围的点将会被裁剪掉。之所以做投影变换,因为在欧式空间中,成像的结果和人眼成像不一致,比如,两条平行直线在传统的欧式空间中永远不相交(有种说法是相较于无穷远处)。但是,在投影空间中,两条平行直线会相交。在投影空间中,成像的结果和人眼成像基本一致。当然,投影变换除了透视投影,还有正投影(平行投影)。
    5.裁剪空间中的坐标通过视口变换光栅化,转换到屏幕坐标系中。
    总结:
    五个空间坐标系和三个变换矩阵(Model Matrix;View Matrix;Projection Matrix)。
    坐标的表示和转换需要向量和矩阵相关知识,下面我们看看向量和矩阵的基本概念和基本运算。
三、向量
  1. 什么是向量
    既有大小又有方向的量,四维向量形式如: υ = ( x , y , z , w ) \upsilon=(x,y,z,w) υ=(x,y,z,w)
  2. 向量的运算 示例 α = ( 1 , 2 , 2 ) , β = ( 2 , 0 , 1 ) \alpha=(1,2,2),\beta=(2,0,1) α=(1,2,2)β=(2,0,1)
    加法: α + β = ( 3 , 2 , 3 ) \alpha + \beta=(3,2,3) α+β=(3,2,3)
    减法: α − β = ( − 1 , 2 , 1 ) \alpha - \beta=(-1,2,1) αβ=(1,2,1)
    数乘: 2 ∗ α = ( 2 , 4 , 4 ) 2*\alpha =(2,4,4) 2α=(2,4,4)
    点乘(内积): α ⋅ β = ∣ α ∣ ∣ β ∣ cos ⁡ θ = ( 1 ∗ 2 + 2 ∗ 0 + 2 ∗ 1 ) = 4 \alpha\cdot \beta = \mid\alpha\mid\mid\beta\mid\cos\theta=(1*2+2*0+2*1) = 4 αβ=αβcosθ=(12+20+21)=4 提示: θ \theta θ为两个向量的夹角
    叉乘(外积): α × β = ( 2 , 3 , − 4 ) \alpha\times \beta =(2,3,-4) α×β=(2,3,4) 大小为 ∣ α ∣ ∣ β ∣ sin ⁡ θ \mid\alpha\mid\mid\beta\mid\sin\theta αβsinθ
  3. 运算的物理意义和几何意义
    两个向量相加/相减,两个向量形成的平行四边形的对角向量。点的平移。
    数乘,表示点的缩放或者向量的模的缩放。
    内积,用于求两个向量的夹角;或者求一个向量在另外一个向量上的投影。(物理上求功)
    叉乘:用于求三角形的面积,某个平面的法向量。
四、矩阵

矩阵是一个按照长方阵列排列的复数或实数集合。比如
A = [ 1 3 2 2 0 1 3 0 1 ] A = \left[ \begin{matrix} 1 & 3 & 2 \\ 2 & 0 & 1 \\ 3 & 0 & 1 \end{matrix} \right] A=123300211 B = [ 2 1 1 0 0 1 1 0 0 ] B = \left[ \begin{matrix} 2 & 1 &1 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{matrix} \right] B=201100110

矩阵的加减法: A + B = [ 3 4 3 2 0 2 4 0 1 ] A+B = \left[ \begin{matrix} 3 & 4 & 3 \\ 2 & 0 & 2 \\ 4 & 0 & 1 \end{matrix} \right] A+B=324400321
矩阵乘法: A ∗ B = [ 4 1 4 5 2 2 7 3 3 ] A*B = \left[ \begin{matrix} 4 &1 &4 \\5 & 2 & 2 \\ 7 &3 & 3 \end{matrix} \right] AB=457123423

矩阵的提出是为了解决线性方程组求解问题
比如一个非齐次线性方程组可以表示为 A x = b Ax=b Ax=b,增广矩阵 B = ( A , b ) B=(A,b) B=(A,b),线性方程组是否有解与A和B的秩和A的行数n有关。
矩阵满足结合路和分配律,不满足交换律

五、齐次坐标

1.齐次坐标是一种用在投影空间中的坐标系统,用n+1维的向量表示n维空间中的一个点。
2.齐次坐标的作用:1.解决欧式空间中两条直线不能相交的问题。 2.刚性变换就可以用一个4x4的矩阵统一表示。3.齐次坐标中的w分量可以表示观察者到投影平面的距离。
设定一个2D的笛卡尔坐标(1,2),它的齐次坐标可以表示为(1,2,1),(2,4,2),(3,6,3).(1a,2a,a),所以,在一个投影空间中,会有多个齐次坐标对应欧式空间中的一个点。

问题? 求解两条平行线的交点

六、平移旋转缩放

旋转和缩放可以使用3x3的矩阵完成,为了统一平移变换,故将三维笛卡尔坐标用齐次坐标表示 ( x , y , z , w ) (x,y,z,w) (x,y,z,w)

旋转:沿着z轴旋转
[ c o s ( θ ) − s i n ( θ ) 0 0 s i n ( θ ) c o s ( θ ) 0 0 0 0 1 0 0 0 0 1 ] [ x y z w ] = [ x c o s ( θ ) − y s i n ( θ ) x s i n ( θ ) + y c o s ( θ ) z w ] \left[ \begin{matrix} cos(\theta) & -sin(\theta) & 0 & 0 \\ sin(\theta) & cos(\theta) & 0 & 0 \\ 0 & 0 & 1& 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ z \\ w \end{matrix} \right]=\left[ \begin{matrix} xcos(\theta)-ysin(\theta)\\ xsin(\theta)+ycos(\theta)\\ z \\ w \end{matrix} \right] cos(θ)sin(θ)00sin(θ)cos(θ)0000100001xyzw=xcos(θ)ysin(θ)xsin(θ)+ycos(θ)zw

缩放:
[ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] [ x y z w ] = [ s x ∗ x s y ∗ y s z ∗ z w ] \left[ \begin{matrix} s_{x} & 0 & 0 & 0 \\ 0 & s_{y}& 0 & 0 \\ 0 & 0 & s_{z}& 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ z \\ w \end{matrix} \right]=\left[ \begin{matrix} s_{x}*x\\ s_{y}*y\\ s_{z}*z \\ w \end{matrix} \right] sx0000sy0000sz00001xyzw=sxxsyyszzw

平移:
[ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] [ x y z w ] = [ w t x + x w t y + y w t z + z w ] \left[ \begin{matrix} 1 & 0 & 0 & t_{x}\\ 0 & 1 & 0 & t_{y} \\ 0 & 0 & 1& t_{z} \\ 0 & 0 & 0 & 1 \end{matrix} \right]\left[ \begin{matrix} x \\ y \\ z \\ w \end{matrix} \right]=\left[ \begin{matrix} wt_{x}+x\\ wt_{y}+y\\ wt_{z}+z \\ w \end{matrix} \right] 100001000010txtytz1xyzw=wtx+xwty+ywtz+zw

七、变换
  • 线性变换

    线性空间V上的一个变换A称为线性变换,对于V中任意的元素α,β和数域P中任意k,都有
    A(α+β)=A(α)+A(β)
    A (kα)=kA(α)
    仿射变换,平移旋转和缩放都属于线性变换

    [ c o s ( θ ) − s i n ( θ ) 0 0 s i n ( θ ) c o s ( θ ) 0 0 0 0 1 0 0 0 0 1 ] [ x + t y + p z + q w + s ] = [ ( x + t ) c o s ( θ ) − ( y + p ) s i n ( θ ) ( x + t ) s i n ( θ ) + ( y + p ) s i n ( θ ) z + q w + s ] \left[ \begin{matrix} cos(\theta) & -sin(\theta) & 0 & 0 \\ sin(\theta) & cos(\theta) & 0 & 0 \\ 0 & 0 & 1& 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]\left[ \begin{matrix} x+t \\ y +p\\ z +q\\ w+s \end{matrix} \right]=\left[ \begin{matrix} (x+t)cos(\theta)-(y+p)sin(\theta)\\ (x+t)sin(\theta)+(y+p)sin(\theta)\\ z +q\\ w +s\end{matrix} \right] cos(θ)sin(θ)00sin(θ)cos(θ)0000100001x+ty+pz+qw+s=(x+t)cos(θ)(y+p)sin(θ)(x+t)sin(θ)+(y+p)sin(θ)z+qw+s

    = [ x c o s ( θ ) − y s i n ( θ ) x s i n ( θ ) + y s i n ( θ ) z w ] + [ t c o s ( θ ) − p s i n ( θ ) t s i n ( θ ) + p s i n ( θ ) s t ] =\left[ \begin{matrix} xcos(\theta)-ysin(\theta)\\ xsin(\theta)+ysin(\theta)\\ z \\ w \end{matrix} \right]+\left[ \begin{matrix} tcos(\theta)-psin(\theta)\\ tsin(\theta)+psin(\theta)\\ s \\ t \end{matrix} \right] =xcos(θ)ysin(θ)xsin(θ)+ysin(θ)zw+tcos(θ)psin(θ)tsin(θ)+psin(θ)st

  • 投影变换
    平行投影

	ortho(float left, float right, float bottom, float top, float nearPlane, float farPlane)

在这里插入图片描述
透视投影

void frustum(float left, float right, float bottom, float top, float nearPlane, float farPlane)
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)

在这里插入图片描述

七、三维旋转

1.欧拉角
2.四元数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xhh-cy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值