变换 && 3D空间的2D图形
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- GLM库从0.9.9版本起,默认会将矩阵类型初始化为一个零矩阵(所有元素均为0),而不是单位矩阵(对角元素为1,其它元素为0)。glm::mat4 mat = glm::mat4(1.0f)
- glm:常用头文件:#include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp>
- 新建uniform变量,将mat4变换矩阵传入
gl_Position = transform * vec4(a_Position, 1.0);
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- 在顶点着色器中的坐标转换:
- 局部空间(相对于局部原点的坐标)(顶点数据初始为标准化坐标)
- 世界空间(*模型 / 变换 矩阵,相对于世界的全局原点,对物体进行位移、缩放、旋转)
- 观察空间(*视图矩阵,变换到摄像机的前方)
- 裁剪空间(*投影矩阵:)
- (设定摄像机范围:)
- (OpenGL将会自动进行裁剪掉超出摄像机范围的部分)
- 透视除法:/w齐次坐标:将4D裁剪空间坐标变换为---3D标准化设备坐标NDC(-1——1)-的过程
- 屏幕空间(映射到glviewport的窗口大小),
- 坐标传入光栅器,将其转化为片段
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- 正射投影矩阵:……(w分量等于1.0)
- 左右坐标,底部和顶部,近平面和远平面
glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width/(float)height, 0.1f, 100.0f);
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- 理解透视除法:
- w分量可以理解为:摄像机到顶点的距离
- 目的:将坐标变化为近大远小
- 例子:想象,有两个相同大小的(x,y,z)物体,一个距离近,w小,一个距离远,w大,那么xyz同时除以更大的w分量,值越小(越远越小)
- w的值:
- ===1时:应用于正交矩阵,无变化
- >0 && < 1 ,转为分数(分子<分母),w越大,1/w越小,*1/w,透视除法值会越小
- >1,w越大,/w透视除法值会更小
- <1,一切都会颠倒过来,从后到前翻转
- =0 ,分母不能为负数,程序会崩溃
- w==1,物体大小不变,>1物体会放大,<1物体会缩小
- 顺序模型矩阵*观察矩阵*投影矩阵(矩阵运算从右到左)
3D立方体 && 多个3D立方体
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- 实际应用:
- 观察矩阵实质是变化矩阵,注意:因为摄像机其实不动,所以顶点位移要取反
- /\/\/\/\/\//\/\/\/\/\/\/\/\//\/\///\/\/\/\//\/\/\/\//\//\/\/\/\/\\/
- 3D立方体可以看作6个平面
float cube_Vertices[] = {//后,前,左,右,下,上(左下,右下,右上,右上,左上,左下)
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,0.5f, -0.5f, 0.5f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
};
unsigned int cube_Indices[36] = { 0, 1, 2, 2, 3, 0 ,4, 5, 6, 6, 7, 4 ,8, 9, 10, 10, 11, 8,12, 13, 14, 14, 15, 12,16, 17, 18, 18, 19, 16,20, 21, 22, 22, 23, 20
};
- 绘制多个,只需要改每个的变换矩阵就可以了(位置—)