This chapter tutorials mainly from: http://ogldev.atspace.co.uk/www/tutorial01/tutorial01.html
Before using OpenGL, install and set freeglut and glew ,:
Http://freeglut.sourceforge.net/
Http://glew.sourceforge.net/
Source code Tutorial: http://ogldev.atspace.co.uk/ogldev_win.zip
As a 3D graphic interface, OpenGL does not provide any window management function. Generally, there are related libraries in different operating systems as interfaces of OpenGL and OS to implement window management, for example, Glx in xwindows, WGL in windows, and CGL in Mac OS. In the following series of tutorials, we use glut, which is a cross-platform library that enables simple window management and input/output operations. It is especially suitable for learning OpenGL or some simple applications.
Next we will learn how to create a window with glut:
# Include <windows. h>
# Include <Gl/freeglut. h>
# Include <stdio. h>
# Include <string>
# Include <iostream>
Using namespace STD;
// Scene Rendering Function
Static void renderscenecb ()
{
// Before rendering each frame, clear the color buffer with the specified color to clear the color specified by glclearcolor.
Glclear (gl_color_buffer_bit );
// Swap the buffer before and after, and display the rendering content on the screen
Gluswapbuffers ();
}
Static void initializeglutcallbacks ()
{
Gludisplayfunc (renderscenecb );
}
Int main (INT argc, char ** argv)
{
GLUT initialization function. The parameters of this function use the command line parameters of the application. Commonly used command line parameters are-sync,-gldebug and so on, detailed parameter information see: http://www.opengl.org/documentation/specs/glut/spec3/node10.html
// Initialize glut
Gluinit (& argc, argv );
The system uses dual-buffer, that is, two color buffers. One is called the front buffer and the other is called the back buffer. The output content of the rendering is first placed in the back buffer, and then swapbuffer is used, this prevents incomplete rendering of one frame of images. Specify the color buffer format.
// Use double buffering. The color buffer pixel format is rgba.
Fig );
// Window size and position
Gluinitwindowsize (800,600 );
Gluinitwindowposition (100,100 );
// Window title
Valley createwindow ("tutorial 01 ");
The callback function is used to specify the callback function of the main window. In this way, when the main function enters the loop, we can interact with the window through the callback function, which is similar to the Windows event mechanism.
// Set the rendering callback function
Initializeglutcallbacks ();
The following code is used to obtain the rendering window handle. The rendering window handle may be used in the interaction program between opencl and OpenGL. Note: The values returned by the glucreatewindow function and the glugetwindow function are not the window handle, but an integer, indicating the window ID number. To get the rendering window handle, we can implement it through the Win32 function below, you can also modify the source code of freeglut to make the glugetwindow function return the window handle.
// Get the handle of the rendering window
Const char * windowtitle = "this is my title ";
Glusetwindowtitle (windowtitle );
Hwnd define whandle = findwindow (null, l "this is my title ");
HDC devicecontext = getdc (windowhandle );
Hglrc renderingcontext = wglgetcurrentcontext ();
Cout <"render window handle:" <export whandle <Endl;
We know that after the OpenGL status is set, the current status will be maintained unless a new status is set again, for example, after the background rendering color is set to black, unless we call glclearcolor again to set a new color, the color buffer is set to black before each frame is rendered.
// Sets the background color to black and the pixel format rgba to clear the background color of the color buffer.
Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f );
The following function tells glut to start an internal loop. During the loop process, it constantly monitors some messages and events and responds to different callback functions based on these message events.
Glumainloop ();
Return 0;
}
After the program is executed, a dark window is displayed, and there is nothing ......