Added the beginings of new osgParticle Explosion, Fire and SmokeEffects.

Added support for generation explosion, fire and smoke effects in osgsimulation
This commit is contained in:
Robert Osfield
2004-05-13 22:09:23 +00:00
parent 45998cfdf7
commit ae5aefac0e
16 changed files with 1091 additions and 176 deletions

View File

@@ -1,19 +1,21 @@
/**********************************************************************
*
* 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<EFBFBD>bastien Domin<EFBFBD>, NVIDIA Corporation
*
* CREATED BY: Rune Schmidt Jensen, rune@schmidt-jensen.com
*
* HISTORY: Created 31.03.2003
*
**********************************************************************/
*
* 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: Sebastien Domine, NVIDIA Corporation
*
* CREATED BY: Rune Schmidt Jensen, rsj@uni-dk
*
* HISTORY: Created 31.03.2003
* Modified 13.05.2004
* by George Tarantilis, gtaranti@nps.navy.mil
*
**********************************************************************/
#include <osg/Texture>
#include <osg/Notify>
@@ -24,6 +26,7 @@
#include <stdio.h>
// NOTICE ON WIN32:
// typedef DWORD unsigned long;
// sizeof(DWORD) = 4
@@ -33,7 +36,6 @@ typedef struct _DDCOLORKEY
unsigned long dwColorSpaceHighValue;
} DDCOLORKEY;
typedef struct _DDPIXELFORMAT
{
unsigned long dwSize;
@@ -87,49 +89,81 @@ typedef struct _DDSURFACEDESC2
unsigned long dwSize;
unsigned long dwFlags;
unsigned long dwHeight;
unsigned long dwWidth;
union
unsigned long dwWidth;
union
{
long lPitch;
unsigned long dwLinearSize;
};
unsigned long dwBackBufferCount;
union
{
unsigned long dwBackBufferCount;
unsigned long dwDepth;
};
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;
unsigned long dwReserved;
unsigned long* lpSurface;
DDCOLORKEY ddckCKDestOverlay;
DDCOLORKEY ddckCKDestBlt;
DDCOLORKEY ddckCKSrcOverlay;
DDCOLORKEY ddckCKSrcBlt;
DDPIXELFORMAT ddpfPixelFormat;
DDSCAPS2 ddsCaps;
unsigned long dwTextureStage;
} DDSURFACEDESC2;
//
// DDSURFACEDESC2 flags that mark the validity of the struct data
//
#define DDSD_CAPS 0x00000001l // default
#define DDSD_HEIGHT 0x00000002l // default
#define DDSD_WIDTH 0x00000004l // default
#define DDSD_PIXELFORMAT 0x00001000l // default
#define DDSD_PITCH 0x00000008l // For uncompressed formats
#define DDSD_MIPMAPCOUNT 0x00020000l
#define DDSD_LINEARSIZE 0x00080000l // For compressed formats
#define DDSD_DEPTH 0x00800000l // Volume Textures
//
// DDPIXELFORMAT flags
//
#define DDPF_ALPHAPIXELS 0x00000001l
#define DDPF_FOURCC 0x00000004l
#define DDPF_RGB 0x00000040l
#define DDPF_FOURCC 0x00000004l // Compressed formats
#define DDPF_RGB 0x00000040l // Uncompressed formats
#define DDPF_ALPHA 0x00000002l
#define DDPF_COMPRESSED 0x00000080l
#define DDPF_LUMINANCE 0x00020000l
//
// DDSCAPS flags
//
#define DDSCAPS_TEXTURE 0x00001000l // default
#define DDSCAPS_COMPLEX 0x00000008l
#define DDSCAPS_MIPMAP 0x00400000l
#define DDSCAPS2_VOLUME 0x00200000l
#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 ))
#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
*/
* 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)
{
@@ -153,7 +187,7 @@ osg::Image* ReadDDSFile(const char *filename)
// Get the surface desc.
fread(&ddsd, sizeof(ddsd), 1, fp);
// Read image data.
// Size of 2d images - 3d images don't set dwLinearSize
unsigned int size = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * (ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1 ? 2: 4) : ddsd.dwLinearSize;
if(size <= 0)
@@ -161,86 +195,109 @@ osg::Image* ReadDDSFile(const char *filename)
osg::notify(osg::WARN)<<"Warning:: dwLinearSize is not defined in dds file, image not loaded."<<std::endl;
return NULL;
}
osg::ref_ptr<osg::Image> osgImage = new osg::Image();
osgImage->setFileName(filename);
unsigned char* imageData = new unsigned char [size];
fread(imageData, 1, size, fp);
// Close the file.
fclose(fp);
//Check valid structure sizes
if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32)
{
fclose(fp);
return NULL;
}
bool is3dImage = false;
int depth = 1;
// Check for volume image
if( ddsd.dwDepth > 0 && (ddsd.dwFlags & DDSD_DEPTH))
{
is3dImage = true;
depth = ddsd.dwDepth;
}
// Retreive image properties.
int s = ddsd.dwWidth;
int t = ddsd.dwHeight;
int r = 1; // we're not a 3d texture...
int r = depth;
unsigned int dataType = GL_UNSIGNED_BYTE;
unsigned int pixelFormat = 0;
unsigned int internalFormat = 0;
// Uncompressed formats will usually use DDPF_RGB to indicate an RGB format,
// while compressed formats will use DDPF_FOURCC with a four-character code.
bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS;
// Uncompressed formats.
if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB)
{
bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS;
switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
{
case 32:
internalFormat = usingAlpha ? 4 : 3;
pixelFormat = usingAlpha ? GL_RGBA : GL_RGB;
break;
case 24:
case 16:
default:
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<<std::endl;
delete [] imageData;
return NULL;
case 32:
internalFormat = usingAlpha ? 4 : 3;
pixelFormat = usingAlpha ? GL_RGBA : GL_RGB;
break;
case 24:
case 16:
default:
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded"<<std::endl;
return NULL;
}
}
// Compressed formats.
else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_LUMINANCE)
{
internalFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;
pixelFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;
}
// Compressed formats
else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
{
bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS;
switch(ddsd.ddpfPixelFormat.dwFourCC)
{
case FOURCC_DXT1:
if (usingAlpha)
{
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
}
else
{
internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGB_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:
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<<std::endl;
delete [] imageData;
return NULL;
case FOURCC_DXT1:
if (usingAlpha)
{
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
}
else
{
internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGB_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:
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<<std::endl;
return NULL;
}
}
else
{
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<<std::endl;
delete [] imageData;
return NULL;
}
if (is3dImage)
size = osg::Image::computeNumComponents(pixelFormat) * ddsd.dwWidth * ddsd.dwHeight * depth;
//delayed allocation og image data after all checks
unsigned char* imageData = new unsigned char [size];
// Read image data
int bytesRead = fread(imageData, 1, size, fp);
// Close the file.
fclose(fp);
// NOTE: We need to set the image data before setting the mipmap data, this
// is because the setImage method clears the _mipmapdata vector in osg::Image.
// Set image data and properties.
@@ -258,13 +315,13 @@ osg::Image* ReadDDSFile(const char *filename)
//debugging messages
float power2_s = logf((float)s)/logf((float)2);
float power2_t = logf((float)t)/logf((float)2);
osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.dwMipMapCount = "<<ddsd.dwMipMapCount<<std::endl;
osg::notify(osg::INFO) << "ReadDDSFile info : s = "<<s<<std::endl;
osg::notify(osg::INFO) << "ReadDDSFile info : t = "<<t<<std::endl;
osg::notify(osg::INFO) << "ReadDDSFile info : power2_s="<<power2_s<<std::endl;
osg::notify(osg::INFO) << "ReadDDSFile info : power2_t="<<power2_t<<std::endl;
mipmaps.resize((unsigned int)osg::maximum(power2_s,power2_t),0);
// Handle compressed mipmaps.
@@ -272,7 +329,7 @@ osg::Image* ReadDDSFile(const char *filename)
{
int width = ddsd.dwWidth;
int height = ddsd.dwHeight;
int blockSize = (ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1) ? 8 : 16;
int blockSize = (ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1) ? 8 : 16;
int offset = 0;
for (unsigned int k = 1; k < ddsd.dwMipMapCount && (width || height); ++k)
{
@@ -280,7 +337,7 @@ osg::Image* ReadDDSFile(const char *filename)
width = 1;
if (height == 0)
height = 1;
size = ((width+3)/4)*((height+3)/4)*blockSize;
size = ((width+3)/4) * ((height+3)/4) * blockSize;
offset += size;
mipmaps[k-1] = offset;
width >>= 1;
@@ -314,34 +371,278 @@ osg::Image* ReadDDSFile(const char *filename)
return osgImage.release();
}
bool WriteDDSFile(const osg::Image *img, const char *filename)
{
FILE *fp = fopen(filename, "wb");
if (!fp)
return false;
// Initialize ddsd structure and its members
DDSURFACEDESC2 ddsd = {0};
DDPIXELFORMAT ddpf = {0};
DDCOLORKEY ddckCKDestOverlay = {0};
DDCOLORKEY ddckCKDestBlt = {0};
DDCOLORKEY ddckCKSrcOverlay = {0};
DDCOLORKEY ddckCKSrcBlt = {0};
DDSCAPS2 ddsCaps = {0};
ddsd.dwSize = sizeof(ddsd);
ddpf.dwSize = sizeof(ddpf);
// Default values and initialization of structures' flags
unsigned int SD_flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
unsigned int CAPS_flags = DDSCAPS_TEXTURE;
unsigned int PF_flags = 0;
unsigned int CAPS2_flags = 0;
// Get image properties
unsigned int dataType = img->getDataType();
unsigned int pixelFormat = img->getPixelFormat();
unsigned int internalFormat = img->getInternalTextureFormat();
unsigned int components = osg::Image::computeNumComponents(pixelFormat);
unsigned int pixelSize = osg::Image::computePixelSizeInBits(pixelFormat, dataType);
unsigned int imageSize = img->getImageSizeInBytes() / components;
bool is3dImage = false;
int s = ddsd.dwWidth = img->s();
int t = ddsd.dwHeight = img->t();
int r = img->r();
if(r > 1) /* check for 3d image */
{
is3dImage = true;
ddsd.dwDepth = r;
SD_flags |= DDSD_DEPTH;
CAPS_flags |= DDSCAPS_COMPLEX;
CAPS2_flags |= DDSCAPS2_VOLUME;
}
// Determine format - set flags and ddsd, ddpf properties
switch (pixelFormat)
{
//Uncompressed
case GL_RGBA:
{
ddpf.dwRBitMask = 0x00ff0000; //1st change
ddpf.dwGBitMask = 0x0000ff00;
ddpf.dwBBitMask = 0x000000ff;
ddpf.dwRGBAlphaBitMask = 0xff000000;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB);
ddpf.dwRGBBitCount = pixelSize;
ddsd.lPitch = img->getRowSizeInBytes();
SD_flags |= DDSD_PITCH;
}
break;
case GL_BGRA:
{
ddpf.dwBBitMask = 0x00ff0000; //2nd change
ddpf.dwGBitMask = 0x0000ff00;
ddpf.dwRBitMask = 0x000000ff;
ddpf.dwRGBAlphaBitMask = 0xff000000;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB);
ddpf.dwRGBBitCount = pixelSize;
ddsd.lPitch = img->getRowSizeInBytes();
SD_flags |= DDSD_PITCH;
}
break;
case GL_LUMINANCE_ALPHA:
{
ddpf.dwRBitMask = 0x00ff0000;
ddpf.dwRGBAlphaBitMask = 0x000000ff;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_LUMINANCE); //3rd change
ddpf.dwRGBBitCount = pixelSize;
ddsd.lPitch = img->getRowSizeInBytes();
SD_flags |= DDSD_PITCH;
}
break;
case GL_RGB:
{
ddpf.dwRBitMask = 0x00ff0000;
ddpf.dwGBitMask = 0x0000ff00;
ddpf.dwBBitMask = 0x000000ff;
PF_flags |= DDPF_RGB;
ddpf.dwRGBBitCount = pixelSize;
ddsd.lPitch = img->getRowSizeInBytes();
SD_flags |= DDSD_PITCH;
}
break;
case GL_LUMINANCE:
{
ddpf.dwRBitMask = 0x00ff0000;
PF_flags |= DDPF_LUMINANCE;
ddpf.dwRGBBitCount = pixelSize;
ddsd.lPitch = img->getRowSizeInBytes();
SD_flags |= DDSD_PITCH;
}
break;
//Compressed
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
{
ddpf.dwFourCC = FOURCC_DXT1;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC);
ddsd.dwLinearSize = imageSize;
SD_flags |= DDSD_LINEARSIZE;
}
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
{
ddpf.dwFourCC = FOURCC_DXT3;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC);
ddsd.dwLinearSize = imageSize;
SD_flags |= DDSD_LINEARSIZE;
}
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
{
ddpf.dwFourCC = FOURCC_DXT5;
PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC);
ddsd.dwLinearSize = imageSize;
SD_flags |= DDSD_LINEARSIZE;
}
break;
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
{
ddpf.dwFourCC = FOURCC_DXT1;
PF_flags |= DDPF_FOURCC; /* No alpha here */
ddsd.dwLinearSize = imageSize;
SD_flags |= DDSD_LINEARSIZE;
}
break;
default:
osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in image, file cannot be written."<<std::endl;
return false;
}
// set even more flags
if(img->isMipmap() && !is3dImage)
{
SD_flags |= DDSD_MIPMAPCOUNT;
CAPS_flags |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
ddsd.dwMipMapCount = img->getNumMipmapLevels();
}
// Assign flags and structure members of ddsd
ddsd.dwFlags = SD_flags;
ddpf.dwFlags = PF_flags;
ddsCaps.dwCaps = CAPS_flags;
ddsCaps.dwCaps2 = CAPS2_flags;
ddsd.ddpfPixelFormat = ddpf;
ddsd.ddsCaps = ddsCaps;
// Write DDS file
unsigned char *header = new unsigned char[128];
char *ddsName = "DDS ";
memcpy(header, ddsName, 4);
memcpy(header+4, &ddsd, sizeof(ddsd));
fwrite(header, sizeof(ddsd)+4, 1, fp); /* write file header */
int isize = img->getTotalSizeInBytesIncludingMipmaps();
unsigned char *buffer = new unsigned char[isize];
unsigned char *dataPtr = buffer;
if(!is3dImage)
{
// Copy main image data //
memcpy(dataPtr, img->data(), imageSize);
// Handle mipmaps if any
if(img->isMipmap())
{
dataPtr += imageSize;
unsigned char *mmdPtr, *next_mmdPtr;
int offset;
unsigned int mipmaps = img->getNumMipmapLevels();
unsigned int blockSize = (pixelFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
mmdPtr = img->getMipmapData(1); /* get the first mipmap address */
for(unsigned int mID = 1 ; mID < mipmaps; mID++)
{
// the last mipmap is 1x1 so we assign size equal to the blockSize
next_mmdPtr = (mID == mipmaps-1) ? (mmdPtr+blockSize) : img->getMipmapData(mID+1);
offset = next_mmdPtr - mmdPtr;
memcpy(dataPtr, mmdPtr, offset);
mmdPtr += offset;
dataPtr += offset;
}
}
}
else /* 3d image */
{
for(int i = 0; i < r; ++i)
{
memcpy(dataPtr, img->data(0, 0, i), imageSize*components); //4th change
dataPtr += imageSize*components;
}
}
// Check for correct saving
if (fwrite(buffer, isize , 1, fp) != 1)
{
fclose(fp);
return false;
}
// close file pointer
fclose(fp);
// If we get that far the file was saved properly //
return true;
}
class ReaderWriterDDS : public osgDB::ReaderWriter
{
public:
virtual const char* className()
{
return "DDS Image Reader";
}
public:
virtual const char* className()
{
return "DDS Image Reader/Writer";
}
virtual bool acceptsExtension(const std::string& extension)
{
return osgDB::equalCaseInsensitive(extension,"dds");
}
virtual bool acceptsExtension(const std::string& extension)
{
return osgDB::equalCaseInsensitive(extension,"dds");
}
virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options*)
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options*)
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
std::string fileName = osgDB::findDataFile( file );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
osg::Image* osgImage = ReadDDSFile(fileName.c_str());
if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED;
return osgImage;
osg::Image* osgImage = ReadDDSFile(fileName.c_str());
if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED;
return osgImage;
}
}
virtual WriteResult writeImage(const osg::Image &img,const std::string& file, const osgDB::ReaderWriter::Options*)
{
std::string ext = osgDB::getFileExtension(file);
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
bool success = WriteDDSFile(&img, file.c_str());
if(success)
return WriteResult::FILE_SAVED;
else
return WriteResult::ERROR_IN_WRITING_FILE;
}
};
// now register with Registry to instantiate the above
// reader/writer.
osgDB::RegisterReaderWriterProxy<ReaderWriterDDS> g_readerWriter_DDS_Proxy;
osgDB::RegisterReaderWriterProxy<ReaderWriterDDS> g_readerWriter_DDS_Proxy;