// OpenGL.cpp: implementation of the COpenGL class.
//
//////////////////////////////////////////////////////////////////////
#include "OpenGL.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COpenGL::COpenGL()
{
m_x_res=GetSystemMetrics(SM_CXSCREEN);
m_y_res=GetSystemMetrics(SM_CYSCREEN);
m_bit_depth=16;
m_tri_angle=7;
m_quad_angle=0;
}
COpenGL::~COpenGL()
{
}
void COpenGL::SceneInit()
{
int w,h;
w=m_x_res;
h=m_y_res;
LoadTextures();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0,0.0,0.0,0.5);
glClearDepth(1.0F);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void COpenGL::SceneResizeViewport()
{
int w,h;
GetClientRect(m_h_wnd,&m_rect);
w=m_rect.right-m_rect.left;
h=m_rect.bottom-m_rect.top;
if(!(w>0 && h>0))
{
return;
}
m_aspect=(GLfloat)w/(GLfloat)h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,m_aspect,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void COpenGL::EnableOpenGL()
{
int iFormat;
PIXELFORMATDESCRIPTOR pfd;
m_h_dc=GetDC(m_h_wnd);
ZeroMemory(&pfd,sizeof(pfd));
pfd.nSize=sizeof(pfd);
pfd.nVersion=1;
pfd.dwFlags=PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.iLayerType=PFD_MAIN_PLANE;
pfd.cColorBits=16;
pfd.cDepthBits=16;
iFormat=ChoosePixelFormat(m_h_dc,&pfd);
SetPixelFormat(m_h_dc,iFormat,&pfd);
m_h_rc=wglCreateContext(m_h_dc);
wglMakeCurrent(m_h_dc,m_h_rc);
}
void COpenGL::DisableOpenGL()
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(m_h_rc);
ReleaseDC(m_h_wnd,m_h_dc);
}
bool COpenGL::ChangeResolution()
{
int w,h,bitdepth;
w=m_x_res;
h=m_y_res;
bitdepth=m_bit_depth;
bool bRet;
DEVMODE devMode;
int modeSwitch;
int closeMode=0;
EnumDisplaySettings(NULL,closeMode,&devMode);
devMode.dmBitsPerPel=bitdepth;
devMode.dmPelsWidth=w;
devMode.dmPelsHeight=h;
devMode.dmFields=DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
modeSwitch=ChangeDisplaySettings(&devMode,CDS_FULLSCREEN);
if(modeSwitch==DISP_CHANGE_SUCCESSFUL)
{
bRet=true;
}
else
{
ChangeDisplaySettings(NULL,0);
bRet=false;
}
return bRet;
}
void COpenGL::SetWindowParam(HWND hWnd, HINSTANCE hInstance,int w, int h, int bitdepth)
{
m_h_wnd=hWnd;
m_h_instance=hInstance;
m_x_res=w;
m_y_res=h;
m_bit_depth=bitdepth;
}
void COpenGL::SceneShow(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawRectange_3d_picture();
}
AUX_RGBImageRec* COpenGL::LoadBMP(char *filename)
{
FILE *file=NULL;
if(!filename)
{
return NULL;
}
file=fopen(filename,"r");
if(file)
{
fclose(file);
return auxDIBImageLoad(filename);
}
return NULL;
}
int COpenGL::LoadTextures()
{
int status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void*)*1);//初始化所要用的贴图空间
if(TextureImage[0]=LoadBMP("1.bmp"))
{
status=TRUE;
glGenTextures(1,&m_texture[0]);//生成纹理贴图
glBindTexture(GL_TEXTURE_2D,m_texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,
TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,
TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}
if(TextureImage[0])
{
if(TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
return status;
}
void COpenGL::DrawRectange_3d_picture()
{
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(m_quad_angle,1.0f,1.0f,1.0f);
m_quad_angle+=0.1;
if(m_quad_angle>=360)
{
m_quad_angle=0;
}
glBindTexture(GL_TEXTURE_2D,m_texture[0]);
glBegin(GL_QUADS);
//前面
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f , 1.0f,1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f ,-1.0f,1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f ,1.0f,1.0f);
//右面
glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f);
//后面
glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f);
//左面
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f , 1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f ,-1.0f,1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f ,1.0f,1.0f);
//上面
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f , 1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,1.0f,1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f ,1.0f,1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f ,1.0f,-1.0f);
//下面
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f ,-1.0f,1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f ,-1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f ,-1.0f,1.0f);
glEnd();
}
\