Construct a Free Curve and Surface Using OpenGL under VC

Source: Internet
Author: User

Summary: This article introduces the application of OpenGL in 3D object modeling, and builds a Free Curve and surface model with VC ++.

  Keywords: OpenGL; Free Curve and surface; OpenGL; Visual C ++

  Introduction

With the rapid development of computer technology, the cost-effectiveness of computers is getting higher and higher, laying a solid foundation for the development of computer graphics. Scientific computing visualization, computer animation, virtual reality and other technologies have been widely used in engineering applications. The realization of these technologies is inseparable from 3D modeling of real objects. In entity modeling, a small amount of control points and a variety of smooth splines and spline surfaces are used to reduce the computational workload and storage space, such as the B-sample) and so on. Free Curve and surface is a very important geometric definition of industrial products. It is widely used in computer aided design software. ISO (International Standardization Organization, International Standardization Organization), 1991) after the STEP standard is promulgated, since the standard clearly specifies that a free curve and surface must use a non-uniform rational B-spline (non_uniform rational B-spline) in engineering) in OpenGL (Open graphics library, open Graphics Library), a high-level programming interface based on the calculator is provided, therefore, in 3D object modeling, OpenGL is used to construct various complex Free Curve and surface types. This article will introduce the use of this programming interface provided by OpenGL under VC, and draw a Free Curve and surface through programming of this interface.

  OpenGL BASIC Program Framework

OpenGL 3D graphics standards are open standards jointly developed by at&t, IBM, Dec, sun, HP, Microsoft, SGI, and other companies based on Gl graphics library standards, its appearance has completely changed the past, and it was only dependent on the history of expensive graphics workstations and complex 3D graphics software for 3D graphics computer applications, this allows programmers to develop complex 3D images in C language on PCs. Because OpenGL is cross-platform, simple, efficient, and fully functional, it has become a de facto industrial standard in 3D graphics production. It not only provides functions for drawing basic shapes such as points, lines, and multilateral shapes, but also provides functions for drawing complex 3D curves and surfaces and 3D shapes. During geometric modeling, OpenGL uses vertices as the elements, and points form a line. Then, the line and its topology form a polygon surface, therefore, the modeling functions provided by OpenGL can be used to construct almost all 3D models.

The role of OpenGL is the client server mechanism. The client (an application that uses OpenGL to draw graphics) sends OpenGL commands to the server (OpenGL kernel, the server is responsible for interpreting and executing these orders. The OpenGL graphics library is encapsulated in the dynamic Connection Library opengl32.dll. When used, the call to OpenGL interface functions from the client application is first processed by opengl32.dll, then transmitted to the server and sent by winsrv. after the DLL is further processed, it is sent to Win32 DDI (device drivers interface, Device Driver Interface), and finally the processed graphic commands are sent to the video display driver for final display.

OpenGL's auxiliary library functions provide a NLE interface for drawing Free Curve and surface, so it is easy to use. Before using OpenGL, you must initialize it using the initialization code and define the storage format of pixels and other information:

Pixelformatdescriptor pixeldesc;
Pixeldesc. nsize = sizeof (pixelformatdescriptor); // PFD size
Pixeldesc. nversion = 1; // version number
Pixeldesc. dwflags = pfd_draw_to_window | // supported window
Pfd_support_opengl | // supports OpenGL
Pfd_doublebuffer; // supports dual-Cache
Pixeldesc. ipixeltype = pfd_type_rgba; // rgba type
Pixeldesc. ccolorbits = 32; // 32-bit color depth
Pixeldesc. credbits = 8; // color bits
Pixeldesc. credshift = 16;
Pixeldesc. cgreenbits = 8;
Pixeldesc. cgreenshift = 8;
Pixeldesc. cbluebits = 8;
Pixeldesc. cblueshift = 0;
Pixeldesc. calphabits = 0; // No Alpha Cache
Pixeldesc. calphashift = 0; // ignore the conversion bit
Pixeldesc. caccumbits = 64; // accumulated bits
Pixeldesc. caccumredbits = 16;
Pixeldesc. caccumgreenbits = 16;
Pixeldesc. caccumbluebits = 16;
Pixeldesc. caccumalphabits = 0;
Pixeldesc. cdepthbits = 32; // 32-bit deep Cache
Pixeldesc. cstencilbits = 0; // no template Cache
Pixeldesc. cauxbuffers = 0; // no secondary Cache
Pixeldesc. ilayertype = pfd_main_plane; // main floor
Pixeldesc. breserved = 0; // Reserved
Pixeldesc. dwlayermask = 0;
Pixeldesc. dwvisiblemask = 0;
Pixeldesc. dwdamagemask = 0;

This is the main content of initialization. The pixelformatdescriptor Structure Variable describing the pixel storage format is filled, and the rest of the work is mainly through the call of choosepixelformat () and setpixelformat () function to return and set the best matching pixel format. Finally, use wglcreatecontext () to create a rendering context RC and use it as a parameter to create a current Drawing description table through wglmakecurrent, and release it after drawing:

// Set the current Drawing description table for a thread
Wglmakecurrent (HDC, m_hglcontext );
// Draw a Free Curve and Surface
Donurbs ();
// Release the Drawing description table
Wglmakecurrent (HDC, null );
// Release RC (generally in the ondestory () Message response function of wm_destory)
Wgldeletecontext (m_hglcontext );

So far, the construction of a basic program framework based on OpenGL has been completed, and the construction of a Free Curve and surface has been completed in donurbs.
Drawing a Free Curve and Surface

One of the major advantages of OpenGL is that it only needs a few control points to accurately describe the smooth curves and surfaces of 3D entities, instead of fitting with a large number of basic elements. We are familiar with the expansion of B-spline, but we have introduced the Weight Factor and expressed its curve with rational fraction. Therefore, you only need to define the node sequence and control points in advance to use the OpenGL programming interface to draw a Free Curve and surface. This allows you to accurately describe the polynomials:

// Node sequence
Glfloat Notes [8] = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f };
// Define Control Points
For (INT I = 0; I <4; I ++)
{
For (Int J = 0; j <4; j ++)
{
Linchpinpt [I] [J] [0] = (glfloat) (3.0f * (i-0.5f ));
Linchpinpt [I] [J] [1] = (glfloat) (3.0f * (j-0.5f ));
If (0 <I & I <3 & 0 <J & J <3)
Linchpinpt [I] [J] [2] = 4.0f;
Else
Linchpinpt [I] [J] [2] =-4.0f;
}
}

It is relatively concise to use the OpenGL programming interface: first, create a non-zero-Curve Surface object and set its attributes as needed. After glubeginsurface () or glubegincurve () is called () then, you can draw the curve and Surface Based on the control points defined above. After the painting is completed, it ends with gluendsurface () or gluendcurve:

// Create a rectangle surface object
Nurb = glunewnurbsrenderer ();
// Set the attributes of a surface object in the form of a rectangle.
Glunurbsproperty (nurb, glu_sampling_tolerance, 30366f );
Glunurbsproperty (nurb, glu_display_mode, glu_fill );
// Start to draw the nurb range
Glubeginsurface (nurb );
// Define the shape of a green surface
Glunurbssurface (nurb, 8, notes, 8,
Notes, 4*3, 3,
& Linchpinpt [0] [0] [0],
4, 4, gl_map2_vertex_3 );
// Draw the ending Surface
Gluendsurface (nurb );
Glpopmatrix ();
// Force plot, no resident Cache
Glflush ();

Glunurbssurface () is a key function used to define the curve and surface of a rectangle. Its function prototype is defined as follows:

Void glunurbssurface (
Glunurbsobj * nobj, // you can call this operation to describe how to create a surface.
Glint sknot_count, // number of nodes in the U direction
Glfloat * sknot, // array pointer of the node in the U direction
Glint tknot_count, // number of nodes in the V direction
Glfloat * tknot, // array pointer to the V Node
Glint s_stride, // data span of the control point in the U direction
Glint t_stride, // data span of the control point in the V direction
Glfloat * ctlarray, // Control Point array pointer
Glint sorder, // polynomial order in the U direction
Glint torder, // polynomial order in the V direction
Glenum type // determine the valuer type
);

The glunurbscurve () function can also be used to define the curve and surface of a curve. The usage is similar to that of a curve and surface.

  Rendering of light and material

To make 3D modeling of entities more realistic, you need to render the constructed free-form curve and surface in light and material. OpenGL provides a series of library functions for creating illumination models, allowing you to easily create the required illumination models in 3D scenarios. The illumination model in OpenGL consists of ambient light, diffuse light, and specular light, you can also set a light attenuation factor to simulate the actual light source effect. The following code defines a yellow light source:

Glfloat light_position [] = {1.0f, 1.0f, 1.0f, 0.0f ,};
Glfloat light_diffuse [] = {1.0f, 1.0f, 0.0f, 1.0f ,};
Gllightfv (gl_light0, gl_position, light_position); // defines the position of the light source.
Gllightfv (gl_light0, gl_diffuse, light_diffuse); // defines the diffuse light of the light source.
Glable (gl_auto_normal); // automatically generates a surface method vector.
Glenable (gl_lighting); // starts the illumination model;
Glable (gl_light0); // valid for the gl_light0 Light Source

The glable (gl_auto_normal) function can automatically generate a surface method vector to process illumination. OpenGL provides gl_light0 ~ Gl_light7: eight light sources. Here, gl_light0 is used by glable. Although the definition of the light source can increase the degree of authenticity, the definition of the material of the 3D object can greatly increase the degree of realism of the surface drawn by the process. All materials referred to in OpenGL refer to the reflectivity of the three primary colors R, G, and B in the illumination model. Similar to the definition of the light source, the definition of the material is divided into components such as environment, diffuse, and mirror reflection. In addition, there are also the mirror highlight index, radiation composition, and so on:

// Set the reflection component of the material.
Glfloat mat_ambient [] = {0.8f, 0.8f, 0.8f, 1.0f };
Glfloat mat_diffuse [] = {0.8f, 0.0f, 0.0f, 1.0f}; // purple
Glfloat mat_specular [] = {1.0f, 0.0f, 1.0f, 1.0f}; // High Brightness purple
Glfloat mat_shininess [] = {100366f}; // highlight Index
Glmaterialfv (gl_front, gl_ambient, mat_ambient); // defines the ambient light reflectivity.
Glmaterialfv (gl_front, gl_diffuse, mat_diffuse); // defines the reflectivity of diffuse light.
Glmaterialfv (gl_front, gl_specular, mat_specular); // defines the reflectivity of the mirror.
Glmaterialfv (gl_front, gl_shininess, mat_shininess); // defines the highlight index.

The color of the material in OpenGL is different from that of the light source in the illumination model. For the definition in the light source, R, G, and B respectively indicate the ratio of the three primary colors in the light source. For the definition of the material, r, G, and B indicate the reflectivity of objects with this material attribute to the primary color. The color displayed by the objects in the scene is the result of the combination of illumination model and material definition.

  Conclusion

This article draws a Free Curve and surface under VC ++ through the OpenGL programming interface provided by the auxiliary library, and introduces the illumination and material rendering methods of the surface. Through understanding this article, we can use OpenGL to draw a Free Curve and surface to better model 3d entities, and further feel the nuances of OpenGL in the application of 3D graphics computers.

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.