Tag Archives: graphics

OpenGL basic graphics programming – OpenGL and 3D graphics world

We live in a three-dimensional world full of three-dimensional objects. In order for a computer to reproduce these objects accurately, we must be able to depict these objects in three-dimensional space. We live in a world full of information, and the ability to understand and use this information as quickly as possible will have a direct impact on the success or failure of our career, so we need to express this information in the most direct form.

In recent years, the development of computer graphics has enabled the formation of 3D presentation technology. These 3D presentation technologies enable us to reproduce objects in the 3D world and express complex information with 3D shapes. This technology is visualization (
Visualization) technology. Visualization technology enables people to operate information with shapes directly in the world of three-dimensional graphics and communicate with computers directly. This kind of technology has unified the power of man and machine in an intuitive and natural way, and this revolutionary change will undoubtedly greatly improve people’s work efficiency. Visualization technology gives people a simulation, three-dimensional and real-time interaction ability, so that people can use previously unimaginable means in the three-dimensional graphic world to obtain information or play their creative thinking. The mechanical engineer can be liberated from the two-dimensional plan directly into the three-dimensional world, so that he can quickly get the three-dimensional model of the mechanical parts he has designed. A doctor can analyze a patient’s lesions from a three-dimensional scan. Military commanders can direct realistic 3D planes, warships and tanks to advance to targets and analyze the effect of combat plans in the face of battlefield terrain generated by 3D graphics technology.

Even more amazing is the development of virtual reality technology, which allows people to enter a three-dimensional, multimedia virtual world, people can visit ancient castles, can roam vast space. All these depend on the development of computer graphics and computer visualization technology. People’s research on computer visualization technology has gone through a long process, and many visualization tools have been formed, among which the GL 3D graphics library launched by SGI has outstanding performance, easy to use and powerful functions. The 3D application software developed by GL is quite popular with many professional and technical personnel, and these 3D application software have been involved in the fields of architecture, product design, medicine, earth science, fluid mechanics and so on. With the continue development of computer technology, GL has been further developed into OpenGL, OpenGL has been considered a high-performance graphics and interactive visual processing standards, currently including ATT company UNIX software lab, DEC, SUN, IBM, HP, Microsoft, SGI company and several in the computer market of leading large companies have adopted the standard OpenGL graphics.

it is worth mentioning that because Microsoft company provide OpenGL graphics standard in Windows NT, OpenGL will be widely used in computer, especially the OpenGL 3 d graphics accelerator card and computer graphics workstations, people can be implemented on microcomputer 3 d graphics applications, such as CAD design, simulation, 3 d games and so on, so as to have a better chance, make it easier to use OpenGL and application software to build their own 3 d graphics in the world.

1.2. OpenGL provides an intuitive 3D graphics development environment

OpenGL is actually a graphics and hardware interface. It includes 120 graphical functions that developers can use to create 3D models and 3D real-time interactions. Unlike other graphical programming interfaces, OpenGL provides very clear graphics functions, so novice programmers can take advantage of OpenGL’s graphics processing power and a color palette of 16.7 million colors to quickly design three-dimensional graphics and three-dimensional interactive software.

OpenGL’s powerful graphics functions do not require developers to write 3D object model data in a fixed data format, so developers can not only use their own data directly, but also can use other data sources in different formats. This flexibility greatly saves developers time and improves software development efficiency.

for a long time, the technical personnel engaged in the development of 3 d graphics have to write in their own applications matrix transformation, the external device access and other functions, such as the modulation is not very close relations with their own software development target function of bother, and OpenGL is to provide an intuitive programming environment, it provides a series of function is greatly simplify the 3 d graphics program. Such as:

> OpenGL provides a series of 3D graphics units for developers to call.
OpenGL provides a series of graph transformation functions. OpenGL provides a series of external device access functions, so that developers can easily access the mouse, keyboard, space ball, data gloves, such as this intuitive 3D graphics development environment reflects the OpenGL technical advantages, which is also the reason why many 3D graphics developers are keen on OpenGL.

In the early days of the development of computer, people began to engage in the development of computer graphics. Until the computer hardware and software and computer graphics highly developed in the 1990s, people found that complex data in the form of visual representation is the most easy to understand, so the rapid development of three-dimensional graphics, so a variety of three-dimensional graphics tool software package has been launched, such as Phigs, Pex, RenderMan. Some of these 3D graphics tool packages focus on ease of use, others on rendering effects or connection to application software, but none of them can match OpenGL in interactive 3D graphics modeling capabilities, external device management, and programming ease.
OpenGL after further development of GL, achieve 2D and 3D advanced graphics technology, in performance is extremely superior, it includes modeling, transformation, light processing, color processing, animation and more advanced capabilities, such as texture mapping, object motion blur, etc.. These capabilities of OpenGL provide excellent software tools for achieving realistic 3D renderings and building interactive 3D landscapes. OpenGL is independent of each other in terms of hardware, window and operating system.
Many computer companies have integrated OpenGL into a variety of Windows and operating systems, which operating systems include UNIX, Windows NT, DOS, etc. Window systems have X Windows, Windows, etc. In order to realize a fully functional graphics processing system, a system structure related to OpenGL is designed as follows: the bottom layer is the graphics hardware, the second layer is the operating system, the third layer is the window system, the fourth layer is OpenGL, the fifth layer is the application software. OpenGL is network transparent, allowing both local and remote drawing in a client-server architecture. So in the network system, OpenGL in the X window, Windows or other window system can appear as a separate graphics window.
OpenGL as a superior performance graphics application design interface (API) and suitable for a wide range of computing environments, from personal computers to workstations and supercomputers, OpenGL can achieve high performance of three-dimensional graphics functions. As many leading computer companies in the computer industry have adopted OpenGL as a 3D graphical application design interface, OpenGL applications have a wide range of portability. Therefore, OpenGL has become the current 3D graphics development standards, is engaged in 3D graphics development of technical personnel must master the development tools.

OpenGL learning — problems encountered in compiling OpenGL program for the first time

The first time you compile and run an OpenGL program, you run into a bug that makes you crazy.
It looks like this:

A lot of search on the Internet, found that this is the first time to compile OpenGL program will meet the error. The solution is as simple as the #include< gl/glut.h> Preface it with a code like this:

Also note that the order of the code must be in #include< gl/glut.h> Before, can not error, otherwise still can not pass.

Learning notes — opengl01

 
The first lesson
1.1 Simple OpenGL program contains:
First, you need to include the header file #include <; GL/glut.h> So this is the header file for GLUT. OpenGL programs typically include <; GL/gl.h> And & lt; GL/glu.h> , but both files are automatically included in the GLUT header file and do not need to be included again.   The functions that begin with GLUT are all functions provided by the GLUT toolkit. Here are some of the functions used: Glutinit initializes GLUT. This function must be called once before another GLUT is used. Glutinit (& Arg C, Arg V. 2, GLUTINITDISPLAYMODE, set the display mode, where GLUT_RGB means to use the RGB color, and GLUT_INDEX (means to use the index color). GLUT_single means to use a single buffer, as does GLUT_DOUBLE (to use a double buffer). GlutinitWindowPosition, which is simple, sets the position of the window on the screen. 4, GlutinitWindowSize, this is also easy, set the window size. 5, GlutCreateWindow, create a window based on the information set above. Parameters will be used as the title of the window. Note: The window is not immediately displayed on the screen after it is created. You need to call glutMainLoop to see the window. 6. GlutdisplayFunc, which sets a function that will be called when a drawing is needed. (This statement is not accurate, but the accurate statement may not be easy for beginners to understand, so say for the time being). 7. GlutMainLoop, which runs a message loop. (This may not be obvious to beginners, but for now it is enough to know that this function displays a window and waits for the window to close before returning.)   All the functions that start with GL are standard OpenGL functions, and the ones that are used are described below. 1, Glclear. GL_Color_BUFFER_BIT clears the color, and glClear clears other things, but I won't cover them here. 2, Glrectf, draw a rectangle. The four parameters represent the horizontal and vertical coordinates of the two points located on the diagonal. Glflush ensures that previous OpenGL commands are executed immediately (rather than having them wait in the buffer). It works similarly to fflush(stdout).   The second lesson 2.1 Draw simple geometric shapes Dots: Dots in OpenGL will be drawn as individual pixels Straight lines: OpenGL's concept of a "line" is close to the mathematical concept of a "line segment," which can be defined by two endpoints. Polygon: A polygon must be a "convex polygon" (defined as: any two points in a polygon are within the polygon, from which it can also be deduced that a convex polygon cannot be hollow), usually triangular By using points, lines and polygons, you can combine various geometric figures   2.2 Specify vertices OpenGL provides a number of functions. They all start with glVertex, followed by a number and one or two letters. Such as: GLVERTEX2D GLVERTEX2F GLVERTEX3F GLVERTEX3FV and so on. The number means the number of arguments, 2 means there are two arguments, 3 means three, and 4 means four. Letters parameter type, s 16-bit integers (due to the type defined as GLshort OpenGL), I said a 32-bit integer (OpenGL in this type is defined as the GLint and GLsizei), f is a 32-bit floating point Numbers (due to the type defined as GLfloat OpenGL and GLclampf), d said 64 floating point Numbers (due to the type defined as GLdouble OpenGL and GLclampd). V indicates that several parameters will be passed using Pointers   2.3 Start drawing OpenGL requires that the command specifying the vertex be included after glBegin and before glEnd (otherwise the specified vertex will be ignored). GlBegin is left to indicate how to use these points. Such as: glBegin(GL_POINTS); GlVertex2f (0.0 0.0 f, f); GlVertex2f (0.0 0.5 f, f); glEnd(); Then these two points will be drawn separately. If you replace GL_POINTS with GL_LINES, then the two points will be considered to be the two endpoints of the line, and OpenGL will draw a line. You can also specify more vertices and then draw more complex shapes. GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, etc. (See Figure 1) The third class 3.1 about the point Point size: Default value is 1.0F, function prototype void glpointSize (GLFloat Size); Example: void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); GlPointSize (5.0 f); GlBegin (GL_POINTS); ... GlEnd (); glFlush(); } 3.2 About Straight Lines 3.2.1 Line width Function prototype void gllineWidth (glFloat Width); The usage is similar to glpointSize. 3.2.2 dotted line First, use glEnable(GL_LINE_STIPPLE); To start the dotted line mode (use glDisable(GL_LINE_STIPPLE) to turn it off). Then, use glLineStipple to set the style of the dotted line. void glLineStipple(GLint factor, GLushort pattern); Pattern is a sequence of length 16 composed of 1 and 0. Viewed from the lowest position, if it is 1, then factor points that should be drawn next on the line will be drawn as solid. If 0, then factor points that should be drawn next on the line will be drawn as virtual. 3.3 About polygons 3.3.1 Both sides of polygons and their drawing methods Although we haven't really used 3D coordinates to draw a picture yet, it is necessary to establish some 3D concepts. In three dimensions, a polygon has two faces. Each face can be drawn in a different way: fill, draw only edges, and draw only vertices, with fill being the default. You can set the two faces in different ways. glPolygonMode(GL_FRONT, GL_FILL); // Set front to fill mode glPolygonMode(GL_BACK, GL_LINE); GlPolygonMode (GL_FRONT_AND_BACK, GL_POINT); glPolygonMode(gl_front, GL_POINT); // Set both sides to vertices 3.3.2 rainfall distribution on 10-12 inversion The convention is that the face whose vertices appear counterclockwise on the screen is "heads" and the other face is "tails". Common surfaces in life can usually be represented with such "heads" and "tails" as "reasonable". But there are some surfaces that are special. For example, a "Maibius strip" can be represented with either all "heads" or all "tails". You can exchange the concepts of "heads" and "tails" through the glFrontFace function. glFrontFace(GL_CCW); // Set the CCW direction to "Front", CCW is Counterclockwise Glfrontface (GL_CW); // Set the CW direction to "Front", CW is ClockWise * There is routine openGL4 3.3.3 Remove polygonal surfaces In three dimensional space, a polygon has two faces, but we can't see the polygons on the back, and some of the polygons are on the front, but are obscured by other polygons. Treating invisible polygons the same as visible polygons will definitely reduce the efficiency of our graphics processing. At times like this, you can cut out unnecessary surfaces. First, use glEnable(GL_CULL_FACE); To enable culling (disable using glDisable(GL_CULL_FACE)), then use glCullFace to cull. The parameter of glCullFace can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK, which are polygons that remove the front, back, and back sides, respectively. Note: the cull function only affects polygons, not points and lines. For example, with glCullFace(GL_FRONT_AND_BACK), all polygons are removed so that only points and lines are visible. 3.3.4 Hollow out polygon Straight lines can be drawn as dotted lines, while polygons can be hollowed out. First, use glEnable(GL_POLYGON_STIPPLE); To start the hollowout mode (which can be turned off with glDisable(GL_POLYGON_STIPPLE)). Then, use glPolygonStipple to set the hollow out style. void glPolygonStipple(const GLubyte *mask); The mask parameter points to a space of 128 bytes, which indicates how a 32 by 32 rectangle should be hollow out. Where: The first byte indicates whether the 8 pixels at the bottom left are hollow (1 means no hollow, showing the pixel; 0 indicates hollowout, showing the color behind it), and the last byte indicates whether the top right eight pixels are hollowout or not. If such data is saved as a picture and edited with a special tool, it is obviously much more convenient. Here's how to do it. First, create a new image using the Windows Brush program and name it mask.bmp. Make sure to save it with "Monochrome Bitmap" selected. In "Image" -> In the Properties dialog box, set the height and width of the image to 32. Look at the picture with a magnifying glass and edit it. Black corresponds to binary zero (hollow out), white corresponds to binary one (no hollow out), after editing saved. You can then use the following code to get the Mask array.
static GLubyte Mask[128];
FILE *fp; fp = fopen(“mask.bmp”, “rb”);
if( ! fp ) exit(0);
// Move the file pointer to this position so that reading sizeof(Mask) will encounter the end of the file
// Note that -(int)sizeof(Mask) is not a good way to write it, but it does work here, right
If (fseek(fp, -(int)sizeof(Mask), SEEK_END) exit(0); if(fseek(fp, -(int)sizeof(Mask), SEEK_END) exit(0);
// Read the sizeof(Mask) bytes into the Mask
if( ! fread(Mask, sizeof(Mask), 1, fp) )
exit(0);
fclose(fp);
 
Edit an image as a mask, and use the above method to get the array of masks. After running, observe the effect. Note: The factor factor can be set when drawing a dashed line, but the factor factor cannot be set when drawing a hollow polygon. Please use the mouse to change the size of the window, observe the change of the hollowout effect.
* There is routine openGL5

How to use higher version of OpenGL SDK in windows?

 
 
Now OpenGL has version 3.2, Visual C ++ 2005 comes with OpenGL SDK is version 1.1, want to use the advanced version of OpenGL, go to the OpenGL official website, but that only defines the API standard, there is no implementation, I searched the Internet OpenGL2.0 SDK OpenGL3.0 SDK, but did not find the download address, finally found that is to use the extension library way.
 
Download glew (The OpenGL Extension Wrangler Library), http://glew.sourceforge.net/index.html
 
What’s inside:

├ ─ bin
│ glew32. DLL
│ glewinfo. Exe opengl support is used to view the current system of the graphics card
│ glewinfo. TXT
│ VisualInfo.exe is also used to view some graphics card situation
│ visualinfo. TXT

├ ─ the include
│ └ ─ GL
│ glew. H
│ glxew. H
│ wglew. H

└ ─ lib
Glew32. Lib
Glew32s. Lib
 
 
usage
 
 
1. First refer to the header file
#include < gl/glew.h> // must be put first
#include < gl/glut.h>
 
2. The initialization
// Glew is initialized after glutCreateWindow
GLenum err = glewInit ();
If (GLEW_OK! = err)
{
/* Problem: glewInit failed, something is seriously wrong. */
Fprintf (stderr, “Error: % s/n,” glewGetErrorString (err));
}
Fprintf (stdout, “Status: Using GLEW %s/n”, glewGetString(GLEW_VERSION));
 
 
Note: If you use a graphics card that does not support the advanced version of OpenGL functions, the operation will crash
 
 

Matlab draw logarithmic coordinates!

In many engineering problems, some characteristics of data can be seen more clearly through logarithmic transformation of data. The curve of data points depicted in logarithmic coordinate system can directly represent logarithmic transformation.
There are two kinds of logarithmic transformation: double logarithmic transformation and single axis logarithmic transformation. Loglog function can be used to achieve the double log coordinate transformation, semilogx and Semilogy functions can be used to achieve the single axis log coordinate transformation.
Loglog (Y) means that the x and Y coordinates are logarithmic coordinates
Semilogx (Y) means that the x axis is a logarithmic coordinate system
Semilogy (…). That means that the Y-axis is a logarithmic coordinate system
So plotyy has two y axes, one on the left and one on the right
Example 1: Create a simple loglog with the square tag.
Solution: Enter a command
X = logspace (1, 2);
loglog(x,exp(x),’-s’)
Grid on % annotated grid
The produced figure is:

Example 2: Create a simple semi-logarithmic graph.
Solve input command:
x=0:.1:10;
semilogy(x,10.^x)
The produced figure is:

example 3: draw function graph, logarithmic graph and semilogarithmic graph of y=x^3.
Solution: Enter in the window:
x=[1:1:100];
Subplot (2,3,1);
plot(x,x.^3);
grid on;
title ‘plot-y=x^3’;
 
Subplot (2, 31);
loglog(x,x.^3);
grid on;
title ‘loglog-logy=3logx’;
 
Subplot (2 filling);
plotyy(x,x.^3,x,x);
grid on;
title ‘plotyy-y=x^3,logy=3logx’;
 
Subplot (2, 4);
semilogx(x,x.^3);
grid on;
title ‘semilogx-y=3logx’;
 
Subplot (2,3,5);
semilogy(x,x.^3);
grid on;
title ‘semilogy-logy=x^3’;
The produced figure is: