📄 cgl.cpp
字号:
// CGL.cpp: Implementierung der Klasse CGL.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CGL.h"
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CGL::CGL()
{
// Init member variables
m_Aspect = 4.0f / 3.0f;
}
CGL::~CGL()
{
}
bool CGL::InitGLState()
{
// Set default values the state machine
// Settings
glEnable(GL_DEPTH_TEST); // Z-Buffer
glEnable(GL_CULL_FACE); // Backface Culling
glEnable(GL_TEXTURE_2D); // Texturing
// Fog
glHint(GL_FOG, GL_NICEST);
/*
glEnable(GL_FOG);
glFogf(GL_FOG_DENSITY, 0.3f);
glFogi(GL_FOG_MODE, GL_EXP2);
glFogf(GL_FOG_START, 100.0f);
GLfloat FogColor[] = { 0.8f, 0.2f, 0.2f, 0.0f };
glFogfv(GL_FOG_COLOR, FogColor);
*/
// Solid polygons
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Enable lighting
glEnable(GL_LIGHTING);
// Lighting settings
GLfloat AmbientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat DiffuseLight[] = { 0.75f, 0.75f, 0.75f, 1.0f };
GLfloat OtherReflectance[] = { 1.75f, 1.75f, 1.75f, 1.0f };
GLfloat LightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
// Define light 0
glLightfv(GL_LIGHT0, GL_AMBIENT, AmbientLight); // Ambient
glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight); // Diffuse
glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Position
glEnable(GL_LIGHT0);
// Ambient reflectance settings
glMaterialfv(GL_FRONT, GL_AMBIENT, OtherReflectance);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
return TRUE;
}
void CGL::Resize(unsigned int iWidth, unsigned int iHeight, unsigned int iFOV, float fViewDepth)
{
// Handle window resizing
double Aspect;
if (iHeight == 0)
Aspect = (double) iWidth;
else
Aspect = (double) iWidth / (double) iHeight;
m_Aspect = (float) Aspect;
glViewport(0, 0, iWidth, iHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(iFOV, Aspect, 0.02, fViewDepth);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
}
bool CGL::InitGL(unsigned int iWidth, unsigned int iHeight, bool bFullscreen, HWND hWnd)
{
// Setup the pixel format
// 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
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);
}
float CGL::GetAspect()
{
return m_Aspect;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -