2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-08 14:10:51 +02:00
|
|
|
#include "precompiled.h"
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
#include <map>
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2005-08-12 19:06:53 +02:00
|
|
|
#include "lib/res/graphics/ogl_tex.h"
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/CLogger.h"
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2004-06-08 14:10:51 +02:00
|
|
|
#include "TextureEntry.h"
|
|
|
|
#include "TextureManager.h"
|
2005-08-07 23:58:36 +02:00
|
|
|
#include "TerrainProperties.h"
|
2004-06-08 14:10:51 +02:00
|
|
|
#include "Texture.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2004-06-08 14:10:51 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"graphics"
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2006-09-26 03:44:20 +02:00
|
|
|
std::map<Handle, CTextureEntry *> CTextureEntry::m_LoadedTextures;
|
2004-06-08 14:10:51 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CTextureEntry constructor
|
2009-11-03 22:46:35 +01:00
|
|
|
CTextureEntry::CTextureEntry(CTerrainPropertiesPtr props, const VfsPath& path):
|
2005-08-07 23:58:36 +02:00
|
|
|
m_pProperties(props),
|
2005-06-20 17:14:36 +02:00
|
|
|
m_Bitmap(NULL),
|
|
|
|
m_Handle(-1),
|
|
|
|
m_BaseColor(0),
|
2005-08-07 23:58:36 +02:00
|
|
|
m_BaseColorValid(false),
|
|
|
|
m_TexturePath(path)
|
2004-12-16 13:01:47 +01:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
if (m_pProperties)
|
|
|
|
m_Groups = m_pProperties->GetGroups();
|
2005-06-20 17:14:36 +02:00
|
|
|
|
|
|
|
GroupVector::iterator it=m_Groups.begin();
|
|
|
|
for (;it!=m_Groups.end();++it)
|
2005-08-07 23:58:36 +02:00
|
|
|
(*it)->AddTerrain(this);
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
m_Tag = fs::basename(m_TexturePath);
|
2005-06-20 17:14:36 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CTextureEntry destructor
|
|
|
|
CTextureEntry::~CTextureEntry()
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
2006-09-26 03:44:20 +02:00
|
|
|
std::map<Handle,CTextureEntry *>::iterator it=m_LoadedTextures.find(m_Handle);
|
2005-08-07 23:58:36 +02:00
|
|
|
if (it != m_LoadedTextures.end())
|
|
|
|
m_LoadedTextures.erase(it);
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
if (m_Handle > 0)
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)ogl_tex_free(m_Handle);
|
2005-08-07 23:58:36 +02:00
|
|
|
|
|
|
|
for (GroupVector::iterator it=m_Groups.begin();it!=m_Groups.end();++it)
|
|
|
|
(*it)->RemoveTerrain(this);
|
2004-12-16 13:01:47 +01:00
|
|
|
}
|
|
|
|
|
2004-06-08 14:10:51 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LoadTexture: actually load the texture resource from file
|
|
|
|
void CTextureEntry::LoadTexture()
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
CTexture texture(m_TexturePath);
|
2005-08-07 23:58:36 +02:00
|
|
|
if (g_Renderer.LoadTexture(&texture,GL_REPEAT)) {
|
|
|
|
m_Handle=texture.GetHandle();
|
|
|
|
m_LoadedTextures[m_Handle] = this;
|
|
|
|
} else {
|
|
|
|
m_Handle=0;
|
2004-06-08 14:10:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-15 23:15:33 +02:00
|
|
|
|
2004-06-08 14:10:51 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2005-08-10 03:12:03 +02:00
|
|
|
// BuildBaseColor: calculate the root colour of the texture, used for coloring minimap, and store
|
2004-06-08 14:10:51 +02:00
|
|
|
// in m_BaseColor member
|
|
|
|
void CTextureEntry::BuildBaseColor()
|
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
if (m_pProperties && m_pProperties->HasBaseColor())
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
m_BaseColor=m_pProperties->GetBaseColor();
|
2005-06-20 17:14:36 +02:00
|
|
|
m_BaseColorValid=true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
LibError ret = ogl_tex_bind(GetHandle());
|
|
|
|
debug_assert(ret == INFO::OK);
|
2004-06-08 14:10:51 +02:00
|
|
|
|
2005-08-10 03:12:03 +02:00
|
|
|
// get root colour for coloring minimap by querying root level of the texture
|
2004-06-08 14:10:51 +02:00
|
|
|
// (this should decompress any compressed textures for us),
|
|
|
|
// then scaling it down to a 1x1 size
|
|
|
|
// - an alternative approach of just grabbing the top level of the mipmap tree fails
|
|
|
|
// (or gives an incorrect colour) in some cases:
|
|
|
|
// - suspect bug on Radeon cards when SGIS_generate_mipmap is used
|
|
|
|
// - any textures without mipmaps
|
2005-10-20 17:27:39 +02:00
|
|
|
// we'll just take the basic approach here.
|
|
|
|
//
|
|
|
|
// jw: this is horribly inefficient (taking 750ms for 10 texture types),
|
|
|
|
// but it is no longer called, since terrain XML files are supposed to
|
|
|
|
// include a minimap color attribute. therefore, leave it as-is.
|
2006-08-19 22:24:49 +02:00
|
|
|
GLint width,height;
|
2004-06-08 14:10:51 +02:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);
|
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);
|
|
|
|
|
|
|
|
unsigned char* buf=new unsigned char[width*height*4];
|
|
|
|
glGetTexImage(GL_TEXTURE_2D,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,buf);
|
|
|
|
gluScaleImage(GL_BGRA_EXT,width,height,GL_UNSIGNED_BYTE,buf,
|
|
|
|
1,1,GL_UNSIGNED_BYTE,&m_BaseColor);
|
|
|
|
delete[] buf;
|
|
|
|
|
|
|
|
m_BaseColorValid=true;
|
2004-06-11 04:14:18 +02:00
|
|
|
}
|
2005-08-07 23:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
CTextureEntry *CTextureEntry::GetByHandle(Handle handle)
|
|
|
|
{
|
2006-09-26 03:44:20 +02:00
|
|
|
std::map<Handle, CTextureEntry *>::iterator it=m_LoadedTextures.find(handle);
|
2005-08-07 23:58:36 +02:00
|
|
|
if (it != m_LoadedTextures.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|