Geometry shader geometric Rendering

Source: Internet
Author: User
Document directory
  • Begisline
  • Sphere subdivision
  • Object silhouette

 

Shader Model 4 brings us the geometry shader. In fact, this thing already exists in some 3D animation production software, such as Maya 8. I took a look at which preview and csustan.edu of dx10 used to show you the usage and features of geometry shader. To be honest, there is really little information about this geometry shader, and there are only a few thin lines on Wikipedia.

The features of the Shader Model 4 and the unified GPU are truly fascinating. The unlimited length of commands and unified structure make GPU's general-purpose computing more and more powerful. Currently, in the realtime rendering field, although geometry shader is not actually used, NVIDIA's mind is very obvious: it uses sophisticated offline animation production techniques on GPU with increasing performance. NVIDIA claims that the geforce8 series GPU can use softimage | xsi shader, which is not only a compiler implementation, but also a future trend of using GPU for offline rendering. In the future, we may be able to see games that are rendered at real-time speed with almost average movie quality. This is not an illusion, but a reality. Let's start with geometry
Shader (Gs for short, vertex shader is similar to pixal shader.

Where is the geometry shader

In short, Gs is located between Vs and PS, and can do a lot of work at the model level, such as the level of detail. In the past, these tasks were completed on the CPU and took up valuable CPU loops-CPU is a very busy thing, and it relies on game logic, music, and input acceptance, but it cannot improve much performance. The parallel computing performance of the CPU is far from comparable to that of the GPU.

What does the geometry shader do

When we first saw Gs, we all had the illusion that it was a unit similar to the vs function. In fact, it was not. There is no relationship between the input object and the output object of Gs. A point can generate a triangle, and a triangle can form a triangle strip. However, GS only accepts "adjustable" elements, which are different from those previously used. These elements are input in GS one by one, and then transmitted to the next flow of the pipeline one by one after processing.

What is the adjacency primtive

In the previous section, we raised the difference between the raw materials accepted by GS and traditional materials. In OpenGL, we defined new primitive types, which are:

  • Gl_lines_adjacency_ext
  • Gl_line_strip_adjacency_ext
  • Gl_triangles_adjacency_ext
  • Gl_triangle_strip_adjecency_ext

We can use them as new parameters in Apis such as glbegin () and gldrawelements. What are their respective characteristics.

Line with adjacency: each vertex is composed of 4 N vertices, and N is the number of line segments. What is actually drawn is #1 and #2, #0 and #3 provide adjustment information. The upper left corner of the image.

Line strip with adjacency: each vertex is composed of N + three vertices. The meaning of N is the same as that of N. A line segment is drawn between the vertices #1 and #2, #2 and #3 until # N and # N + 1. The upper right corner of the image.

Triangle with adjacency: each vertex is composed of 6 N vertices, and N refers to the number of triangles. #0 #2 #4 defines the original triangle, while #1 #3 #5 defines the corrected triangle ajacent triangle.

Triangle strip with adjacency: Each triangle consists of 4 N + 2, meaning n is the same as above. #2 #4 #6 #8 defines the original triangle band, while #1 #3 #5 defines the corrected Triangle Group.

What's new in OpenGL

From glew 1.36 and Glee 5.21, The ryshader-related expansion was integrated. Paste the code that uses Gs before making a statement.

Gluint DL = glgenlists (1 );
Glnewlist (DL, gl_compile );
...
Program = glcreateprogram ();
...
Glprogramparameteriext (Program, gl_geometry_input_type_ext, inputgeometrytype );
Glprogramparameteriext (Program, gl_geometry_output_type_ext, outputgeometrytype );

Glprogramparameteriext (Program, gl_geometry_vertices_out_ext, 101 );
Gllinkprogram (Program );
Gluseprogram (Program );
...
Glendlist ();

It should be very familiar, very similar to using VS/Fs. According to the description in NVIDIA OpenGL extension specifications, the new enumeration accepted by glcreateshader () is:

  • Geometry_shader_ext

The newly added functions include:

  • Void programparameteriext (uint program, Enum pname, int value );

The enumerated values accepted by the preceding functions include:

  • Geometry_vertices_out_ext
  • Geometry_input_type_ext
  • Geometry_output_type_ext

The following enumerated values are accepted by APIs such as glbegin () and gldrawelements:

  • Lines_adjacency_ext
  • Line_strip_adjacency_ext
  • Triangles_adjacency_ext
  • Triangle_strip_adjacency_ext

For more detailed descriptions and definitions, see NVIDIA OpenGL extension specifications.

In the above sample code, we can easily know the process of using GS: first create a program handle, and then call glprogramparameteriext to pass in the specific type of input and output elements and the number of output elements, it must be called twice, and then linked and enabled. The inputgeometrytype after gl_geometry_input_type_ext can be the following enumerated values:

  • Gl_points
  • Gl_lines
  • Gl_lines_adjacency_ext
  • Gl_triangles
  • Gl_triangles_adjacency_ext

This is an intuitive call. However, it should be noted that the objects that GS can output depend on the input object type: 1. If gl_lines is output, gl_lines, gl_line_strip, and gl_line_loop must be input; 2. If gl_lines_adjacency_ext is output, you must enter gl_lines_adjacency_ext or timeline. 3. If gl_triangles is output, you must enter gl_triangles, gl_triangle_strip, and gl_triangle_fan. 4. If the timeline is output, you must enter timeline or timeline.

The outputgeometrytype after gl_geometry_output_type_ext can be the following enumerated values:

  • Gl_points
  • Gl_line_strip
  • Gl_triangle_strip
What's new in glsl

First, we need to know that after GS vs, if GS needs the data after VS calculation, it needs to declare the "varying in" variable in two shader. PS is later than Gs. Similarly, if PS needs to use the data calculated by GS, You need to define the "varying out" type variable in the two shader. GS can also access uniform constants like Vs and PS, and GS can access all built-in uniform of OpenGL, such as modelview matrix. You can even complete the transformation in Gs as long as it is suitable.

After Gs is located in Vs, the following describes how to interact between the two shader. If we use GS, we must also use. GS uses vs to calculate the written uniform, including gl_position, gl_normal, and gl_frontcolor. We need to know That vs cannot modify the built-in uniform value, such as gl_vertex, but can write uniform such as gl_position at will.

  • Gl_positionin [#]
  • Gl_normalin [#]
  • Gl_texcoordin [] [#]
  • Gl_frontcolorin [#]
  • Gl_backcolorin [#]
  • Gl_pointsizein [#]
  • Gl_layerin [#]
  • Gl_primitiveidin [#]

The "#" in the array symbol is generally determined by the const int type gl_verticesin. The gl_verticesin value is determined at the link. The specific value indicates the maximum dimension of the input element type, as shown below:

  • Gl_points 1
  • Gl_lines 2
  • Gl_lines_adjacency_ext 4
  • Gl_triangles 3
  • Gl_triangles_adjacency_ext 6

We can clearly see how many vertices each element consists.

Several examplesBegisline

Based on the basic principle of betiller, input several control points to obtain a Smooth Spline. The Code is as follows:

/*
Geometryinput gl_lines_adjacency
Geometryoutput gl_line_strip
Vertex besuppliers. Vert
Geometry besuppliers. Geom
Fragment besuppliers. Frag
Program bezr fpnum <2. 10. 50.>
Linewidth 3.
Linesadjacency [0. 0. 0.] [1. 1. 1.] [2. 1. 2.] [3.-1. 0.]
*/
# Version 120
# Extension gl_ext_geometry_shader4: Enable
Uniform float fpnum;
Void main ()
{
Int num = int (fpnum + 0.99 );
Float dt = 1./float (Num );
Float T = 0 .;
For (INT I = 0; I <= num; I ++ ){
Float OMT = 1.-T;
Float omt2 = OMT * OMT;
Float omt3 = OMT * omt2;
Float t2 = T * t;
Float T3 = T * t2;
Vec4 xyzw = omt3 * gl_positionin [0]. xyzw +
3. * T * omt2 * gl_positionin [1]. xyzw +
3. * t2 * OMT * gl_positionin [2]. xyzw +
T3 * gl_positionin [3]. xyzw;
Gl_position = xyzw;
Emitvertex ();
T + = DT;
}
}

By inputting different fpnum, we can control the accuracy of the spline. Here, we directly write gl_position Without multiplying it by gl_modelviewprojectionmatrix, because we have already made a reduction in Vs, and the precision of interpolation in space reduction is the same as that in World Space. (Big big question: Really? In the texturing & modeling a procedural approach third edition of Ken Perlin, we specifically mentioned that Pixar renderman is interpolated in the world space, which is more accurate than screen interpolation .)

Sphere subdivision

The sphere is separated. A large triangle is gradually divided into many small triangles and eventually becomes a sphere. As follows:

The Code is as follows.

/*
# Version 120
# Extension gl_ext_geometry_shader4: Enable */

Uniform float fplevel;
Varying float lightintensity;

Vec3 v0, v01, v02;

Void producevertex (float S, float T)
{
Const vec3 lightpos = vec3 (0 .., 10 .., 0 .);
Vec3 v = V0 + S * v01 + T * v02;
V = normalize (v );
Vec3 n = V;
Vec3 tnorm = normalize (gl_normalmatrix * n); // The transformed normal
Vec4 ecposition = gl_modelviewmatrix * vec4 (radius * V), 1 .);
Lightintensity = dot (normalize (lightPos-ECposition.xyz), tnorm );
Lightintensity = ABS (lightintensity );
Lightintensity * = 1.5;
Gl_position = gl_projectionmatrix * ecposition;
Emitvertex ();
}

Void
Main ()
{
V01 = (gl_positionin [1]-gl_positionin [0]). XYZ;
V02 = (gl_positionin [2]-gl_positionin [0]). XYZ;
V0 = gl_positionin [0]. XYZ;
Int level = int (fplevel );
Int numlayers = 1 <level;
Float dt = 1./float (numlayers );
Float t_top = 1 .;
For (INT it = 0; it <numlayers; it ++)
{
Float t_bot = t_top-dT;
Float smax_top = 1.-t_top;
Float smax_bot = 1.-t_bot;
Int Nums = It + 1;
Float ds_top = smax_top/float (Nums-1 );
Float ds_bot = smax_bot/float (Nums );
Float s_top = 0 .;
Float s_bot = 0 .;
For (Int Is = 0; is <Nums; Is ++)
{
Producevertex (s_bot, t_bot );
Producevertex (s_top, t_top );
S_top + = ds_top;
S_bot + = ds_bot;
}
Producevertex (s_bot, t_bot );
Endprimitive ();
T_top = t_bot;
T_bot-= DT;
}
}

The result is as follows:

The passed level controls the number of iterations. When level = 3, level = 1 <3 = 8.

Object silhouette

Use gs to stroke the model. The Code is as follows:

/*
Geometryinput gl_triangles_adjacency
Geometryoutput gl_line_strip
Vertex silh. Vert
Geometry silh. Geom
Fragment silh. Frag
Program silhouette color {0. 1. 0 .}
*/

# Version 120
# Extension gl_ext_geometry_shader4: Enable
Void main ()
{
Vec3 V0 = gl_positionin [0]. XYZ;
Vec3 V1 = gl_positionin [1]. XYZ;
Vec3 v2 = gl_positionin [2]. XYZ;
Vec3 V3 = gl_positionin [3]. XYZ;
Vec3 V4 = gl_positionin [4]. XYZ;
Vec3 V5 = gl_positionin [5]. XYZ;
Vec3 n042 = cross (V4-V0, V2-V0 );
Vec3 n021 = cross (V2-V0, V1-V0 );
Vec3 n243 = cross (V4-V2, V3-V2 );
Vec3 n405 = cross (V0-V4, V5-V4 );
If (dot (n042, n021) <0 .)
N021 = vec3 (0., 0., 0.)-n021;
If (dot (n042, n243) <0 .)
N243 = vec3 (0., 0., 0.)-n243;
If (dot (n042, n405) <0 .)
N405 = vec3 (0., 0., 0.)-n405;
If (n042.z * n021.z <0 .)
{
Gl_position = gl_projectionmatrix * vec4 (v0, 1 .);
Emitvertex ();
Gl_position = gl_projectionmatrix * vec4 (V2, 1 .);
Emitvertex ();
Endprimitive ();
}
If (n042.z * n243.z <0 .)
{
Gl_position = gl_projectionmatrix * vec4 (V2, 1 .);
Emitvertex ();
Gl_position = gl_projectionmatrix * vec4 (V4, 1 .);
Emitvertex ();
Endprimitive ();
}
If (n042.z * n405.z <0 .)
{
Gl_position = gl_projectionmatrix * vec4 (V4, 1 .);
Emitvertex ();
Gl_position = gl_projectionmatrix * vec4 (v0, 1 .);
Emitvertex ();
Endprimitive ();
}
}

The effect is as follows.

From the above three examples, we can see that the powerful functions of GS can not only modify the original model, but also implement geometric processing.

PS: The glew 1.4 library is required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.