当前位置:网站首页>【OpenGL】透视投影矩阵推导
【OpenGL】透视投影矩阵推导
2022-08-07 05:14:00 【抽象鸡肋】
项目场景:
- 系统:ubuntu
- glad + glfw + opengl3.3
复习games101MVP变换,在使用OpenGL检验推导透视投影矩阵时,发现得出结果的Z坐标与把不符合目标预期。可以看到下面代码,四个矩形从前到后的顺序应该是RGB然后才到粉色,可是实际是反过来的。
问题描述
为什么缩放Z轴需要为负数(红圈的地方,已经加入负号,最终效果正确)。
原因分析:
为什么会这样呢?在纸上反复计算了半天后,刷手机看机了罗斯大佬的视频
探秘三维透视投影 - 齐次坐标的妙用,才发现原来OpenGL转NDC时,默认是近平面映射到-1,远平面映射到1,我大呼白费力气,原来一开始就错了,难怪算半天没发现问题,原来不是算错了。
后来查看OpenGL wiki glDepthRange 、glDepthFunc和glClearDepth。
Parameters
nearVal
Specifies the mapping of the near clipping plane to window coordinates. The initial value is 0.
farVal
Specifies the mapping of the far clipping plane to window coordinates. The initial value is 1.
Description
After clipping and division by w, depth coordinates range from −1 to 1, corresponding to the near and far clipping planes. glDepthRange specifies a linear mapping of the normalized depth coordinates in this range to window depth coordinates. Regardless of the actual depth buffer implementation, window coordinate depth values are treated as though they range from 0 through 1 (like color components). Thus, the values accepted by glDepthRange are both clamped to this range before they are accepted.
The setting of (0,1) maps the near plane to 0 and the far plane to 1. With this mapping, the depth buffer range is fully utilized.
这里意思是
压缩的空间可以自定义,默认(0,1),OpenGL深度测试默认比小,每个像素深度值默认为1。
这也就是为什么默认值OpenGL要在透视投影变换中把Z轴翻转。
如果不想翻转,只需设置
glDepthFunc(GL_GREATER); // 设置为比大
glClearDepth(0.0f); // 设置默认深度值为0
修改后效果:
参考代码:OpenGL_Learn
边栏推荐
猜你喜欢

AD封装库的下载与导入

基于rt-thread studio的STM32裸机开发——LED

This指向问题

DCDC DC low voltage step-up voltage isolation power module (with short circuit protection)

FPGA基础设计之数码管显示

DIN11 IPO 压力应变桥信号处理系列隔离放大器 配电 10V 2mV/V转4-20mA 一入一出

YL70 remote I/O module, can be used to measure four-way switch signal, and has four-way relay signal output

第11天 - 矩阵

基于rt-thread studio的STM32裸机开发第一节:点亮一个LED

阿里云部署
随机推荐
DGIOT IoT Open Source Platform - Tencent Cloud Lightweight Application Server Deployment
DOS常用命令
ArrayList的扩容机制是如何实现的?
5路热电阻温度信号转RS-485/232,温度采集转换模块 YL25
【DGIOT】Support remote printing of barcode/QR code and full life cycle management of one-code equipment
HRF系列隔离宽电压输入高电压稳压输出5V12V24V转50V65V100V115V120V165V200V250V400V600V800V1000V
AD封装库的免费下载与导入
DGIOT device configuration synchronously sends control commands
规则引擎
「SwiftUI」DateFormatter使用和时间倒计时
2022/6/27 Quartz(定时任务)讲解+入门案例
List、Set、Map、Queue、Deque、Stack的遍历方法总结
【数据库基础】MySQL索引
云服务器配置jupyter
3*3卷积核生成
入坑大学时光、单片机开发,嵌入式网站推荐
开关量8入4出,高速以太网通讯Socket自由协议远程IO模块 YJ94
腾讯云部署
OV5640摄像头驱动开发讲解
uniapp的map地图组件介绍