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-12-01 07:52:39 +01:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2004-09-23 06:00:56 +02:00
|
|
|
#include "Material.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Player.h"
|
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/Overlay.h" // for CColor
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2004-09-23 07:10:03 +02:00
|
|
|
CMaterial NullMaterial;
|
|
|
|
CMaterial IdentityMaterial;
|
|
|
|
|
2004-09-23 06:00:56 +02:00
|
|
|
// Values as taken straight from the Blue Book (god bless the Blue Book)
|
2004-09-23 07:10:03 +02:00
|
|
|
static SMaterialColor IdentityDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
|
|
|
|
static SMaterialColor IdentityAmbient(0.2f, 0.2f, 0.2f, 1.0f);
|
|
|
|
static SMaterialColor IdentitySpecular(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
static SMaterialColor IdentityEmissive(0.0f, 0.0f, 0.0f, 1.0f);
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2006-11-07 03:48:27 +01:00
|
|
|
static SMaterialColor BrokenColor(0.3f, 0.3f, 0.3f, 1.0f);
|
2005-04-07 06:29:07 +02:00
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
bool SMaterialColor::operator==(const SMaterialColor& color)
|
2005-01-13 01:17:31 +01:00
|
|
|
{
|
|
|
|
return (
|
|
|
|
r == color.r &&
|
|
|
|
g == color.g &&
|
|
|
|
b == color.b &&
|
|
|
|
a == color.a
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2004-09-23 06:00:56 +02:00
|
|
|
CMaterial::CMaterial()
|
2004-09-24 05:52:03 +02:00
|
|
|
: m_Diffuse(IdentityDiffuse),
|
2005-04-07 06:29:07 +02:00
|
|
|
m_Ambient(IdentityAmbient),
|
|
|
|
m_Specular(IdentitySpecular),
|
|
|
|
m_Emissive(IdentityEmissive),
|
2004-09-23 06:00:56 +02:00
|
|
|
m_SpecularPower(0.0f),
|
2005-01-27 03:52:26 +01:00
|
|
|
m_Alpha(false),
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
m_PlayerID(PLAYER_ID_NONE),
|
2005-04-07 06:29:07 +02:00
|
|
|
m_TextureColor(BrokenColor)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2005-04-07 06:29:07 +02:00
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
CMaterial::CMaterial(const CMaterial& material)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2005-04-07 06:29:07 +02:00
|
|
|
(*this) = material;
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CMaterial::~CMaterial()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::operator=(const CMaterial& material)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
m_Diffuse = material.m_Diffuse;
|
2005-04-07 06:29:07 +02:00
|
|
|
m_Ambient = material.m_Ambient;
|
|
|
|
m_Specular = material.m_Specular;
|
|
|
|
m_Emissive = material.m_Emissive;
|
|
|
|
|
|
|
|
m_SpecularPower = material.m_SpecularPower;
|
|
|
|
m_Alpha = material.m_Alpha;
|
|
|
|
m_PlayerID = material.m_PlayerID;
|
|
|
|
m_TextureColor = material.m_TextureColor;
|
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
bool CMaterial::operator==(const CMaterial& material)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2005-04-07 06:29:07 +02:00
|
|
|
return(
|
|
|
|
m_Texture == m_Texture &&
|
|
|
|
m_Diffuse == material.m_Diffuse &&
|
|
|
|
m_Ambient == material.m_Ambient &&
|
|
|
|
m_Specular == material.m_Specular &&
|
|
|
|
m_Emissive == material.m_Emissive &&
|
|
|
|
m_SpecularPower == material.m_SpecularPower &&
|
|
|
|
m_Alpha == material.m_Alpha &&
|
|
|
|
m_PlayerID == material.m_PlayerID &&
|
|
|
|
m_TextureColor == material.m_TextureColor
|
|
|
|
);
|
2004-09-24 05:52:03 +02:00
|
|
|
}
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
void CMaterial::Bind()
|
|
|
|
{
|
2005-10-12 06:16:41 +02:00
|
|
|
glMaterialf(GL_FRONT, GL_SHININESS, m_SpecularPower);
|
|
|
|
glMaterialfv(GL_FRONT, GL_DIFFUSE, &m_Diffuse.r);
|
2004-10-30 23:57:46 +02:00
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT, &m_Ambient.r);
|
|
|
|
glMaterialfv(GL_FRONT, GL_SPECULAR, &m_Specular.r);
|
|
|
|
glMaterialfv(GL_FRONT, GL_EMISSION, &m_Emissive.r);
|
2004-09-24 05:52:03 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
void CMaterial::Unbind()
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
}
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
SMaterialColor CMaterial::GetDiffuse()
|
|
|
|
{
|
|
|
|
return m_Diffuse;
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
SMaterialColor CMaterial::GetAmbient()
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
return m_Ambient;
|
|
|
|
}
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
SMaterialColor CMaterial::GetSpecular()
|
|
|
|
{
|
|
|
|
return m_Specular;
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SMaterialColor CMaterial::GetEmissive()
|
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
return m_Emissive;
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2005-04-07 06:29:07 +02:00
|
|
|
SMaterialColor CMaterial::GetPlayerColor()
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
debug_assert(m_PlayerID != PLAYER_ID_NONE);
|
2005-04-07 06:29:07 +02:00
|
|
|
// because this should never be called unless IsPlayer returned true
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
if (m_PlayerID == PLAYER_ID_OTHER /* TODO: or if player-colour is globally disabled */ )
|
2005-04-07 06:29:07 +02:00
|
|
|
return m_TextureColor;
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
if (m_PlayerID <= PLAYER_ID_LAST_VALID)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
|
|
|
CPlayer* player = g_Game->GetPlayer(m_PlayerID);
|
|
|
|
if (player)
|
|
|
|
{
|
|
|
|
const SPlayerColour& c (player->GetColour());
|
|
|
|
return SMaterialColor(c.r, c.g, c.b, c.a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Oops, something failed.
|
|
|
|
return BrokenColor;
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
void CMaterial::SetPlayerColor(size_t id)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
if (m_PlayerID == PLAYER_ID_COMING_SOON || m_PlayerID <= PLAYER_ID_LAST_VALID)
|
2005-04-07 06:29:07 +02:00
|
|
|
m_PlayerID = id;
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetPlayerColor(CColor& colour)
|
2005-04-07 06:29:07 +02:00
|
|
|
{
|
|
|
|
m_TextureColor = SMaterialColor(colour.r, colour.g, colour.b, colour.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetTexture(const CStr& texture)
|
2004-09-24 05:52:03 +02:00
|
|
|
{
|
|
|
|
m_Texture = texture;
|
|
|
|
ComputeHash();
|
|
|
|
}
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
void CMaterial::SetVertexProgram(const CStr& prog)
|
2004-10-30 23:57:46 +02:00
|
|
|
{
|
|
|
|
m_VertexProgram = prog;
|
|
|
|
ComputeHash();
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
void CMaterial::SetFragmentProgram(const CStr& prog)
|
2004-10-30 23:57:46 +02:00
|
|
|
{
|
|
|
|
m_FragmentProgram = prog;
|
|
|
|
ComputeHash();
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetDiffuse(const SMaterialColor& color)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
m_Diffuse = color;
|
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetAmbient(const SMaterialColor& color)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
m_Ambient = color;
|
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetSpecular(const SMaterialColor& color)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
m_Specular = color;
|
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CMaterial::SetEmissive(const SMaterialColor& color)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2004-09-24 05:52:03 +02:00
|
|
|
m_Emissive = color;
|
|
|
|
ComputeHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMaterial::SetSpecularPower(float power)
|
|
|
|
{
|
|
|
|
m_SpecularPower = power;
|
|
|
|
ComputeHash();
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2004-09-24 05:52:03 +02:00
|
|
|
void CMaterial::SetUsesAlpha(bool flag)
|
|
|
|
{
|
|
|
|
m_Alpha = flag;
|
|
|
|
ComputeHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMaterial::ComputeHash()
|
|
|
|
{
|
|
|
|
m_Hash =
|
|
|
|
m_Diffuse.Sum() +
|
|
|
|
m_Ambient.Sum() +
|
|
|
|
m_Specular.Sum() +
|
|
|
|
m_Emissive.Sum() +
|
|
|
|
m_SpecularPower +
|
2004-10-30 23:57:46 +02:00
|
|
|
(float)m_Texture.GetHashCode() +
|
|
|
|
(float)m_VertexProgram.GetHashCode() +
|
|
|
|
(float)m_FragmentProgram.GetHashCode();
|
2004-09-24 05:52:03 +02:00
|
|
|
}
|