Simple Introduction to Geometry Shaders in GLSL (Part 2)


Simple Introduction to Geometry Shaders in GLSL (Part 2)



Firenze...

Content:

You missed the first part of this introduction to GS? No problem, you can find it HERE.

1 – Doubling the geometry with the geometry shader

Let’s see a more interesting use of the GS: a geometry doubler. This geometry doubler duplicates the input primitives and translates them along the vertex normal.

GeeXLab, geometry shader in GLSL
The GS doubler demo in GeeXLab – green: input geometry – yellow: duplicated geometry

Like in the first example, the GLSL program includes a VS, A GS and a PS.

The geometry doubler allows us to see how to pass data from vertex shader to geometry shader.

Vertex shader:

#version 330 compatibility
out vec4 normal;
void main()
{	
  gl_Position = gl_Vertex;
  normal =  vec4(gl_Normal.xyz, 0.0);
}

As you can see, this vertex shader is rather mininal (a pass-trought VS). Why? because the vertex transformation will be done in the geometry shader.

In this vertex shader, normal is a vertex attribute and is sent to the geometry shader like the built-in gl_Position. Now let’s see how to read the vertex normal in the GS.

Geomerty shader:

#version 330 compatibility

layout(triangles) in;
layout(triangle_strip, max_vertices=6) out;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;

in vec4 normal[];

out vec4 vertex_color;

void main()
{	
  mat4 modelViewMatrix = viewMatrix * modelMatrix;
  mat4 viewProjectionMatrix = projectionMatrix * viewMatrix;
  int i;
  
  //====== First triangle - identical to the input one.
  //
  for(i=0; i<3; i++)
  {
    vec4 view_pos = modelViewMatrix * gl_in[i].gl_Position;
    gl_Position = projectionMatrix * view_pos;
    vertex_color = vec4(0.0, 1.0, 0.0, 1.0);
    EmitVertex();
  }
  EndPrimitive();
  
  //====== Second triangle - translated version of the 
  // input triangle.
  //
  for(i=0; i<3; i++)
  {
    vec4 N =  normal[i];
    vec4 world_pos = modelMatrix * (gl_in[i].gl_Position + normalize(N) * 10.0);
    gl_Position = viewProjectionMatrix * world_pos;
    vertex_color = vec4(1.0, 1.0, 0.0, 1.0);
    EmitVertex();
  }
  EndPrimitive();
}

Remember, we are in a geometry shader: that’s why we retrieve per-vertex normals in an array. There is one normal per vertex and the size of the array is… 3 elements because the GS takes a triangle as input data. The number of elements of the array is automatically set by the GLSL compiler according to the input layout (here a triangle).

The geometry doubler shader outputs two triangles: one the is input triangle and the second one is a new triangle: the double. This second triangle is translated along the vertex normal. The translation is done in object space (object space? refresh your memory with this article about transformation spaces) that’s why we do all the computations in the geometry shader. If you try to translate along the normal in another space (world, view, projection), you will end up with strange results…

In order to tell OpenGL we output two triangles, we must modify a bit the output layout compared to our first GS in the part 1:

layout(triangle_strip, max_vertices=6) out;

The GS outputs now 6 vertices.

We used an array to retrieve the normals in the GS. But if you need to pass more vertex attributes between the VS and the GS, there is a more handy way to do it: by using an interface block. An interface block allows to pack in a structure all vertex attributes and to pass a single array from the VS to the GS:

Vertex shader with interface block:

#version 330 compatibility

out VertexAttrib
{
  vec3 normal;
  vec4 color;
} vertex;

void main()
{	
  gl_Position = gl_Vertex;
  vertex.normal =  vec4(gl_Normal.xyz, 0.0);
  vertex.color =  vec4(1.0, 1.0, 0.0, 1.0);
}

Geomerty shader with interface block:

#version 330 compatibility

layout(triangles) in;
layout(triangle_strip, max_vertices=6) out;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;

in VertexAttrib
{
  vec3 normal;
  vec4 color;
} vertex[];

out vec4 vertex_color;

void main()
{	
  mat4 modelViewMatrix = viewMatrix * modelMatrix;
  mat4 viewProjectionMatrix = projectionMatrix * viewMatrix;
  int i;
  
  //====== First triangle - identical to the input one.
  //
  for(i=0; i<3; i++)
  {
    vec4 view_pos = modelViewMatrix * gl_in[i].gl_Position;
    gl_Position = projectionMatrix * view_pos;
    vertex_color = vec4(0.0, 1.0, 0.0, 1.0);
    EmitVertex();
  }
  EndPrimitive();
  
  //====== Second triangle - translated version of the 
  // input triangle.
  //
  for(i=0; i<3; i++)
  {
    vec4 N =  vertex[i].normal;
    vec4 world_pos = modelMatrix * (gl_in[i].gl_Position + normalize(N) * 10.0);
    gl_Position = viewProjectionMatrix * world_pos;
    vertex_color = vertex[i].color;
    EmitVertex();
  }
  EndPrimitive();
}

And the last shader of our GLSL program:
Pixel shader:

#version 330 compatibility
in vec4 vertex_color;
void main(void)
{
  gl_FragColor = vertex_color;	
}

Nothing special about the PS.

2 – The Demo

 The GeeXLab demo is available in the GLSL_Geometry_Shader/ folder of GeeXLab code sample pack:
[download#40#image]

This demo requires GeeXLab 0.3.2+ (GS support has been added in the 0.3.x branch of GeeXLab).

GeeXLab, geometry shader in GLSL
The GS doubler demo in GeeXLab with live coding



http://www.geeks3d.com/20111117/simple-introduction-to-geometry-shader-in-glsl-part-2/

投影几何学是一门研究几何图形如何与平面或空间中的投影相关联的数学学科。它的基本思想是通过把空间中的点映射到较低维度的投影平面上,来研究几何图形的性质。 投影几何学的一个重要概念是投影,即将空间中的点映射到一个低维度的平面上。可以使用各种投影方式,如平行投影、透视投影等。在平行投影中,所有平行的线段都会被映射为平行线段,而在透视投影中,线段会按照一定的角度收敛到一个点。 投影几何学的另一个重要概念是射影空间。射影空间是基于投影的概念建立起来的一种数学结构。它允许我们在一个维度较低的空间中进行几何运算,而不会丧失原始空间中的性质。例如,在射影平面中,平行的线段会相交于一个点。射影空间的引入使得我们可以在一个更抽象的层面上研究几何性质。 投影几何学在许多领域中有广泛的应用,包括计算机图形学、计算机视觉、物理学等。在计算机图形学中,投影几何学被用来模拟光线在摄像机和屏幕之间的传播路径,以实现逼真的图像渲染。在计算机视觉中,投影几何学被用来进行3D物体的识别和重建。在物理学中,投影几何学被用来研究光的传播和反射。 总之,投影几何学是一门研究几何图形与投影之间关系的数学学科。它的应用范围广泛,可以应用于计算机图形学、计算机视觉和物理学等领域。通过投影几何学,我们可以更深入地理解和分析几何图形及其性质。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值