OpenGL Learning Note -2015.3.24--transform Feedback cache & Particle System Sample Analysis

Source: Internet
Author: User

Transform feedback is one of the coolest features in OpenGL that allows client applications to get vertex data on the rendering pipeline. Based on this feature, the Z-pass scene decision rendering technology is realized, which is of course not to understand what is the Z-pass scene decision rendering technology, in short, it can effectively reduce the rendering data delivery. This is just a simple example system to understand how transform feedback objects are used.

  Body:        1, transform feedback:                  & nbsp  transform feedback is a step before the assembly and rasterization of an element in the OpenGL rendering pipeline, after the end of the vertex processing phase. Transform feedback can recapture the vertices that will be assembled as elements (points, segments, triangles), and then you pass some or all of their properties to the cached object. Each vertex is passed to the element assembly stage (before), recording all the property data that needs to be captured into one or more cache objects (as far as capturing those data is under our control), the program can read the data back through the cache, or use them for subsequent rendering operations.             Tansform feedback all states are managed through a transform feedback object, which includes all cache objects used to record vertex data. The counter that identifies the cache object (the name glgen* series function returns), and the amount of state that is used to identify whether the transform feedback is currently enabled. Transform object creation binding process is similar to general OpenGL object:                A. void glgentransformfeedbacks (Glsizei N, gluint* IDs) generates n names for transform feedback objects and records the generated names into the array IDs. 0 is reserved name, opengl default Tranform feedback, function will never return 0.                B. void GLBINDTRANSFO Rmfeedback (glenum target, gluint ID)     binds an ID (the name returned by glgen*) to the environment specified by the specified target (which must be gl_transform_feedback). He did three jobs: 1. If you bind this name for the first time, you will create a tranform The feedback object, initialized with the default state value, is set to the current transform feedback;2, otherwise it simply helps the transform feedback activate as current; 3, if the ID is 0, it is equivalent to returning to the default transform Feedback object (Unbind all valuable transform feedback objects)                 C. Glboolean Glistransf Ormfeedback (glenum ID) determines whether the ID is a transform feedback object.                 D. When a transform feedback object is bound, all may affect transform Commands for the feedback state will act on this object. However, there is no need to specifically define a transform feedback object in order to use transform feedback related functions, because a default transform feedback object (named ID = 0) is built into the system                 E. Void Gldeletetransformfeedbacks (Glsizei n, const gluint* ids) Delete n tra Nsform Feedback object. Not back after this transform feedback. Delete         2.transform feedback cache                 Transform feedback is primarily used to manage the associated state of snapping vertices to cached objects. This state contains the cache object that is currently connected to the transform feedback cache binding point. You can bind multiple caches to transform feedback at the same time, or you can bind multiple child blocks of a cached object, or even bind the same cache object to a different transform feedback cache binding point without a child block. Binding interface: void GlbindbuffErbase (glenum target, gluint index, gluint buffer) where target must naturally be gl_transform_feedback_buffer, and index must be the current bound TRANSFORM FEEDBACK Object Cache Binding Fixed-point index, the maximum number of binding points with the device implementation can be cleared Gl_max_transform_feedback_buffers value to query. All OpenGL device implementations can support a minimum of 64 binding points. You can also bind a portion of a cached object to a transform feedback binding point: void Glbindbufferrange (Glenum gluint index, gluint buffer, glintptr offset, Glsizeiptr size)    offset and size units are bytes, defining the range of buffer caches. Special attention is paid to the highlights: 1. Ensure that a cache is bound to multiple transform feedback bound points when they use an area that cannot overlap. 2., Glbindbufferbase (), and Glbindbufferrange () do not actively create cache objects (this differs from Glbindbuffer), while the work of creating cached objects is performed by Glbindbuffer (). The data operation is still done by Glbufferdata (), Glbuffersubdata (), Glgetbuffersubdata (). (Explanation of cache binding points: Here you might ask what is the cache binding point?) is actually a point, can also be understood as a name, he has the maximum limit, at least 64, is a shader to access the cache object of a bridge (fast and convenient), a little search, there is a call Ubo (uniform Block Object) of the application, see how much will be aware of the binding point, Unfortunately not found, transform feedback about the bridge to ensure accurate data to find it? Now it's not a good idea, mark. Next Address:http://www.zwqxin.com/archives/opengl/talk-about-transform-feedback.html  Describes the advanced applications of transform feedback)Example of initialization process://cache name Gluint buffer;    Glgenbuffers (1, &buffer)//bind to Transform_feedback to create the cache object. Glbindbuffer (Gl_transform_feedback_buffer, BUFFER)
Glbufferdata initialization data, parameter null, request space reserved using Glbufferdata (Gl_tranform_feedback_buffer, 1024*1024, NULL, gl_dynamic_copy)
Bind to cache binding point, typically used for index = 1, commonly used for index = 0 glbindbufferrange (gl_transform_feedback_buffer, 0, BUFFER, 0, 512*1024) GlB Indbufferrange (Gl_transform_feedback_buffer, 1, BUFFER, 512*1024, 512*1024) 3, TRANSFORM FEEDBACK variable configuration TRANSFORM fe Edback the cache that is bound is a transform feedback object, the output of the vertex (or geometry) shader is recorded in those caches, and the configuration information is stored in the current program object.            OpenGL provides the appropriate interface for us to configure those variables that should be logged. void Gltransformfeedbackvaryings (Gluint program, Glsizei count, const glchar** varyings, Glenum buffermode) parameter var yings specifies an array of variables, which are those that need to be feedback. Program is the corresponding shader application, count represents the number of strings in varyings, BufferMode specifies the mode of capturing variables: A, detach mode (GL_SEPARATE_ATTRIBS): Each variable will be recorded in a separate cache object; Cross mode (GL_INTERLEAVED_ATTRIBS):then all variables are one next to the cache object bound to the first binding point of the current transform feedback Object!         OpenGL also provides built-in variables to help us more flexibly control how the captured data is aligned in the cache: Gl_skipcomponents1, gl_skipcomponents2, Gl_skipcomponents3, Gl_ SKIPCOMPONENTS4, Gl_nextbuffer. For Gl_skipcomponents*,opengl, a specified number of voids (1,2,3,4) will be set aside. These gl_skipcomponents* built-in variables can be used when the capture mode is cross-mode, whereas for GL_NEXTBUFFER,OPENGL the variable is passed to the next transform feedback cache of the current binding point, so that the Gl_ Interleaved_attribs mode can also be stored in a separate cache, if the capture mode is Gl_separate_attribs is encountered Gl_nextbuffer, or gl_interleaved_ Successive encounters with Gl_nextbuffer (two attribs) will skip the current binding point and no data will be logged in the currently bound cache.It seems to be possible to answer the question about the binding point in the parentheses above: first, each binding point corresponds to a cache or part of the cache, and he always has the specified cache independently and cannot intersect with other bindings. Binding points seem to be interpreted as addresses for vertex shaders and geometry shaders. The capture data is always preceded by the index of the first binding point index of the current transform feedback object (the index value is the smallest that) output data, and according to Gltransformfeedbackvaryings () varyings The alignment specified by the parameter is stored in the cache.        4, transform feedback capture start and stop,             Transform feedback can be started and stopped at any time, or even paused. Some control transform start stop api:void glbegintransformfeedback (glenum primitivemode): Start and set transform feedback the entity class that will be recorded Type, the Primitivemode must be: gl_points, Gl_lines, Gl_triangles three one. In the subsequent drawing commands, the type of the member needs to match, or the output type in the geometry shader needs to match it. , void Glpuasetransformfeedback (): Pauses transform feedback The record of the variable, but transform feedback is still in the startup state.                OpenGL generates an error if transform feedback is not started. void Glresumetransformfeedback (): Re-open a feedback procedure that was previously paused by Glpuasetransformfeedback () If transform feedback did not start,                Or is not active, an OpenGL error is generated. void Glendtransformfeedback () ends the transform feedback process.
       5. Particle system analysis based on transform feedback
The program is divided into two processing processes, and 2 shaders are created to manage the two processes separately. The first one is responsible for rendering the model object, and the second is responsible for rendering the particles and implementing collision detection with the model object. The model vertex data used in the collision detection process will be obtained through the transform feedback, and because the position speed information needed to record the particle needs to be retained for the current render use, the single buffer cannot record the new data, so a double buffer is used, and the same uses the transform            Feedback to record the results of this particle rendering and use it for the next frame.  Particle system rendering vertex shader Analysis: #version 410 uniform mat4 Model_matrix;  Uniform mat4 Projection_matrix;  Uniform int triangle_count;//Model, the number of assigned triangles before plotting the updated particles//input layout (location = 0) in vec4 position; Layout (location = 1) in VEC3 velocity;  Output out VEC4 position_out; Out VEC3 velocity_out;
In transform feedback recorded data, use texture lookup tbo to get the Triangle plane uniform samplerbuffer geometry_tbo; Uniform float time_step = 0.02;//Update Time step
Intersection of segment and triangle Plane test bool intersect (VEC3 origin, vec3 direction, vec3 V0, VEC3 v1, VEC3 v2, out VEC3 point) {vec3 u, V,      N      VEC3 W0, W; Float R, a, B;
U = (v1-v0);      v = (v2-v0);      n = Cross (U, v); if (length (n) < 0.1) return false;
W0 = Origin-v0;      A =-dot (n, W0);      b = Dot (n, direction);       if (ABS (b) < 0.1) return false;      R = A/b;       if (R < 0.0 | | R > 1.0) return false; Point = Origin + R * direction;
Float UU, UV, VV, Wu, WV, D;
UU = dot (u, u);      UV = dot (u, v);      VV = dot (V, v);      W = point-v0;      Wu = dot (w, u);      WV = Dot (w, v); D = UV * Uv-uu * VV;
float s, t;
s = (UV * WV-VV * wu)/D;      if (S < 0.0 | | s > 1.0) return false;      t = (UV * WU-UU * WV)/D; if (T < 0.0 | |        (S + t) > 1.0) return false;    return true; }
Post-collision reflection vector calculation   VEC3 reflect_vector (vec3 V, vec3 N)   {      return v-2.0 * DOT (V, N) * n; }& nbsp;  void Main (void)   {      VEC3 accelleration = vec3 (0.0, -0.3, 0.0);      VEC 3 new_velocity = velocity + accelleration * time_step;      VEC4 new_position = position + VEC4 (new_velocit Y * time_step, 0.0);//calculate particle new speed and position       VEC3 v0, v1, v2;      VEC3 point;      int i;     for (i = 0; i < Triangle_count; i++)       {           //Use texture lookup tbo to get the triangle plane of the model. Do the Intersect test if the intersection calculates the new velocity vector and position after the bounce at the intersection           V0 = Texelfetch (Geometry_tbo, I * 3) .xyz    &NBS P     V1 = Texelfetch (Geometry_tbo, I * 3 + 1) .xyz;          v2 = Texelfetch (geometry_tbo , I * 3 + 2) .xyz;          if (intersect (POSITION.XYZ, POSITION.XYZ-NEW_POSITION.XYZ,V0, V1, v2, point))           {              VEC3 n = normalize (Cross (V1-v0, v2-v0));              new_position = VEC4 (Point + reflect_vector (new_ Position.xyz-point, N), 1.0);              new_velocity = 0.8 * Reflect_vector (New_ve Locity, N);         }     }
Beyond a certain range, regression.          if (New_position.y < -80.0) {new_position = VEC4 (-new_position.x * 0.3, POSITION.Y + 200.0, 0.0, 1.0);      New_velocity *= VEC3 (0.2, 0.1,-0.3);      } velocity_out = new_velocity * 0.9999;//almost no attenuation of speed position_out = new_position;  Gl_position = Projection_matrix * (Model_matrix * Position); }
Particle system Slice Shader: Very simple, just set the color of the particles to white. #version 410
Layout (location = 0) out VEC4 color;\
void Main (void) {color = VEC4 (1.0); }
There is no difference between the vertex shader and the slice shader that the model renders and the previous example. Can be seen inside the code. Or see the detailed comments in the previous article
5. Particle instance Run

Particles falling from the top of the model


When the particle hits the model, it bounces off.


Then end~~~(Discontent: Increasingly boring life, increasingly no passion for work, youth is just passing, no traces)

OpenGL Learning Note -2015.3.24--transform Feedback cache & Particle System Sample Analysis

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.