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
| #include "stdafx.h" #include "Texture.h" #include "../CxImage/ximage.h" #include <stdlib.h> #include <stdio.h> #pragma comment(lib, "cximage.lib")
int CTexture::LoadFromFile(const char* pcszFileName) { if (pcszFileName != NULL) { strcpy(m_szTexName, pcszFileName); } CxImage image; image.Load(pcszFileName); if (image.IsValid() == false) { return 0; }
m_nWidth = image.GetWidth(); m_nHeight = image.GetHeight(); if (m_byData) { delete m_byData; } m_byData = new BYTE[m_nWidth * m_nHeight * 3]; for (int i = 0; i < m_nHeight; ++i) { for (int j = 0; j < m_nWidth; ++j) { RGBQUAD rgb = image.GetPixelColor(j, i); m_byData[(i * m_nWidth + j) * 3] = rgb.rgbRed; m_byData[(i * m_nWidth + j) * 3 + 1] = rgb.rgbGreen; m_byData[(i * m_nWidth + j) * 3 + 2] = rgb.rgbBlue; } }
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_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
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_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
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; }
|