- cleanup of texture compression. We output stats for textures that we attempt to compress and what percent they were compressed (before and after byte sizes)

This commit is contained in:
Mark Vejvoda 2010-10-28 06:59:43 +00:00
parent de3a92081d
commit df3f56a451
7 changed files with 255 additions and 146 deletions

View File

@ -89,9 +89,11 @@ void CoreData::load(){
waterSplashTexture->getPixmap()->load(dir+"/misc_textures/water_splash.tga");
buttonSmallTexture= renderer.newTexture2D(rsGlobal);
buttonSmallTexture->setForceCompressionDisabled(true);
buttonSmallTexture->getPixmap()->load(dir+"/menu/textures/button_small.tga");
buttonBigTexture= renderer.newTexture2D(rsGlobal);
buttonBigTexture->setForceCompressionDisabled(true);
buttonBigTexture->getPixmap()->load(dir+"/menu/textures/button_big.tga");
//display font

View File

@ -24,20 +24,24 @@ namespace Shared{ namespace Graphics{ namespace Gl{
class TextureGl {
protected:
GLuint handle;
GLuint handle;
public:
TextureGl();
GLuint getHandle() const {return handle;}
void OutputTextureDebugInfo(Texture::Format format, int components, const string path);
void OutputTextureDebugInfo(Texture::Format format, int components, const string path,uint64 rawSize);
};
// =====================================================
// class Texture1DGl
// =====================================================
class Texture1DGl: public Texture1D, public TextureGl{
class Texture1DGl: public Texture1D, public TextureGl {
public:
Texture1DGl();
virtual ~Texture1DGl();
virtual void init(Filter filter, int maxAnisotropy= 1);
virtual void end();
};
@ -48,6 +52,9 @@ public:
class Texture2DGl: public Texture2D, public TextureGl{
public:
Texture2DGl();
virtual ~Texture2DGl();
virtual void init(Filter filter, int maxAnisotropy= 1);
virtual void end();
};
@ -58,6 +65,10 @@ public:
class Texture3DGl: public Texture3D, public TextureGl{
public:
Texture3DGl();
virtual ~Texture3DGl();
virtual void init(Filter filter, int maxAnisotropy= 1);
virtual void end();
};
@ -68,6 +79,10 @@ public:
class TextureCubeGl: public TextureCube, public TextureGl{
public:
TextureCubeGl();
virtual ~TextureCubeGl();
virtual void init(Filter filter, int maxAnisotropy= 1);
virtual void end();
};

View File

@ -24,6 +24,7 @@ using Shared::Platform::int16;
using Shared::Platform::uint16;
using Shared::Platform::int32;
using Shared::Platform::uint32;
using Shared::Platform::uint64;
using Shared::Platform::float32;
namespace Shared{ namespace Graphics{
@ -97,7 +98,7 @@ public:
// class Pixmap1D
// =====================================================
class Pixmap1D{
class Pixmap1D {
protected:
int w;
int components;
@ -124,13 +125,14 @@ public:
uint8 *getPixels() const {return pixels;}
void deletePixels();
string getPath() const { return path;}
uint64 getPixelByteCount() const;
};
// =====================================================
// class Pixmap2D
// =====================================================
class Pixmap2D{
class Pixmap2D {
protected:
int h;
int w;
@ -199,6 +201,7 @@ public:
void copy(const Pixmap2D *sourcePixmap);
void subCopy(int x, int y, const Pixmap2D *sourcePixmap);
string getPath() const { return path;}
uint64 getPixelByteCount() const;
private:
bool doDimensionsAgree(const Pixmap2D *pixmap);
@ -208,7 +211,7 @@ private:
// class Pixmap3D
// =====================================================
class Pixmap3D{
class Pixmap3D {
protected:
int h;
int w;
@ -240,13 +243,14 @@ public:
uint8 *getPixels() const {return pixels;}
void deletePixels();
string getPath() const { return path;}
uint64 getPixelByteCount() const;
};
// =====================================================
// class PixmapCube
// =====================================================
class PixmapCube{
class PixmapCube {
public:
enum Face{
fPositiveX,
@ -276,6 +280,7 @@ public:
const Pixmap2D *getFace(int face) const {return &faces[face];}
void deletePixels();
string getPath(int face) const { return path[face];}
uint64 getPixelByteCount() const;
};
}}//end namespace

View File

@ -61,6 +61,7 @@ protected:
Format format;
bool inited;
bool forceCompressionDisabled;
public:
Texture();
@ -82,6 +83,10 @@ public:
virtual void deletePixels() = 0;
virtual void reseInitState() { inited = false; }
virtual void setForceCompressionDisabled(bool value) { forceCompressionDisabled = value;}
virtual bool getForceCompressionDisabled() const {return forceCompressionDisabled;}
};
// =====================================================
@ -105,7 +110,7 @@ public:
// class Texture2D
// =====================================================
class Texture2D: public Texture{
class Texture2D: public Texture {
protected:
Pixmap2D pixmap;

View File

@ -23,6 +23,8 @@ namespace Shared{ namespace Graphics{ namespace Gl{
using namespace Platform;
const uint64 MIN_BYTES_TO_COMPRESS = 12;
GLint toCompressionFormatGl(GLint format) {
if(Texture::useTextureCompression == false) {
return format;
@ -134,20 +136,31 @@ GLint toInternalFormatGl(Texture::Format format, int components){
}
}
TextureGl::TextureGl() {
handle=0;
}
// =====================================================
// class Texture1DGl
// =====================================================
Texture1DGl::Texture1DGl() {}
void Texture1DGl::init(Filter filter, int maxAnisotropy){
Texture1DGl::~Texture1DGl() {
end();
}
void Texture1DGl::init(Filter filter, int maxAnisotropy) {
assertGl();
if(!inited) {
if(inited == false) {
//params
GLint wrap= toWrapModeGl(wrapMode);
GLint glFormat= toFormatGl(format, pixmap.getComponents());
GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents());
GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat);
if(forceCompressionDisabled == true || (pixmap.getPixelByteCount() > 0 && pixmap.getPixelByteCount() <= MIN_BYTES_TO_COMPRESS)) {
glCompressionFormat = glInternalFormat;
}
//pixel init var
const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL;
@ -164,7 +177,7 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
}
if(mipmap){
if(mipmap) {
GLuint glFilter= filter==fTrilinear? GL_LINEAR_MIPMAP_LINEAR: GL_LINEAR_MIPMAP_NEAREST;
//build mipmaps
@ -175,14 +188,14 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){
GL_TEXTURE_1D, glCompressionFormat, pixmap.getW(),
glFormat, GL_UNSIGNED_BYTE, pixels);
if(error!=0){
if(error != 0) {
//throw runtime_error("Error building texture 1D mipmaps");
char szBuf[1024]="";
sprintf(szBuf,"Error building texture 1D mipmaps, returned: %d [%s] w = %d",error,pixmap.getPath().c_str(),pixmap.getW());
throw runtime_error(szBuf);
}
}
else{
else {
//build single texture
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -200,17 +213,20 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){
}
}
inited= true;
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath());
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath(),pixmap.getPixelByteCount());
}
assertGl();
}
void Texture1DGl::end(){
if(inited){
void Texture1DGl::end() {
if(inited == true) {
assertGl();
glDeleteTextures(1, &handle);
assertGl();
handle=0;
inited=false;
deletePixels();
}
}
@ -218,16 +234,24 @@ void Texture1DGl::end(){
// class Texture2DGl
// =====================================================
void Texture2DGl::init(Filter filter, int maxAnisotropy){
Texture2DGl::Texture2DGl() {}
Texture2DGl::~Texture2DGl() {
end();
}
void Texture2DGl::init(Filter filter, int maxAnisotropy) {
assertGl();
if(!inited) {
if(inited == false) {
//params
GLint wrap= toWrapModeGl(wrapMode);
GLint glFormat= toFormatGl(format, pixmap.getComponents());
GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents());
GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat);
if(forceCompressionDisabled == true || (pixmap.getPixelByteCount() > 0 && pixmap.getPixelByteCount() <= MIN_BYTES_TO_COMPRESS)) {
glCompressionFormat = glInternalFormat;
}
//pixel init var
const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL;
@ -241,11 +265,11 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
//maxAnisotropy
if(isGlExtensionSupported("GL_EXT_texture_filter_anisotropic")){
if(isGlExtensionSupported("GL_EXT_texture_filter_anisotropic")) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
}
if(mipmap){
if(mipmap) {
GLuint glFilter= filter==fTrilinear? GL_LINEAR_MIPMAP_LINEAR: GL_LINEAR_MIPMAP_NEAREST;
//build mipmaps
@ -257,14 +281,14 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){
pixmap.getW(), pixmap.getH(),
glFormat, GL_UNSIGNED_BYTE, pixels);
if(error!=0){
if(error != 0) {
//throw runtime_error("Error building texture 2D mipmaps");
char szBuf[1024]="";
sprintf(szBuf,"Error building texture 2D mipmaps, returned: %d [%s] w = %d, h = %d",error,(pixmap.getPath() != "" ? pixmap.getPath().c_str() : this->path.c_str()),pixmap.getW(),pixmap.getH());
throw runtime_error(szBuf);
}
}
else{
else {
//build single texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -277,25 +301,28 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){
//throw runtime_error("TEST!");
if(error!=GL_NO_ERROR){
if(error != GL_NO_ERROR) {
char szBuf[1024]="";
sprintf(szBuf,"Error creating texture 2D, returned: %d [%s] w = %d, h = %d, glInternalFormat = %d, glFormat = %d",error,pixmap.getPath().c_str(),pixmap.getW(),pixmap.getH(),glInternalFormat,glFormat);
throw runtime_error(szBuf);
}
}
inited= true;
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath());
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath(),pixmap.getPixelByteCount());
}
assertGl();
}
void Texture2DGl::end(){
if(inited) {
void Texture2DGl::end() {
if(inited == true) {
//printf("==> Deleting GL Texture [%s] handle = %d\n",getPath().c_str(),handle);
assertGl();
glDeleteTextures(1, &handle);
assertGl();
handle=0;
inited=false;
deletePixels();
}
}
@ -303,16 +330,24 @@ void Texture2DGl::end(){
// class Texture3DGl
// =====================================================
void Texture3DGl::init(Filter filter, int maxAnisotropy){
Texture3DGl::Texture3DGl() {}
Texture3DGl::~Texture3DGl() {
end();
}
void Texture3DGl::init(Filter filter, int maxAnisotropy) {
assertGl();
if(!inited){
if(inited == false) {
//params
GLint wrap= toWrapModeGl(wrapMode);
GLint glFormat= toFormatGl(format, pixmap.getComponents());
GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents());
GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat);
if(forceCompressionDisabled == true || (pixmap.getPixelByteCount() > 0 && pixmap.getPixelByteCount() <= MIN_BYTES_TO_COMPRESS)) {
glCompressionFormat = glInternalFormat;
}
//pixel init var
const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL;
@ -336,7 +371,7 @@ void Texture3DGl::init(Filter filter, int maxAnisotropy){
0, glFormat, GL_UNSIGNED_BYTE, pixels);
GLint error= glGetError();
if(error!=GL_NO_ERROR){
if(error != GL_NO_ERROR) {
//throw runtime_error("Error creating texture 3D");
char szBuf[1024]="";
sprintf(szBuf,"Error creating texture 3D, returned: %d [%s] w = %d, h = %d, d = %d",error,pixmap.getPath().c_str(),pixmap.getW(),pixmap.getH(),pixmap.getD());
@ -344,17 +379,21 @@ void Texture3DGl::init(Filter filter, int maxAnisotropy){
}
inited= true;
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath());
OutputTextureDebugInfo(format, pixmap.getComponents(),getPath(),pixmap.getPixelByteCount());
}
assertGl();
}
void Texture3DGl::end(){
if(inited){
void Texture3DGl::end() {
if(inited == true) {
assertGl();
glDeleteTextures(1, &handle);
assertGl();
handle=0;
inited=false;
deletePixels();
}
}
@ -362,11 +401,16 @@ void Texture3DGl::end(){
// class TextureCubeGl
// =====================================================
void TextureCubeGl::init(Filter filter, int maxAnisotropy){
TextureCubeGl::TextureCubeGl() {}
TextureCubeGl::~TextureCubeGl() {
end();
}
void TextureCubeGl::init(Filter filter, int maxAnisotropy) {
assertGl();
if(!inited){
if(inited == false) {
//gen texture
glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_CUBE_MAP, handle);
@ -377,42 +421,45 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, wrap);
//filter
if(mipmap){
if(mipmap) {
GLuint glFilter= filter==fTrilinear? GL_LINEAR_MIPMAP_LINEAR: GL_LINEAR_MIPMAP_NEAREST;
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, glFilter);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else{
else {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
for(int i=0; i<6; ++i){
for(int i = 0; i < 6; ++i) {
//params
const Pixmap2D *currentPixmap= pixmap.getFace(i);
GLint glFormat= toFormatGl(format, currentPixmap->getComponents());
GLint glInternalFormat= toInternalFormatGl(format, currentPixmap->getComponents());
GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat);
if(forceCompressionDisabled == true || (currentPixmap->getPixelByteCount() > 0 && currentPixmap->getPixelByteCount() <= MIN_BYTES_TO_COMPRESS)) {
glCompressionFormat = glInternalFormat;
}
//pixel init var
const uint8* pixels= pixmapInit? currentPixmap->getPixels(): NULL;
GLenum target= GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
if(mipmap){
if(mipmap) {
int error= gluBuild2DMipmaps(
target, glCompressionFormat,
currentPixmap->getW(), currentPixmap->getH(),
glFormat, GL_UNSIGNED_BYTE, pixels);
if(error!=0){
if(error != 0) {
//throw runtime_error("Error building texture cube mipmaps");
char szBuf[1024]="";
sprintf(szBuf,"Error building texture cube mipmaps, returned: %d [%s] w = %d, h = %d",error,currentPixmap->getPath().c_str(),currentPixmap->getW(),currentPixmap->getH());
throw runtime_error(szBuf);
}
}
else{
else {
glTexImage2D(
target, 0, glCompressionFormat,
currentPixmap->getW(), currentPixmap->getH(),
@ -420,14 +467,14 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){
}
int error = glGetError();
if(error!=GL_NO_ERROR){
if(error != GL_NO_ERROR) {
//throw runtime_error("Error creating texture cube");
char szBuf[1024]="";
sprintf(szBuf,"Error creating texture cube, returned: %d [%s] w = %d, h = %d",error,currentPixmap->getPath().c_str(),currentPixmap->getW(),currentPixmap->getH());
throw runtime_error(szBuf);
}
OutputTextureDebugInfo(format, currentPixmap->getComponents(),getPath());
OutputTextureDebugInfo(format, currentPixmap->getComponents(),getPath(),currentPixmap->getPixelByteCount());
}
inited= true;
@ -436,19 +483,23 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){
assertGl();
}
void TextureCubeGl::end(){
if(inited){
void TextureCubeGl::end() {
if(inited == true) {
assertGl();
glDeleteTextures(1, &handle);
assertGl();
handle=0;
inited=false;
deletePixels();
}
}
void TextureGl::OutputTextureDebugInfo(Texture::Format format, int components,const string path) {
void TextureGl::OutputTextureDebugInfo(Texture::Format format, int components,const string path,uint64 rawSize) {
if(Texture::useTextureCompression == true) {
GLint glFormat= toFormatGl(format, components);
printf("**** Texture filename: [%s] format = %d components = %d, glFormat = %d\n",path.c_str(),format,components,glFormat);
printf("**** Texture filename: [%s] format = %d components = %d, glFormat = %d, rawSize = %llu\n",path.c_str(),format,components,glFormat,(long long unsigned int)rawSize);
GLint compressed=0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &compressed);
@ -456,10 +507,17 @@ void TextureGl::OutputTextureDebugInfo(Texture::Format format, int components,co
printf("**** Texture compressed status: %d, error [%d]\n",compressed,error);
bool isCompressed = (compressed == 1);
compressed=0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressed);
error = glGetError();
printf("**** Texture image size in video RAM: %d, error [%d]\n",compressed,error);
double percent = 0;
if(isCompressed == true) {
percent = ((double)compressed / (double)rawSize) * (double)100.0;
}
printf("**** Texture image size in video RAM: %d [%.2f%%], error [%d]\n",compressed,percent,error);
compressed=0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &compressed);

View File

@ -84,19 +84,20 @@ const int tgaUncompressedBw= 3;
// class PixmapIoTga
// =====================================================
PixmapIoTga::PixmapIoTga(){
PixmapIoTga::PixmapIoTga() {
file= NULL;
}
PixmapIoTga::~PixmapIoTga(){
if(file!=NULL){
PixmapIoTga::~PixmapIoTga() {
if(file != NULL) {
fclose(file);
file=NULL;
}
}
void PixmapIoTga::openRead(const string &path){
void PixmapIoTga::openRead(const string &path) {
file= fopen(path.c_str(),"rb");
if (file==NULL){
if (file == NULL) {
throw runtime_error("Can't open TGA file: "+ path);
}
@ -105,53 +106,53 @@ void PixmapIoTga::openRead(const string &path){
size_t readBytes = fread(&fileHeader, sizeof(TargaFileHeader), 1, file);
//check that we can load this tga file
if(fileHeader.idLength!=0){
if(fileHeader.idLength != 0) {
throw runtime_error(path + ": id field is not 0");
}
if(fileHeader.dataTypeCode!=tgaUncompressedRgb && fileHeader.dataTypeCode!=tgaUncompressedBw){
if(fileHeader.dataTypeCode != tgaUncompressedRgb && fileHeader.dataTypeCode != tgaUncompressedBw) {
throw runtime_error(path + ": only uncompressed BW and RGB targa images are supported");
}
//check bits per pixel
if(fileHeader.bitsPerPixel!=8 && fileHeader.bitsPerPixel!=24 && fileHeader.bitsPerPixel!=32){
if(fileHeader.bitsPerPixel != 8 && fileHeader.bitsPerPixel != 24 && fileHeader.bitsPerPixel !=32) {
throw runtime_error(path + ": only 8, 24 and 32 bit targa images are supported");
}
h= fileHeader.height;
w= fileHeader.width;
components= fileHeader.bitsPerPixel/8;
components= fileHeader.bitsPerPixel / 8;
}
void PixmapIoTga::read(uint8 *pixels){
void PixmapIoTga::read(uint8 *pixels) {
read(pixels, components);
}
void PixmapIoTga::read(uint8 *pixels, int components){
for(int i=0; i<h*w*components; i+=components){
uint8 r, g, b, a, l;
void PixmapIoTga::read(uint8 *pixels, int components) {
for(int i=0; i<h*w*components; i+=components) {
uint8 r=0, g=0, b=0, a=0, l=0;
if(this->components==1){
if(this->components == 1) {
size_t readBytes = fread(&l, 1, 1, file);
r= l;
g= l;
b= l;
a= 255;
}
else{
else {
size_t readBytes = fread(&b, 1, 1, file);
readBytes = fread(&g, 1, 1, file);
readBytes = fread(&r, 1, 1, file);
if(this->components==4){
if(this->components == 4) {
readBytes = fread(&a, 1, 1, file);
}
else{
else {
a= 255;
}
l= (r+g+b)/3;
}
switch(components){
switch(components) {
case 1:
pixels[i]= l;
break;
@ -170,13 +171,13 @@ void PixmapIoTga::read(uint8 *pixels, int components){
}
}
void PixmapIoTga::openWrite(const string &path, int w, int h, int components){
void PixmapIoTga::openWrite(const string &path, int w, int h, int components) {
this->w= w;
this->h= h;
this->components= components;
file= fopen(path.c_str(),"wb");
if (file==NULL){
if (file == NULL) {
throw runtime_error("Can't open TGA file: "+ path);
}
@ -191,12 +192,12 @@ void PixmapIoTga::openWrite(const string &path, int w, int h, int components){
fwrite(&fileHeader, sizeof(TargaFileHeader), 1, file);
}
void PixmapIoTga::write(uint8 *pixels){
if(components==1){
void PixmapIoTga::write(uint8 *pixels) {
if(components == 1) {
fwrite(pixels, h*w, 1, file);
}
else{
for(int i=0; i<h*w*components; i+=components){
else {
for(int i=0; i<h*w*components; i+=components) {
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);
fwrite(&pixels[i], 1, 1, file);
@ -211,13 +212,14 @@ void PixmapIoTga::write(uint8 *pixels){
// class PixmapIoBmp
// =====================================================
PixmapIoBmp::PixmapIoBmp(){
PixmapIoBmp::PixmapIoBmp() {
file= NULL;
}
PixmapIoBmp::~PixmapIoBmp(){
PixmapIoBmp::~PixmapIoBmp() {
if(file!=NULL){
fclose(file);
file=NULL;
}
}
@ -246,12 +248,12 @@ void PixmapIoBmp::openRead(const string &path){
components= 3;
}
void PixmapIoBmp::read(uint8 *pixels){
void PixmapIoBmp::read(uint8 *pixels) {
read(pixels, 3);
}
void PixmapIoBmp::read(uint8 *pixels, int components){
for(int i=0; i<h*w*components; i+=components){
void PixmapIoBmp::read(uint8 *pixels, int components) {
for(int i=0; i<h*w*components; i+=components) {
uint8 r, g, b;
size_t readBytes = fread(&b, 1, 1, file);
readBytes = fread(&g, 1, 1, file);
@ -276,13 +278,13 @@ void PixmapIoBmp::read(uint8 *pixels, int components){
}
}
void PixmapIoBmp::openWrite(const string &path, int w, int h, int components){
void PixmapIoBmp::openWrite(const string &path, int w, int h, int components) {
this->w= w;
this->h= h;
this->components= components;
file= fopen(path.c_str(),"wb");
if (file==NULL){
if (file == NULL) {
throw runtime_error("Can't open BMP file for writting: "+ path);
}
@ -311,7 +313,7 @@ void PixmapIoBmp::openWrite(const string &path, int w, int h, int components){
fwrite(&infoHeader, sizeof(BitmapInfoHeader), 1, file);
}
void PixmapIoBmp::write(uint8 *pixels){
void PixmapIoBmp::write(uint8 *pixels) {
for (int i=0; i<h*w*components; i+=components){
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);
@ -349,7 +351,11 @@ void Pixmap1D::init(int components){
void Pixmap1D::init(int w, int components){
this->w= w;
this->components= components;
pixels= new uint8[w*components];
pixels= new uint8[getPixelByteCount()];
}
uint64 Pixmap1D::getPixelByteCount() const {
return (w * components);
}
void Pixmap1D::deletePixels() {
@ -361,72 +367,72 @@ Pixmap1D::~Pixmap1D(){
deletePixels();
}
void Pixmap1D::load(const string &path){
void Pixmap1D::load(const string &path) {
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
if(extension=="bmp") {
loadBmp(path);
}
else if(extension=="tga"){
else if(extension=="tga") {
loadTga(path);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
else {
throw runtime_error("Unknown pixmap extension: " + extension);
}
this->path = path;
}
void Pixmap1D::loadBmp(const string &path){
void Pixmap1D::loadBmp(const string &path) {
this->path = path;
PixmapIoBmp plb;
plb.openRead(path);
//init
if(plb.getH()==1){
if(plb.getH()==1) {
w= plb.getW();
}
else if(plb.getW()==1){
else if(plb.getW()==1) {
w= plb.getH();
}
else{
else {
throw runtime_error("One of the texture dimensions must be 1");
}
if(components==-1){
if(components == -1) {
components= 3;
}
if(pixels==NULL){
pixels= new uint8[w*components];
if(pixels == NULL) {
pixels= new uint8[getPixelByteCount()];
}
//data
plb.read(pixels, components);
}
void Pixmap1D::loadTga(const string &path){
void Pixmap1D::loadTga(const string &path) {
this->path = path;
PixmapIoTga plt;
plt.openRead(path);
//init
if(plt.getH()==1){
if(plt.getH()==1) {
w= plt.getW();
}
else if(plt.getW()==1){
else if(plt.getW()==1) {
w= plt.getH();
}
else{
else {
throw runtime_error("One of the texture dimensions must be 1");
}
int fileComponents= plt.getComponents();
if(components==-1){
if(components == -1) {
components= fileComponents;
}
if(pixels==NULL){
pixels= new uint8[w*components];
if(pixels == NULL) {
pixels= new uint8[getPixelByteCount()];
}
//read data
@ -465,7 +471,11 @@ void Pixmap2D::init(int w, int h, int components) {
this->w= w;
this->h= h;
this->components= components;
pixels= new uint8[h*w*components];
pixels= new uint8[getPixelByteCount()];
}
uint64 Pixmap2D::getPixelByteCount() const {
return (h * w * components);
}
void Pixmap2D::deletePixels() {
@ -473,7 +483,7 @@ void Pixmap2D::deletePixels() {
pixels = NULL;
}
Pixmap2D::~Pixmap2D(){
Pixmap2D::~Pixmap2D() {
deletePixels();
}
@ -496,14 +506,14 @@ void Pixmap2D::load(const string &path) {
void Pixmap2D::save(const string &path) {
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
if(extension == "bmp") {
saveBmp(path);
}
else if(extension=="tga"){
else if(extension == "tga") {
saveTga(path);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
else {
throw runtime_error("Unknown pixmap extension: " + extension);
}
}
@ -519,28 +529,28 @@ void Pixmap2D::saveTga(const string &path) {
pst.write(pixels);
}
void Pixmap2D::getPixel(int x, int y, uint8 *value) const{
void Pixmap2D::getPixel(int x, int y, uint8 *value) const {
for(int i=0; i<components; ++i){
value[i]= pixels[(w*y+x)*components+i];
}
}
void Pixmap2D::getPixel(int x, int y, float32 *value) const{
for(int i=0; i<components; ++i){
void Pixmap2D::getPixel(int x, int y, float32 *value) const {
for(int i=0; i<components; ++i) {
value[i]= pixels[(w*y+x)*components+i]/255.f;
}
}
void Pixmap2D::getComponent(int x, int y, int component, uint8 &value) const{
void Pixmap2D::getComponent(int x, int y, int component, uint8 &value) const {
value= pixels[(w*y+x)*components+component];
}
void Pixmap2D::getComponent(int x, int y, int component, float32 &value) const{
void Pixmap2D::getComponent(int x, int y, int component, float32 &value) const {
value= pixels[(w*y+x)*components+component]/255.f;
}
//vector get
Vec4f Pixmap2D::getPixel4f(int x, int y) const{
Vec4f Pixmap2D::getPixel4f(int x, int y) const {
Vec4f v(0.f);
for(int i=0; i<components && i<4; ++i){
v.ptr()[i]= pixels[(w*y+x)*components+i]/255.f;
@ -548,7 +558,7 @@ Vec4f Pixmap2D::getPixel4f(int x, int y) const{
return v;
}
Vec3f Pixmap2D::getPixel3f(int x, int y) const{
Vec3f Pixmap2D::getPixel3f(int x, int y) const {
Vec3f v(0.f);
for(int i=0; i<components && i<3; ++i){
v.ptr()[i]= pixels[(w*y+x)*components+i]/255.f;
@ -556,29 +566,29 @@ Vec3f Pixmap2D::getPixel3f(int x, int y) const{
return v;
}
float Pixmap2D::getPixelf(int x, int y) const{
float Pixmap2D::getPixelf(int x, int y) const {
return pixels[(w*y+x)*components]/255.f;
}
float Pixmap2D::getComponentf(int x, int y, int component) const{
float Pixmap2D::getComponentf(int x, int y, int component) const {
float c;
getComponent(x, y, component, c);
return c;
}
void Pixmap2D::setPixel(int x, int y, const uint8 *value){
for(int i=0; i<components; ++i){
void Pixmap2D::setPixel(int x, int y, const uint8 *value) {
for(int i=0; i<components; ++i) {
pixels[(w*y+x)*components+i]= value[i];
}
}
void Pixmap2D::setPixel(int x, int y, const float32 *value){
for(int i=0; i<components; ++i){
void Pixmap2D::setPixel(int x, int y, const float32 *value) {
for(int i=0; i<components; ++i) {
pixels[(w*y+x)*components+i]= static_cast<uint8>(value[i]*255.f);
}
}
void Pixmap2D::setComponent(int x, int y, int component, uint8 value){
void Pixmap2D::setComponent(int x, int y, int component, uint8 value) {
pixels[(w*y+x)*components+component]= value;
}
@ -769,7 +779,11 @@ void Pixmap3D::init(int w, int h, int d, int components){
this->h= h;
this->d= d;
this->components= components;
pixels= new uint8[h*w*d*components];;
pixels= new uint8[getPixelByteCount()];
}
uint64 Pixmap3D::getPixelByteCount() const {
return (h * w * d * components);
}
void Pixmap3D::init(int d, int components){
@ -780,7 +794,7 @@ void Pixmap3D::init(int d, int components){
pixels= NULL;
}
void Pixmap3D::init(int components){
void Pixmap3D::init(int components) {
this->w= -1;
this->h= -1;
this->d= -1;
@ -793,19 +807,19 @@ void Pixmap3D::deletePixels() {
pixels = NULL;
}
Pixmap3D::~Pixmap3D(){
Pixmap3D::~Pixmap3D() {
deletePixels();
}
void Pixmap3D::loadSlice(const string &path, int slice){
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
void Pixmap3D::loadSlice(const string &path, int slice) {
string extension= path.substr(path.find_last_of('.') + 1);
if(extension == "bmp") {
loadSliceBmp(path, slice);
}
else if(extension=="tga"){
else if(extension == "tga") {
loadSliceTga(path, slice);
}
else{
else {
throw runtime_error("Unknown pixmap extension: "+extension);
}
this->path = path;
@ -824,7 +838,7 @@ void Pixmap3D::loadSliceBmp(const string &path, int slice){
components= 3;
}
if(pixels==NULL){
pixels= new uint8[w*h*d*components];
pixels= new uint8[getPixelByteCount()];
}
//data
@ -847,7 +861,7 @@ void Pixmap3D::loadSliceTga(const string &path, int slice){
components= fileComponents;
}
if(pixels==NULL){
pixels= new uint8[w*h*d*components];
pixels= new uint8[getPixelByteCount()];
}
//read data
@ -858,20 +872,29 @@ void Pixmap3D::loadSliceTga(const string &path, int slice){
// class PixmapCube
// =====================================================
void PixmapCube::init(int w, int h, int components){
for(int i=0; i<6; ++i){
void PixmapCube::init(int w, int h, int components) {
for(int i=0; i<6; ++i) {
faces[i].init(w, h, components);
}
}
void PixmapCube::init(int components){
for(int i=0; i<6; ++i){
void PixmapCube::init(int components) {
for(int i=0; i<6; ++i) {
faces[i].init(components);
}
}
//load & save
void PixmapCube::loadFace(const string &path, int face){
uint64 PixmapCube::getPixelByteCount() const {
uint64 result = 0;
for(int i=0; i<6; ++i) {
result += faces[i].getPixelByteCount();
}
return result;
}
//load & save
void PixmapCube::loadFace(const string &path, int face) {
this->path[face] = path;
faces[face].load(path);

View File

@ -22,17 +22,18 @@ namespace Shared{ namespace Graphics{
// class Texture
// =====================================================
const int Texture::defaultSize= 256;
const int Texture::defaultSize = 256;
const int Texture::defaultComponents = 4;
bool Texture::useTextureCompression = false;
bool Texture::useTextureCompression = false;
Texture::Texture(){
Texture::Texture() {
mipmap= true;
pixmapInit= true;
wrapMode= wmRepeat;
format= fAuto;
inited= false;
forceCompressionDisabled=false;
}
// =====================================================
@ -55,7 +56,7 @@ string Texture1D::getPath() const {
}
void Texture1D::deletePixels() {
//printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str());
//printf("+++> Texture1D pixmap deletion for [%s]\n",getPath().c_str());
pixmap.deletePixels();
}
@ -79,7 +80,7 @@ string Texture2D::getPath() const {
}
void Texture2D::deletePixels() {
//printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str());
//printf("+++> Texture2D pixmap deletion for [%s]\n",getPath().c_str());
pixmap.deletePixels();
}
@ -103,7 +104,7 @@ string Texture3D::getPath() const {
}
void Texture3D::deletePixels() {
//printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str());
//printf("+++> Texture3D pixmap deletion for [%s]\n",getPath().c_str());
pixmap.deletePixels();
}
@ -139,7 +140,7 @@ string TextureCube::getPath() const {
}
void TextureCube::deletePixels() {
//printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str());
//printf("+++> TextureCube pixmap deletion for [%s]\n",getPath().c_str());
pixmap.deletePixels();
}