OpenGL Super Treasure Note one-basic rendering

Source: Internet
Author: User



1. Double buffering in rendering:
When using but buffering, when rendering each frame of the drawing, the artboard will be erased and then slowly fill the drawing, if the drawing time is too long, there will be flickering phenomenon. To solve this problem, the introduction of double buffering
double buffering is equivalent to recreating another artboard in the displayed artboard, drawing the process in another artboard, and then replacing the contents of the displayed artboard directly with the contents of another artboard after the drawing is completed. In this way, there will be no flicker in the process, even if the drawing process is slow, at most is the appearance of the phenomenon of Dayton

In the renderscene () function rendered in the example, Glutswapbuffers () is called after each render to toggle the buffer rendering

void Renderscene () {Glclearcolor (0.0f, 0.0f, 1.0f, 0.0f); Glclear (gl_color_buffer_bit); Glclearcolor (1.0f,0.0f,0.0f, 0.0f); Glscissor (100,100,600,400); glenable (gl_scissor_test); Glclear (gl_color_buffer_bit); Glclearcolor (0.0f,1.0f , 0.0f,0.0f); Glscissor (200,200,400,200); Glclear (gl_color_buffer_bit); gldisable (gl_scissor_test); glutSwapBuffers ();}


2. Drawing process:
The final rendering of the entity is actually a process of a pipeline, rendering an entity, we must need the vertex data, and we need to finally fill the shader of the imager. The process of rendering the final pixel through these two shaders is the line of the graph.


OpenGL2.0 After the use of programmable pipelines, that is, rendering elements, we need to pass the shader program, generally divided into two processes, one is the processing of vertex shader, one is the processing of fragment shader.


vertex shader processes data entered by the client, applied transformations, mathematical calculations (illumination, color, displacement), vertex-by-pixel processing
The fragment shader outputs the pixel color values that we eventually see on the screen, processing by pixel.


Shader Data transfer: Vertex attribute data, uniform data, texture data
vertex data: Vertex position, vertex color value, texture coordinate, light normal. These data are typically processed by incoming vertex shaders.
Uniform Data: Unlike property data, a uniform variable can also be found in a fragment shader. The uniform variable is not vertex-by-batch when it is changed. Often used to set the transformation matrix
texture data: Texture data is provided to the fragment shader for sampling, applied to the pixel of the entity based on texture coordinates
Output variable: You can pass the value of the vertex shader to the fragment shader


3. Coordinate system and projection
glfrustum: A container that uses this class as a projection matrix

Orthographic projection: The final drawn image is projected by invoking a function called Glfrustum::setorthographic (Xmin,xmax,ymin,ymax,zmin,zmax). This projection is typically used for 2D drawings.

Perspective projection: Gets a projection matrix by calling the Glfrustum::setperspective (Ffov,faspect,fnear,ffar) function.
Ffov: Visual angle in the vertical direction
Faspect: Aspect ratio of window width height
fnear: Distance from near clipping surface
Ffar: Distance from the far cutting surface


4, a set of ready-packaged shader manager
we use the Glshadermanager in Gltools to render. Comes with a lot of pre-defined shader.
must be initialized before use: Shadermanager.initializestockshaders ()


vertex data and shader bindings: There are 16 types, numbered 0-15. Predefined types are generally glt_attribute_vertex ... (glt_attribute_xxx)


Uniform value setting: Glshadermanager::usestockshader (Glenum shader,....)
The following parameters are the uniform values required by shader .


5, based on the gltools of the entity drawing
Use the Glbatch class in Gltools to draw an entity, specify the type of primitive to draw, the number of vertices, and the vertex data before drawing
Glbatch::begin (glenum primitive,gluint nverts,gluint ntextureunits = 0)
optional private includes Gl_points,gl_lines,gl_line_strip,gl_line_loop,gl_triangles,gl_triangle_strip,gl_triangle_fan.
Special explanations are gl_line_strip and Gl_line_loop, one is a wire band, one is a wire ring, one is not closed, one has a closed
Gl_triangle_strip and Gl_triangle_fan, one is a triangular band, one is a triangular fan, the latter can be connected to a fan or even a circle


after the various attribute data of the vertex is copied by copyxxxdata, the specified shader is called, and the Batch.draw is called to draw the entity


6. Back rejection and depth buffering

1, do not turn on the back and depth test case, there is a triangle overlap, it is difficult to distinguish which triangles should be in front of which in the back, drawing order is a mess


2, an optimized test method: First render the far triangle and then render the near Triangle.

Cons: All triangles need to be sorted, consumed large, and overlapping pixels need to be written two times, write operations in the store will be slow
3. Turn on the back reject: Glenable (Gl_cull_face) rejects the back by default, can be modified by Glcullface (mode) to remove the front or back.
Pros: Discard some triangles during the rendered element assembly phase to improve performance
Cons: Normal rendering order can be disrupted when overlapping occurs

Note: When we rotate, the original back will become positive, the front will become positive, this should be determined by the camera's normal to determine the positive and negative side of the triangle, that is not necessarily clockwise around the back will never see. (http://bbs.csdn.net/topics/380040702)


4, open the depth test: glenable (gl_depth_test) to open the depth test, of course, before opening, you should initialize a depth buffer Glutinitdisplaymode (glut_double| glut_rgba| glut_depth);
Pros: The depth test will eliminate pixels that should be overlaid with existing pixels, solving the problem of back culling

Cons: Requires more memory overhead


7, the effect of stroke:
after opening the depth test, if you want to stroke or make the effect of decals, there may be a z-axis conflict phenomenon, through the polygon offset can solve the problem
Glpolygonoffset ( -1.0f,-1.0f)
glenable (gl_polygon_offset_line)


8. Cropping: Determines the viewport size of the final render to the window
Open: Glenable (gl_scissor_test)
setting: Glscissor (x,y,width,height)


9, mixing: Alpha display and anti-tooth saw support conditions, only open mixing will have translucent and anti-tooth saw effect
Open: Glenable (gl_blend)
Common Color Blends: Glblendfunc (Gl_src_alpha,gl_one_minus_src_alpha): The source color is multiplied by the ALPHA value of your body, plus the target color multiplied by the 1-Alplha value of the source color. The higher the alpha value of the source color, the more source color components are added, and the fewer components the target color retains


10, anti-tooth saw:
Open: Glenable (gl_blend)
Glblendfunc (Gl_src_alpha,gl_one_minus_src_alpha) first turn on color blending
glenable (Gl_point_smooth)Dot Smooth
glenable (Gl_line_smooth)Smooth Line
glenable (Gl_polygon_smooth)polygon Smooth, less use, generally using multiple sampling instead
setting: Glhint (gl_point_smooth_hint,gl_nicest)an algorithm for specifying an anti-tooth saw


11, multi-sampling: need to open a buffer, each time the pixel update, you need to sample to generate a new value
Open: Glutinitdisplaymode (xxx| Glut_multisample)
glenable (glut_multisample)
settings: The RGB value of the fragment used by default does not include a value
can be modified by Glsamplecoverage (Value,invert)
gl_sample_alpha_to_coverage-Use ALPHA value
 ..

OpenGL Super Treasure Note one-basic rendering

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.