|
The establishment of OpenGL application framework
#include <gl\gl.h> // Header file for OpenGL32 library
#include <gl\glu.h> // Header file for GLu32 library
#include <gl\glaux.h> // Header file for the GLaux library
Initialize OpenGL
SetOpenGLInterface ()
PIXELFORMATDESCRIPTOR pfd = {
// Initialize the pixel storage format
Sizeof (PIXELFORMATDESCRIPTOR), // size of pfd
1, // version number
PFD_DRAW_TO_WINDOW | // Support window
PFD_SUPPORT_OPENGL | // Support OpenGL
PFD_DOUBLEBUFFER, // support double buffer
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // each color bit (ignored)
0, // no alpha cache
0, // ignore conversion bits
0, // no accumulation
0, 0, 0, 0,
32, // 32-bit depth buffer
0, // no template cache
0, // no auxiliary cache
PFD_MAIN_PLANE, // main drawing layer
0, // reserved
0, 0, 0 // ignored layer mask
};
m_pDC = GetDC (); // Get device environment handle
int iFormat = ChoosePixelFormat (m_pDC-> m_hDC,&pfd); // Set the pixel format
SetPixelFormat (m_pDC-> m_hDC, iFormat,&pfd);
m_hGlrc = wglCreateContext (m_pDC-> m_hDC); // Create a rendering context
wglMakeCurrent (m_pDC-> m_hDC, m_hGlrc); // Set the current drawing description table of a thread
DEM data structure definition
ReleaseDC (m_pDC); // Release DC
if (m_hGlrc! = NULL) // release RC
wglDeleteContext (m_hGlrc);
Set up the scene
InitOpenGL ()
GLfloat light_position [] = {0.0, 0.0, 1.0, 0.0}; // define the position coordinates of the light source
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
GLfloat light_ambient [] = {0.0, 0.0, 0.0, 1.0}; // define ambient reflected light
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
GLfloat light_diffuse [] = {1.0, 1.0, 1.0, 1.0}; // define diffuse reflection light
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
GLfloat light_specular [] = {1.0, 1.0, 1.0, 1.0}; // define specular reflection light
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
GLfloat light_model_ambient [] = {0.4f, 0.4f, 0.4f, 1.0f}; // define light model parameters
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
GLfloat local_view [] = {0.0};
glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glEnable (GL_LIGHTING); // GL_LIGHTING is valid
glEnable (GL_LIGHT0); // GL_LIGHT0 is valid
glEnable (GL_DEPTH_TEST); // Allow deep comparison
glDepthFunc (GL_LESS); // activate depth comparison
glClearColor (0.1f, 0.1f, 0.5f, 0.0f); // Set the blue background
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); // Trade-off image quality and drawing speed
Define the viewport
InitViewPort ()
CRect rect; // Get the size of the drawing client area
GetClientRect (rect);
glMatrixMode (GL_PROJECTION); // Set the projection mode
glLoadIdentity (); // Load identity matrix
if (m_nViewMode == 0) // establish a perspective projection matrix
GluPerspective (90.0, rect.Width () / rect.Height (), 1.0, 10000.0);
if (m_nViewMode == 1) // establish an orthographic projection matrix
GlOrtho (-0.5 * 10000.0, 0.5 * 10000.0, -0.5 * 10000.0, 0.5 * 10000.0, 1.0, 10000.0); glViewport (0, 0, rect.Width (), rect.Height ()); // Reset viewport
GlMatrixMode (GL_MODELVIEW); // determine the current matrix mode
GlLoadIdentity (); // load identity matrix
This is the code to find online, I don't know much, I hope to know how to help heroes debug. |
|