1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include "bitmapTex.h" #include "CxBitmap.h" #include "tool.h" #include #include #include
int CBitmapTex::LoadBitmapTex(const char* pcszFileName) { CxBitmap* pBitmap = new CxBitmap; int i; int nBytesPerLine;
strcpy(m_szTexName, pcszFileName); if (pBitmap->LoadBitmap(pcszFileName) == NULL) { return 0; } m_nWidth = pBitmap->GetWidth(); m_nHeight = pBitmap->GetHeight(); m_nBytesPerPixel = pBitmap->GetBytesPerPixel(); m_byData = (BYTE*)malloc(m_nWidth * m_nHeight * m_nBytesPerPixel); if (m_byData == NULL) { return 0; } nBytesPerLine = m_nWidth * m_nBytesPerPixel; if (pBitmap->GetBytesPerLine() == nBytesPerLine) { memcpy(m_byData, pBitmap->GetBuffer(), m_nHeight * m_nWidth * m_nBytesPerPixel); } else { for (i = 0; i < m_nHeight; ++i) { memcpy(m_byData + i * nBytesPerLine, pBitmap->GetBuffer() + i * pBitmap->GetBytesPerLine(), nBytesPerLine); } }
delete pBitmap;
return 1; }
void CBitmapTex::InitTex() { int nFormat; int nWidthPowerOfTwo = ::Pow2Big(m_nWidth); int nHeightPowerOfTwo = ::Pow2Big(m_nHeight);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, m_uTexName); glBindTexture(GL_TEXTURE_2D, m_uTexName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
if (m_nBytesPerPixel == 3) { nFormat = GL_BGR_EXT; } else if (m_nBytesPerPixel == 4) { nFormat = GL_BGRA_EXT; }
if (nWidthPowerOfTwo == m_nWidth nHeightPowerOfTwo == m_nHeight) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidthPowerOfTwo, nHeightPowerOfTwo, 0, nFormat, GL_UNSIGNED_BYTE, m_byData); m_fTexScaleX = m_fTexScaleY = 1.0; } else { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nWidthPowerOfTwo, nHeightPowerOfTwo, 0, nFormat, GL_UNSIGNED_BYTE, NULL); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_nWidth, m_nHeight, nFormat, GL_UNSIGNED_BYTE, m_byData); m_fTexScaleX = 1.0 * m_nWidth / nWidthPowerOfTwo; m_fTexScaleY = 1.0 * m_nHeight / nHeightPowerOfTwo; } }
void CBitmapTex::SetTexture() { glBindTexture(GL_TEXTURE_2D, m_uTexName); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glScaled(m_fTexScaleX, m_fTexScaleY, 1.0); }
|