OpenGL学习 Following the Pipeline
Passing Data to the Vertex Shader
Vertex Attributes
At the start of the OpenGL pipeline,we use the in keyword to bring inputs into the vertex shader.
Between stages,in and out can be used to form conduits from shader to shader and pass data
between them.
For now, consider the input to the vertex shader
and what happens if you declare a variable with an in storage qualifier. This
marks the variable as an input to the vertex shader, which means that it is
automatically filled in by the fixed-function vertex fetch stage. The variable
becomes known as a vertex attribute.
Vertex attributes are how vertex data is
introduced into the OpenGL pipeline. To declare a vertex attribute, declare a
variable in the vertex shader using the in storage qualifier.
#version core
// "offset" is an input vertex attribute
layout (location = ) in vec4 offset;
void main(void)
{
const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
// Add "offset" to our hard-coded vertex position
gl_Position = vertices[gl_VertexID] + offset;
}
We can tell this stage what to fill the variable
with by using one of the many variants of the vertex attribute functions,
glVertexAttrib*(). The prototype for glVertexAttrib4fv(),
which we use in
this example, is
void glVertexAttrib4fv(GLuint index, const GLfloat
* v);
index is used to reference the
attribute and v is a pointer to the new data to put into the
attribute.
The layout (location = 0) code in the declaration
of the offset attribute.This is a layout qualifier, and we have used it to set
the location of the vertex attribute to zero. This location is the value we’ll
pass in index to refer to the attribute.
Passing Data from Stage to
Stage
built-in variables such as gl_VertexID and
gl_Position.
We use in and out to pass data from a stage to
another.
Interface Blocks
Declare interface variables to communicate a
number of different pieces of data between stages, and these may include arrays,
structures, and other complex arrangements of variables.To achieve this, we can
group together a number of variables into an interface block. The declaration of
an interface block looks a lot like a structure declaration, except that it is
declared using the in or out keyword depending on whether it is an input to or
output from the shader.
Interface blocks are matched between stages using
the block name (VS_OUT in this case), but are referenced in shaders using the
instance name.
Matching interface blocks by block name but
allowing block instances to have different names in each shader stage serves two
important purposes:
First, it allows the name by which you refer to
the block to be different in each stage, avoiding confusing things such as
having to use vs_out in a fragment shader;
Second, it allows interfaces to go from being
single items to arrays when crossing between certain shader stages, such as the
vertex and tessellation or geometry shader stages as we will see in a short
while.
Note that interface blocks are only for moving
data from shader stage to shader stage — you can’t use them to group together
inputs to the vertex shader or outputs from the fragment shader.
Tessellation
Tessellation is the process of breaking a
high-order primitive (which is known as a patch in OpenGL) into many smaller,
simpler primitives such as triangles for rendering.
OpenGL includes a fixed-function, configurable
tessellation engine that is able to break up quadrilaterals, triangles, and
lines into a potentially large number of smaller points, lines, or triangles
that can be directly consumed by the normal rasterization hardware further down
the pipeline.
Logically, the tessellation phase sits directly
after the vertex shading stage in the OpenGL pipeline and is made up of three
parts: the tessellation control shader, the fixed-function tessellation engine,
and the tessellation evaluation shader.
Tessellation Control
Shaders
Each patch is formed from a number of control
points. The number of control points per patch is configurable and set by
calling glPatchParameteri() with pname set to GL_PATCH_VERTICES and value set to
the number of control points that will be used to construct each patch. The
prototype of glPatchParameteri() is
void glPatchParameteri(GLenum pname, GLint
value);
By default, the number of control points per patch
is three.
If this is what you want (as in our example
application), you don’t need to call it at all.
The vertex shader runs once per control point
whilst the tessellation control shader runs in batches on groups of control
points where the size of each batch is the same as the number of vertices per
patch.
Vertices are used as control points, and the
result of the vertex shader is passed in batches to the tessellation control
shader as its input. The number of control points per patch can be changed such
that the number of control points that is output by the tessellation control
shader can be different from the number of control points that it consumes. The
number of control points produced by the control shader is set using an output
layout qualifier in the control shader’s source code. Such a layout qualifier
looks like: layout (vertices = N) out; Here, N is the number of control points
per patch.
The control shader is responsible for calculating
the values of the output control points and for setting the tessellation factors
for the resulting patch that will be sent to the fixed-function tessellation
engine. The output tessellation factors are written to the gl_TessLevelInner and
gl_TessLevelOuter built-in output variables, whereas any other data that is
passed down the pipeline is written to user-defined output variables (those
declared using the out keyword, or the special built-in gl_out array) as
normal.
Listing 3.7 shows a simple tessellation control
shader. It sets the number of output control points to three (the same as the
default number of input control points) using the layout (vertices = 3) out;
layout qualifier, copies its input to its output (using the built-in variables
gl_in and gl_out), and sets the inner and outer tessellation level to 5. The
built-in input variable gl_InvocationID is used to index into the gl_in and
gl_out arrays. This variable contains the zero-based index of the control point
within the patch being processed by the current invocation of the tessellation
control shader:
#version core ayout (vertices = ) out; void main(void) { if (gl_InvocationID == ) {
gl_TessLevelInner[] = 5.0;
gl_TessLevelOuter[] = 5.0;
gl_TessLevelOuter[] = 5.0;
gl_TessLevelOuter[] = 5.0;
} gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; }
The tessellation engine
The tessellation engine is a
fixed-function part of the OpenGL pipeline that takes high-order surfaces
represented as patches and breaks them down into simpler primitives such as
points, lines, or triangles. Before the tessellation engine receives a patch,
the tessellation control shader processes the incoming control points and sets
tessellation factors that are used to break down the patch. After the
tessellation engine produces the output primitives, the vertices representing
them are picked up by the tessellation evaluation shader. The tessellation
engine is responsible for producing the parameters that are fed to the
invocations of the tessellation evaluation shader, which it then uses to
transform the resulting primitives and get them ready for
rasterization.
Tessellation Evaluation
Shaders
【To be continue...】
OpenGL学习 Following the Pipeline的更多相关文章
- OpenGL学习笔记3——缓冲区对象
在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...
- OpenGL学习进程(12)第九课:矩阵乘法实现3D变换
本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈. (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
- OpenGL学习进程(11)第八课:颜色绘制的详解
本节是OpenGL学习的第八个课时,下面将详细介绍OpenGL的颜色模式,颜色混合以及抗锯齿. (1)颜色模式: OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. R ...
- OpenGL学习笔记:拾取与选择
转自:OpenGL学习笔记:拾取与选择 在开发OpenGL程序时,一个重要的问题就是互动,假设一个场景里面有很多元素,当用鼠标点击不同元素时,期待作出不同的反应,那么在OpenGL里面,是怎么知道我当 ...
- OpenGL学习之路(一)
1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...
- OpenGL学习之路(三)
1 引子 这些天公司一次次的软件发布节点忙的博主不可开交,另外还有其它的一些事也占用了很多时间.现在坐在电脑前,在很安静的环境下,与大家分享自己的OpenGL学习笔记和理解心得,感到格外舒服.这让我回 ...
- OpenGL学习之路(四)
1 引子 上次读书笔记主要是学习了应用三维坐标变换矩阵对二维的图形进行变换,并附带介绍了GLSL语言的编译.链接相关的知识,之后介绍了GLSL中变量的修饰符,着重介绍了uniform修饰符,来向着色器 ...
- OpenGL学习之路(五)
1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放 ...
- OpenGL学习之windows下安装opengl的glut库
OpenGL学习之windows下安装opengl的glut库 GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows环境下的GLUT下载地址:(大小约为15 ...
随机推荐
- CF .Beautiful numbers 区间有多少个数字是可以被它的每一位非零位整除。(数位DP)
题意:数字满足的条件是该数字可以被它的每一位非零位整除. 分析:大概的思路我是可以想到的 , 但没有想到原来可以这样极限的化简 , 在数位dp 的道路上还很长呀 : 我们都知道数位dp 的套路 , 核 ...
- SQL Connect By 的例子
看到一个较为通俗易懂的connect by的例子,是百度知道的答案,稍微整理了一下.我自己这样理解:connect by prior "id" = "p_id" ...
- day22 正则表达式 re
1. 正则表达式 正则表达式是对字符串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤. 工具: 各大文本编辑器⼀般都有正则匹配功能. 我们也可以去http://tool.china ...
- PV并发UV
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'返回结果:SYN_RECV 2 (SYN连接请求收到2个 等待确 ...
- linux输入输出及vim管理
一.理解系统的输入输出 输入输出系统是计算机重要组成部分,是沟通计算机与外界的桥梁. 二.管理输入输出的符号 1.输出重定向 > ##重定向正确输出 ...
- idea(2)快捷键
Ctrl+E:最近编辑文件 Ctrl+J:自动代码快捷 Ctrl+N:查找类 Ctrl+U:大小写转换 Ctrl+F12:outline Alt+1:全屏 Alt+F1:类定位到左侧目录 Alt+In ...
- java——抽象类、接口、二者区别
抽象类: 抽象方法:不包含方法体的方法为抽象方法,抽象方法必须使用abstract关键字来修饰: abstract void method(); 抽象类:当一个类中包含了抽象方法时,该类必须使用abs ...
- python 运算符-赋值运算
结果是值 算数运算: a = 10 * 10 赋值运算: a = a + 1 a +=1 结果是布尔值 比较运算: a = 1 > 5 逻辑运算: a = 1 > 6 or 1= ...
- leetcode 627. Swap Salary 数据库带判断的更新操作
https://leetcode.com/problems/swap-salary/description/ 用 set keyWord = Case depentedWord when haha ...
- [PHP]AES加密----PHP服务端和Android客户端
本文采取128位AES-CBC模式加密和解密 1.首先对服务端安装mcrypt: sudo apt-get install php5-mcrypt php5-dev sudo php5enmod mc ...