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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include "stdafx.h" #include "Texture.h" #include "CxImage/ximage.h" #include <string.h> #include <stdlib.h> #include <stdio.h>
int CTexture::LoadFromFile(const char* pcszFileName) { if (pcszFileName != NULL) { strcpy(m_szTexName, pcszFileName); }
int nStrLen = strlen(pcszFileName); int nLen = MultiByteToWideChar(CP_ACP, 0, pcszFileName, nStrLen, NULL, 0); MultiByteToWideChar(CP_ACP, 0, pcszFileName, nStrLen, m_char16ImgName, nLen); m_char16ImgName[nLen] = '\0';
Gdiplus::Bitmap image(m_char16ImgName);
m_nWidth = image.GetWidth(); m_nHeight = image.GetHeight(); Gdiplus::Rect rect(0, 0, m_nWidth, m_nHeight); Gdiplus::BitmapData bitmapData; image.LockBits(rect, Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, PixelFormat24bppRGB, bitmapData);
if (m_byData) { delete m_byData; m_byData = NULL; } m_byData = new BYTE[m_nWidth * m_nHeight * 3];
BYTE* pByte = (BYTE*)bitmapData.Scan0; int nOffset = bitmapData.Stride - m_nWidth * 3; for (int i = 0; i < m_nHeight; ++i) { for (int j = 0; j < m_nWidth; ++j) { m_byData[(i * m_nWidth + j) * 3] = pByte[0]; m_byData[(i * m_nWidth + j) * 3 + 1] = pByte[1]; m_byData[(i * m_nWidth + j) * 3 + 2] = pByte[2]; pByte += 3; } pByte += nOffset; } image.UnlockBits(bitmapData);
return 1; }
void CTexture::Init2DTex() { 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
int nRet = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_nWidth, m_nHeight, GL_RGB, GL_UNSIGNED_BYTE, m_byData); if (nRet != 0) { AfxMessageBox("加载纹理失败"); } }
void CTexture::Init1DVertTex() { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, m_uTexName); glBindTexture(GL_TEXTURE_1D, m_uTexName);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
BYTE* pbyData = new BYTE[m_nHeight * 3]; int nAdd = m_nWidth / 2; for (int i = 0; i < m_nHeight; ++i) { pbyData[i * 3] = m_byData[(i * m_nWidth + nAdd) * 3]; pbyData[i * 3 + 1] = m_byData[(i * m_nWidth + nAdd) * 3 + 1]; pbyData[i * 3 + 2] = m_byData[(i * m_nWidth + nAdd) * 3 + 2]; }
int nRet = gluBuild1DMipmaps(GL_TEXTURE_1D, 3, m_nHeight, GL_RGB, GL_UNSIGNED_BYTE, pbyData); if (nRet != 0) { AfxMessageBox("加载纹理失败"); }
delete pbyData; }
CTexture::~CTexture() { if (m_byData) delete m_byData; }
|