1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
|
/**
* $Id: RAS_MeshObject.cpp,v 1.16 2006/04/02 21:04:20 erwin Exp $
* ***** 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 *****
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "RAS_MeshObject.h"
#include "RAS_IRasterizer.h"
#include "MT_MinMax.h"
#include "MT_Point3.h"
STR_String RAS_MeshObject::s_emptyname = "";
KX_ArrayOptimizer::~KX_ArrayOptimizer()
{
for (vector<KX_VertexArray*>::iterator itv = m_VertexArrayCache1.begin();
!(itv == m_VertexArrayCache1.end());++itv)
{
delete (*itv);
}
for (vector<KX_IndexArray*>::iterator iti = m_IndexArrayCache1.begin();
!(iti == m_IndexArrayCache1.end());++iti)
{
delete (*iti);
}
m_TriangleArrayCount.clear();
m_VertexArrayCache1.clear();
m_IndexArrayCache1.clear();
}
RAS_MeshObject::RAS_MeshObject(int lightlayer)
: m_bModified(true),
m_lightlayer(lightlayer),
m_zsort(false),
m_MeshMod(true),
m_class(0)
{
}
bool RAS_MeshObject::MeshModified()
{
return m_MeshMod;
}
RAS_MeshObject::~RAS_MeshObject()
{
for (vector<RAS_Polygon*>::iterator it=m_Polygons.begin();!(it==m_Polygons.end());it++)
{
delete (*it);
}
ClearArrayData();
}
unsigned int RAS_MeshObject::GetLightLayer()
{
return m_lightlayer;
}
int RAS_MeshObject::NumMaterials()
{
return m_materials.size();
}
const STR_String& RAS_MeshObject::GetMaterialName(unsigned int matid)
{
RAS_MaterialBucket* bucket = GetMaterialBucket(matid);
return bucket?bucket->GetPolyMaterial()->GetMaterialName():s_emptyname;
}
RAS_MaterialBucket* RAS_MeshObject::GetMaterialBucket(unsigned int matid)
{
if (m_materials.size() > 0 && (matid < m_materials.size()))
{
RAS_MaterialBucket::Set::const_iterator it = m_materials.begin();
while (matid--) ++it;
return *it;
}
return NULL;
}
int RAS_MeshObject::NumPolygons()
{
return m_Polygons.size();
}
RAS_Polygon* RAS_MeshObject::GetPolygon(int num)
{
return m_Polygons[num];
}
RAS_MaterialBucket::Set::iterator RAS_MeshObject::GetFirstMaterial()
{
return m_materials.begin();
}
RAS_MaterialBucket::Set::iterator RAS_MeshObject::GetLastMaterial()
{
return m_materials.end();
}
void RAS_MeshObject::SetName(STR_String name)
{
m_name = name;
}
const STR_String& RAS_MeshObject::GetName()
{
return m_name;
}
const STR_String& RAS_MeshObject::GetTextureName(unsigned int matid)
{
RAS_MaterialBucket* bucket = GetMaterialBucket(matid);
return bucket?bucket->GetPolyMaterial()->GetTextureName():s_emptyname;
}
void RAS_MeshObject::AddPolygon(RAS_Polygon* poly)
{
m_Polygons.push_back(poly);
}
void RAS_MeshObject::DebugColor(unsigned int abgr)
{
/*
int numpolys = NumPolygons();
for (int i=0;i<numpolys;i++)
{
RAS_Polygon* poly = m_polygons[i];
for (int v=0;v<poly->VertexCount();v++)
{
RAS_TexVert* vtx = poly->GetVertex(v);
vtx->setDebugRGBA(abgr);
}
}
*/
m_debugcolor = abgr;
}
void RAS_MeshObject::SchedulePoly(const KX_VertexIndex& idx,
int numverts,
RAS_IPolyMaterial* mat)
{
//int indexpos = m_IndexArrayCount[idx.m_vtxarray];
//m_IndexArrayCount[idx.m_vtxarray] = indexpos + 3;
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[0]);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[1]);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[2]);
if (!mat->UsesTriangles()) //if (!m_bUseTriangles)
{
//m_IndexArrayCount[idx.m_vtxarray] = indexpos+4;
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[3]);
}
}
void RAS_MeshObject::ScheduleWireframePoly(const KX_VertexIndex& idx,
int numverts,
int edgecode,
RAS_IPolyMaterial* mat)
{
//int indexpos = m_IndexArrayCount[idx.m_vtxarray];
int edgetrace = 1<<(numverts-1);
bool drawedge = (edgecode & edgetrace)!=0;
edgetrace = 1;
int prevvert = idx.m_indexarray[numverts-1];
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);
for (int v = 0; v < numverts; v++)
{
unsigned int curvert = idx.m_indexarray[v];
if (drawedge)
{
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(prevvert);
ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(curvert);
}
prevvert = curvert;
drawedge = (edgecode & edgetrace)!=0;
edgetrace*=2;
}
//m_IndexArrayCount[idx.m_vtxarray] = indexpos;
}
int RAS_MeshObject::FindOrAddVertex(int vtxarray,
const MT_Point3& xyz,
const MT_Point2& uv,
const MT_Vector4& tangent,
const unsigned int rgbacolor,
const MT_Vector3& normal,
RAS_IPolyMaterial* mat,
int orgindex)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
int numverts = ao->m_VertexArrayCache1[vtxarray]->size();//m_VertexArrayCount[vtxarray];
RAS_TexVert newvert(xyz,uv,tangent,rgbacolor,normal, 0);
#define KX_FIND_SHARED_VERTICES
#ifdef KX_FIND_SHARED_VERTICES
for (std::vector<RAS_MatArrayIndex>::iterator it = m_xyz_index_to_vertex_index_mapping[orgindex].begin();
it != m_xyz_index_to_vertex_index_mapping[orgindex].end();
it++)
{
if ((*it).m_arrayindex1 == ao->m_index1 &&
(*it).m_array == vtxarray &&
*(*it).m_matid == *mat &&
(*ao->m_VertexArrayCache1[vtxarray])[(*it).m_index].closeTo(&newvert)
)
{
return (*it).m_index;
}
}
#endif // KX_FIND_SHARED_VERTICES
// no vertex found, add one
ao->m_VertexArrayCache1[vtxarray]->push_back(newvert);
// printf("(%f,%f,%f) ",xyz[0],xyz[1],xyz[2]);
RAS_MatArrayIndex idx;
idx.m_arrayindex1 = ao->m_index1;
idx.m_array = vtxarray;
idx.m_index = numverts;
idx.m_matid = mat;
m_xyz_index_to_vertex_index_mapping[orgindex].push_back(idx);
return numverts;
}
const vecVertexArray& RAS_MeshObject::GetVertexCache (RAS_IPolyMaterial* mat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
return ao->m_VertexArrayCache1;
}
int RAS_MeshObject::GetVertexArrayLength(RAS_IPolyMaterial* mat)
{
int len = 0;
const vecVertexArray & vertexvec = GetVertexCache(mat);
vector<KX_VertexArray*>::const_iterator it = vertexvec.begin();
for (; it != vertexvec.end(); ++it)
{
len += (*it)->size();
}
return len;
}
RAS_TexVert* RAS_MeshObject::GetVertex(unsigned int matid,
unsigned int index)
{
RAS_TexVert* vertex = NULL;
RAS_MaterialBucket* bucket = GetMaterialBucket(matid);
if (bucket)
{
RAS_IPolyMaterial* mat = bucket->GetPolyMaterial();
if (mat)
{
const vecVertexArray & vertexvec = GetVertexCache(mat);
vector<KX_VertexArray*>::const_iterator it = vertexvec.begin();
for (unsigned int len = 0; it != vertexvec.end(); ++it)
{
if (index < len + (*it)->size())
{
vertex = &(*(*it))[index-len];
break;
}
else
{
len += (*it)->size();
}
}
}
}
return vertex;
}
const vecIndexArrays& RAS_MeshObject::GetIndexCache (RAS_IPolyMaterial* mat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]);
return ao->m_IndexArrayCache1;
}
KX_ArrayOptimizer* RAS_MeshObject::GetArrayOptimizer(RAS_IPolyMaterial* polymat)
{
KX_ArrayOptimizer** aop = (m_matVertexArrayS[*polymat]);
if (aop)
return *aop;
int numelements = m_matVertexArrayS.size();
m_sortedMaterials.push_back(polymat);
KX_ArrayOptimizer* ao = new KX_ArrayOptimizer(numelements);
m_matVertexArrayS.insert(*polymat,ao);
return ao;
}
void RAS_MeshObject::Bucketize(double* oglmatrix,
void* clientobj,
bool useObjectColor,
const MT_Vector4& rgbavec)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
ms.m_bObjectColor = useObjectColor;
ms.m_RGBAcolor = rgbavec;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
RAS_MaterialBucket* bucket = *it;
bucket->SchedulePolygons(0);
// KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial());
bucket->SetMeshSlot(ms);
}
}
void RAS_MeshObject::MarkVisible(double* oglmatrix,
void* clientobj,
bool visible,
bool useObjectColor,
const MT_Vector4& rgbavec)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
ms.m_RGBAcolor = rgbavec;
ms.m_bObjectColor= useObjectColor;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
RAS_MaterialBucket* bucket = *it;
bucket->SchedulePolygons(0);
// KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial());
bucket->MarkVisibleMeshSlot(ms,visible,useObjectColor,rgbavec);
}
}
void RAS_MeshObject::RemoveFromBuckets(double* oglmatrix,
void* clientobj)
{
KX_MeshSlot ms;
ms.m_clientObj = clientobj;
ms.m_mesh = this;
ms.m_OpenGLMatrix = oglmatrix;
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
RAS_MaterialBucket* bucket = *it;
// RAS_IPolyMaterial* polymat = bucket->GetPolyMaterial();
bucket->SchedulePolygons(0);
//KX_ArrayOptimizer* oa = GetArrayOptimizer(polymat);
bucket->RemoveMeshSlot(ms);
}
}
/*
* RAS_MeshObject::GetVertex returns the vertex located somewhere in the vertexpool
* it is the clients responsibility to make sure the array and index are valid
*/
RAS_TexVert* RAS_MeshObject::GetVertex(short array,
unsigned int index,
RAS_IPolyMaterial* polymat)
{
KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);//*(m_matVertexArrays[*polymat]);
return &((*(ao->m_VertexArrayCache1)[array])[index]);
}
void RAS_MeshObject::ClearArrayData()
{
for (int i=0;i<m_matVertexArrayS.size();i++)
{
KX_ArrayOptimizer** ao = m_matVertexArrayS.at(i);
if (ao)
delete *ao;
}
}
/**
* RAS_MeshObject::CreateNewVertices creates vertices within sorted pools of vertices that share same material
*/
int RAS_MeshObject::FindVertexArray(int numverts,
RAS_IPolyMaterial* polymat)
{
// bool found=false;
int array=-1;
KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);
for (unsigned int i=0;i<ao->m_VertexArrayCache1.size();i++)
{
if ( (ao->m_TriangleArrayCount[i] + (numverts-2)) < BUCKET_MAX_TRIANGLES)
{
if((ao->m_VertexArrayCache1[i]->size()+numverts < BUCKET_MAX_INDICES))
{
array = i;
ao->m_TriangleArrayCount[array]+=numverts-2;
break;
}
}
}
if (array == -1)
{
array = ao->m_VertexArrayCache1.size();
vector<RAS_TexVert>* va = new vector<RAS_TexVert>;
ao->m_VertexArrayCache1.push_back(va);
KX_IndexArray *ia = new KX_IndexArray();
ao->m_IndexArrayCache1.push_back(ia);
ao->m_TriangleArrayCount.push_back(numverts-2);
}
return array;
}
//void RAS_MeshObject::Transform(const MT_Transform& trans)
//{
//m_trans.translate(MT_Vector3(0,0,1));//.operator *=(trans);
// for (int i=0;i<m_Polygons.size();i++)
// {
// m_Polygons[i]->Transform(trans);
// }
//}
/*
void RAS_MeshObject::RelativeTransform(const MT_Vector3& vec)
{
for (int i=0;i<m_Polygons.size();i++)
{
m_Polygons[i]->RelativeTransform(vec);
}
}
*/
void RAS_MeshObject::UpdateMaterialList()
{
m_materials.clear();
unsigned int numpolys = m_Polygons.size();
// for all polygons, find out which material they use, and add it to the set of materials
for (unsigned int i=0;i<numpolys;i++)
{
m_materials.insert(m_Polygons[i]->GetMaterial());
}
}
struct RAS_MeshObject::polygonSlot
{
float m_z;
RAS_Polygon *m_poly;
polygonSlot(float z, RAS_Polygon* poly) :
m_z(z),
m_poly(poly)
{}
/**
* pnorm and pval form the plane equation that the distance from is used to
* sort against.
*/
polygonSlot(const MT_Vector3 &pnorm, const MT_Scalar &pval, RAS_MeshObject *mesh, RAS_Polygon* poly) :
m_poly(poly)
{
const KX_VertexIndex &base = m_poly->GetIndexBase();
RAS_TexVert *vert = mesh->GetVertex(base.m_vtxarray, base.m_indexarray[0], poly->GetMaterial()->GetPolyMaterial());
m_z = MT_dot(pnorm, vert->getLocalXYZ()) + pval;
for(int i = 1; i < m_poly->VertexCount(); i++)
{
vert = mesh->GetVertex(base.m_vtxarray, base.m_indexarray[i], poly->GetMaterial()->GetPolyMaterial());
float z = MT_dot(pnorm, vert->getLocalXYZ()) + pval;
m_z += z;
}
m_z /= m_poly->VertexCount();
}
};
struct RAS_MeshObject::backtofront
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z < b.m_z;
}
};
struct RAS_MeshObject::fronttoback
{
bool operator()(const polygonSlot &a, const polygonSlot &b) const
{
return a.m_z > b.m_z;
}
};
void RAS_MeshObject::SortPolygons(const MT_Transform &transform)
{
if (!m_zsort)
return;
// Extract camera Z plane...
const MT_Vector3 pnorm(transform.getBasis()[2]);
const MT_Scalar pval = transform.getOrigin()[2];
unsigned int numpolys = m_Polygons.size();
std::multiset<polygonSlot, backtofront> alphapolyset;
std::multiset<polygonSlot, fronttoback> solidpolyset;
for (unsigned int p = 0; p < numpolys; p++)
{
RAS_Polygon* poly = m_Polygons[p];
if (poly->IsVisible())
{
if (poly->GetMaterial()->GetPolyMaterial()->IsTransparant())
{
alphapolyset.insert(polygonSlot(pnorm, pval, this, poly));
} else {
solidpolyset.insert(polygonSlot(pnorm, pval, this, poly));
}
}
}
// Clear current array data.
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
vector<KX_IndexArray*> *indexcache = &GetArrayOptimizer((*it)->GetPolyMaterial())->m_IndexArrayCache1;
for (vector<KX_IndexArray*>::iterator iit = indexcache->begin(); iit != indexcache->end(); ++iit)
(*iit)->clear();
}
std::multiset<polygonSlot, fronttoback>::iterator sit = solidpolyset.begin();
for (; sit != solidpolyset.end(); ++sit)
SchedulePoly((*sit).m_poly->GetVertexIndexBase(), (*sit).m_poly->VertexCount(), (*sit).m_poly->GetMaterial()->GetPolyMaterial());
std::multiset<polygonSlot, backtofront>::iterator ait = alphapolyset.begin();
for (; ait != alphapolyset.end(); ++ait)
SchedulePoly((*ait).m_poly->GetVertexIndexBase(), (*ait).m_poly->VertexCount(), (*ait).m_poly->GetMaterial()->GetPolyMaterial());
}
void RAS_MeshObject::SchedulePolygons(const MT_Transform &transform, int drawingmode)
{
// int nummaterials = m_materials.size();
int i;
if (m_bModified)
{
for (RAS_MaterialBucket::Set::iterator it = m_materials.begin();it!=m_materials.end();++it)
{
RAS_MaterialBucket* bucket = *it;
bucket->SchedulePolygons(drawingmode);
if (bucket->GetPolyMaterial()->IsZSort())
m_zsort = true;
}
int numpolys = m_Polygons.size();
if ((drawingmode > RAS_IRasterizer::KX_BOUNDINGBOX) &&
(drawingmode < RAS_IRasterizer::KX_SOLID))
{
for (i=0;i<numpolys;i++)
{
RAS_Polygon* poly = m_Polygons[i];
if (poly->IsVisible())
ScheduleWireframePoly(poly->GetVertexIndexBase(),poly->VertexCount(),poly->GetEdgeCode()
,poly->GetMaterial()->GetPolyMaterial());
}
m_zsort = false;
}
else
{
if (!m_zsort)
{
for (i=0;i<numpolys;i++)
{
RAS_Polygon* poly = m_Polygons[i];
if (poly->IsVisible())
{
SchedulePoly(poly->GetVertexIndexBase(),poly->VertexCount(),poly->GetMaterial()->GetPolyMaterial());
}
}
}
}
m_bModified = false;
m_MeshMod = true;
}
}
|