From Rune Schmidt Jensen - new dds loader.
This commit is contained in:
@@ -141,6 +141,9 @@ unsigned int Image::computeNumComponents(GLenum format)
|
||||
{
|
||||
switch(format)
|
||||
{
|
||||
case(GL_COMPRESSED_RGB_S3TC_DXT1_EXT): return 3;
|
||||
case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT): return 4;
|
||||
case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT): return 4;
|
||||
case(GL_COLOR_INDEX): return 1;
|
||||
case(GL_STENCIL_INDEX): return 1;
|
||||
case(GL_DEPTH_COMPONENT): return 1;
|
||||
@@ -163,6 +166,12 @@ unsigned int Image::computePixelSizeInBits(GLenum format,GLenum type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case(GL_COMPRESSED_RGB_S3TC_DXT1_EXT): return 4;
|
||||
|
||||
case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT): return 8;
|
||||
|
||||
case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT): return 8;
|
||||
|
||||
case(GL_BITMAP): return computeNumComponents(format);
|
||||
|
||||
case(GL_BYTE):
|
||||
@@ -320,6 +329,51 @@ void Image::readPixels(int x,int y,int width,int height,
|
||||
}
|
||||
|
||||
|
||||
void Image::readImageFromCurrentTexture(unsigned int contextID)
|
||||
{
|
||||
const osg::Texture::Extensions* extensions = osg::Texture::getExtensions(contextID,true);
|
||||
|
||||
GLint internalformat;
|
||||
GLint width;
|
||||
GLint height;
|
||||
|
||||
if (extensions->isCompressedTexImage2DSupported())
|
||||
{
|
||||
GLint compressed;
|
||||
GLint compressed_size;
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB,&compressed);
|
||||
|
||||
/* if the compression has been successful */
|
||||
if (compressed == GL_TRUE)
|
||||
{
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &compressed_size);
|
||||
|
||||
allocateImage(width,height,1,internalformat,internalformat);
|
||||
|
||||
extensions->glGetCompressedTexImage(GL_TEXTURE_2D, 0, _data);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// non compressed texture implemention.
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
|
||||
|
||||
allocateImage(width,height,1,internalformat,GL_UNSIGNED_BYTE);
|
||||
|
||||
_internalTextureFormat = internalformat;
|
||||
|
||||
glGetTexImage(GL_TEXTURE_2D,0,_pixelFormat,_dataType,_data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Image::scaleImage(int s,int t,int r)
|
||||
{
|
||||
if (_s==s && _t==t && _r==r) return;
|
||||
|
||||
@@ -918,7 +918,7 @@ void Texture::Extensions::setupGLExtenions()
|
||||
|
||||
_glCompressedTexImage2D = getGLExtensionFuncPtr("glCompressedTexImage2D","glCompressedTexImage2DARB");
|
||||
_glCompressedTexSubImage2D = getGLExtensionFuncPtr("glCompressedTexSubImage2D","glCompressedTexSubImage2DARB");;
|
||||
|
||||
_glGetCompressedTexImage = getGLExtensionFuncPtr("glGetCompressedTexImage","glGetCompressedTexImageARB");;
|
||||
}
|
||||
|
||||
void Texture::Extensions::glCompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data) const
|
||||
@@ -948,3 +948,15 @@ void Texture::Extensions::glCompressedTexSubImage2D(GLenum target, GLint level,
|
||||
}
|
||||
|
||||
}
|
||||
void Texture::Extensions::glGetCompressedTexImage(GLenum target, GLint level, GLvoid *data) const
|
||||
{
|
||||
if (_glGetCompressedTexImage)
|
||||
{
|
||||
typedef void (APIENTRY * GetCompressedTexImageArbProc) (GLenum target, GLint level, GLvoid *data);
|
||||
((GetCompressedTexImageArbProc)_glGetCompressedTexImage)(target, level, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
notify(WARN)<<"Error: glGetCompressedTexImage not supported by OpenGL driver"<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
13
src/osgPlugins/dds/GNUmakefile
Normal file
13
src/osgPlugins/dds/GNUmakefile
Normal file
@@ -0,0 +1,13 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
ReaderWriterDDS.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(OTHER_LIBS)
|
||||
|
||||
TARGET_BASENAME = dds
|
||||
include $(TOPDIR)/Make/cygwin_plugin_def
|
||||
PLUGIN = $(PLUGIN_PREFIX)$(TARGET_BASENAME).$(PLUGIN_EXT)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
209
src/osgPlugins/dds/ReaderWriterDDS.cpp
Normal file
209
src/osgPlugins/dds/ReaderWriterDDS.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
/**********************************************************************
|
||||
*
|
||||
* FILE: ReaderWriterdds.cpp
|
||||
*
|
||||
* DESCRIPTION: Class for reading a DDS file into an osg::Image.
|
||||
*
|
||||
* Example on reading a DDS file code can be found at:
|
||||
* http://developer.nvidia.com/docs/IO/1327/ATT/
|
||||
* ARB_texture_compression.pdf
|
||||
* Author: S<>bastien Domin<69>, NVIDIA Corporation
|
||||
*
|
||||
* CREATED BY: Rune Schmidt Jensen, rsj@uni-dk
|
||||
*
|
||||
* HISTORY: Created 31.03.2003
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include <osg/Texture>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/FileNameUtils>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// NOTICE ON WIN32:
|
||||
// typedef DWORD unsigned long;
|
||||
// sizeof(DWORD) = 4
|
||||
typedef struct _DDCOLORKEY
|
||||
{
|
||||
unsigned long dwColorSpaceLowValue;
|
||||
unsigned long dwColorSpaceHighValue;
|
||||
} DDCOLORKEY;
|
||||
|
||||
|
||||
typedef struct _DDPIXELFORMAT
|
||||
{
|
||||
unsigned long dwSize;
|
||||
unsigned long dwFlags;
|
||||
unsigned long dwFourCC;
|
||||
union
|
||||
{
|
||||
unsigned long dwRGBBitCount;
|
||||
unsigned long dwYUVBitCount;
|
||||
unsigned long dwZBufferBitDepth;
|
||||
unsigned long dwAlphaBitDepth;
|
||||
};
|
||||
union
|
||||
{
|
||||
unsigned long dwRBitMask;
|
||||
unsigned long dwYBitMask;
|
||||
};
|
||||
union
|
||||
{
|
||||
unsigned long dwGBitMask;
|
||||
unsigned long dwUBitMask;
|
||||
};
|
||||
union
|
||||
{
|
||||
unsigned long dwBBitMask;
|
||||
unsigned long dwVBitMask;
|
||||
};
|
||||
union
|
||||
{
|
||||
unsigned long dwRGBAlphaBitMask;
|
||||
unsigned long dwYUVAlphaBitMask;
|
||||
unsigned long dwRGBZBitMask;
|
||||
unsigned long dwYUVZBitMask;
|
||||
};
|
||||
} DDPIXELFORMAT;
|
||||
|
||||
typedef struct _DDSCAPS2
|
||||
{
|
||||
unsigned long dwCaps;
|
||||
unsigned long dwCaps2;
|
||||
unsigned long dwCaps3;
|
||||
union
|
||||
{
|
||||
unsigned long dwCaps4;
|
||||
unsigned long dwVolumeDepth;
|
||||
};
|
||||
} DDSCAPS2;
|
||||
|
||||
typedef struct _DDSURFACEDESC2 {
|
||||
unsigned long dwSize;
|
||||
unsigned long dwFlags;
|
||||
unsigned long dwHeight;
|
||||
unsigned long dwWidth;
|
||||
union
|
||||
{
|
||||
long lPitch;
|
||||
unsigned long dwLinearSize;
|
||||
};
|
||||
unsigned long dwBackBufferCount;
|
||||
union
|
||||
{
|
||||
unsigned long dwMipMapCount;
|
||||
unsigned long dwRefreshRate;
|
||||
};
|
||||
unsigned long dwAlphaBitDepth;
|
||||
unsigned long dwReserved;
|
||||
unsigned long* lpSurface;
|
||||
DDCOLORKEY ddckCKDestOverlay;
|
||||
DDCOLORKEY ddckCKDestBlt;
|
||||
DDCOLORKEY ddckCKSrcOverlay;
|
||||
DDCOLORKEY ddckCKSrcBlt;
|
||||
DDPIXELFORMAT ddpfPixelFormat;
|
||||
DDSCAPS2 ddsCaps;
|
||||
unsigned long dwTextureStage;
|
||||
} DDSURFACEDESC2;
|
||||
|
||||
#ifndef MAKEFOURCC
|
||||
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
||||
((unsigned long)(char)(ch0) | ((unsigned long)(char)(ch1) << 8) | \
|
||||
((unsigned long)(char)(ch2) << 16) | ((unsigned long)(char)(ch3) << 24 ))
|
||||
#endif //defined(MAKEFOURCC)
|
||||
|
||||
/*
|
||||
* FOURCC codes for DX compressed-texture pixel formats
|
||||
*/
|
||||
#define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
|
||||
#define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
|
||||
#define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
|
||||
#define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
|
||||
#define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
|
||||
|
||||
osg::Image* ReadDDSFile(const char *filename){
|
||||
osg::Image* osgImage = new osg::Image();
|
||||
|
||||
DDSURFACEDESC2 ddsd;
|
||||
|
||||
char filecode[4];
|
||||
FILE *fp;
|
||||
|
||||
// Open file.
|
||||
fp = fopen(filename, "rb");
|
||||
if (fp == NULL)
|
||||
return NULL;
|
||||
|
||||
// Verify that this is a DDS file.
|
||||
fread(filecode, 1, 4, fp);
|
||||
if (strncmp(filecode, "DDS ", 4) != 0) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get the surface desc.
|
||||
fread(&ddsd, sizeof(ddsd), 1, fp);
|
||||
|
||||
// Read image data.
|
||||
unsigned int size = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * 2 : ddsd.dwLinearSize;
|
||||
unsigned char* imageData = (unsigned char*) malloc(size * sizeof(unsigned char));
|
||||
fread(imageData, 1, size, fp);
|
||||
// Close the file.
|
||||
fclose(fp);
|
||||
|
||||
// Retreive image properties.
|
||||
int s = ddsd.dwWidth;
|
||||
int t = ddsd.dwHeight;
|
||||
int r = ddsd.dwMipMapCount>1?ddsd.dwMipMapCount:0;
|
||||
unsigned int dataType = GL_UNSIGNED_BYTE;
|
||||
unsigned int pixelFormat;
|
||||
unsigned int internalFormat;
|
||||
switch(ddsd.ddpfPixelFormat.dwFourCC){
|
||||
case FOURCC_DXT1:
|
||||
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
||||
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
||||
break;
|
||||
case FOURCC_DXT3:
|
||||
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
||||
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
||||
break;
|
||||
case FOURCC_DXT5:
|
||||
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
break;
|
||||
default:
|
||||
free(imageData);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Set image data and properties.
|
||||
osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, imageData, osg::Image::USE_NEW_DELETE);
|
||||
|
||||
// Return Image.
|
||||
return osgImage;
|
||||
}
|
||||
|
||||
class ReaderWriterDDS : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
virtual const char* className() {
|
||||
return "DDS Image Reader";
|
||||
}
|
||||
|
||||
virtual bool acceptsExtension(const std::string& extension) {
|
||||
return osgDB::equalCaseInsensitive(extension,"dds");
|
||||
}
|
||||
|
||||
virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options*) {
|
||||
osg::Image* osgImage = ReadDDSFile(fileName.c_str());
|
||||
if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED;
|
||||
return osgImage;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// now register with Registry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<ReaderWriterDDS> g_readerWriter_DDS_Proxy;
|
||||
Reference in New Issue
Block a user