最简单的几何着色器(Geometry Shader)【OpenGL】【GLSL】
来源:互联网 发布:今天eia数据 编辑:程序博客网 时间:2024/06/05 04:48
以绘制模型的法线的为例,效果如图:
Torus:
Dragon:
关键代码如下:
1.顶点着色器
static const char * vs_source[] = { "#version 410 core \n" " \n" "layout (location = 0) in vec4 position; \n" "layout (location = 1) in vec3 normal; \n" " \n" "out VS_OUT \n" "{ \n" " vec3 normal; \n" " vec4 color; \n" "} vs_out; \n" " \n" "void main(void) \n" "{ \n" " gl_Position = position; \n" " vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0); \n" " vs_out.normal = normalize(normal); \n" "} \n" };
2.几何着色器
static const char * gs_source[] ={"#version 410 core \n"" \n""layout (triangles) in; \n" // 输入的图元类型 "layout (line_strip, max_vertices = 7) out; \n" // 输出的图元类型和最大的顶点数 " \n""uniform mat4 mv_matrix; \n""uniform mat4 proj_matrix; \n"" \n""in VS_OUT \n" // 顶点着色器输出的顶点属性 "{ \n"" vec3 normal; \n"" vec4 color; \n""} gs_in[]; \n"" \n""out GS_OUT \n" // 几何着色器输出的顶点属性 "{ \n"" vec3 normal; \n"" vec4 color; \n""} gs_out; \n"" \n""uniform float normal_length = 0.4; \n"" \n""void main(void) \n""{ \n"" mat4 mvp = proj_matrix * mv_matrix; \n"" vec3 ab = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz; \n"" vec3 ac = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz; \n"" vec3 face_normal = normalize(cross(ab, ac)); \n"" \n"" vec4 tri_centroid = (gl_in[0].gl_Position + \n"" gl_in[1].gl_Position + \n"" gl_in[2].gl_Position) / 3.0; \n"" \n"" gl_Position = mvp * tri_centroid; \n"" gs_out.normal = gs_in[0].normal; \n"" gs_out.color = gs_in[0].color; \n"" EmitVertex(); \n" // 生成新顶点 " \n"" gl_Position = mvp * (tri_centroid + \n" // 三角图元的中心法线 " vec4(face_normal * normal_length, 0.0)); \n"" gs_out.normal = gs_in[0].normal; \n"" gs_out.color = gs_in[0].color; \n"" EmitVertex(); \n"" EndPrimitive(); \n" // 结束本图元(以下为新图元) " \n"//" gl_Position = mvp * gl_in[0].gl_Position; \n" // 第一个顶点的法线 //" gs_out.normal = gs_in[0].normal; \n" //" gs_out.color = gs_in[0].color; \n" //" EmitVertex(); \n" //" \n" //" gl_Position = mvp * (gl_in[0].gl_Position + \n" //" vec4(gs_in[0].normal * normal_length, 0.0)); \n" //" gs_out.normal = gs_in[0].normal; \n" //" gs_out.color = gs_in[0].color; \n" //" EmitVertex(); \n" //" EndPrimitive(); \n" " \n"" gl_Position = mvp * gl_in[0].gl_Position; \n" // 三角形的三个顶点 " gs_out.normal = gs_in[0].normal; \n"" gs_out.color = vec4(1.); \n"" EmitVertex(); \n"" \n"" gl_Position = mvp * gl_in[1].gl_Position; \n"" gs_out.normal = gs_in[1].normal; \n"" gs_out.color = vec4(1.); \n"" EmitVertex(); \n"" \n"" gl_Position = mvp * gl_in[2].gl_Position; \n"" gs_out.normal = gs_in[2].normal; \n"" gs_out.color = vec4(1.); \n"" EmitVertex(); \n"" EndPrimitive(); \n""} \n"};
3.片元着色器
static const char * fs_source[] = { "#version 410 core \n" " \n" "out vec4 color; \n" " \n" "in GS_OUT \n" "{ \n" " vec3 normal; \n" " vec4 color; \n" "} fs_in; \n" " \n" "void main(void) \n" "{ \n" " color = fs_in.color * abs(normalize(fs_in.normal).z); \n" "} \n" };
0 0
- 最简单的几何着色器(Geometry Shader)【OpenGL】【GLSL】
- 基于几何着色器的模型“爆炸”效果(Geometry Shader)【OpenGL】【GLSL】
- 最简单的曲面细分着色器(Tesselation Shader)【OpenGL】【GLSL】
- OpenGL 几何着色器 Geometry Shader 初探
- OpenGL学习:几何着色器(geometry shader)
- 【OpenGL4.0】GLSL-几何着色器详解和实例(GS:Geometry Shader)
- 【OpenGL4.0】GLSL-几何着色器详解和实例(GS:Geometry Shader)
- OpenGL学习脚印:几何着色器(geometry shader)
- GLSL opengl着色器参数传递Shader
- 几何着色器 Opengl Geometry Shaders 笔记
- opengl glsl 几何着色器 1
- Geometry Shader 几何着色渲染
- 使用计算着色器(Compute Shader)模拟粒子效果【OpenGL】【GLSL】
- OpenGL 着色器语言GLSL的数据类型
- 【OpenGL】着色器 Shader
- opengl编程glsl-着色器
- 最简单的 UBO(Uniform Buffer Object) 【OpenGL】【GLSL】
- OpenGl中使用着色器的基本步骤及GLSL渲染简单示例
- cocoapods快速安装法
- #bzoj1084#草地排水(网络流)
- CentOS zookeeper dockerfile
- Win10系统打开共享文件提示没有权限使用网络资源怎么处理?
- View 体系
- 最简单的几何着色器(Geometry Shader)【OpenGL】【GLSL】
- A/D采集模块总结
- 【随时加】广告
- SQL语句分类和差别
- 1451: 幸运数字
- Android捕捉图像后在SurfaceView上变形显示问题的处理
- hibernate的基本使用
- 正则表达式
- 异步 同步 阻塞 非阻塞