OpenGL - Tessellation Shader 【转】
http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html
Patch is just an ordered list of vertices (在tessellation shader里面比较重要的概念就是这个patch,patch是一系列的顶点,OpenGL规定patch的vertex数量必须至少大于等于3)
The tessellation process doesn’t operate on OpenGL’s classic geometric primitives: points, lines, and triangles, but uses a new primitive called a patch (Tessellation shader就是针对patch来进行处理的而并非点,县,三角形)
Patch – are processed by all of active shading stage in the pipeline (patch可被所有的shader处理)
Two shading stages of Tessellation shading:
1. Tessellation Control Shader (optional)
In tessellation control shader, gl_PatchVerticesIn provides the number of elements in gl_in (gl_in 用于在tessellation control shader里面访问传进的来patch里面的顶点)
gl_out 用于控制patch里面的vertex从tessellation control shader输出后的属性
gl_InvocationID is used to access the specific vertex of a patch (gl_InvocationID 用于访问传入patch里的特定顶点)
Layout (vertices = *) out; (用于指定输出的patch里面有多少个顶点)
gl_TessLevelInner
Specify how the interior of the domain is subdivided and stored in
a two element array named gl_TessLevelInner(指定多边形内部如何细分)
gl_TessLevelOuter:
Control how the perimeter of the domain is subdivided, and is
stored in an implicitly declared four-element array named
gl_TessLevelOuter(指定多边形边界上的边被如何细分)
gl_TessLevelInner & gl_TessLevelOuter
根据多边形内部区域的类型会有不同的分割法
Tessellation Evaluation Shader (optional)
The TES is executed on all
generated domain
locations.Positions each of the
vertices in the final mesh (TES是针对从tessellation control shader
里面通过细分生成的顶点来进行运算,通过gl_in和gl_VocationID来访问我们传入的patch的一系列顶点信息,结合gl_TessCoord访问我们生成的新顶点的纹理信息来计算新的坐标位置,从而实现细分多边形的效果)
layout (quads, equal_spacing, ccw) in;
(指定新生成的多边形类型等信息)
glPatchParameteri() -- 告诉程序我们定义多少个顶点为一个patch
barrier() – .....
If you have additional per-vertex attribute values, either for
input or output, these need to be declared as either in or out
arrays in your tessellation control shader (需要传入TCS额外的顶点信息,需要定义额外in
& out array)
glPatchParameterfv() -- can be used to set the inner and
outer-tessellation levels(可以用于指定inner和outer的数值,当然我们也可以在tessellation
control shader里面通过gl_TessLevelInner &
gl_TessLevelOuter直接指定)
三中不同类型的domain -- 会决定我们inner和outer的具体含义:
Quad Tessellation:
…..
Isoline Tessellation:
Use only two of the outer-tessellation levels to determine the
amount of subdivision
…..
Triangle Tessellation:
Triangular domains use barycentric coordinates to specify their
Tessellation coordinates
…..
最终渲染效果:
- Tessellation Shader" title="OpenGL - Tessellation Shader" height="291" width="452">
- Tessellation Shader" title="OpenGL - Tessellation Shader" height="318" width="448">
- Tessellation Shader" title="OpenGL - Tessellation Shader" height="291" width="455">
- Tessellation Shader" title="OpenGL - Tessellation Shader" height="298" width="444">
Main.cpp Source Code below:
#include
#include
#include
// TODO: 在此处引用程序需要的其他头文件
#include
#include "vgl.h"
#include "mat.h"
#include "LoadShaders.h"
#include "Shapes/Teapot.h"
using namespace std;
GLuint PM; // Projection
matrix
GLuint
MVM;
// Model view matrix
GLuint InnerL; // Inner
tessellation paramter
GLuint OuterL; // Outer
tessellation paramter
GLfloat Inner = 1.0;
GLfloat Outer = 1.0;
#define SQUARE_VERTEX_NUMBER 4
//----------------------------------------------------------------------------
void init( void )
{
//create
shader and link program first
ShaderInfo
shaders[] = {
{
GL_VERTEX_SHADER,
"square.vert" },
{
GL_TESS_CONTROL_SHADER,
"square.cont" },
{
GL_TESS_EVALUATION_SHADER, "square.eval" },
{
GL_FRAGMENT_SHADER,
"square.frag" },
{ GL_NONE,
NULL }
};
//Shader
program
GLuint
program;
//load
shaders
program =
LoadShaders(shaders);
//link
shader program and use it
glUseProgram(program);
//vertex
array
GLuint
VA;
glGenVertexArrays(1, &VA);
glBindVertexArray(VA);
//Vertex
buffer
GLuint
VB;
glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
//define
square vertex data
const
GLfloat square_vertex_data[SQUARE_VERTEX_NUMBER][2] = {
{ 1.0, 1.0
},
{ -1.0,
1.0},
{ -1.0,
-1.0},
{ 1.0,
-1.0}
};
//vertex
buffer data
glBufferData(GL_ARRAY_BUFFER, sizeof(square_vertex_data),
square_vertex_data, GL_STATIC_DRAW);
//Obtain
vertex position in program
GLint
vPostionIndex = glGetAttribLocation(program, "vPosition");
//Enable
vertex attribute
glEnableVertexAttribArray( vPostionIndex );
//define an
array of generic vertex attribute data for shader
(让shader知道怎么去读取我们传入的vertex 信息)
glVertexAttribPointer(vPostionIndex, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0));
//Obtain
uniform variable of program
PM =
glGetUniformLocation(program, "PJ");
MVM =
glGetUniformLocation(program, "MV");
InnerL =
glGetUniformLocation(program, "inner");
OuterL =
glGetUniformLocation(program, "outer");
//Set some
default value for uniform variable
glUniform1f(InnerL, Inner);
glUniform1f(OuterL, Outer);
mat4 modelview = Translate( 0.0, 0.0, -2.0 )
* RotateX(
-50.0 );
glUniformMatrix4fv( MVM, 1, GL_TRUE, modelview );
//Define how
many vertices composed one patch (定义多少个vertices定义一个patch)
glPatchParameteri(GL_PATCH_VERTICES, 4);
//Enable
some relative setting
glEnable(GL_DEPTH_TEST);
glClearColor( 0.0, 0.0, 0.0, 1.0 );
}
//----------------------------------------------------------------------------
void display( void )
{
glClear(
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUniform1f(InnerL, Inner);
glUniform1f(OuterL, Outer);
glDrawArrays(GL_PATCHES, 0, SQUARE_VERTEX_NUMBER);
glutSwapBuffers();
}
//----------------------------------------------------------------------------
void reshape( int width, int height )
{
glViewport(
0, 0, width, height );
GLfloat aspect = GLfloat(width)/height;
mat4 projection = Perspective( 60.0, aspect, 1, 3
);
glUniformMatrix4fv( PM, 1, GL_TRUE, projection );
glutPostRedisplay();
}
//----------------------------------------------------------------------------
void keyboard( unsigned char key, int x, int y )
{
switch ( key
) {
case 'q':
case 'Q': case 033 :
exit(
EXIT_SUCCESS );
break;
case
'i':
Inner--;
if ( Inner
< 1.0 ) Inner = 1.0;
glUniform1f(
InnerL, Inner );
break;
case
'I':
Inner++;
if ( Inner
> 64 ) Inner = 64.0;
glUniform1f(
InnerL, Inner );
break;
case
'o':
Outer--;
if ( Outer
< 1.0 ) Outer = 1.0;
glUniform1f(
OuterL, Outer );
break;
case
'O':
Outer++;
if ( Outer
> 64 ) Outer = 64.0;
glUniform1f(
OuterL, Outer );
break;
case
'r':
Inner =
1.0;
Outer =
1.0;
glUniform1f(
InnerL, Inner );
glUniform1f(
OuterL, Outer );
break;
case 'm':
{
static
GLenum mode = GL_FILL;
mode = (
mode == GL_FILL ? GL_LINE : GL_FILL );
glPolygonMode( GL_FRONT_AND_BACK, mode );
} break;
}
glutPostRedisplay();
}
//----------------------------------------------------------------------------
int main( int argc, char *argv[] )
{
glutInit(
&argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 512, 512 );
glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "teapot" );
glewExperimental =
GL_TRUE;
//注意这里有个坑 --
这一句很关键,不加这一句gl_GenVertexArray()会报错,好像说是GLEW对openGL的core
context有一些问题
GLenum error
= glewInit();
if( error !=
GLEW_OK )
{
cout<<"glewInit failed, aborting"<<endl;
}
if (
GLEW_ARB_vertex_array_object == NULL )
{
cout<<"GLEW_ARB_vertex_array_object =
NULL"<<endl;
}
cout<<"Error info:"<<glGetError()<<endl;
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMainLoop();
return
0;
}
square.vert source code below:
#version 400 core
in vec4 vPosition;
void main()
{
//gl_Position is used to access the vertex position that is input
from application
gl_Position
= vPosition;
}
square.cont source code below:
#version 400 core
//The main purpose of tessellation control shader is:
//Generate the tessellation output patch vertices that are passed
to the tessellation
//evaluation shader, as well as update any per-vertex
//Define how many vertices will be used as one patch
layout (vertices = 4) out;
//uniform type is used to define the variable that can be used to
communicate between shader and application
uniform float inner;
uniform float outer;
void main()
{
//gl_TessLevelInner is used to define:
//how the
interior of the domain is subdivided and stored in a two element
array named
gl_TessLevelInner[0] = inner;
gl_TessLevelInner[1] = inner;
//gl_TessLevelOuter is used to define:
//how the
perimeter of the domain is subdivided, and is stored in an
//implicitly
declared four-element array
gl_TessLevelOuter[0] = outer;
gl_TessLevelOuter[1] = outer;
gl_TessLevelOuter[2] = outer;
gl_TessLevelOuter[3] = outer;
//gl_in is
used to access the number of elements that are define by
glPatchParameteri()
//glPatchParameteri() define how many vertices as a patch
//gl_out is
used to access the output vertex position of tessellation control
shader
//gl_in
vertex shader structure below:
//in
gl_PerVertex {
//
vec4
gl_Position;
//
float
gl_PointSize;
//
float
gl_ClipDistance[]
//}
gl_in[gl_PatchVerticesIn];
//gl_out
vertex shader structure is similar to gl_in structure
//gl_InvocationID is used to access the specific vertex of a
patch
gl_out[gl_InvocationID].gl_Position =
gl_in[gl_InvocationID].gl_Position;
}
square.eval source code below:
#version 400 core
//The main purpose of tessellation evaluation shader is:
//configure the primitive generator, which is done using a layout
directive
//Specifying the face winding for generated primitives
//(the order the vertices are issued determines the facedness of
the primitive)
layout (quads, equal_spacing, ccw) in;
//The TES is executed on all generated domain locations.
//The bound tessellation evaluation shader is
//executed one for each tessellation coordinate that the primitive
generator
//Tessellation Shaders emits, and is responsible for determining
the position
//of the vertex derived from the tessellation coordinate.
uniform mat4 PJ;
uniform mat4 MV;
#define M_PI
3.14159265358979323846
//----------------------------------------------------------------------------
float Hanning( vec2 p )
{
p -= 0.5; //
map unit square to [-.5, .5]
float r =
length( p );
r = cos(
M_PI * r / 2.0 );
r *=
r;
return
r;
}
void main()
{
//gl_TessCoord is used to access the Tessellation coordinates that
are
//generated
by tessellation control shader
float u =
gl_TessCoord.x;
float v =
gl_TessCoord.y;
//use
Tessellation coordinates to calculate position for new vertex
that
//is
generated by tessellation control shader
#define
p(i) gl_in[i].gl_Position
vec4 pos =
v*(u*p(0) + (1-u)*p(1)) + (1-v)*(u*p(3) + (1-u)*p(2));
pos.z =
Hanning( gl_TessCoord.xy );
gl_Position
= PJ * MV * pos;
}
square.frag source code below:
#version 400 core
out vec4 fColor;
void main()
{
fColor = (1
- gl_FragCoord.z) * vec4( 1.0, 0.0, 0.0, 1.0 );
}
总结:
tessellation shader是可选的shader,不是必须的
tessellation shader与vertex shader不一样,tessellation
shader是针对patch(一系列顶点)来处理而不是一个顶点 (因为tessellation
shader需要通过传入的patch(一系列顶点)来计算新顶点的位置信息)
tessellation control shader负责对patch的细分设定
tessellation evaluation
shader负责对TCS细分出来的顶点进行位置等信息运算从而实现LOD(level of detail --
根据与camera的距离不同而细分程度不同)等效果
Bezier曲线在这里是一种细分后位置的计算方法来实现曲面的平滑效果
tessellation shader有个重要的应用就是LOD(Level of
Detail),通过在判断物体与视线的距离来设定tessellation control里面的factor
level从而实现近细分多,远细分少的效果
还有一个应用叫displacement mapping,在tessellation evaluation
shader里面通过tessellation coordinate的值来映射纹理(sample a texture)
OpenGL - Tessellation Shader 【转】的更多相关文章
- OpenGL 4.0的Tessellation Shader(细分曲面着色器)
细分曲面着色器(Tessellation Shader)处于顶点着色器阶段的下一个阶段,我们可以看以下链接的OpenGL渲染流水线的图:https://www.opengl.org/wiki/Rend ...
- Modern OpenGL用Shader拾取VBO内单一图元的思路和实现
Modern OpenGL用Shader拾取VBO内单一图元的思路和实现 什么意思? 拾取 最简单的理解拾取的方式大概是到(http://www.yakergong.net/nehe/course/t ...
- Modern OpenGL用Shader拾取VBO内单一图元的思路和实现(2)
Modern OpenGL用Shader拾取VBO内单一图元的思路和实现(2) 上一篇里介绍了Color-Coded Picking的思路和最基本的实现.在处理GL_POINTS时已经没有问题,但是处 ...
- Modern OpenGL用Shader拾取VBO内单一图元的思路和实现(3)
Modern OpenGL用Shader拾取VBO内单一图元的思路和实现(3) 到上一篇为止,拾取一个VBO里的单个图元的问题已经彻底解决了.那么来看下一个问题:一个场景里可能会有多个VBO,此时每个 ...
- OpenGL Compute Shader靠谱例子及读取二进制Shader,SPIR-V
学OpenGL以来一直苦恼没有像DX那样可以读取二进制Shader使用的方法,除去有时不想公开自己写的牛逼Shader的心理(虽然目前还从没写过什么牛逼的Shader), 主要是不用现场编译,加快读取 ...
- 【OpenGL】Shader实例分析(七)- 雪花飘落效果
转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如 ...
- OpenGL中shader使用
学了接近一个月的OpenGL,最终要排上用场了...好吧,就从学到的shader(着色器)开刀吧. 先简单的介绍shader,shader事实上是显卡的功能,就是利用显卡的GPU去做图像处理的工作,而 ...
- OpenGL中shader读取实现
1.需要shader在OpenGL中工作,必须经过如下过程 2.代码实现 /********** * loadshader.h **********/ #pragma once #define _CR ...
- OpenGL之shader着色器的应用,三色渐变的三角形
学习自: https://learnopengl-cn.github.io/01%20Getting%20started/05%20Shaders/#_7 首先放一张效果图: 本次教程,将着色器单独定 ...
随机推荐
- Python动态属性和特性(一)
在Python中,数据的属性和处理数据的方法统称为属性.其实,方式只是可调用的属性.除了这二者之外,我们还可以创建特性(property),在不改变类接口的前提下,使用存取方法(即读取值和设置值方法) ...
- Leetcode37--->Sudoku Solver(填充数独)
题目: 给定一个不完整的数独,要求填充好数独:最初给出的数独是有效的,且假设一定有答案: 举例: A sudoku puzzle... 解题思路: 该题与青蛙走迷宫问题很相似,都是用深度优先: 代码如 ...
- [办公软件][2]screenToGif
https://github.com/NickeManarin/ScreenToGif/wiki/help 下载: .Net Framework 4.6.1 https://www.microso ...
- 2016湖南省赛----G - Parenthesis (括号匹配)
2016湖南省赛----G - Parenthesis (括号匹配) Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...
- 第001弹:Java 中创建对象的4种方式
Java 是面向对象的语言,不可避免的,“对象”这个概念是 Java 语言的核心部分,这里来简单讨论一下在 Java 中创建一般对象的方法. 总结下来有以下4种创建对象的方法: 使用 new 关键字调 ...
- iOS学习笔记35-社交分享
一.社交分享 现在很多应用都内置“社交分享”功能,可以将看到的新闻.博客.广告等内容分享到微博.微信.QQ.人人网等,其实从iOS6.0开始苹果官方就内置了Social.framework专门来实现社 ...
- [POI2005]SKA-Piggy Banks (Tarjan缩点)
题目链接 Solution \(Tarjan\) 缩点乱搞. 考虑到环内如果有一个被打开,那么也就全部打开了. 然后很显然入度为 \(0\) 的点需要被砸破. 所以缩点之后找到入度为 \(0\) 的即 ...
- scrapy之小试身手
要爬取的网址是:http://quotes.toscrape.com/ 磕磕绊绊的写完了 spiders import scrapy from kkk.items import * class Quo ...
- css3实现连续不断的波浪
给的波浪要比容器大,然后在左边准备一个相同的,注意,波浪头尾要能衔接起来,接着运动的长度为波浪的宽度,然后不断重复就好了
- SpringBoot重点详解--使用Junit进行单元测试
目录 添加依赖与配置 ApplicationContext测试 Environment测试 MockBean测试 Controller测试 情况一 情况二 方法一 方法二 本文将对在Springboo ...