OpenGL Super Treasure Notes-Bezier curves and surfaces

Source: Internet
Author: User

http://my.oschina.net/sweetdark/blog/183721

Parametric equation representation

In high school, we all learned the parametric equation of the straight line: y = kx + B; where k is the slope and B is the intercept (that is, the intersection coordinates with the y axis). Similarly, we can also use a parametric equation to represent a curve. In 1962, the French engineer, Bezier, invented the Bezier curve equation. A detailed introduction to Bezier curves can be consulted (Vikibeser). Only the functions of the OpenGL implementation of Bessel are described here.

When OpenGL defines a curve, it also defines it as a curve equation. We make the parameter of this curve U, and its range is the definition field of the curve. surfaces require both the U and v two parameters to describe them. Note that the U and V parameters only represent the range of parametric equations that describe the curve, and they do not reflect the actual coordinate values. Its coordinates can be expressed as:

x = f (u); y = g (u); z = h (u);

Such as:

Points

The shape of a Bezier curve is controlled by a control point. The number of control points for a Bezier curve is the order of the curve. According to the number of control points, Bezier curve is divided into two Bezier curve, three times Bezier curve, Gauche Besel curve.

Linear curve

Linear Bezier Presentation animation,T in [0,1]

Quadratic Square curve

To construct a two-time Bezier curve, you can mediate points q0 and Q1 as a Tfrom 0 to 1:

    • The continuous point Q0 from p0 to P1 describes a linear Bezier curve.

    • The continuous point Q1 from P1 to P2 describes a linear Bezier curve.

    • A continuous point B(t) from q0 to Q1 that describes a two-time Bezier curve.


The structure of quadratic Bezier curves
Quadratic Bezier curve demo animation,t in [0,1]

Three-cubic-square curve

To construct higher-order curves, more intermediary points are needed. For three curves, the mediation point q0,q1,Q2, as described by the linear Bezier curve, and the points Rtwo,r0 described by the 1 curves are constructed:


Structure of three times Bezier curve
Three-time Bezier curve demo animation,t in [0,1]

Continuity

Whether the two-segment curve is connected, indicates whether the two curves are continuous. The continuity of the curve is divided into 4 kinds, no continuous, point continuous, tangent continuous, curvature continuous. Each of these cases is indicated in the following ways:

Where the curvature of a continuous curve transitions smoother. We can use parameters to set the continuity of the curve.

Value-Finding device

OpenGL provides a number of functions to draw Bezier curves and surfaces. We only need to provide control points and u,v as parameters, and then call the evaluation function to draw the curve.

Examples of the curve:

Control Point  glint numofpoints = 4; static glfloat controlpoints[4][3] =  {{-4.0f, 0.0f, 0.0f},{-6.0f, 4.0f, 0.0f},{6.0f, -4.0f, 0.0f},{4.0f,  0.0F,&NBSP;0.0F}};&NBSP;VOID&NBSP;SETUPRC () {  glclearcolor (0.0f, 0.0f, 0.0f, 1.0f);   glcolor3f (1.0f, 0.0f, 1.0f);}  //Draw Control Point void drawpoints () {  glpointsize (2.5f);   glbegin (gl_points);  for   (int i = 0; i < numofpoints; ++i)     {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;GLVERTEX3FV (Controlpoints[i]);     }  glend ();}  void changesize (glsizei w, glsizei h) {  if  (h == 0)    {    h = 1;  }  glviewport (0,&NBSP;0,&NBSP;W,&NBSP;H);    //using orthogonal projection   glmatrixmode (gl_projection);   glloadidentiTy ();   gluortho2d ( -10.0f, 10.0f, -10.0f, 10.0f);   glmatrixmode (GL_MODELVIEW );   glloadidentity ();}  void renderscene () {  glclear (gl_color_buffer_bit);    //set Bezier curve, This function actually only needs to be called once, can be placed in the SETUPRC set     glmap1f (gl_map1_vertex_3, //generated data type     Upper bounds of the lower bound       100.0f, //u value of the   0.0f, //u value         3, //vertex in the data interval, x, y, z so the interval is 3        numofpoints, The order of the  //u direction, that is, the number of control points          &controlpoints[0][0] // Pointer to control point data  )    //must turn on    glenable (gl_map1_vertex_3) before drawing vertices;     //uses a line drawing to connect points    glbegin (Gl_line_strip);  for  (int i =  0; i <= 100; i++)   {    glevalcoord1f ((GLfloat) i); & NBsp; }  glend ();   drawpoints ();   glutswapbuffers ();} 

Call glmap1f in the Renderscene function to create a map for the curve. The first parameter is GL_MAP1_VERTEX3, and the setpoint is set to the ternary group (x, Y, z). You can also set the texture coordinates and color information to be generated. Refer to GlMap1. The following two parameters set the value range of U [0,100], the fourth parameter specifies the interval of the vertex in the array, since the vertex is composed of 3 floating-point numbers, the interval is 3. The fifth parameter specifies the number of control points, and the last parameter is the control point array. Then we need to enable the evaluation device, which is called as follows:

Glenable (GL_MAP1_VERTEX3);

The GLEVALCOORD1F function accepts a parameter as a parameter value for the curve. Call this function to calculate the vertex coordinate value by the evaluation function, and then call the Glvertex internally. Here you connect these vertices using the wired way:

Glbegin (Gl_line_strip);

for (i = 0; I <= 100; i++)

{

GLEVALCOORD1F ((glfloat) i);

}

Glend ();

Calculate curve

OpenGL also provides an easier way to accomplish the above tasks. We can use the Glmapgrid function to set up a grid to tell OpenGL to create a spatially symmetric grid with various points within the range of the U. We then call Glevalmesh and use the specified entity (gl_line or gl_points) to link each point.

We use the following two function calls

GLMAPGRID1F (0.0f, 100.0f); GLEVALMESH1 (gl_line, 0, 100);

You can replace the following code

Glbegin (Gl_line_strip);  for (int i = 0; I <=; i++) {glevalcoord1f ((glfloat) i); }glend ();

This is a more compact way to use.

3D surface

Creating a Bezier surface is similar to creating a Bezier curve. In addition to the definition field of U, the definition field of V is given. The following example creates a Bezier surface. Unlike before, we defined 3 sets of control points along the definition field of V. To keep the surface simple, these sets of control points are only different Z-values. The surface that is drawn in this way looks like an extension of the curve along the z axis.

Control Points   GLint nNumPoints = 3; glfloat ctrlpoints[3][3][3]= {{{  -4.0f, 0.0f, 4.0f},    {  -2.0f, 4.0f, 4.0f},    {  4.0f, 0.0f, 4.0f }},{{   -4.0f, 0.0f, 0.0f},    { -2.0f, 4.0f, 0.0f},     {  4.0f, 0.0f, 0.0f }},{{  -4.0f, 0.0f, -4.0f},     { -2.0f, 4.0f, -4.0f},    {  4.0f,  0.0f, -4.0f }}}; //Draw Control Point   void drawpoints (void) { int i,j;       glcolor3f (1.0f, 0.0f, 0.0f);  //zoom in a bit to see more clearly   glpointsize (5.0f );   glbegin (gl_points);    for (i = 0; i < nnumpoints;  i++)    for (j = 0; j < 3; j++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;GLVERTEX3FV (Ctrlpoints[i][j]);   glend ();}  void renderscene (void) { // clear the window with current  Clearing color   glclear (gl_color_buffer_bit | gl_depth_buffer_bit);    //  Save Model View Matrix      glmatrixmode (Gl_modelview);   glpushmatrix ();    //rotation of a certain angle to facilitate observation of &NBSP;&NBSP;&NBSP;&NBSP;GLROTATEF (45.0f, 0.0f, 1.0f, 0.0f);   glrotatef (60.0f, 1.0f, 0.0f, 0.0f);   glcolor3f (0.0f, 0.0f, 1.0f );  //sets the mapping method, which needs to be set only once and can be called in SETUPRC. &NBSP;&NBSP;&NBSP;&NBSP;GLMAP2F (gl_map2_vertex_3, //generated data type     0.0f, //  The upper bound of the Nether    10.0f, //u of U     3, //the interval     3 of the midpoint of the data, The upper bound of the lower     10.0f, //v of the order     0.0f, //v in  //u direction      9, //  the interval between the control points     3, // v the order     &ctrlpoints[0][0 in the direction ][0]);  //control Point Array    //enable the evaluation     glenable (GL_MAP2_VERTEX_3);    //maps a grid     glmapgrid2f (10,0.0f,10.0f,10,0.0f,10.0f) with 10 points from 0 to 10    / /  Compute grid &NBSP;&NBSP;&NBSP;&NBSP;GLEVALMESH2 (gl_line,0,10,0,10);    //Draw Control Point      drawpoints ();     glpopmatrix ();   glutswapbuffers ();}

Here we replace the previous glmap1f with GLMAP2F, which specifies the points on both the U and v two fields. In addition to specifying the upper and lower bounds of U, specify the upper and lower bounds of v. The distance of the point within the defined field is 9, because the 3-dimensional array is used, contains 3 U-values, and each U-value contains 3 points, 3x3=9. Then specify the order in the V direction, that is, how many points are in the V direction on each U branch. The last parameter is a pointer to a control point.

Then we set up the evaluation device.

Enable the evaluation device

Glenable (Gl_map2_vertex_3);
Maps a grid with 10 points from 0 to 10

GLMAPGRID2F (10,0.0f,10.0f,10,0.0f,10.0f);

Calculates the grid surface of a mesh, represented by a line.

Calculation grid
GLEVALMESH2 (gl_line,0,10,0,10);

Lighting and Normals

The evaluation can also help us to generate the normal of the surface, simply modify some code:

Put GlEvalMesh2 (gl_line, 0, 10, 0, 10), replace with GLEVALMESH2 (Gl_fill, 0, 10, 0, 10), and then call SETUPRC (glenable) in Gl_auto_normal at initialization time; You can get a surface that receives light.

OpenGL Super Treasure Notes-Bezier curves and surfaces (RPM)

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.