2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program 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. The Blender
|
|
|
|
* Foundation also sells licenses for use in proprietary software under
|
|
|
|
* the Blender License. See http://www.blender.org/BL/ for information
|
|
|
|
* about this.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
|
|
|
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2002-11-25 15:29:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
// don't show these anoying STL warnings
|
|
|
|
#pragma warning (disable:4786)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "GEN_Map.h"
|
|
|
|
#include "RAS_MaterialBucket.h"
|
|
|
|
#include "STR_HashedString.h"
|
|
|
|
#include "RAS_MeshObject.h"
|
|
|
|
#define KX_NUM_MATERIALBUCKETS 100
|
|
|
|
#include "RAS_IRasterizer.h"
|
|
|
|
#include "RAS_IRenderTools.h"
|
|
|
|
|
|
|
|
#include "RAS_BucketManager.h"
|
|
|
|
|
2004-05-21 08:55:12 +00:00
|
|
|
#include <set>
|
2004-03-22 22:02:18 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
RAS_BucketManager::RAS_BucketManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RAS_BucketManager::~RAS_BucketManager()
|
|
|
|
{
|
|
|
|
RAS_BucketManagerClearAll();
|
|
|
|
}
|
|
|
|
|
2004-07-17 05:23:17 +00:00
|
|
|
/**
|
|
|
|
* struct alphamesh holds a mesh, (m_ms) it's depth, (m_z) and the bucket it came from (m_bucket.)
|
|
|
|
*/
|
|
|
|
struct RAS_BucketManager::alphamesh
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MT_Scalar m_z;
|
|
|
|
RAS_MaterialBucket::T_MeshSlotList::iterator m_ms;
|
|
|
|
RAS_MaterialBucket *m_bucket;
|
|
|
|
alphamesh(MT_Scalar z, RAS_MaterialBucket::T_MeshSlotList::iterator &ms, RAS_MaterialBucket *bucket) :
|
|
|
|
m_z(z),
|
|
|
|
m_ms(ms),
|
|
|
|
m_bucket(bucket)
|
|
|
|
{}
|
|
|
|
};
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2004-07-17 05:23:17 +00:00
|
|
|
struct RAS_BucketManager::backtofront
|
|
|
|
{
|
|
|
|
bool operator()(const alphamesh &a, const alphamesh &b)
|
|
|
|
{
|
|
|
|
return a.m_z < b.m_z;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-05-21 08:55:12 +00:00
|
|
|
|
|
|
|
void RAS_BucketManager::RenderAlphaBuckets(
|
|
|
|
const MT_Transform& cameratrans, RAS_IRasterizer* rasty, RAS_IRenderTools* rendertools)
|
|
|
|
{
|
|
|
|
BucketList::iterator bit;
|
|
|
|
std::multiset<alphamesh, backtofront> alphameshset;
|
|
|
|
RAS_MaterialBucket::T_MeshSlotList::iterator mit;
|
|
|
|
|
2004-05-26 12:01:08 +00:00
|
|
|
/* Camera's near plane equation: cam_norm.dot(point) + cam_origin */
|
|
|
|
const MT_Vector3 cam_norm(cameratrans.getBasis()[2]);
|
|
|
|
const MT_Scalar cam_origin = cameratrans.getOrigin()[2];
|
2004-05-21 08:55:12 +00:00
|
|
|
for (bit = m_AlphaBuckets.begin(); bit != m_AlphaBuckets.end(); ++bit)
|
|
|
|
{
|
|
|
|
(*bit)->ClearScheduledPolygons();
|
|
|
|
for (mit = (*bit)->msBegin(); mit != (*bit)->msEnd(); ++mit)
|
|
|
|
{
|
|
|
|
if ((*mit).m_bVisible)
|
|
|
|
{
|
|
|
|
MT_Point3 pos((*mit).m_OpenGLMatrix[12], (*mit).m_OpenGLMatrix[13], (*mit).m_OpenGLMatrix[14]);
|
2004-05-26 12:01:08 +00:00
|
|
|
alphameshset.insert(alphamesh(MT_dot(cam_norm, pos) + cam_origin, mit, *bit));
|
2004-05-21 08:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It shouldn't be strictly necessary to disable depth writes; but
|
|
|
|
// it is needed for compatibility.
|
|
|
|
rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_DISABLED);
|
|
|
|
|
2005-01-16 06:02:06 +00:00
|
|
|
int drawingmode;
|
2004-05-21 08:55:12 +00:00
|
|
|
std::multiset< alphamesh, backtofront>::iterator msit = alphameshset.begin();
|
|
|
|
for (; msit != alphameshset.end(); ++msit)
|
|
|
|
{
|
2005-01-16 06:02:06 +00:00
|
|
|
while ((*msit).m_bucket->ActivateMaterial(cameratrans, rasty, rendertools, drawingmode))
|
|
|
|
(*msit).m_bucket->RenderMeshSlot(cameratrans, rasty, rendertools, *(*msit).m_ms, drawingmode);
|
2004-05-21 08:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_ENABLED);
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void RAS_BucketManager::Renderbuckets(
|
|
|
|
const MT_Transform& cameratrans, RAS_IRasterizer* rasty, RAS_IRenderTools* rendertools)
|
|
|
|
{
|
2004-05-21 08:55:12 +00:00
|
|
|
BucketList::iterator bucket;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
rasty->EnableTextures(false);
|
|
|
|
rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_ENABLED);
|
|
|
|
|
|
|
|
// beginning each frame, clear (texture/material) caching information
|
|
|
|
rasty->ClearCachingInfo();
|
|
|
|
|
|
|
|
RAS_MaterialBucket::StartFrame();
|
2004-05-21 08:55:12 +00:00
|
|
|
for (bucket = m_MaterialBuckets.begin(); bucket != m_MaterialBuckets.end(); ++bucket)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2004-03-22 22:02:18 +00:00
|
|
|
(*bucket)->ClearScheduledPolygons();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2004-05-21 08:55:12 +00:00
|
|
|
for (bucket = m_MaterialBuckets.begin(); bucket != m_MaterialBuckets.end(); ++bucket)
|
2006-01-06 03:46:54 +00:00
|
|
|
{
|
2006-04-11 05:57:30 +00:00
|
|
|
RAS_IPolyMaterial *tmp = (*bucket)->GetPolyMaterial();
|
|
|
|
if(tmp->IsZSort() || tmp->GetFlag() &RAS_FORCEALPHA )
|
2006-01-06 03:46:54 +00:00
|
|
|
rasty->SetAlphaTest(true);
|
|
|
|
else
|
|
|
|
rasty->SetAlphaTest(false);
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
(*bucket)->Render(cameratrans,rasty,rendertools);
|
2006-01-06 03:46:54 +00:00
|
|
|
}
|
|
|
|
rasty->SetAlphaTest(false);
|
|
|
|
|
2004-05-21 08:55:12 +00:00
|
|
|
RenderAlphaBuckets(cameratrans, rasty, rendertools);
|
2004-03-22 22:02:18 +00:00
|
|
|
RAS_MaterialBucket::EndFrame();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 23:12:40 +00:00
|
|
|
RAS_MaterialBucket* RAS_BucketManager::RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material, bool &bucketCreated)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2008-03-23 23:12:40 +00:00
|
|
|
bucketCreated = false;
|
2004-05-21 08:55:12 +00:00
|
|
|
BucketList::iterator it;
|
2004-03-22 22:02:18 +00:00
|
|
|
for (it = m_MaterialBuckets.begin(); it != m_MaterialBuckets.end(); it++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2004-03-22 22:02:18 +00:00
|
|
|
if (*(*it)->GetPolyMaterial() == *material)
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (it = m_AlphaBuckets.begin(); it != m_AlphaBuckets.end(); it++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2004-03-22 22:02:18 +00:00
|
|
|
if (*(*it)->GetPolyMaterial() == *material)
|
|
|
|
return *it;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2004-03-22 22:02:18 +00:00
|
|
|
|
|
|
|
RAS_MaterialBucket *bucket = new RAS_MaterialBucket(material);
|
2008-03-23 23:12:40 +00:00
|
|
|
bucketCreated = true;
|
2004-03-22 22:02:18 +00:00
|
|
|
if (bucket->IsTransparant())
|
|
|
|
m_AlphaBuckets.push_back(bucket);
|
|
|
|
else
|
|
|
|
m_MaterialBuckets.push_back(bucket);
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RAS_BucketManager::RAS_BucketManagerClearAll()
|
|
|
|
{
|
2004-05-21 08:55:12 +00:00
|
|
|
BucketList::iterator it;
|
2004-03-22 22:02:18 +00:00
|
|
|
for (it = m_MaterialBuckets.begin(); it != m_MaterialBuckets.end(); it++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2004-03-22 22:02:18 +00:00
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
for (it = m_AlphaBuckets.begin(); it != m_AlphaBuckets.end(); it++)
|
|
|
|
{
|
|
|
|
delete(*it);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
m_MaterialBuckets.clear();
|
|
|
|
m_AlphaBuckets.clear();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|