📄 initopengl.cpp
字号:
// InitOpenGL.cpp: implementation of the InitOpenGL class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Shadow.h"
#include "extgl.h"
#include "InitOpenGL.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Method to Initialize OpenGL
//////////////////////////////////////////////////////////////////////
BOOL InitializeOpenGL(HGLRC m_hRC, HDC m_hDC)
{
if ( NULL == m_hDC ) // failure to get DC
return FALSE;
if (!SetupPixelFormat(m_hDC))
return FALSE;
if ( 0 == (m_hRC = wglCreateContext(m_hDC)))
{
AfxMessageBox("Can not create context!");
return FALSE;
}
if (FALSE == wglMakeCurrent(m_hDC, m_hRC))
{
AfxMessageBox("Can not make context current!");
return FALSE;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
///Set up the pixel format for OpenGL
/////////////////////////////////////////////////////////////////////////////
BOOL SetupPixelFormat(HDC m_hDC)
{
int m_PixelFormat;
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
32, // 32-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
8, // no alpha buffer
0, // shift bit ignored
32, // 32 accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
if ( 0 == (m_PixelFormat = ChoosePixelFormat(m_hDC, &pfd)) )
return FALSE;
if ( FALSE == SetPixelFormat(m_hDC, m_PixelFormat, &pfd) )
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
/// Check the current system support OpenGL
/////////////////////////////////////////////////////////////////////////////
void CheckOpenGLSupport(HDC m_hDC)
{
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(m_hDC, 1, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
//PFD_GENERIC_FORMAT被置1,并且PFD_GENERIC_ACCELERATED被置0,即:
if((pfd.dwFlags & PFD_GENERIC_FORMAT) && !(pfd.dwFlags & PFD_GENERIC_ACCELERATED))
{
AfxMessageBox("表明该像素格式不被显卡硬件支持,使用该像素格式的OpenGL程序将使用纯软件模式渲染。");
}
///PFD_GENERIC_FORMAT被置1,并且PFD_GENERIC_ACCELERATED被置1,即:
if((pfd.dwFlags & PFD_GENERIC_FORMAT) && (pfd.dwFlags & PFD_GENERIC_ACCELERATED))
{
AfxMessageBox("表明该像素格式被显卡硬件支持,并且程序使用MCD模式渲染。");
}
//PFD_GENERIC_FORMAT被置0,并且PFD_GENERIC_ACCELERATED被置0,即:
if(!(pfd.dwFlags & PFD_GENERIC_FORMAT) && !(pfd.dwFlags & PFD_GENERIC_ACCELERATED))
{
AfxMessageBox("表明该像素格式被显卡硬件支持,并且程序使用ICD模式渲染。");
}
return;
}
/////////////////////////////////////////////////////////////////////////////
/// Save OpenGL showing to images as BMP file or TXT file
/////////////////////////////////////////////////////////////////////////////
void SaveBufferToImage(HGLRC m_hRC, HDC m_hDC)
{
int viewx,viewy;
int x,y,i,j;
int Xlen, Ylen;
GLint port[4];
unsigned char pcrgb;
unsigned short *rbuf = NULL;
unsigned short *gbuf = NULL;
unsigned short *bbuf = NULL;
unsigned char *pbuf = NULL;
wglMakeCurrent(m_hDC, m_hRC);
glGetIntegerv(GL_VIEWPORT,port);
viewx = port[2]-port[0];
viewy = port[3]-port[1];
int mod = viewx%4;
viewx = viewx - mod;
Xlen = viewx;
Ylen = viewy;
pbuf = new unsigned char [viewx*viewy*3];
rbuf = new unsigned short [Xlen*sizeof(unsigned short)];
gbuf = new unsigned short [Xlen*sizeof(unsigned short)];
bbuf = new unsigned short [Xlen*sizeof(unsigned short)];
if ( (pbuf == NULL)||(rbuf == NULL)||(gbuf == NULL)||(bbuf == NULL) )
{
AfxMessageBox("Malloc buffer memory failure \n");
return;
}
//////Use the readpiexels here before the dialog display can avoid the wrong image
glReadPixels(0,0,viewx,viewy,GL_RGB,GL_UNSIGNED_BYTE,pbuf);
CFileDialog dlg(FALSE,"",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Microsoft BMP(*.bmp)|*.bmp|");
if(dlg.DoModal() == IDOK)
{
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
FILE *fp;
CString tmpName=dlg.GetFileName();
fp=fopen(tmpName.GetBuffer(tmpName.GetLength()),"wb");
if(fp==NULL)
{
AfxMessageBox("Can't Create Image Result File\n");
return;
}
head.bfType =0x4d42;
head.bfSize =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+Xlen*Ylen*3;
head.bfReserved1=0;
head.bfReserved2=0;
head.bfOffBits =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
info.biSize =sizeof(BITMAPINFOHEADER);
info.biWidth =Xlen;
info.biHeight =Ylen;
info.biPlanes =1;
info.biBitCount =24;
info.biCompression =0;
info.biSizeImage =0;
info.biXPelsPerMeter=0;
info.biYPelsPerMeter=0;
info.biClrUsed =0;
info.biClrImportant =0;
fwrite(&head,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&info,sizeof(BITMAPINFOHEADER),1,fp);
i=0;
for( y=0; y<Ylen; y++)
{
for(x=0; x<Xlen; x++)
{
rbuf[x] = (short)(pbuf[(i+x)*3]);
gbuf[x] = (short)(pbuf[(i+x)*3+1]);
bbuf[x] = (short)(pbuf[(i+x)*3+2]);
}
i += Xlen;
for(j=0;j<Xlen;j++)
{
pcrgb=(unsigned char)bbuf[j];
fwrite(&pcrgb,1,1,fp);
pcrgb=(unsigned char)gbuf[j];
fwrite(&pcrgb,1,1,fp);
pcrgb=(unsigned char)rbuf[j];
fwrite(&pcrgb,1,1,fp);
}
}
fclose(fp);
}
delete[] pbuf;
delete[] rbuf;
delete[] gbuf;
delete[] bbuf;
wglMakeCurrent(m_hDC, NULL);
}
unsigned char* OpenTexImage(char* ImageFile, int *rslx, int *rsly )
{
FILE *fp;
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
int srcx, srcy, src_byte_x, x, y;
int i, rslp, srcp, srcp2;
float daltarx, daltary;
unsigned char *SImageData, *ImageData, tmp_rgb;
// load a image
fp=fopen(ImageFile, "rb");
if(fp == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Error to open texture image!");
return(NULL);
}
fread(&head,sizeof(BITMAPFILEHEADER),1,fp);
fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
if( (head.bfType != 19778) || // not a bitmap
info.biBitCount != 24 ) // not 24-bit ture color
{
*rslx = *rsly = 0;
AfxMessageBox("Error:\nBad texture image.\nNot a 24-bits bitmap.");
return(NULL);
}
srcx = info.biWidth;
srcy = info.biHeight;
src_byte_x = srcx*3;
while(src_byte_x & 3) src_byte_x ++;
SImageData = new unsigned char[src_byte_x*srcy];
if(SImageData == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Malloc Failure ");
return(NULL);
}
fread( SImageData, 1, src_byte_x*srcy, fp );
fclose(fp);
i = 0;
for(y=0; y<srcy; y++)
{
i = y*src_byte_x;
for(x=0; x<srcx; x++)
{
tmp_rgb = SImageData[i];
SImageData[i] = SImageData[i+2];
SImageData[i+2] = tmp_rgb;
i += 3;
}
}
// Texture size
*rslx = *rsly = 1;
while( ((*rslx)<<1) <= srcx ) (*rslx) <<= 1;
while( ((*rsly)<<1) <= srcy ) (*rsly) <<= 1;
ImageData = new unsigned char[(*rslx)*(*rsly)*3];
if(ImageData == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Malloc Failure");
delete []SImageData;
return(NULL);
}
// Calc. daltar
daltarx = (float)srcx/(float)(*rslx);
daltary = (float)srcy/(float)(*rsly);
// map source picture to texture-image
rslp = srcp = 0;
for( y = 0; y < (*rsly); y++ )
{
srcp2 = src_byte_x * (int)( daltary*y );
for( x = 0; x < (*rslx); x ++ )
{
srcp = srcp2 + 3 * (int)(x*daltarx);
ImageData[rslp] = SImageData[srcp];
ImageData[rslp+1] = SImageData[srcp+1];
ImageData[rslp+2] = SImageData[srcp+2];
rslp += 3;
}
}
// free memory
delete []SImageData;
return( ImageData );
}
unsigned char* OpenTexImage4D(char* ImageFile, int *rslx, int *rsly )
{
FILE *fp;
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
int srcx, srcy, src_byte_x, x, y;
int i, rslp, srcp, srcp2;
float daltarx, daltary;
unsigned char *SImageData, *ImageData, tmp_rgb;
// load a image
fp=fopen(ImageFile, "rb");
if(fp == NULL)
{
*rslx = *rsly = 0;
return(NULL);
}
fread(&head,sizeof(BITMAPFILEHEADER),1,fp);
fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
if( (head.bfType != 19778) || // not a bitmap
info.biBitCount != 24 ) // not 24-bit ture color
{
*rslx = *rsly = 0;
AfxMessageBox("Error:\nBad texture image.\nNot a 24-bits bitmap.");
return(NULL);
}
srcx = info.biWidth;
srcy = info.biHeight;
src_byte_x = srcx*3;
while(src_byte_x & 3) src_byte_x ++;
SImageData = new unsigned char[src_byte_x*srcy];
if(SImageData == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Malloc Failure");
return(NULL);
}
fread( SImageData, 1, src_byte_x*srcy, fp );
fclose(fp);
i = 0;
for(y=0; y<srcy; y++)
{
i = y*src_byte_x;
for(x=0; x<srcx; x++)
{
tmp_rgb = SImageData[i];
SImageData[i] = SImageData[i+2];
SImageData[i+2] = tmp_rgb;
i += 3;
}
}
// Texture size
*rslx = *rsly = 1;
while( ((*rslx)<<1) <= srcx ) (*rslx) <<= 1;
while( ((*rsly)<<1) <= srcy ) (*rsly) <<= 1;
ImageData = new unsigned char[(*rslx)*(*rsly)*4];
if(ImageData == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Malloc Failure");
delete []SImageData;
return(NULL);
}
// Calc. daltar
daltarx = (float)srcx/(float)(*rslx);
daltary = (float)srcy/(float)(*rsly);
// map source picture to texture-image
///////////////////////////////////////////
//Here ImageData include RGBA 4D for alpha
///////////////////////////////////////////
rslp = srcp = 0;
for( y = 0; y < (*rsly); y++ )
{
srcp2 = src_byte_x * (int)( daltary*y );
for( x = 0; x < (*rslx); x ++ )
{
srcp = srcp2 + 3 * (int)(x*daltarx);
ImageData[rslp] = SImageData[srcp];//>20?SImageData[srcp]:0;
ImageData[rslp+1] = SImageData[srcp+1];//>20?SImageData[srcp+1]:0;
ImageData[rslp+2] = SImageData[srcp+2];//>20?SImageData[srcp+2]:0;
ImageData[rslp+3] = (ImageData[rslp] + ImageData[rslp+1] + ImageData[rslp+2])/3;
//if(ImageData[rslp+3] <20)
// ImageData[rslp+3] = 0;
//else
// ImageData[rslp+3] = 255;
rslp += 4;
}
}
// free memory
delete []SImageData;
return( ImageData );
}
unsigned char* OpenTexImage1D(char* ImageFile, int *rslx, int *rsly )
{
FILE *fp;
BITMAPFILEHEADER head;
BITMAPINFOHEADER info;
int srcx, srcy, src_byte_x, x, y;
int i, rslp, srcp, srcp2;
float daltarx, daltary;
unsigned char *SImageData, *ImageData, tmp_rgb;
// load a image
fp=fopen(ImageFile, "rb");
if(fp == NULL)
{
*rslx = *rsly = 0;
return(NULL);
}
fread(&head,sizeof(BITMAPFILEHEADER),1,fp);
fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
if( (head.bfType != 19778) || // not a bitmap
info.biBitCount != 24 ) // not 24-bit ture color
{
*rslx = *rsly = 0;
AfxMessageBox("Error:\nBad texture image.\nNot a 24-bits bitmap.");
return(NULL);
}
srcx = info.biWidth;
srcy = info.biHeight;
src_byte_x = srcx*3;
while(src_byte_x & 3) src_byte_x ++;
SImageData = new unsigned char[src_byte_x*srcy];
if(SImageData == NULL)
{
*rslx = *rsly = 0;
AfxMessageBox("Malloc Failure");
return(NULL);
}
fread( SImageData, 1, src_byte_x*srcy, fp );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -