|
|
代码都是copy别人的,glew库1.5.1版本,还使用了glut和mfc支持(因为要打开文件需要CFileDialog类),一执行loadCompressedTexture就会出现glCompressedTexImage2DARB读取0位置内存了,代码如下:
DDS_IMAGE_DATA* loadDDSTextureFile( const char *filename )
{
DDS_IMAGE_DATA *pDDSImageData;
DDSURFACEDESC2 ddsd;
char filecode[4];
std::ifstream file; //输入文件
int factor;
int bufferSize;
//打开文件
file.open(filename,std::ios::in|std::ios::binary);
if (file == NULL)
{
std::cout<<"open dds wrong!"<<std::endl;
return NULL;
}
//验证是否真的dds
file.read(filecode,4);
if( strncmp( filecode, "DDS ", 4 ) != 0 )
{
char str[255];
sprintf( str, "The file \"%s\" doesn't appear to be a valid .dds file!", filename );
MessageBox( NULL, str, "ERROR", MB_OK|MB_ICONEXCLAMATION );
file.close();
return NULL;
}
//获取表面descriptor
file.read((char *)&ddsd,sizeof(ddsd));
pDDSImageData = (DDS_IMAGE_DATA*) malloc(sizeof(DDS_IMAGE_DATA));
memset( pDDSImageData, 0, sizeof(DDS_IMAGE_DATA) );
//
// This .dds loader supports the loading of compressed formats DXT1, DXT3
// and DXT5.
//
switch(ddsd.ddpfPixelFormat.dwFourCC)
{
case FOURCC_DXT1:
// DXT1's compression ratio is 8:1
pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
factor = 2;
break;
case FOURCC_DXT3:
// DXT3's compression ratio is 4:1
pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
factor = 4;
break;
case FOURCC_DXT5:
// DXT5's compression ratio is 4:1
pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
factor = 4;
break;
default:
char str[255];
sprintf( str, "The file \"%s\" doesn't appear to be compressed "
"using DXT1, DXT3, or DXT5!", filename );
MessageBox( NULL, str, "ERROR", MB_OK|MB_ICONEXCLAMATION );
return NULL;
}
//
// How big will the buffer need to be to load all of the pixel data
// including mip-maps?
//
if( ddsd.dwLinearSize == 0 )
{
MessageBox( NULL, "dwLinearSize is 0!","ERROR",
MB_OK|MB_ICONEXCLAMATION);
}
if( ddsd.dwMipMapCount > 1 )
bufferSize = ddsd.dwLinearSize * factor;
else
bufferSize = ddsd.dwLinearSize;
pDDSImageData->pixels = (unsigned char*)malloc(bufferSize * sizeof(unsigned char));
//读入纹理数据了
file.read((char*)pDDSImageData->pixels,bufferSize);
//关闭文件
file.close();
pDDSImageData->width = ddsd.dwWidth;
pDDSImageData->height = ddsd.dwHeight;
pDDSImageData->numMipMaps = ddsd.dwMipMapCount;
if( ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1 )
pDDSImageData->components = 3;
else
pDDSImageData->components = 4;
return pDDSImageData;
}
void loadCompressedTexture( GLuint& g_compressedTextureID )
{
// NOTE: Unlike "lena.bmp", "lena.dds" actually contains its own mip-map
// levels, which are also compressed.
DDS_IMAGE_DATA *pDDSImageData = loadDDSTextureFile( "F:\\nocull_uw0757b.dds" );<-这是存在的,断点调试也发现读取到的内容完全正确!!!
if( pDDSImageData != NULL )
{
int nHeight = pDDSImageData->height;
int nWidth = pDDSImageData->width;
int nNumMipMaps = pDDSImageData->numMipMaps;
int nBlockSize;
if( pDDSImageData->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT )
nBlockSize = 8;
else
nBlockSize = 16;
glGenTextures( 1, &g_compressedTextureID );
glBindTexture( GL_TEXTURE_2D, g_compressedTextureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
int nSize;
int nOffset = 0;
// Load the mip-map levels
for( int i = 0; i < nNumMipMaps && (nWidth>1 && nHeight>1); ++i )
{
if( nWidth == 0 ) nWidth = 1;
if( nHeight == 0 ) nHeight = 1;
nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize;
glCompressedTexImage2DARB( GL_TEXTURE_2D,
i,
pDDSImageData->format,
nWidth,
nHeight,
0,
nSize,
pDDSImageData->pixels + nOffset );//<-第一次运行到这里就会出现读取0x00位置内存错误!!!!
nOffset += nSize;
// Half the image size for the next mip-map level...
nWidth = (nWidth / 2);
nHeight = (nHeight / 2);
}
}
if( pDDSImageData != NULL )
{
if( pDDSImageData->pixels != NULL )
free( pDDSImageData->pixels );
free( pDDSImageData );
}
} |
|