Getting started-OpenGL's simplest getting started

Source: Internet
Author: User

Getting started series -- the simplest getting started with OpenGL

It's now. before going to bed, write something... think about it. Let's get started with OpenGL. In Windows, users all know the direct X Series drivers. OpenGL works very well with direct3d. A detailed introduction to DirectX and OpenGL,ArticleToo many articles have been introduced in detail in China, and the history is also very clear. We recommend that you take a look at http://www.zkxt.com/d3d_opengl_1.htm. AsProgramPersonnel, it is not enough to understand this. However, if you do not read this article, you cannot read it first.

OpenGL, short for "Open graphics library", is a low-layer 3D graphics API developed by SGI. It has become an industrial standard in the graphic development field. As a standard, you can view its standard on its official website: callback (for example, gl4java ). However, it is mainly used for C/C ++. At the strong demand of some game manufacturers, Microsoft has also implemented OpenGL library support for its own operating systems on platforms above Windows 95, and SGI has its own set of implementations, it is implemented as software and does not support hardware acceleration.

Here is a brief introduction to the basic knowledge:

1. Environment selection:

Since many users in China use Windows operating systems, I will briefly introduce how to compile my first OpenGL program in Dev-C ++. Dev-C ++ is developed based on mingw. For more information, go to the Internet. Download information please see my other blog (ftp://ftp.jaist.ac.jp) provides you with a high-speed open source, Login server, In the SourceForge series to find the dev-CPP folder, and then download it on OK.

All right, open Dev-C ++, select file/New/project/multimedia, select OpenGL, C ++ project, and click OK. By default, a sample file is created for you. You can also compile and run it now, with the F9 shortcut key (or use menu: Run/compile and run as well ). Well, wait. A triangle is moving.

And so on. If it's that simple, I will be scolded, especially for those who are not familiar with the WIN32API function.CodeWhat are you doing. Now I want to simplify it.

2. Simple learning:

Find the Code:

Else

{

/* OpenGL animation code goes here */

Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f); // This line starts.

....

Swapbuffers (HDC );

Theta + = 1.0f; // to this line

Sleep (1 );

}

Okay, the main content is here. Now we will introduce the content of this OpenGL drawing to a function drawgrapchics and put it in other files (in this case, in learning, we can simply ignore this main. CPP file ).

Now, we create a new simple file. HPP, right-click the project management on the left, create a unit, and save the file as simple. HPP: cut down the above Code:

Float Theta = 1.0f; // note that this line related to the original main. cpp is also cut out (this is mainly the rotation control angle)

Void drawgraphics (HDC & HDC ){

Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f );

......

Swapbuffers (HDC); // call the swap buffer data, which must be called at the end.

Theta-= 1.0; // control angle, Rotation Angle

}

Main. cpp is changed:

...

# Include "Simple. HPP" // Add a header file

....

Else

{

/* OpenGL animation code goes here */

Drawgraphics (HDC); // simplified to function operations

Sleep (1 );

}

...

If you want to practice other things in the future, you can add other things and change # include "Simple. HPP" to your own testing file. You don't need to change anything else.

3. A large number of exercises and learning:

For programmers, searching without API functions is the most painful thing, and Java is the best in this regard. Dev-C ++ provides reference manuals for WIN32API and OpenGL API documentation. Please download them from the one I mentioned above.

The best entry material than OpenGL Redbook the http://www.opengl.org/documentation/red_book_1.0/ official website provides this version of 1.1 free download, other versions of their own money ....~~. Note: During the learning process, you should not look at other framework implementations. You should mainly look at the implementation of the main functions of OpenGL. Because Redbook uses the method of creating windows of AUX, so we can omit this part, and we will learn the implementation of GL.

Next, let's take a look at the nehe tutorial. Now there is a Chinese version of download in China. Just download it by Google search.

Others are waiting for learning, writing code by yourself, and learning from experience ....

4. Check the complete code (so as not to misunderstand my explanation ~~~) :

The first is main. cpp:

/**************************

* Des

*

**************************/

# Include <windows. h>

# Include <Gl/Gl. h>

# Include "Simple. HPP"

//: Void drawgraphics (HDC &); use OpenGL

/**************************

* Function declarations

*

**************************/

Lresult callback wndproc (hwnd, uint message,

Wparam, lparam );

Void enableopengl (hwnd, HDC * HDC, hglrc * HRC );

Void disableopengl (hwnd, HDC, hglrc HRC );

/**************************

* Winmain

*

**************************/

int winapi winmain (hinstance,
hinstance hprevinstance,
lpstr lpcmdline,
int icmdshow)

{< br>
wndclass WC;
hwnd;
HDC;
hglrc HRC;
MSG;
bool bquit = false;

/* Register window class */
WC. style = cs_owndc;
WC. lpfnwndproc = wndproc;
WC. cbclsextra = 0;
WC. cbwndextra = 0;
WC. hinstance = hinstance;
WC. hicon = loadicon (null, idi_application);
WC. hcursor = loadcursor (null, idc_arrow);
WC. hbrbackground = (hbrush) getstockobject (black_brush);
WC. lpszmenuname = NULL;
WC. lpszclassname = "glsample";
registerclass (& WC);

/* Create main window */

Hwnd = createwindow (

"Glsample", "OpenGL sample ",

Ws_caption | ws_popupwindow | ws_visible,

0, 0,256,256,

Null, null, hinstance, null );

/* Enable OpenGL for the window */

Enableopengl (hwnd, & HDC, & HRC );

/* Program Main Loop */

While (! Bquit)

{

/* Check for messages */

If (peekmessage (& MSG, null, 0, 0, pm_remove ))

{

/* Handle or dispatch messages */

If (msg. Message = wm_quit)

{

Bquit = true;

}

Else

{

Translatemessage (& MSG );

Dispatchmessage (& MSG );

}

}

Else

{

/* OpenGL animation code goes here */

Drawgraphics (HDC );

Sleep (2 );

}

}

/* Shutdown OpenGL */

Disableopengl (hwnd, HDC, HRC );

/* Destroy the window explicitly */

Destroywindow (hwnd );

Return msg. wparam;

}


/********************

* Window procedure

*

********************/

Lresult callback wndproc (hwnd, uint message,

Wparam, lparam)

{

Switch (Message)

{

Case wm_create:

Return 0;

Case wm_close:

Postquitmessage (0 );

Return 0;

Case wm_destroy:

Return 0;

Case wm_keydown:

Switch (wparam)

{

Case vk_escape:

Postquitmessage (0 );

Return 0;

}

Return 0;

Default:

Return defwindowproc (hwnd, message, wparam, lparam );

}

}


/*******************

* Enable OpenGL

*

*******************/

Void enableopengl (hwnd, HDC * HDC, hglrc * HRC)

{

Pixelformatdescriptor PFD;

Int iformat;

/* Get the device context (DC )*/

* HDC = getdc (hwnd );

/* Set the pixel format for the DC */

Zeromemory (& PFD, sizeof (PFD ));

PFD. nsize = sizeof (PFD );

PFD. nversion = 1;

PFD. dwflags = pfd_draw_to_window |

Pfd_support_opengl | pfd_doublebuffer;

PFD. ipixeltype = pfd_type_rgba;

PFD. ccolorbits = 24;

PFD. cdepthbits = 16;

PFD. ilayertype = pfd_main_plane;

Iformat = choosepixelformat (* HDC, & PFD );

Setpixelformat (* HDC, iformat, & PFD );

/* Create and enable the render context (RC )*/

* HRC = wglcreatecontext (* HDC );

Wglmakecurrent (* HDC, * HRC );

}


/******************

* Disable OpenGL

*

******************/

Void disableopengl (hwnd, HDC, hglrc HRC)

{

Wglmakecurrent (null, null );

Wgldeletecontext (HRC );

Releasedc (hwnd, HDC );

}

Followed by simple. HPP:

// Simple. HPP

Float Theta = 1.0f;

Void drawgraphics (HDC & HDC ){

Glclear (gl_color_buffer_bit );

Glpushmatrix ();

Glrotatef (Theta, 0.0f, 0.0f, 1.0f );

Glbegin (gl_triangles );

Glcolor3f (1.0f, 0.0f, 0.0f); glvertex2f (0.0f, 1.0f );

Glcolor3f (0.0f, 1.0f, 0.0f); glvertex2f (0.87f,-0.5f );

Glcolor3f (0.0f, 0.0f, 1.0f); glvertex2f (-0.87f,-0.5f );

Glend ();

Glpopmatrix ();

Swapbuffers (HDC );

Theta-= 1.0;

}

5. Well, it's time to briefly introduce how to get started. If you want to learn OpenGL, and mm, you have to work on yourself and try to see it, try to write it... don't trust your eyesight, don't trust your understanding, practice is the truth :-)

Note: Oh, it took more than 40 minutes to write... depressed! Going to bed ~~~ If you have any questions, please leave a message to me. Or check my contact information and send me an email. Go offline and go to bed. Good night!


Author's blog:Http://blog.csdn.net/jwsh1984/

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.