Document directory
- 3.1 One-dimensional valuer
- 3.2 One-dimensional uniform Calculator
- 3.3 two-dimensional Calculator
- 4.1 select rendering Properties
- 4.2 create and draw a curve and Surface
By yurunsun@gmail.com
Sina Weibo @ sun yurun Sina Blog
Csdn blog
Date: 2013-6-13
Content of this chapter:
- Concave Polygon partitioning
- Quadratic equation Surface
- Calculator
- OpenGL
1. Concave Polygon partitioning
When OpenGL is used to display a concave polygon, it must be first decomposed into a simple convex polygon. This division method is called polygon partitioning (tesselation, returns a combination of triangles, triangle networks, triangle fans, and straight lines.
Create a new lattice object
GLUtesselator* gluNewTess(void);
Register callback Functions
void gluTessCallback(GLUtesselator *tessobj, GLenum type, void (*fn)());
Specify the lattice attribute, such as surround rules to determine which areas are filled and which areas are not colored.
void gluTessProperty(GLUtesselator *tessobj, GLenum property, GLdouble value);void gluTessNormal(GLUtesselator *tessobj, GLdouble x, GLdouble y, GLdouble z);
Specify the contour lines of one or more closed polygon to render the divided polygon. It is best to put the divided Polygon into the display list.
void gluTessBeginPolygon(GLUtesselator *tessobj, void *user_data);void gluNextContour(GLUtriangulatorObj *tessobj, GLenum type);void gluTessEndPolygon(GLUtesselator *tessobj);void gluTessBeginContour(GLUtesselator *tessobj);void gluNextContour(GLUtriangulatorObj *tessobj, GLenum type);void gluTessEndContour(GLUtesselator *tessobj);void gluTessVertex(GLUtesselator *tessobj, GLdouble coords[3], void *vertex_data);
Continue to use this lattice object or delete it
void gluDeleteTess(GLUtesselator *tessobj);
2. quadratic equation Surface
Follow these steps to create and render the surface of a quadratic equation using Glu:
gluNewQuadric()
Function creates a quadratic equation object
- Specify the properties of the quadratic equation object
gluQuadricOrientation()
The function controls the surround direction and distinguishes internal and external regions.
gluQuadricDrawStyle()
Function Selection renders objects into points, straight lines, and filled Polygon
gluQuadricNormal()
The function specifies a normal for each vertex or plane.
gluQuadricTexture()
Texture coordinates generated by the function
gluQuadricCallback()
Register an error handler for a function.
Call the surface rendering function of the corresponding quadratic equation as needed:
gluSphere();gluCylinder();gluDisk();gluPartialDisk();
gluDeleteQuadric()
Destroy quadratic equation objects
3. estimator 3.1 One-dimensional Calculator
GLfloat ctrlpoints[4][3] = {{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0}, {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};void init(void) { glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); glEnable(GL_MAP1_VERTEX_3);}void display(void) { glBegin(GL_LINE_STRIP); for (int i = 0; i <= 30; i++) glEvalCoord1f((GLfloat) i/30.0); glEnd();}
Use the following function to define a one-dimensional calculator:
void glMap1f(GLenum target, float u1, float u2, GLint stride, GLint order, const float *points);GL_MAP1_VERTEX_3 //x, y, zvertex coordinatesGL_MAP1_VERTEX_4 //x, y, z, wvertex coordinatesGL_MAP1_INDEX //color indexGL_MAP1_COLOR_4 //R, G, B, AGL_MAP1_NORMAL //normal coordinatesGL_MAP1_TEXTURE_COORD_1 //stexture coordinatesGL_MAP1_TEXTURE_COORD_2 //s, ttexture coordinatesGL_MAP1_TEXTURE_COORD_3 //s, t, rtexture coordinatesGL_MAP1_TEXTURE_COORD_4 //s, t, r, qtexture coordinates
- Target: GlMap1Vertex_3 specifies the three-dimensional control points provided by the application and the three-dimensional vertices to be generated.
- U1: 0.0 indicates the low value of the parameter U
- U2: 1.0 indicates the high value of the U parameter.
- Stride: 3 indicates how many floating point values should be promoted from a control point to the next Control Point
- Order: 4 indicates the order of the spline.
- Points: & ctrlpoints [0] [0] indicates a pointer to the data at the first control point.
The following function is used to calculate an enabled calculator:
void glEvalCoord1{fd}(TYPE u);void glEvalCoord1{fd}v(const TYPE *u);
You can use multiple evaluators for computing at a time. That is, if you have defined and enabledGL_MAP1_VERTEX_3
And oneGL_MAP1_COLOR_4
You can call the 'glevalcoord1 () function to generate a position and a color at the same time.
3.2 One-dimensional uniform Calculator
In the above example, the courseware uses I/30, even if the U value is evenly distributed. The more convenient method is to callglMapGrid1f()
The function defines a one-dimensional grid and then usesglEvalMesh1()
Function Application Grid.
void glMapGrid1{fd}(GLint n, TYPE u1, TYPE u2);void glEvalMesh1(GLenum mode, GLint p1, GLint p2);
The second function is equivalent
glBegin(GL_POINTS); for (i = p1; i <= p2; i++) glEvalCoord1(u1 + i*(u2-u1)/n);glEnd();
3.3 two-dimensional Calculator
The difference from the one-dimensional calculator is that two Parameters u and v need to be considered.
void glMap2{fd}(GLenum target, TYPE u1, TYPE u2, GLint ustride, GLint uorder,TYPE v1, TYPE v2, GLint vstride, GLint vorder, TYPE points);void glEvalCoord2{fd}(TYPE u, TYPE v);void glEvalCoord2{fd}v(const TYPE *values);
There is also a two-dimensional even valuer:
void glMapGrid2{fd}(GLint nu,TYPE u1, TYPE u2, GLint nv,TYPE v1, TYPE v2);void glEvalMesh2(GLenummode, GLint i1, GLint i2, GLint j1, GLint j2);
4. API
Based on the functions of the OpenGL valuer, the Glu provides the interfaces for the use of NLE. The procedure is as follows:
glEnable(GL_AUTO_NORMAL)
Automatically generate the normal (you can also calculate it yourself)
gluNewNurbsRENDERER()
Create a pointer to a green chart object
gluNurbsProperty()
Select rendering Properties
gluNurbsCallback()
Error callback function Registration
gluBeginCurve()/gluBeginSurface()
Declare to start plotting a curve and Surface
gluNurbsCurve()/gluNurbsSurface()
Generate and render curve and Surface
gluEndCurve()/gluEndSurface()
Declare end to draw a curve and Surface
The following describes several steps:
4.1 select rendering Properties
Struct (glunurbsobj * nobj, glenum property, glfloat value); glu_display_mode, // glu_fill, delimiter, delimiter, // delimiter, delimiter, // delimiter, // glu_path_lenth, glu_parametric_error, dependencies, glu_parametric_tolerance, glu_u_step, glu_v_step, glu_auto_load_matrix // do you need to download the projection matrix, modulo matrix, and viewport from the OpenGL server? Otherwise, use void gluloadsamplingmatrices, const glfloat modelmatrix [16], const glfloat projmatrix [16], const glint viewport [4]);
4.2 create and draw a curve and Surface
void gluNurbsSurface(GLUnurbsObj *nobj, GLint uknot_count, GLfloat *uknot, GLint vknot_count, GLfloat *vknot, GLint u_stride, GLint v_stride, GLfloat *ctlarray, GLint uorder, GLint vorder, GLenum type);gluBeginSurface(nobj); gluNurbsSurface(nobj, ..., GL_MAP2_TEXTURE_COORD_2); gluNurbsSurface(nobj, ..., GL_MAP2_NORMAL); gluNurbsSurface(nobj, ..., GL_MAP2_VERTEX_3);gluEndSurface(nobj);