WinCE OpenGL繪制立方體和紋理貼圖
glDisableClientState(GL_TEXTURE_COORD_ARRAY);glDisableClientState(GL_VERTEX_ARRAY);
eglSwapBuffers(glesDisplay, glesSurface);}
void Clean()
{ if(glesDisplay)
{ eglMakeCurrent(glesDisplay, NULL, NULL, NULL);if(glesContext) eglDestroyContext(glesDisplay, glesContext);if(glesSurface) eglDestroySurface(glesDisplay, glesSurface);eglTerminate(glesDisplay);}
BOOL LoadTexture(LPCTSTR lpFileName,GLuint *id)
{ if(!g_Image.Load(lpFileName))
return FALSE;
// 創(chuàng)建紋理glGenTextures(1, id);// 綁定紋理glBindTexture(GL_TEXTURE_2D, *id);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, g_Image.Width(), g_Image.Height(), 0, GL_RGB, GL_UNSIGNED_BYTE, g_Image.GetBmpImage());
g_Image.Free();
return TRUE;}
以下實(shí)現(xiàn)了一個(gè)文件加載類,用以將外部圖片資源轉(zhuǎn)化成繪制紋理時(shí)所需的位圖數(shù)據(jù)。參考代碼如下:
view plain /******************************************************************** filename: CImgLoader.h created: 2011-01-05 author: firehood
purpose: 文件加載類,將外部圖片資源轉(zhuǎn)化成繪制紋理時(shí)所需的位圖數(shù)據(jù)圖片格式支持bmp、png、jpg. *********************************************************************/ #pragma once
class CImgLoader { public:CImgLoader(void);~CImgLoader(void);public:// 加載圖片資源BOOL Load(LPCTSTR lpFileName);// 獲取位圖數(shù)據(jù)unsigned char* GetBmpImage(void);// 釋放圖片資源void Free();// 獲取圖像寬度int Width();// 獲取圖像高度int Height();private:int m_Width; // 圖像寬度int m_Height; // 圖像高度unsigned char *m_pImage; // 指向圖像數(shù)據(jù)的指針};
view plain /******************************************************************** filename: CImgLoader.cpp created: 2011-01-05 author: firehood
purpose: 文件加載類,將外部圖片資源轉(zhuǎn)化成繪制紋理時(shí)所需的位圖數(shù)據(jù)圖片格式支持bmp、png、jpg. *********************************************************************/
#include StdAfx.h #include ImgLoader.h // IImage Includer #include
CImgLoader::CImgLoader(void)
{ m_pImage = NULL;m_Width = 0;m_Height = 0;}
CImgLoader::~CImgLoader(void)
{ }
// 加載圖片資源BOOL CImgLoader::Load(LPCTSTR lpFileName)
{ IImagingFactory *pImgFactory = NULL;IImage *pImage = NULL;IBitmapImage *pBmpImg = NULL;// CoInitializeEx(NULL, COINIT_MULTITHREADED);if (!SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **) pImgFactory)))
return FALSE;if (!SUCCEEDED(pImgFactory->CreateImageFromFile(lpFileName, pImage)))
return FALSE;// 獲取圖像大小信息ImageInfo ImgInfo;pImage->GetImageInfo(ImgInfo);
m_Width = ImgInfo.Width;m_Height = ImgInfo.Height;
if (FAILED(pImgFactory->CreateBitmapFromImage(pImage,m_Width,m_Height,PixelFormat24bppRGB,InterpolationHintDefault,pBmpImg)))
{ return FALSE;} RECT rect = {0,0,m_Width,m_Height};BitmapData *BmpData = new BitmapData;
if (FAILED(pBmpImg->LockBits(rect,ImageLockModeRead|ImageLockModeWrite,PixelFormat24bppRGB,BmpData)))
{ return FALSE;} int line = BmpData->Stride;LPBYTE lpData, lpLine, lpCurPixel;lpData = lpLine = (LPBYTE)BmpData->Scan0; // 獲取BMP位圖實(shí)際值的地址指針// 若為Bottom-Up(從下到上)的位圖,則指向buffer的結(jié)尾// 若為Top-Down(從上到下)的位圖,則指向buffer的開頭// int nBytesPP = nBPP >> 3; // 左移三位即除以8,獲取圖像每像素字節(jié)數(shù)m_pImage = new unsigned char[m_Width * m_Height * 3];memset(m_pImage, 0, m_Width * m_Height * 3);if(m_pImage == NULL)
return FALSE;if (line>0)
{ int pos = m_Width * m_Height * 3-1;for(int i = 0; i
{ return FALSE;} delete BmpData;pBmpImg = NULL;pImage->Release();pImgFactory->Release();return TRUE;}
// 獲取圖片數(shù)據(jù)unsigned char* CImgLoader::GetBmpImage(void)
{ return m_pImage;}
// 獲取圖像寬度int CImgLoader::Width()
{ return m_Width;}
// 獲取圖像高度int CImgLoader::Height()
{ return m_Height;}
// 釋放圖片資源void CImgLoader::Free()
{ if(m_pImage)
{ delete []m_pImage;m_pImage = NULL;}
評(píng)論