📄 glsetup.cpp
字号:
/*****************************************************************************
*
*
* Routine written by & copyright (c) Brian Tischler 07/2000
* Author e-mail : briandeb@telusplanet.net
*
* The source can be modified, reused & redistributed for non-profitable
* uses. Use for commercial purposes without author's permission prohibited.
*
*****************************************************************************/
// GLSetup.cpp: implementation of the CGLH class.
//
//////////////////////////////////////////////////////////////////////
#include "GLSetup.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGLSetup::CGLSetup()
{}
CGLSetup::~CGLSetup()
{}
//
CGLSetup::InitGL(GLsizei Width, GLsizei Height)
{
// Enable Texture Mapping
glEnable(GL_TEXTURE_2D);
// This Will Clear The Background Color To Black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Enables Clearing Of The Depth Buffer
glClearDepth(1.0);
// Enables Smooth Color Shading
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
// Reset The Projection Matrix
glLoadIdentity();
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,1.3333f,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW);
// Set The Blending Function For Translucency
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glEnable(GL_BLEND);
//don't compute the back of the bitmap
glEnable(GL_CULL_FACE);
}
//////////////////////
CGLSetup::ReSizeGLScene(GLsizei Width, GLsizei Height)
{
// Prevent A Divide By Zero If The Window Is Too Small
if (Height==0)
Height=1;
// Reset The Current Viewport And Perspective Transformation
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set the perspective
gluPerspective(45.0f,1.3333f,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
///////////////////////////////////////////////////////////////////////////////
// This function loads a 24-bit color Windows bitmap and sets it as the current
// texture. The bitmap must be 24-bit color, and the dimensions must be powers
// of 2 (no additional checks are performed).
BOOL CGLSetup::LoadBMP(TCHAR* szFileName)
{
HANDLE hFileHandle;
BITMAPINFO *pBitmapInfo = NULL;
unsigned long lInfoSize = 0;
unsigned long lBitSize = 0;
int nTextureWidth;
int nTextureHeight;
BYTE *pBitmapData;
// Open the Bitmap file
hFileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
// Check for open failure (most likely file does not exist).
if(hFileHandle == INVALID_HANDLE_VALUE)
return FALSE;
// File is Open. Read in bitmap header information
BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),
&dwBytes,NULL);
if(dwBytes != sizeof(BITMAPFILEHEADER))
return FALSE;
// Check format of bitmap file
if(bitmapHeader.bfType != 'MB')
return FALSE;
// Read in bitmap information structure
lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
pBitmapInfo = (BITMAPINFO *) new BYTE[lInfoSize];
ReadFile(hFileHandle,pBitmapInfo,lInfoSize,&dwBytes,NULL);
if(dwBytes != lInfoSize)
return FALSE;
nTextureWidth = pBitmapInfo->bmiHeader.biWidth;
nTextureHeight = pBitmapInfo->bmiHeader.biHeight;
lBitSize = pBitmapInfo->bmiHeader.biSizeImage;
if(lBitSize == 0)
lBitSize = (nTextureWidth *
pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
abs(nTextureHeight);
// Allocate space for the actual bitmap
pBitmapData = new BYTE[lBitSize];
// Read in the bitmap bits
ReadFile(hFileHandle,pBitmapData,lBitSize,&dwBytes,NULL);
if(lBitSize != dwBytes)
{
if(pBitmapData)
delete [] (BYTE *) pBitmapData;
pBitmapData = NULL;
return FALSE;
}
CloseHandle(hFileHandle);
if(pBitmapInfo != NULL)
delete [] (BYTE *)pBitmapInfo;
// This is specific to the binary format of the data read in.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexImage2D(GL_TEXTURE_2D, 0, 3, nTextureWidth, nTextureHeight, 0,
GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);
if(pBitmapData)
delete [] (BYTE *) pBitmapData;
return TRUE;
}
CGLSetup::GLLoadTextures(GLuint* ptList)
{
// Generate 7 texture object ID's
glGenTextures(8, ptList);
glBindTexture(GL_TEXTURE_2D, ptList[KEY]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("key.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[STAR]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("star.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[BUBBLE]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("bubble.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[CLOUD]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("CLOUD.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[CIRCLE]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("circle.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[SPARK]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("spark.bmp");
glBindTexture(GL_TEXTURE_2D, ptList[LOWKEY]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
LoadBMP("LOWKEY.bmp");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -