Merge branch 'master' of github.com:supertuxkart/stk-code

This commit is contained in:
hiker 2014-09-11 21:48:44 +10:00
commit 6958539a71
8 changed files with 219 additions and 197 deletions

View File

@ -1,3 +1,4 @@
#if __VERSION__ >= 330
layout(location=0) in vec3 Position;
layout(location = 1) in float lifetime;
layout(location = 2) in float size;
@ -7,6 +8,17 @@ layout(location = 4) in vec2 quadcorner;
layout(location = 5) in vec3 rotationvec;
layout(location = 6) in float anglespeed;
#else
in vec3 Position;
in float lifetime;
in float size;
in vec2 Texcoord;
in vec2 quadcorner;
in vec3 rotationvec;
float anglespeed;
#endif
out float lf;
out vec2 tc;

View File

@ -217,6 +217,8 @@ void setAttribute(AttributeType Tp, GLuint ProgramID)
glBindAttribLocation(ProgramID, 1, "lifetime");
glBindAttribLocation(ProgramID, 2, "size");
glBindAttribLocation(ProgramID, 4, "quadcorner");
glBindAttribLocation(ProgramID, 5, "rotationvec");
glBindAttribLocation(ProgramID, 6, "anglespeed");
break;
}
}
@ -247,182 +249,6 @@ GLuint LoadTFBProgram(const char * vertex_file_path, const char **varyings, unsi
return Program;
}
GLuint getTextureGLuint(irr::video::ITexture *tex)
{
return static_cast<irr::video::COpenGLTexture*>(tex)->getOpenGLTextureName();
}
GLuint getDepthTexture(irr::video::ITexture *tex)
{
assert(tex->isRenderTarget());
return static_cast<irr::video::COpenGLFBOTexture*>(tex)->DepthBufferTexture;
}
std::set<irr::video::ITexture *> AlreadyTransformedTexture;
void resetTextureTable()
{
AlreadyTransformedTexture.clear();
}
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha)
{
if (AlreadyTransformedTexture.find(tex) != AlreadyTransformedTexture.end())
return;
AlreadyTransformedTexture.insert(tex);
glBindTexture(GL_TEXTURE_2D, getTextureGLuint(tex));
std::string cached_file;
if (UserConfigParams::m_texture_compression)
{
// Try to retrieve the compressed texture in cache
std::string tex_name = irr_driver->getTextureName(tex);
if (!tex_name.empty()) {
cached_file = file_manager->getTextureCacheLocation(tex_name) + ".gltz";
if (!file_manager->fileIsNewer(tex_name, cached_file)) {
if (loadCompressedTexture(cached_file))
return;
}
}
}
size_t w = tex->getSize().Width, h = tex->getSize().Height;
unsigned char *data = new unsigned char[w * h * 4];
memcpy(data, tex->lock(), w * h * 4);
tex->unlock();
unsigned internalFormat, Format;
if (tex->hasAlpha())
Format = GL_BGRA;
else
Format = GL_BGR;
if (premul_alpha)
{
for (unsigned i = 0; i < w * h; i++)
{
float alpha = data[4 * i + 3];
if (alpha > 0.)
alpha = pow(alpha / 255.f, 1.f / 2.2f);
data[4 * i ] = (unsigned char)(data[4 * i ] * alpha);
data[4 * i + 1] = (unsigned char)(data[4 * i + 1] * alpha);
data[4 * i + 2] = (unsigned char)(data[4 * i + 2] * alpha);
}
}
if (!UserConfigParams::m_texture_compression)
{
if (srgb)
internalFormat = (tex->hasAlpha()) ? GL_SRGB_ALPHA : GL_SRGB;
else
internalFormat = (tex->hasAlpha()) ? GL_RGBA : GL_RGB;
}
else
{
if (srgb)
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
else
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
}
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, Format, GL_UNSIGNED_BYTE, (GLvoid *)data);
glGenerateMipmap(GL_TEXTURE_2D);
delete[] data;
if (UserConfigParams::m_texture_compression && !cached_file.empty())
{
// Save the compressed texture in the cache for later use.
saveCompressedTexture(cached_file);
}
}
//-----------------------------------------------------------------------------
/** Try to load a compressed texture from the given file name.
* Data in the specified file need to have a specific format. See the
* saveCompressedTexture() function for a description of the format.
* \return true if the loading succeeded, false otherwise.
* \see saveCompressedTexture
*/
bool loadCompressedTexture(const std::string& compressed_tex)
{
std::ifstream ifs(compressed_tex.c_str(), std::ios::in | std::ios::binary);
if (!ifs.is_open())
return false;
int internal_format;
int w, h;
int size = -1;
ifs.read((char*)&internal_format, sizeof(int));
ifs.read((char*)&w, sizeof(int));
ifs.read((char*)&h, sizeof(int));
ifs.read((char*)&size, sizeof(int));
if (ifs.fail() || size == -1)
return false;
char *data = new char[size];
ifs.read(data, size);
if (!ifs.fail())
{
glCompressedTexImage2D(GL_TEXTURE_2D, 0, internal_format,
w, h, 0, size, (GLvoid*)data);
glGenerateMipmap(GL_TEXTURE_2D);
delete[] data;
ifs.close();
return true;
}
delete[] data;
return false;
}
//-----------------------------------------------------------------------------
/** Try to save the last texture sent to glTexImage2D in a file of the given
* file name. This function should only be used for textures sent to
* glTexImage2D with a compressed internal format as argument.<br>
* \note The following format is used to save the compressed texture:<br>
* <internal-format><width><height><size><data> <br>
* The first four elements are integers and the last one is stored
* on \c size bytes.
* \see loadCompressedTexture
*/
void saveCompressedTexture(const std::string& compressed_tex)
{
int internal_format, width, height, size, compressionSuccessful;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint *)&internal_format);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint *)&width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint *)&height);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, (GLint *)&compressionSuccessful);
if (!compressionSuccessful)
return;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, (GLint *)&size);
char *data = new char[size];
glGetCompressedTexImage(GL_TEXTURE_2D, 0, (GLvoid*)data);
std::ofstream ofs(compressed_tex.c_str(), std::ios::out | std::ios::binary);
if (ofs.is_open())
{
ofs.write((char*)&internal_format, sizeof(int));
ofs.write((char*)&width, sizeof(int));
ofs.write((char*)&height, sizeof(int));
ofs.write((char*)&size, sizeof(int));
ofs.write(data, size);
ofs.close();
}
delete[] data;
}
static unsigned colorcount = 0;
video::ITexture* getUnicolorTexture(video::SColor c)
{
video::SColor tmp[4] = {
c, c, c, c
};
video::IImage *img = irr_driver->getVideoDriver()->createImageFromData(video::ECF_A8R8G8B8, core::dimension2d<u32>(2, 2), tmp);
img->grab();
std::string name("color");
name += colorcount++;
return irr_driver->getVideoDriver()->addTexture(name.c_str(), img);
}
void setTexture(unsigned TextureUnit, GLuint TextureId, GLenum MagFilter, GLenum MinFilter, bool allowAF)
{
glActiveTexture(GL_TEXTURE0 + TextureUnit);

View File

@ -129,13 +129,7 @@ public:
// core::rect<s32> needs these includes
#include <rect.h>
#include "utils/vec3.hpp"
GLuint getTextureGLuint(irr::video::ITexture *tex);
GLuint getDepthTexture(irr::video::ITexture *tex);
void resetTextureTable();
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha = false);
bool loadCompressedTexture(const std::string& compressed_tex);
void saveCompressedTexture(const std::string& compressed_tex);
#include "texturemanager.hpp"
void draw3DLine(const core::vector3df& start,
const core::vector3df& end, irr::video::SColor color);

View File

@ -68,6 +68,9 @@ void LODNode::render()
*/
int LODNode::getLevel()
{
if (m_nodes.size() == 0)
return -1;
// If a level is forced, use it
if(m_forced_lod>-1)
return m_forced_lod;
@ -79,14 +82,13 @@ int LODNode::getLevel()
AbstractKart* kart = camera->getKart();
const Vec3 &pos = kart->getFrontXYZ();
// Assumes all children are at the same location
const int dist =
(int)((getPosition() + m_nodes[0]->getPosition()).getDistanceFromSQ( core::vector3df(pos.getX(), pos.getY(), pos.getZ())));
(int)((m_nodes[0]->getAbsolutePosition()).getDistanceFromSQ( core::vector3df(pos.getX(), pos.getY(), pos.getZ())));
for (unsigned int n=0; n<m_detail.size(); n++)
{
if (dist < m_detail[n])
return n;
return n;
}
// If it's the shadow pass, and we would have otherwise hidden the item, show the min one
@ -108,7 +110,7 @@ void LODNode::forceLevelOfDetail(int n)
// ----------------------------------------------------------------------------
void LODNode::OnAnimate(u32 timeMs)
{
if (isVisible())
if (isVisible() && m_nodes.size() > 0)
{
// update absolute position
updateAbsolutePosition();

View File

@ -1512,8 +1512,6 @@ namespace FullScreenShader
RadianceHintsConstructionShader::RadianceHintsConstructionShader()
{
if (irr_driver->getGLSLVersion() < 150)
return;
if (irr_driver->hasVSLayerExtension())
{
Program = LoadProgram(OBJECT,
@ -1534,8 +1532,6 @@ namespace FullScreenShader
NVWorkaroundRadianceHintsConstructionShader::NVWorkaroundRadianceHintsConstructionShader()
{
if (irr_driver->getGLSLVersion() < 150)
return;
Program = LoadProgram(OBJECT,
GL_VERTEX_SHADER, file_manager->getAsset("shaders/slicedscreenquad_nvworkaround.vert").c_str(),
GL_GEOMETRY_SHADER, file_manager->getAsset("shaders/rhpassthrough.geom").c_str(),
@ -1581,9 +1577,6 @@ namespace FullScreenShader
ComputeGaussian17TapHShader::ComputeGaussian17TapHShader()
{
if (irr_driver->hasARBComputeShaders())
return;
Program = LoadProgram(OBJECT,
GL_COMPUTE_SHADER, file_manager->getAsset("shaders/bilateralH.comp").c_str());
TU_source = 0;
@ -1625,8 +1618,6 @@ namespace FullScreenShader
ComputeGaussian17TapVShader::ComputeGaussian17TapVShader()
{
if (irr_driver->hasARBComputeShaders())
return;
Program = LoadProgram(OBJECT,
GL_COMPUTE_SHADER, file_manager->getAsset("shaders/bilateralV.comp").c_str());
TU_source = 0;

View File

@ -405,9 +405,9 @@ parseSceneManager(core::list<scene::ISceneNode*> List, std::vector<scene::IScene
{
if (LODNode *node = dynamic_cast<LODNode *>(*I))
node->updateVisibility();
(*I)->updateAbsolutePosition();
if (!(*I)->isVisible())
continue;
(*I)->updateAbsolutePosition();
if (ParticleSystemProxy *node = dynamic_cast<ParticleSystemProxy *>(*I))
{

View File

@ -0,0 +1,182 @@
#include "texturemanager.hpp"
#include <fstream>
#include <string>
#include "../../lib/irrlicht/source/Irrlicht/COpenGLTexture.h"
#include "irr_driver.hpp"
GLuint getTextureGLuint(irr::video::ITexture *tex)
{
return static_cast<irr::video::COpenGLTexture*>(tex)->getOpenGLTextureName();
}
GLuint getDepthTexture(irr::video::ITexture *tex)
{
assert(tex->isRenderTarget());
return static_cast<irr::video::COpenGLFBOTexture*>(tex)->DepthBufferTexture;
}
std::set<irr::video::ITexture *> AlreadyTransformedTexture;
void resetTextureTable()
{
AlreadyTransformedTexture.clear();
}
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha)
{
if (AlreadyTransformedTexture.find(tex) != AlreadyTransformedTexture.end())
return;
AlreadyTransformedTexture.insert(tex);
glBindTexture(GL_TEXTURE_2D, getTextureGLuint(tex));
std::string cached_file;
if (UserConfigParams::m_texture_compression)
{
// Try to retrieve the compressed texture in cache
std::string tex_name = irr_driver->getTextureName(tex);
if (!tex_name.empty()) {
cached_file = file_manager->getTextureCacheLocation(tex_name) + ".gltz";
if (!file_manager->fileIsNewer(tex_name, cached_file)) {
if (loadCompressedTexture(cached_file))
return;
}
}
}
size_t w = tex->getSize().Width, h = tex->getSize().Height;
unsigned char *data = new unsigned char[w * h * 4];
memcpy(data, tex->lock(), w * h * 4);
tex->unlock();
unsigned internalFormat, Format;
if (tex->hasAlpha())
Format = GL_BGRA;
else
Format = GL_BGR;
if (premul_alpha)
{
for (unsigned i = 0; i < w * h; i++)
{
float alpha = data[4 * i + 3];
if (alpha > 0.)
alpha = pow(alpha / 255.f, 1.f / 2.2f);
data[4 * i] = (unsigned char)(data[4 * i] * alpha);
data[4 * i + 1] = (unsigned char)(data[4 * i + 1] * alpha);
data[4 * i + 2] = (unsigned char)(data[4 * i + 2] * alpha);
}
}
if (!UserConfigParams::m_texture_compression)
{
if (srgb)
internalFormat = (tex->hasAlpha()) ? GL_SRGB_ALPHA : GL_SRGB;
else
internalFormat = (tex->hasAlpha()) ? GL_RGBA : GL_RGB;
}
else
{
if (srgb)
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
else
internalFormat = (tex->hasAlpha()) ? GL_COMPRESSED_RGBA_S3TC_DXT5_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
}
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, Format, GL_UNSIGNED_BYTE, (GLvoid *)data);
glGenerateMipmap(GL_TEXTURE_2D);
delete[] data;
if (UserConfigParams::m_texture_compression && !cached_file.empty())
{
// Save the compressed texture in the cache for later use.
saveCompressedTexture(cached_file);
}
}
//-----------------------------------------------------------------------------
/** Try to load a compressed texture from the given file name.
* Data in the specified file need to have a specific format. See the
* saveCompressedTexture() function for a description of the format.
* \return true if the loading succeeded, false otherwise.
* \see saveCompressedTexture
*/
bool loadCompressedTexture(const std::string& compressed_tex)
{
std::ifstream ifs(compressed_tex.c_str(), std::ios::in | std::ios::binary);
if (!ifs.is_open())
return false;
int internal_format;
int w, h;
int size = -1;
ifs.read((char*)&internal_format, sizeof(int));
ifs.read((char*)&w, sizeof(int));
ifs.read((char*)&h, sizeof(int));
ifs.read((char*)&size, sizeof(int));
if (ifs.fail() || size == -1)
return false;
char *data = new char[size];
ifs.read(data, size);
if (!ifs.fail())
{
glCompressedTexImage2D(GL_TEXTURE_2D, 0, internal_format,
w, h, 0, size, (GLvoid*)data);
glGenerateMipmap(GL_TEXTURE_2D);
delete[] data;
ifs.close();
return true;
}
delete[] data;
return false;
}
//-----------------------------------------------------------------------------
/** Try to save the last texture sent to glTexImage2D in a file of the given
* file name. This function should only be used for textures sent to
* glTexImage2D with a compressed internal format as argument.<br>
* \note The following format is used to save the compressed texture:<br>
* <internal-format><width><height><size><data> <br>
* The first four elements are integers and the last one is stored
* on \c size bytes.
* \see loadCompressedTexture
*/
void saveCompressedTexture(const std::string& compressed_tex)
{
int internal_format, width, height, size, compressionSuccessful;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint *)&internal_format);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint *)&width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint *)&height);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, (GLint *)&compressionSuccessful);
if (!compressionSuccessful)
return;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, (GLint *)&size);
char *data = new char[size];
glGetCompressedTexImage(GL_TEXTURE_2D, 0, (GLvoid*)data);
std::ofstream ofs(compressed_tex.c_str(), std::ios::out | std::ios::binary);
if (ofs.is_open())
{
ofs.write((char*)&internal_format, sizeof(int));
ofs.write((char*)&width, sizeof(int));
ofs.write((char*)&height, sizeof(int));
ofs.write((char*)&size, sizeof(int));
ofs.write(data, size);
ofs.close();
}
delete[] data;
}
static unsigned colorcount = 0;
video::ITexture* getUnicolorTexture(video::SColor c)
{
video::SColor tmp[4] = {
c, c, c, c
};
video::IImage *img = irr_driver->getVideoDriver()->createImageFromData(video::ECF_A8R8G8B8, core::dimension2d<u32>(2, 2), tmp);
img->grab();
std::string name("color");
name += colorcount++;
return irr_driver->getVideoDriver()->addTexture(name.c_str(), img);
}

View File

@ -0,0 +1,15 @@
#ifndef MEMORYMANAGER_HPP
#define MEMORYMANAGER_HPP
#include "gl_headers.hpp"
#include <ITexture.h>
#include <string>
GLuint getTextureGLuint(irr::video::ITexture *tex);
GLuint getDepthTexture(irr::video::ITexture *tex);
void resetTextureTable();
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha = false);
bool loadCompressedTexture(const std::string& compressed_tex);
void saveCompressedTexture(const std::string& compressed_tex);
#endif