www.pudn.com > 林海血原源代码.zip > CGL.cpp
// CGL.cpp: implementation of the CGL class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CGL.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGL::CGL()
{
m_ScrWidth=800;
m_ScrHeight=600;
m_FOV=60;
}
CGL::~CGL()
{
}
bool CGL::InitGLState()
{
// Set default values the state machine
// Enable Texture Mapping
glClearColor(0.0f,0.0f,0.0f,1); // This Will Clear The Background Color To Black
// glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST ); // Really Nice Perspective Calculations
/* glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);
// glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
*/
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
/////////fog
glFogf(GL_FOG_MODE,GL_LINEAR);
GLfloat fogcolor[]={0.7f,0.7f,0.7f,1};
glFogfv(GL_FOG_COLOR,fogcolor);
glFogf(GL_FOG_DENSITY,1.78f);
glFogf(GL_FOG_START,1300);
glFogf(GL_FOG_END,2000);
// glEnable(GL_FOG);
GLfloat mat_ambient[]={1,1,1,1.0f};
GLfloat mat_diffuse[]={1,1,1,1.0f};
// GLfloat mat_emission[]={0.0f,0.0f,0.0f,1.0f};
GLfloat mat_shininess[]={0.0f};
GLfloat mat_specular[]={0.0f,0.0f,0.0f,1.0f};
/*********************************************/
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
// glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mat_emission);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
GLfloat light0_diffuse[]={0.75f,0.6f,0.5f,1.0f};
GLfloat light0_ambient[]={0.5f,0.5f,0.5f,1.0f};
GLfloat light0_emission[]={0.0f,0.0f,0.0f,0.0f};
// GLfloat light0_position[]={1500.0f,150.0f,1500.0f,1.0f};
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
glLightfv(GL_LIGHT0,GL_EMISSION,light0_emission);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
// glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1);
// glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.1f);
// glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,2);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
///////////////////////////////////////////
glAlphaFunc(GL_GEQUAL,0.1f);
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glReadBuffer(GL_FRONT);
return TRUE;
}
void CGL::Resize(int iFOVAngle,int iHeight)
{
// Handle window resizing
double Aspect;
m_FOV=iFOVAngle;
if(m_FOV>90)m_FOV=60;
if (iHeight == 0)
Aspect = (double) m_ScrWidth;
else
Aspect = (double) m_ScrWidth / (double) m_ScrHeight;
glViewport(0, 0, m_ScrWidth, m_ScrHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_FOV, Aspect, 1.0f, 100000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glDrawBuffer(GL_BACK);
}
void CGL::ChangeFOVAngle(int state)//0: RButtonUp 1:RButtonDown
{
static int FOVAngle=60;
static bool bZooming=false;
static bool bZoomed=false;
if(state==1 && !bZoomed)
{
bZooming=true;
FOVAngle-=2;
}
if(state==0 )
{
if(!bZoomed)FOVAngle=60;
else
{
if(!bZooming)FOVAngle=60;
}
bZooming=false;
}
if(FOVAngle<20)bZoomed=true;
else bZoomed=false;
if(FOVAngle>60)FOVAngle=60;
if(FOVAngle<2)FOVAngle=2;
glViewport(0, 0, m_ScrWidth, m_ScrHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(FOVAngle, (double) m_ScrWidth / (double) m_ScrHeight,1.0f, 100000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
bool CGL::InitGL(unsigned int iWidth, unsigned int iHeight, bool bFullscreen, HWND hWnd)
{
m_ScrWidth=iWidth;
m_ScrHeight=iHeight;
// Pixel format storage
unsigned int PixelFormat;
// Pixel format descriptor
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR), // Size of this pixel format descriptor
1, // Version number (?)
PFD_DRAW_TO_WINDOW | // Format must support window
PFD_SUPPORT_OPENGL | // Format must support OpenGL
PFD_DOUBLEBUFFER, // Must support double buffering
PFD_TYPE_RGBA, // Request an RGBA format
16, // Select 16 bit 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, // 16 bit 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 (?)
};
// Save passed hWnd
m_hWnd = hWnd;
// Get a device context for the window
m_hDC = GetDC(m_hWnd);
// Find the closest match to the pixel format we set above
PixelFormat = ChoosePixelFormat(m_hDC, &pfd);
// No matching pixel format ?
if (!PixelFormat)
{
MessageBox(0,"Can't find a suitable pixelformat", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
// Can we set the pixel mode?
if (!SetPixelFormat(m_hDC,PixelFormat,&pfd))
{
MessageBox(0,"Can't set the pixelformat", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
// Grab a rendering context
m_hRC = wglCreateContext(m_hDC);
// Did we get one?
if (!m_hRC)
{
MessageBox(0, "Can't create a GL rendering context", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
// Can we make the RC active?
if (!wglMakeCurrent(m_hDC, m_hRC))
{
MessageBox(0, "Can't activate GLRC", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
// Change the resolution
if (bFullscreen)
if (!ChangeResolution(iWidth, iHeight))
{
MessageBox(0, "Can't switch resolution", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
// Init the state machine
if (!InitGLState())
{
MessageBox(0, "Can't init the state machine", "Error", MB_OK | MB_ICONERROR);
// Failed
return FALSE;
}
return TRUE;
}
bool CGL::ChangeResolution(unsigned int iWidth, unsigned int iHeight)
{
// Change the resolution
m_ScrWidth=iWidth;
m_ScrHeight=iHeight;
DEVMODE dmScreenSettings;
// Zero ram to store settings
memset(&dmScreenSettings, 0, sizeof(DEVMODE));
dmScreenSettings.dmSize = sizeof(DEVMODE); // Size of the devmode structure
dmScreenSettings.dmPelsWidth = iWidth; // Screen width
dmScreenSettings.dmPelsHeight = iHeight; // Screen height
dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; // Pixel mode
// Switch to full screen
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
return FALSE;
return TRUE;
}
void CGL::DestroyGL()
{
// Switch resolution back and shutdown GL
ChangeDisplaySettings(NULL, 0); // Disable fullscreen mode
wglMakeCurrent(m_hDC, NULL); // Make te DC crrent
wglDeleteContext(m_hRC); // Kill the RC
ReleaseDC(m_hWnd, m_hDC); // Free the DC
}
void CGL::SwapBuffers()
{
// Swap the buffers
::SwapBuffers(m_hDC);
}