www.pudn.com > Ì廿֯Âë.rar > openglsupport.cpp, change:2001-11-27,size:8780b
/*
* This Code Was Created By Tom Stanis / Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing The Base Code, Making It More Flexible!
* If You've Found This Code Useful, Please Let Me Know.
* Visit My Site At nehe.gamedev.net
*/
#include "stdafx.h"
#include <stdio.h> // Header File For Standard Input/Output
#include <gl/gl.h> // Header File For The OpenGL32 Library
#include <gl/glu.h> // Header File For The GLu32 Library
#include <gl/glaux.h> // Header File For The Glaux Library
#include "math.h"
#include "openglsupport.h"
GLfloat LightAmbient[]= {0.2f, 0.2f, 0.2f, 0.2f };
GLfloat LightDiffuse[]= {0.8f, 0.8f, 0.8f, 0.8f };
GLfloat LightPosition[]= {0.0f, 0.0f, 2.0f, 0.0f };
LRESULT CALLBACK OpenGLWndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
MOUSEPROC GLMouseHandler = NULL;
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
// if (height==0) height=1;
glViewport(0,0,width,height); // Reset The Current Viewport
// glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
// glLoadIdentity(); // Reset The Projection Matrix
// glOrtho(0, width, 0, height, -1, 1);
// Calculate The Aspect Ratio Of The Window
// gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
// glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
// glLoadIdentity(); // Reset The Modelview Matrix
}
/*
GLvoid RotateGLScene()
{
glMultMatrixf((const float *)rotmatrix.matrix);
}
*/
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glEnable(GL_NORMALIZE);
glShadeModel(GL_FLAT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glEnable(GL_POINT_SMOOTH);
// glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
// glPointSize(2.0f);
// glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LESS); // The Type Of Depth Testing To Do
glClearDepth(1.0f); // Depth Buffer Setup
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0); // Enable Light One
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); // Position The Light
return TRUE; // Initialization Went OK
}
//===========================================================================================
GLWindow::GLWindow(HINSTANCE hInstance, HWND hWndParent, WNDPROC WndProc, MOUSEPROC MouseProc)
{
hDC = NULL;
hRC = NULL;
hWnd = NULL;
hInst = hInstance;
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwStyle; // Window Style
wc.style = /*CS_HREDRAW | CS_VREDRAW |*/ CS_OWNDC | CS_NOCLOSE; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC)OpenGLWndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL"; // Set The Class Name
GLMouseHandler = MouseProc;
if (!RegisterClass(&wc))// Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return;
}
dwStyle = WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; // Windows Style
if (!(hWnd=CreateWindow("OpenGL", "OpenGL", dwStyle, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
hWndParent, NULL, hInst, NULL)))
{
MessageBox(NULL,"GLWindow: Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_DOUBLEBUFFER | // Must Support Double Buffering
PFD_SUPPORT_OPENGL, // Format Must Support OpenGL
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
MessageBox(NULL,"GLWindow: Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
MessageBox(NULL,"GLWindow: Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
MessageBox(NULL,"GLWindow: Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
MessageBox(NULL,"GLWindow: Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
MessageBox(NULL,"GLWindow: Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
if (!InitGL()) // Initialize Our Newly Created GL Window
{
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return; // Return FALSE
}
return; // Success
}
GLWindow::~GLWindow() // Properly Kill The Window
{
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
if (!UnregisterClass("OpenGL",hInst)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInst=NULL; // Set hInstance To NULL
}
}
void
GLWindow::Swap()
{
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
HWND
GLWindow::GetHWND()
{
return hWnd;
}
//--------------------------------------------------------------------------
LRESULT CALLBACK OpenGLWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) // Check For Windows Messages
{
case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}
case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
case WM_MOUSEMOVE:
{
if (GLMouseHandler != NULL) GLMouseHandler(hWnd, uMsg, wParam, lParam);
break;
}
}
// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}