II. Positioning---Chapter 4----The Unreal World

2023-11-30 19:48

本文主要是介绍II. Positioning---Chapter 4----The Unreal World,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Chapter 4. Objects at Rest


Thus far, we have seen very flat things. Namely, a single triangle. Maybe the triangle moved around or had some colors.

This tutorial is all about how to create a realistic world of objects.

这章讲如何创建一个object的真实世界.

The Unreal World

The Orthographic Cube tutorial renders a rectangular prism (a 3D rectangle). The dimensions of the prism are 0.5x0.5x1.5, so it is longer in the Z axis by 3x the X and Y.

The code in this tutorial should be familiar, for the most part. We simply draw 12 triangles rather than one. The rectangular faces of the prism are made of 2 triangles, splitting the face along one of the diagonals.

The vertices also have a color. However, the color for the 6 vertices that make up a face is always the same; this gives each face a single, uniform color.

顶点也有颜色,但是,一个面上6个顶点的颜色值总是一样的,这赋给了每个面一个单独的uniform color.

The vertex shader is a combination of things we know. It passes a color through to the fragment stage, but it also takes a vec2 offset uniform that it adds an offset to the X and Y components of the position. The fragment shader simply takes the interpolated color and uses it as the output color.

vertex shader是一个我们所知道things的结合.它将颜色值传递到fragment stage,它也有一个vec2类型的offset用来作用在position数据的X,Y坐标上.fragment shader简单的获得颜色插值并将其用作输出颜色值.

Face Culling

There is one very noteworthy code change, however: the initialization routine. It has a few new functions that need to be discussed.

初始化程序代码中有一个非常值得注意的变化,程序中有几个新函数需要讨论一下.

Example 4.1. Face Culling Initialization

void init()
{InitializeProgram();InitializeVertexBuffer();glGenVertexArrays(1, &vao);glBindVertexArray(vao);glEnable(GL_CULL_FACE);glCullFace(GL_BACK);glFrontFace(GL_CW);
}

The last three lines are new.

The glEnable function is a multi-purpose tool. There are a lot of binary on/off flags that are part of OpenGL's state. glEnable is used to set these flags to the on position. Similarly, there is a glDisable function that sets the flag to off.

glEnable()函数开启某些state,glDisable()关闭相应state.

The GL_CULL_FACE flag, when enabled, tells OpenGL to activate face culling. Up until now, we have been rendering with face culling off.

Face culling is a useful feature for saving performance. Take our rectangular prism, for example. Pick up a remote control; their general shape is that of a rectangular prism. No matter how you look at it or orient it, you can never see more than 3 sides of it at once. So why bother spending all that fragment processing time drawing the other three sides?

开启GL_CULL_FACE flag时,告诉OpenGL激活face culling.直到现在我们一直是关闭face culling来进行rendering.Face culling 对于提高性能是一个很有用的特性.

Face culling is a way of telling OpenGL not to draw the sides of an object that you cannot see. It is quite simple, really.

Face culling是告诉OpenGL不要去绘制那些你看不到的object的面.

In window space, after the transform from normalized device coordinates, you have a triangle. Each vertex of that triangle was presented to OpenGL in a specific order. This gives you a way of numbering the vertices of the triangle.

在窗口空间中,当你将归一化后的设备坐标进行transform后,你会得到一个三角形.呈现在OpenGL中的三角形的每个顶点都有一个确定顺序.这给你一个将三角形顶点编号的方法.

No matter what size or shape the triangle is, you can classify the ordering of a triangle in two ways: clockwise or counter-clockwise. That is, if the order of the vertices from 1 to 2 to 3 moves clockwise in a circle, relative to the triangle's center, then the triangle is facing clockwise relative to the viewer. Otherwise, the triangle is counter-clockwise relative to the viewer. This ordering is called the winding order.

Figure 4.1. Triangle Winding Order


The left triangle has a clockwise winding order; the triangle on the right has a counter-clockwise winding order.

左面的三角形为clockwise顺时针方向,右面的为counter-clockwise逆时针方向.

Face culling in OpenGL works based on this ordering. Setting this is a two-step process, and is accomplished by the last two statements of the initialization function.

默认情况下,OpenGL中的Face culling为逆时针方向.也可以按两个步骤进行设置,

The glFrontFace defines which winding order, clockwise or counter-clockwise, is considered to be the front side of the triangle. This function can be given either GL_CW or GL_CCW, for clockwise and counter-clockwise respectively.

glFrontFace()定义了是逆时针还是顺时针方向被认为是三角形的前面.这个函数的两个参数是  GL_CW 和 GL_CCW,分别表示逆时针和顺时针.

The glCullFace function defines which side, front or back, gets culled. This can be given GL_BACKGL_FRONT, or GL_FRONT_AND_BACK. The latter culls everything, so no triangles are rendered. This can be useful for measuring vertex shader performance but is less useful for actually drawing anything.

glCullFace ()函数定义了前面和后面中哪一个面会被cull.有三个参数 GL_BACKGL_FRONT, 或者 GL_FRONT_AND_BACK. 后面的GL_FRONT_AND_BACK参数把所有面都cull了,所以不会绘制任何三角形.这在提高vertex shader性能上有用,当确实去绘制something时候用处不大.也就是说以下:

glCullFace(GL_FRONT);
glFrontFace(GL_CCW);
等同于:            
	glCullFace(GL_BACK);
	glFrontFace(GL_CW); 

The triangle data in the tutorial is specifically ordered so that the clockwise facing of the triangles face out. This prevents the drawing of the rear-facing faces.

Lack of Perspective

So, the image looks like this:

Figure 4.2. Orthographic Prism


There's something wrong with this. Namely, that it looks like a square.

Pick up a remote control again. Point it directly at your eye and position it so that it is in the center of your vision. You should only be able to see the front panel of the remote.

Now, move it to the right and up, similar to where the square is. You should be able to see the bottom and left side of the remote.

So we should be able to see the bottom and left side of our rectangular prism. But we cannot. Why not?

Think back to how rendering happens. In clip-space, the vertices of the back end of the rectangular prism are directly behind the front end. And when we transform these into window coordinates, the back vertices are still directly behind the front vertices. This is what the rasterizer sees, so this is what the rasterizer renders.

There has to be something that reality is doing that we are not. That something is called perspective.

这篇关于II. Positioning---Chapter 4----The Unreal World的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/438316

相关文章

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

从0到1,AI我来了- (7)AI应用-ComfyUI-II(进阶)

上篇comfyUI 入门 ,了解了TA是个啥,这篇,我们通过ComfyUI 及其相关Lora 模型,生成一些更惊艳的图片。这篇主要了解这些内容:         1、哪里获取模型?         2、实践如何画一个美女?         3、附录:               1)相关SD(稳定扩散模型的组成部分)               2)模型放置目录(重要)

学习记录:js算法(二十八):删除排序链表中的重复元素、删除排序链表中的重复元素II

文章目录 删除排序链表中的重复元素我的思路解法一:循环解法二:递归 网上思路 删除排序链表中的重复元素 II我的思路网上思路 总结 删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 图一 图二 示例 1:(图一)输入:head = [1,1,2]输出:[1,2]示例 2:(图

LeetCode:3177. 求出最长好子序列 II 哈希表+动态规划实现n*k时间复杂度

3177. 求出最长好子序列 II 题目链接 题目描述 给你一个整数数组 nums 和一个非负整数k 。如果一个整数序列 seq 满足在下标范围 [0, seq.length - 2] 中 最多只有 k 个下标i满足 seq[i] != seq[i + 1] ,那么我们称这个整数序列为好序列。请你返回 nums中好子序列的最长长度。 实例1: 输入:nums = [1,2,1,1,3],

代码训练营 Day26 | 47.排序II | 51. N-皇后 |

47.排序II 1.跟46题一样只不过加一个树层去重 class Solution(object):def backtracking(self,nums,path,result,used):# recursion stopif len(path) == len(nums):# collect our setresult.append(path[:])return for i in range(

Chapter 13 普通组件的注册使用

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、组件创建二、局部注册三、全局注册 前言 在 Vue.js 中,组件是构建应用程序的基本单元。本章详细讲解了注册和使用 Vue 的普通组件的两种方式:局部注册和全局注册。 本篇文章参考黑马程序员 一、组件创建 ①定义 Vue 组件是一种具有特定功能的 Vue 实

代码随想录训练营day37|52. 携带研究材料,518.零钱兑换II,377. 组合总和 Ⅳ,70. 爬楼梯

52. 携带研究材料 这是一个完全背包问题,就是每个物品可以无限放。 在一维滚动数组的时候规定了遍历顺序是要从后往前的,就是因为不能多次放物体。 所以这里能多次放物体只需要把遍历顺序改改就好了 # include<iostream># include<vector>using namespace std;int main(){int n,m;cin>>n>>m;std::vector<i

Chapter 10 Stability and Frequency Compensation

Chapter 10 Stability and Frequency Compensation Chapter 8介绍了负反馈, 这一章介绍稳定性, 如果设计不好, 负反馈系统是要发生震荡的. 首先我们学习理解稳定判断标准和条件, 然后学习频率补偿, 介绍适用于不同运放的补偿方式, 同时介绍不同补偿对两级运放slew rate的影响, 最后介绍Nyquist’s判断标准 10.1 Gener

代码随想录刷题day25丨491.递增子序列 ,46.全排列 ,47.全排列 II

代码随想录刷题day25丨491.递增子序列 ,46.全排列 ,47.全排列 II 1.题目 1.1递增子序列 题目链接:491. 非递减子序列 - 力扣(LeetCode) 视频讲解:回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列_哔哩哔哩_bilibili 文档讲解:https://programmercarl.com/0491.%E9%80%92%E