From 78f6f49cb6190e7c1999aeb1eb4ba8d7e2dde6e2 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 27 Oct 2010 15:04:47 +0000 Subject: [PATCH] - fixed shadow mapping by removing the use of the extension: GL_ARB_shadow_ambient (thanks: asmodeus and Ishmaru) - added some initial work for texture compression (but not active yet) --- source/glest_game/graphics/renderer.cpp | 16 +++- source/glest_game/main/main.cpp | 1 + .../include/graphics/gl/texture_gl.h | 4 +- .../sources/graphics/gl/texture_gl.cpp | 78 +++++++++++++++++-- source/shared_lib/sources/graphics/pixmap.cpp | 23 +++--- 5 files changed, 101 insertions(+), 21 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 9fe1fd81d..71d31269d 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -299,9 +299,10 @@ void Renderer::initGame(const Game *game){ //shadow mapping glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, 1.0f-shadowAlpha); + //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, 1.0f-shadowAlpha); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, shadowTextureSize, shadowTextureSize, @@ -1355,6 +1356,12 @@ void Renderer::renderSurface(const int renderFps) { glActiveTexture(fowTexUnit); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + + //glCompressedTexSubImage2D( + // GL_TEXTURE_2D, 0, 0, 0, + // fowTex->getPixmap()->getW(), fowTex->getPixmap()->getH(), + // GL_ALPHA, GL_UNSIGNED_BYTE, fowTex->getPixmap()->getPixels()); + glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, fowTex->getPixmap()->getW(), fowTex->getPixmap()->getH(), @@ -2872,7 +2879,8 @@ void Renderer::autoConfig(){ Config &config= Config::getInstance(); bool nvidiaCard= toLower(getGlVendor()).find("nvidia")!=string::npos; bool atiCard= toLower(getGlVendor()).find("ati")!=string::npos; - bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow") && isGlExtensionSupported("GL_ARB_shadow_ambient"); + //bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow") && isGlExtensionSupported("GL_ARB_shadow_ambient"); + bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow"); //3D textures config.setBool("Textures3D", isGlExtensionSupported("GL_EXT_texture3D")); @@ -3349,7 +3357,7 @@ void Renderer::checkGlOptionalCaps() { //shadow mapping if(shadows == sShadowMapping) { checkExtension("GL_ARB_shadow", "Shadow Mapping"); - checkExtension("GL_ARB_shadow_ambient", "Shadow Mapping"); + //checkExtension("GL_ARB_shadow_ambient", "Shadow Mapping"); //checkExtension("GL_ARB_depth_texture", "Shadow Mapping"); } } diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index cff414205..d6c8cc681 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -768,6 +768,7 @@ int glestMain(int argc, char** argv){ if(hasCommandArgument(argc, argv,GAME_ARGS[GAME_ARG_OPENGL_INFO]) == true) { //Renderer &renderer= Renderer::getInstance(); printf("%s",renderer.getGlInfo().c_str()); + printf("%s",renderer.getGlMoreInfo().c_str()); delete mainWindow; return -1; diff --git a/source/shared_lib/include/graphics/gl/texture_gl.h b/source/shared_lib/include/graphics/gl/texture_gl.h index dbd9e9601..ed6bb90b6 100644 --- a/source/shared_lib/include/graphics/gl/texture_gl.h +++ b/source/shared_lib/include/graphics/gl/texture_gl.h @@ -22,12 +22,14 @@ namespace Shared{ namespace Graphics{ namespace Gl{ // class TextureGl // ===================================================== -class TextureGl{ +class TextureGl { protected: GLuint handle; public: GLuint getHandle() const {return handle;} + + void OutputTextureDebugInfo(const Pixmap2D *pixmap,Texture::Format format, int components, const string path); }; // ===================================================== diff --git a/source/shared_lib/sources/graphics/gl/texture_gl.cpp b/source/shared_lib/sources/graphics/gl/texture_gl.cpp index 7de32ce0f..ece719f99 100644 --- a/source/shared_lib/sources/graphics/gl/texture_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/texture_gl.cpp @@ -96,6 +96,44 @@ GLint toInternalFormatGl(Texture::Format format, int components){ } } +GLint toCompressionFormatGl(GLint format) { + return format; + + //GL_COMPRESSED_ALPHA <- white things but tile ok! + //GL_COMPRESSED_LUMINANCE <- black tiles + //GL_COMPRESSED_LUMINANCE_ALPHA <- black tiles + //GL_COMPRESSED_INTENSITY <- black tiles + //GL_COMPRESSED_RGB <- black tiles + //GL_COMPRESSED_RGBA <- black tiles + + // With the following extension (GL_EXT_texture_compression_s3tc) + //GL_COMPRESSED_RGB_S3TC_DXT1_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT1_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT3_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT5_EXT + +/* + switch(format) { + case GL_LUMINANCE: + case GL_LUMINANCE8: + return GL_COMPRESSED_LUMINANCE; + case GL_RGB: + case GL_RGB8: + //return GL_COMPRESSED_RGB; + return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + case GL_RGBA: + case GL_RGBA8: + //return GL_COMPRESSED_RGBA; + return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + case GL_ALPHA: + case GL_ALPHA8: + return GL_COMPRESSED_ALPHA; + default: + return format; + } +*/ +} + // ===================================================== // class Texture1DGl // ===================================================== @@ -103,7 +141,7 @@ GLint toInternalFormatGl(Texture::Format format, int components){ void Texture1DGl::init(Filter filter, int maxAnisotropy){ assertGl(); - if(!inited){ + if(!inited) { //params GLint wrap= toWrapModeGl(wrapMode); @@ -187,6 +225,7 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){ GLint wrap= toWrapModeGl(wrapMode); GLint glFormat= toFormatGl(format, pixmap.getComponents()); GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents()); + GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat); //pixel init var const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL; @@ -212,7 +251,7 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int error= gluBuild2DMipmaps( - GL_TEXTURE_2D, glInternalFormat, + GL_TEXTURE_2D, glCompressionFormat, pixmap.getW(), pixmap.getH(), glFormat, GL_UNSIGNED_BYTE, pixels); @@ -229,10 +268,9 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D( - GL_TEXTURE_2D, 0, glInternalFormat, + GL_TEXTURE_2D, 0, glCompressionFormat, pixmap.getW(), pixmap.getH(), 0, glFormat, GL_UNSIGNED_BYTE, pixels); - GLint error= glGetError(); //throw runtime_error("TEST!"); @@ -242,6 +280,8 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){ 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); } + + OutputTextureDebugInfo(&pixmap,format, pixmap.getComponents(),getPath()); } inited= true; } @@ -348,6 +388,7 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){ GLint glFormat= toFormatGl(format, currentPixmap->getComponents()); GLint glInternalFormat= toInternalFormatGl(format, currentPixmap->getComponents()); + GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat); //pixel init var const uint8* pixels= pixmapInit? currentPixmap->getPixels(): NULL; @@ -355,7 +396,7 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){ if(mipmap){ int error= gluBuild2DMipmaps( - target, glInternalFormat, + target, glCompressionFormat, currentPixmap->getW(), currentPixmap->getH(), glFormat, GL_UNSIGNED_BYTE, pixels); @@ -368,7 +409,7 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){ } else{ glTexImage2D( - target, 0, glInternalFormat, + target, 0, glCompressionFormat, currentPixmap->getW(), currentPixmap->getH(), 0, glFormat, GL_UNSIGNED_BYTE, pixels); } @@ -380,6 +421,8 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){ 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(currentPixmap,format, currentPixmap->getComponents(),getPath()); } inited= true; @@ -396,4 +439,27 @@ void TextureCubeGl::end(){ } } +void TextureGl::OutputTextureDebugInfo(const Pixmap2D *pixmap,Texture::Format format, int components,const string path) { + + GLint glFormat= toFormatGl(format, components); + + printf("**** Texture filename: [%s] format = %d components = %d, glFormat = %d, path [%s]\n",pixmap->getPath().c_str(),format,components,glFormat,path.c_str()); + + GLint compressed=0; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &compressed); + int error = glGetError(); + + printf("**** Texture compressed status: %d, error [%d]\n",compressed,error); + + 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); + + compressed=0; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &compressed); + error = glGetError(); + printf("**** Texture image compression format used: %d, error [%d]\n",compressed,error); +} + }}}//end namespace diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index f76b5d129..5e6734afd 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -434,29 +434,29 @@ void Pixmap1D::loadTga(const string &path){ // ===================== PUBLIC ======================== -Pixmap2D::Pixmap2D(){ +Pixmap2D::Pixmap2D() { h= -1; w= -1; components= -1; pixels= NULL; } -Pixmap2D::Pixmap2D(int components){ +Pixmap2D::Pixmap2D(int components) { init(components); } -Pixmap2D::Pixmap2D(int w, int h, int components){ +Pixmap2D::Pixmap2D(int w, int h, int components) { init(w, h, components); } -void Pixmap2D::init(int components){ +void Pixmap2D::init(int components) { this->w= -1; this->h= -1; this->components= components; pixels= NULL; } -void Pixmap2D::init(int w, int h, int components){ +void Pixmap2D::init(int w, int h, int components) { this->w= w; this->h= h; this->components= components; @@ -468,6 +468,8 @@ Pixmap2D::~Pixmap2D(){ } Pixmap2D* Pixmap2D::loadPath(const string& path) { + printf("Loading Pixmap2D [%s]\n",path.c_str()); + Pixmap2D *pixmap = FileReader::readPath(path); if(pixmap != NULL) { pixmap->path = path; @@ -475,13 +477,14 @@ Pixmap2D* Pixmap2D::loadPath(const string& path) { return pixmap; } -void Pixmap2D::load(const string &path){ +void Pixmap2D::load(const string &path) { + //printf("Loading Pixmap2D [%s]\n",path.c_str()); + FileReader::readPath(path,this); this->path = path; } - -void Pixmap2D::save(const string &path){ +void Pixmap2D::save(const string &path) { string extension= path.substr(path.find_last_of('.')+1); if(extension=="bmp"){ saveBmp(path); @@ -494,13 +497,13 @@ void Pixmap2D::save(const string &path){ } } -void Pixmap2D::saveBmp(const string &path){ +void Pixmap2D::saveBmp(const string &path) { PixmapIoBmp psb; psb.openWrite(path, w, h, components); psb.write(pixels); } -void Pixmap2D::saveTga(const string &path){ +void Pixmap2D::saveTga(const string &path) { PixmapIoTga pst; pst.openWrite(path, w, h, components); pst.write(pixels);