abstract
Open general ledger status
High function
Introduction to OpenGL programming
The difference between OpenGL and DirectX
abstract
Opengl-the industry standard of high performance graphics algorithm
OpenGL? It is the most widely accepted 2D/3D graphics API in the industry. Since its birth, it has spawned thousands of excellent applications on various computer platforms and devices. OpenGL? It is independent of Windows operating system or other operating systems, and the network is transparent. In the fields including CAD, content creation, energy, entertainment, game development, manufacturing, pharmaceutical industry and virtual reality, OpenGL? Help programmers to develop graphics processing software with high performance, great impact and high visual expression on PC, workstation, supercomputer and other hardware devices.
OpenGL (Full Write Open Graphics Library) is a specification that defines cross-programming languages and cross-platform programming interfaces. Used for three-dimensional images (two-dimensional is also possible). OpenGL is a professional graphic programming interface and a powerful and convenient underlying graphic library. OpenGL's predecessor is IRIS GL developed by SGI company for its graphics workstation. IRIS GL is an industrial standard 3D graphics software interface, which is powerful but not portable, so SGI developed OpenGL based on IRIS GL. The English full name of OpenGL is "open graphics library", and as the name implies, OpenGL is "open graphics program interface". Although DirectX leads the home market in an all-round way, OpenGL is an irreplaceable protagonist in the field of professional high-end drawing.
OpenGL is a hardware-independent software interface, which can be transplanted between different platforms such as Windows 95, Windows NT, Unix, Linux, MacOS and OS/2. Therefore, the software supporting OpenGL has good portability and can be widely used. Because OpenGL is the bottom graphics library of graphics and does not provide geometric entity primitives, it cannot be directly used to describe the scene. The DXF and 3DS model files made by AutoCAD, 3DS/3DSMAX and other three-dimensional graphic design software can be easily converted into OpenGL vertex arrays through some conversion programs.
On the basis of OpenGL, there are many advanced graphics libraries, such as Open Inventor, Cosmo3D and Optimizer, which are suitable for different applications. Among them, Open Inventor is the most widely used. The software is an object-oriented toolkit based on OpenGL, which provides objects and methods for creating interactive 3D graphics applications, predefined objects and event processing modules for interaction, advanced application units for creating and editing 3D scenes, and the ability to print objects and exchange data with other graphics formats.
The development of OpenGL has been in a relatively slow situation. Every time the version is improved, few new technologies are added, and most of them are just modifications and improvements. 1In July, 1992, SGI released version 1.0 of OpenGL, and then jointly developed the Windows NT version of OpenGL with Microsoft, so that some large 3D graphics processing software that had to run on top graphics workstations could also be used on microcomputers. 1995 OpenGL version 1. 1 went on the market. Compared with 1.0, it has many improvements and added some new functions. These include improving printer support, including enhancing the call of OpenGL in metafile, new features of vertex array, improving the transmission speed of vertex position, normal, color, color index, texture coordinates and polygon edge recognition, and introducing new texture features. OpenGL 1.5 adds "OpenGL coloring language", which is the core of "OpenGL 2.0" and is used to expand the functions of object coloring, vertex coloring and segment coloring technology.
The main maker of OpenGL 2.0 standard is not SGI, but 3DLabs, which gradually takes the initiative in ARB. The first thing that Version 2.0 should do is to be fully compatible with the old version, and at the same time, it should cooperate with DirectX*** in vertex, pixel and memory management to maintain the balance of power. OpenGL 2.0 will consist of the existing functions of OpenGL 1.3 plus new functions that are completely compatible with it (as shown in figure 1). In this way, all kinds of tangled extended instruction sets introduced by various schools in the ARB stagnation era can be completely simplified. In addition, the realization of hardware programmability also provides a better way to integrate existing extended instructions.
At present, with the continuous development and improvement of DirectX, the advantages of OpenGL are gradually lost. Up to now, although the 2.0 version advocated by 3Dlabs has been released, and many designs similar to the programmable units in DirectX have been added, the users' cognitive level of the manufacturers is not high, and they are confused about the future development prospects of OpenGL.
[Edit this paragraph] Open the general ledger status.
Open GL is still the only API that can replace Microsoft's complete control over 3D graphics technology. It still has some vitality, but Silicon Map no longer promotes Open GL in any way that makes Microsoft unhappy, so it has high risks. Game developers are a group with independent thoughts, and many important developers still use Open GL. Therefore, hardware developers are trying to strengthen their support for it. Direct3D can't support high-end graphics devices and professional applications at present; Open GL is dominant in these areas. Finally, the open source community (especially the Mesa project) has been committed to providing Open GL support for any type of computer, whether they use Microsoft's operating system or not.
OpenGL3.0 version 3.0 was officially released in 2008. And with the support of nv, its official website provided the sdk download of N card.
[Edit this paragraph] Advanced functions
OpenGL is designed to output only, so it only provides rendering function. The core API has no concept of window system, audio, printing, keyboard/mouse or other input devices. Although this may seem like a restriction at first, it allows rendering code to be completely independent of the operating system it runs, thus allowing cross-platform development. However, something integrated into the native window system needs to be allowed to interact with the host system. This is achieved through the following additional APIs:
* GLX-X 1 1 (including transparent network)
* WGL-Microsoft Windows System
* AGL-Apple MacOS
In addition, the GLUT library can provide basic window functions in a portable way.
[Edit this paragraph] Introduction to ]OpenGL programming
OpenGL drawing is very convenient, so it is becoming more and more popular. But for many people, it is done on a microcomputer. The first problem we encounter is how to adapt to the microcomputer environment. This is usually the most critical step, although it is also the most basic step. Generally speaking, I don't recommend using the glut package. It is difficult to give full play to the function of windows interface.
The following describes how to program OpenGL on VC++. The general process of OpenGL drawing can be seen as follows: firstly, draw a picture in the OpenGL drawing environment RenderContext (RC) with OpenGL statements, and then transfer the picture to the drawing environment DeviceContext (DC) of the operating system through an exchange buffer process, and actually draw it on the screen.
Taking drawing Bezier curve as an example, the method of OpenGL programming in VC++ environment is introduced in detail. This paper gives detailed notes to give beginners a clear guide. Follow the instructions step by step, and you will successfully draw the first batch of graphics on the OpenGL platform.
First, the program framework Test.dsw is generated.
New Project | MFC Application Wizard (EXE) | Test | OK
* Note *: The "plus sign" refers to the manually typed string.
Second, import the Bezier curve file.
Use the following methods to generate two files: beziercurve.hbeiziercove.cpp:
Workspace | Class View | Test Class |< Right-click to pop up > New Class | Generic Class (without MFC class) | "CBezierCurve" | OK
Third, edit the definition and implementation of Bezier curve class.
Write the following two documents:
BezierCurve.h BezierCurve.cpp
Fourth, set the compilation environment:
1. In BezierCurve.h and TestView.h, add the following:
# include & ltGL/GL . h & gt;
# include & ltGL/Glu . h & gt;
# include & ltGL/glaux . h & gt;
2. In an integrated environment
Project | Settings | Links | Object/Library Module | "opengl32.libglu32.libglaux.lib" | OK
Verb (abbreviation of verb) sets the OpenGL working environment: (the following operation is aimed at TestView.cpp)
1. Process PreCreateWindow (): Set the style of OpenGL drawing window.
CS . style | = WS _ clip siblings | WS _ clip children | CS _ own DC;
2.Process OnCreate (): Create a drawing device for OpenGL.
The mechanism of OpenGL drawing is: firstly, draw with the drawing context of OpenGL (RC for short), and then pass the drawing result to the drawing context of Window (DC for short) through the SwapBuffer () function. It should be noted that there can be multiple DC's during the program running, but there is only one RC. Therefore, when DC finishes drawing, it needs to release RC immediately so that other DCs can use it. In the following code, there will be detailed comments.
int ctest view::OnCreate(LPCREATESTRUCT LPCREATESTRUCT)
{
if(CView::OnCreate(lpCreateStruct)= =- 1)
return- 1;
myInitOpenGL();
Returns 0;
}
void CTestView::myInitOpenGL()
{
m _ pDC = new CClientDC(this); //create DC
Assertion (m_pDC! = NULL);
If (! MySetupixElFormat())// Sets the bitmap format of the drawing. These functions are listed below.
Return;
m _ hRC = wglCreateContext(m _ pDC-& gt; m _ hDC); //create RC
wglMakeCurrent(m _ pDC-& gt; m_hDC,m _ hRC); //RC is associated with the current DC
}//cc lient * m _ pDC; HGLRC m _ hRC is a member variable of CTestView.
BOOL ctest view::mySetupPixelFormat()
{//We don't care about the specific content of the format for the time being, and then change the format when we are familiar with it.
Static pixel format descriptor pfd =
{
Sizeof(PIXELFORMATDESCRIPTOR),//the size of this pfd.
1,//version number
PFD_DRAW_TO_WINDOW | // Support window
PFD_SUPPORT_OPENGL | // supports OPENGL.
PFD_DOUBLEBUFFER,//double buffer
PFD_TYPE_RGBA, // RGBA type
24, // 24 bit color depth
0, 0, 0, 0, 0, 0,//color bits are ignored.
0,//No alpha buffer.
0,//Ignore shift bit
0,//No accumulation buffer.
0, 0, 0, 0,//Cumulative bits are ignored.
32, // 32-bit Z buffer
0,//No mold buffer.
0,//No auxiliary buffer.
PFD_MAIN_PLANE,//main layer
0,//Reserved
0,0,0//layer mask ignored.
};
int pixelformat
if((pixel format = choosepixel format(m _ pDC-& gt; m _ hDC & amp; pfd)) == 0)
{
MessageBox("ChoosePixelFormat failed ");
Returns FALSE
}
if(SetPixelFormat(m _ pDC-& gt; M_hDC, pixel format. Pfd) == false)
{
MessageBox("SetPixelFormat failed ");
Returns FALSE
}
Return TRUE
}
3. Handle OnDestroy ()
void CTestView::OnDestroy()
{
wglMakeCurrent(m _ pDC-& gt; m_hDC,NULL); //release the RC corresponding to m_hDC.
wglDeleteContext(m _ hRC); //delete RC
Intermediate frequency (m_pDC)
Delete m _ pDC// Delete the DC owned by the current view.
CView::on destroy();
}
4. Handle OnEraseBkgnd ()
BOOL ctest view::OnEraseBkgnd(CDC * pDC)
{
// TODO: Add your message handler code here and/or call default.
//return CView::OnEraseBkgnd(pDC); (PDC);
//comment out this sentence, otherwise, the window
//will be refreshed with a white north view, causing the picture to flicker.
Returns TRUE// Only empty.
}
5. Handle OnDraw ()
void CTestView::OnDraw(CDC* pDC)
{
wglMakeCurrent(m _ pDC-& gt; m_hDC,m _ hRC); //Associate the RC with the current DC.
mydrawsscene(); //Specific drawing function, drawn with RC.
swap buffers(m _ pDC-& gt; m _ hDC); //Transfer the drawing in RC to the current DC, so
//Display on the screen
wglMakeCurrent(m _ pDC-& gt; m_hDC,NULL); //release RC for other DC to draw.
}
void CTestView::myDrawScene()
{
glClearColor(0.0f,0.0f,0.0f, 1.0f); //Set the background color to black.
GL clear(GL _ COLOR _ BUFFER _ BIT | GL _ DEPTH _ BUFFER _ BIT);
glPushMatrix();
glTranslated(0.0f,0.0f,-3.0f); //Translate the object in the direction of (0,0,-1)
//so as to be visible when projected. Because the default viewpoint is (0,0,0), it should be removed.
//You can see the object.
//This example is a demonstration of a plane Bezier curve, with only one rotation.
//transformation, you can see its 3D effect more clearly.
//Draw a Bezier curve below.
bezier _ curve . my polygon(); //Draw the control polygon of Bezier curve
bezier _ curve . mydraw(); //CBezierCurve Bessel curve _ curve
//is a member variable of CTestView.
//See the appendix for specific functions.
glPopMatrix();
GL flush(); //End RC drawing
Return;
}
6. Handle OnSize ()
void ctest view::on size(UINT nType,int cx,int cy)
{
CView::OnSize(nType,cx,cy);
Verification (wglmakecurrent (m _ PDC->; m_hDC,m _ hRC)); //Confirm that RC is associated with the current DC.
w = cx
h = cy
VERIFY(wglMakeCurrent(NULL,NULL)); //confirm that DC releases RC.
}
7 handling OnLButtonDown ()
void ctest view::OnLButtonDown(UINT nFlags,CPoint)
{
CView::OnLButtonDown(nFlags,point);
if(bezier _ curve . m _ N & gt; MAX- 1)
{
MessageBox ("the number of vertices exceeds the maximum max = 50");
Return;
}
//The following is to prepare for coordinate transformation.
GetClientRect(& amp; m _ client rect); //Get the viewport area size
w = m _ client rect . right-m _ client rect . left; //Window width w
h = m _ client rect . bottom-m _ client rect . top; //viewport height h
//w, h is a member variable of CTestView.
centex =(m _ client rect . left+m _ client rect . right)/2; //center position,
centery =(m _ client rect . top+m _ client rect . bottom)/2; //Take it as the origin
//centerx, centery is a member variable of CTestView.
GLdouble tmpx,tmpy
tmpx = scrx 2g LX(point . x); //The coordinates of points on the screen are converted into the standard coordinates of OpenGL drawing.
tmpy = scry 2 Gly(point . y);
Bezier curve. m vertex [Bezier curve. m N]. X = tmpx// Add a vertex.
Bezier curve. m vertex [Bezier curve. m N]. y = tmpy
Bezier curve. m _ n++;+; //number of vertices plus 1
invalidatory(NULL,TRUE); //Send refresh and redraw messages
}
double ctest view::scrx 2 glx(int scrx)
{
return(double)(scrx-centex)/double(h);
}
double ctest view::scry 2 Gly(int scry)
{
}
Appendix:
1 CBezierCurve: (BezierCurve.h)
CBezierCurve class
{
Public:
my point 2d m _ Vertex[MAX]; //Control vertices, stored in an array.
//myPOINT2D is a structure with two-dimensional points.
//the member is gldouble x, y.
int m _ N; //Control the number of vertices
Public:
CBezierCurve();
Virtual ~ cbeziercurve ();
void bezier _ generation(my point 2d P[MAX],int level);
The concrete implementation of//algorithm
void myDraw(); //Curve drawing function
void my polygon(); //Draw the control polygon
};
2. implementation of 2.cbeziercurve: (BezierCurve.cpp)
CBezierCurve::CBezierCurve()
{
m _ N = 4;
m_Vertex[0]。 x =-0.5f;
m_Vertex[0]。 y =-0.5f;
m_Vertex[ 1]。 x =-0.5f;
m_Vertex[ 1]。 y = 0.5f
m_Vertex[2]。 x = 0.5f
m_Vertex[2]。 y = 0.5f
m_Vertex[3]。 x = 0.5f
m_Vertex[3]。 y =-0.5f;
}
CBezierCurve::~CBezierCurve()
{
}
void CBezierCurve::myDraw()
{
Bessel _ generation (m _ vertex, level);
}
void CBezierCurve::bezier _ generation(my point 2d P[MAX],int level)
{//For a detailed description of the algorithm, please refer to related books.
int i,j;
level-;
If (level & lt0) returns;
If (level ==0)
{
glColor3f( 1.0f, 1.0f, 1.0f);
GL begin(GL _ LINES); //Draw a line segment
glVertex2d(P[0])。 x,P[0]。 y);
glVertex2d(P[m_N- 1]。 x,P[m_N- 1]。 y);
glEnd(); //End of line drawing
Return; //Recursive to the end, jump out of recursion
}
myPOINT2D Q[MAX],R[MAX];
for(I = 0; I {
q . x = p . x;
q . y = p . y;
}
for(I = 1; I<m _ N;; i++)
{
R[m_N-i]。 x=Q[m_N- 1]。 x;
R[m_N-i]。 y=Q[m_N- 1]。 y;
for(j = m _ N- 1; j & gt= I; j -)
{
Q[j]。 x=(Q[j- 1]。 x+Q[j]。 x)/double(2);
Q[j]。 y=(Q[j- 1]。 y+Q[j]。 y)/double(2);
}
}
R[0]。 x=Q[m_N- 1]。 x;
R[0]。 y=Q[m_N- 1]。 y;
Bessel _generation(Q, level);
Bessel _generation(R, level);
}
void CBezierCurve::myPolygon()
{
GL begin(GL _ LINE _ STRIP); //Draw a connecting line segment
glColor3f(0.2f,0.4f,0.4f);
for(int I = 0; I<m _ N;; i++)
{
glVertex2d(m_Vertex.x,m _ vertex . y);
}
glEnd(); //End the drawing connection segment
}
[Edit this paragraph] The difference between ]OpenGL and DirectX
OpenGL is just a graphic function library.
DirectX includes graphics, sound, input, network and other modules.
OpenGL is stable and can be used across platforms. DirectX can only be used on Windows series platforms, including Windows Mobile/CE series and XBOX/XBOX360.
-
From 1995 to 1996, Microsoft implemented a new plan to support running games on Windows95, with the goal of expanding the market to the game field controlled by Nintendo and Sega. However, Microsoft does not want to use OpenGL technology already provided on nt. Microsoft acquired Rendermorphics, Ltd and got his 3D API called RealityLab. After the reorganization, Microsoft released a new 3D API——Direct3D.
Microsoft, promote Direct3D and freeze OpenGL!
Microsoft refused to support OpenGL on Window95. More than that, Microsoft has taken unusual measures to withdraw its support for the MCD driver interface of OpenGL, so that hardware manufacturers have to give up the OpenGL driver that has entered the final test. Microsoft's marketing department began to promote Direct3D to game developers, hardware manufacturers and news publishing organizations, while rejecting OpenGL.
The battle of API!
Silicon graphics and many OpenGL users rely on the innovation and high-performance technology of OpenGL. However, it is obvious that Microsoft intends to replace OpenGL with Direct3D. Although D3D has many problems, it cannot be extended by hardware manufacturers like OpenGL. Silicon graphics decided to give a presentation at the 1996 SIGGRAPH conference. The demonstration proves that OpenGL is at least as fast as D3D, thus refuting Microsoft's market argument. Because OpenGL is a recognized standard in the industry, it has more functions and higher image quality than D3D, so the demonstration has caused a heated debate in computer graphics and game development.
Game developers require OpenGL and D3D to stand on the same footing!
When the technical and market problems were exposed, the strong support for OpenGL began. John carmack, the Doomsday Warrior, declared that he rejected D3D. Chris Hecker published a comprehensive analysis of two sets of APIs in Game Development magazine, and concluded that Microsoft should give up D3D. Game developers have submitted petitions to Microsoft twice. For the first time, 56 chief game developers asked Microsoft to release the OpenGL MCD driver, but it failed because it would make OpenGL compete with D3D. The second open letter started with 254 signatures and finally reached 1400. Microsoft's answer is still to reaffirm the old market position. Although the petitioner explicitly asked the two APIs to compete on an equal footing to promote development, Microsoft responded by increasing its investment in D3D and reducing its investment in OpenGL.
The merger of Fahrenheit -D3D and OpenGL?
Silicon Map, Microsoft, Hewlett-Packard and Intel reached an agreement to jointly develop the next generation of 3D API-Fahrenheit. But nothing happened, because Microsoft's plan was to apply OpenGL technology to D3D to drive away the threat of OpenGL in this name. (It is estimated that DirectX 8 Graphics is the Fahrenheit developed by Microsoft alone, which absorbs many things from OpenGL. )
OpenGL's heroism is undiminished!
OpenGL is still the only API that can compete with D3D controlled by Microsoft alone, although Silicon Graphics no longer promotes OpenGL in any way that Microsoft can't accept. Game development is independent, and many key people are using OpenGL, so hardware manufacturers are trying to improve their support. D3D still can't support high-end images and professional applications, and OpenGL is dominant in these fields. In the open source community, the Mesa project is providing OpenGL drivers independent of Microsoft.
Translator's Note: On the surface, D3D seems to support more functions than OpenGL. In fact, because D3D doesn't support hardware extension, new technologies such as hardware panoramic shadow and hardware rendering order independent translucent materials can't be used at all. When HAL is used and hardware doesn't support it, only a small part of functions provided by D3D (especially DD3D8) can be simulated. You have to use a lot of code to analyze hardware capabilities and adopt different strategies.