OpenGL-18-变换与矩阵堆栈

2020-08-05  本文已影响0人  宇宙那么大丶

一、变换

1、OpenGL中的变换

图片.png

2、视图变换

3、模型变换

模型变换: ⽤于操纵模型与其中某特定变换。这些变换将对象通过旋转、缩放、平移等操作移动到需要的位置。

image.png image.png image.png

API:
平移

//参数:结果,在X轴上平移多少,在Y轴上平移多少,在Z轴上平移多少
void m3dTranslationMatrix44(M3DMatrix44f m, floata x, float y, float z);

旋转

//参数:这里是弧度(需要把角度转成弧度),围绕X/Y/Z旋转,围绕谁转写1,相反写0
//这个API是有返回结果呢
m3dRotationMatrix44(m3dDegToRad(45.0), floata x, float y, float z);

缩放

//参数:结果,围绕X/Y/Z进行缩放。如果写-1,就是整体翻转
void m3dScaleMatrix44(M3DMatrix44f m, floata xScale, float yScale, float zScale);

综合变换

//结果,第一个变换,第二个变换。相乘
void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, const M3DMatrix44f b);

二、矩阵堆栈

1、基础API介绍

类型

GLMatrixStack::GLMatrixStack(int iStackDepth = 64); 

在堆栈顶部载⼊⼀个单元矩阵

void GLMatrixStack::LoadIdentity(void); 

在堆栈顶部载⼊任何矩阵

//参数:4*4矩阵
void GLMatrixStack::LoadMatrix(const M3DMatrix44f m); 

矩阵乘以矩阵堆栈顶部矩阵,相乘结果存储到堆栈的顶部

void GLMatrixStack::MultMatrix(const M3DMatrix44f); 

获取矩阵堆栈顶部的值 GetMatrix 函数

//为了适应GLShaderMananger的使⽤,或者获取顶部矩阵的副本
const M3DMatrix44f & GLMatrixStack::GetMatrix(void); 
void GLMatrixStack::GetMatrix(M3DMatrix44f mMatrix);

2、压栈和出栈及过程分析

压栈

//将当前矩阵压⼊堆栈(栈顶矩阵copy ⼀份到栈顶) 
void GLMatrixStack::PushMatrix(void); 

压栈一个矩阵

//将M3DMatrix44f 矩阵对象压⼊当前矩阵堆栈
void PushMatrix(const M3DMatrix44f mMatrix); 
//将GLFame 对象压⼊矩阵对象
void PushMatrix(GLFame &frame); 

出栈

//出栈(出栈指的是移除顶部的矩阵对象)
void GLMatrixStack::PopMatrix(void);

过程分析:

image.png

参考之前的文章:观察方式与MV矩阵堆栈关系

3、放射变换

直接对当前矩阵堆栈进行变换操作

//旋转
//Rotate 函数angle参数是传递的度数,⽽不是弧度
void MatrixStack::Rotate(GLfloat angle,GLfloat x,GLfloat y,GLfloat z); 

//平移
void MatrixStack::Translate(GLfloat x,GLfloat y,GLfloat z); 


//缩放
void MatrixStack::Scale(GLfloat x,GLfloat y,GLfloat z);

举例:

modelViewMatrix.PushMatrix();
//围绕Y轴翻转 
modelViewMatrix.Scale(1.0f, -1.0f, 1.0f);
//沿着Y轴偏移一下
modelViewMatrix.Translate(0.0f, 0.8f, 0.0f);
modelViewMatrix.PopMatrix();

4、GLFrame的API

对GLFrame对象,例如观察者对象,进行操作

class GLFrame 
 { 
 protected: 
         M3DVector3f vOrigin; // Where am I? 
         M3DVector3f vForward; // Where am I going? 
         M3DVector3f vUp; // Which way is up? 
}


cameraFrame.SetOrigin(float x, float y, float z)
cameraFrame.SetUpVector(float x, float y, float z)
cameraFrame.SetForwardVector(float x, float y, float z)
//将堆栈的顶部压⼊任何矩阵
void GLMatrixStack::LoadMatrix(GLFrame &frame); 


//矩阵乘以矩阵堆栈顶部的矩阵。相乘结果存储在堆栈的顶部
void GLMatrixStack::MultMatrix(GLFrame &frame); 


//将当前的矩阵压栈
void GLMatrixStack::PushMatrix(GLFrame &frame);
上一篇 下一篇

猜你喜欢

热点阅读