59bool SortingRendererClass::_EnableTriangleDraw=
true;
60static unsigned DEFAULT_SORTING_POLY_COUNT = 16384;
61static unsigned DEFAULT_SORTING_VERTEX_COUNT = 32768;
65 DEFAULT_SORTING_VERTEX_COUNT = val;
66 DEFAULT_SORTING_POLY_COUNT = val/2;
95 while (insert != begin && insert[-1] > val) {
96 insert[0] = insert[-1];
107 const int diff = end - begin;
110 InsertionSort(begin, end);
116 std::swap(mid[0], begin[1]);
117 if (begin[1] > end[-1]) {
118 std::swap(begin[1], end[-1]);
120 if (begin[0] > end[-1]) {
121 std::swap(begin[0], end[-1]);
123 if (begin[1] > begin[0]) {
124 std::swap(begin[1], begin[0]);
134 do ++left;
while (left[0] < begin[0]);
135 do --right;
while (right[0] > begin[0]);
137 do ++left;
while (left < end1 && left[0] < begin[0]);
138 do --right;
while (right > begin1 && right[0] > begin[0]);
140 if (right < left)
break;
141 std::swap(left[0], right[0]);
143 std::swap(begin[0], right[0]);
146 if (right - begin > end - (right + 1)) {
147 Sort(right + 1, end);
151 Sort(right + 1, end);
176static unsigned total_sorting_vertices;
197static unsigned temp_index_array_count;
201 if (count < DEFAULT_SORTING_POLY_COUNT)
202 count = DEFAULT_SORTING_POLY_COUNT;
203 if (count>temp_index_array_count) {
204 delete[] temp_index_array;
206 temp_index_array_count=count;
208 return temp_index_array;
219 unsigned short start_index,
220 unsigned short polygon_count,
221 unsigned short min_vertex_index,
222 unsigned short vertex_count)
229 SNAPSHOT_SAY((
"SortingRenderer::Insert(start_i: %d, polygons : %d, min_vi: %d, vertex_count: %d)\n",
230 start_index,polygon_count,min_vertex_index,vertex_count));
256 D3DXVECTOR4 transformed_vec;
269 if (sorted_list.Head()==sorted_list.Tail())
270 sorted_list.Add_Head(state);
277 if (!node) sorted_list.Add_Tail(state);
280 unsigned short* indices=
NULL;
306 unsigned short start_index,
307 unsigned short polygon_count,
308 unsigned short min_vertex_index,
309 unsigned short vertex_count)
312 Insert_Triangles(sphere,start_index,polygon_count,min_vertex_index,vertex_count);
335static unsigned overlapping_node_count;
336static unsigned overlapping_polygon_count;
337static unsigned overlapping_vertex_count;
338static const unsigned MAX_OVERLAPPING_NODES=4096;
345 if (overlapping_node_count>=MAX_OVERLAPPING_NODES) {
351 overlapping_nodes[overlapping_node_count]=state;
354 overlapping_node_count++;
412void SortingRendererClass::Flush_Sorting_Pool()
414 if (!overlapping_node_count)
return;
419 TempIndexStruct* tis=Get_Temp_Index_Array(overlapping_polygon_count);
421 unsigned vertexAllocCount = overlapping_vertex_count;
423 vertexAllocCount = DEFAULT_SORTING_VERTEX_COUNT;
424 if (overlapping_vertex_count > vertexAllocCount)
425 vertexAllocCount = overlapping_vertex_count;
426 WWASSERT(DEFAULT_SORTING_VERTEX_COUNT == 1 || vertexAllocCount <= DEFAULT_SORTING_VERTEX_COUNT);
430 VertexFormatXYZNDUV2* dest_verts=(VertexFormatXYZNDUV2 *)lock.Get_Formatted_Vertex_Array();
432 unsigned polygon_array_offset=0;
433 unsigned vertex_array_offset=0;
434 for (
unsigned node_id=0;node_id<overlapping_node_count;++node_id) {
435 SortingNodeStruct* state=overlapping_nodes[node_id];
436 VertexFormatXYZNDUV2* src_verts=
NULL;
439 src_verts=vertex_buffer->VertexBuffer;
448 memcpy(dest_verts, src_verts,
sizeof(VertexFormatXYZNDUV2)*state->
vertex_count);
452 const Matrix4x4& mtx=(
const Matrix4x4&)d3d_mtx;
454 unsigned short* indices=
NULL;
462 if (mtx[0][2] == 0.0f && mtx[1][2] == 0.0f && mtx[3][2] == 0.0f && mtx[2][2] == 1.0f) {
471 const VertexFormatXYZNDUV2 *v1 = src_verts + idx1;
472 const VertexFormatXYZNDUV2 *v2 = src_verts + idx2;
473 const VertexFormatXYZNDUV2 *v3 = src_verts + idx3;
474 unsigned array_index=i+polygon_array_offset;
475 WWASSERT(array_index<overlapping_polygon_count);
476 TempIndexStruct *tis_ptr = tis + array_index;
477 tis_ptr->
tri.
i = idx1 + vertex_array_offset;
478 tis_ptr->
tri.
j = idx2 + vertex_array_offset;
479 tis_ptr->
tri.
k = idx3 + vertex_array_offset;
480 tis_ptr->
idx = node_id;
481 tis_ptr->
z = (v1->
z + v2->
z + v3->
z)/3.0f;
482 DEBUG_ASSERTCRASH((! _isnan(tis_ptr->
z) && _finite(tis_ptr->
z)), (
"Triangle has invalid center"));
492 const VertexFormatXYZNDUV2 *v1 = src_verts + idx1;
493 const VertexFormatXYZNDUV2 *v2 = src_verts + idx2;
494 const VertexFormatXYZNDUV2 *v3 = src_verts + idx3;
495 unsigned array_index=i+polygon_array_offset;
496 WWASSERT(array_index<overlapping_polygon_count);
497 TempIndexStruct *tis_ptr = tis + array_index;
498 tis_ptr->
tri.
i = idx1 + vertex_array_offset;
499 tis_ptr->
tri.
j = idx2 + vertex_array_offset;
500 tis_ptr->
tri.
k = idx3 + vertex_array_offset;
501 tis_ptr->
idx = node_id;
502 tis_ptr->
z = (mtx[0][2]*(v1->
x + v2->
x + v3->
x) +
503 mtx[1][2]*(v1->
y + v2->
y + v3->
y) +
504 mtx[2][2]*(v1->
z + v2->
z + v3->
z))/3.0f + mtx[3][2];
505 DEBUG_ASSERTCRASH((! _isnan(tis_ptr->
z) && _finite(tis_ptr->
z)), (
"Triangle has invalid center"));
516 Sort(tis, tis + overlapping_polygon_count);
534 unsigned polygonAllocCount = overlapping_polygon_count;
536 polygonAllocCount = DEFAULT_SORTING_POLY_COUNT;
537 if (overlapping_polygon_count > polygonAllocCount)
538 polygonAllocCount = overlapping_polygon_count;
539 WWASSERT(DEFAULT_SORTING_POLY_COUNT <= 1 || polygonAllocCount <= DEFAULT_SORTING_POLY_COUNT);
544 ShortVectorIStruct* sorted_polygon_index_array=(ShortVectorIStruct*)lock.Get_Index_Array();
547 for (
unsigned a=0;a<overlapping_polygon_count;++a) {
548 sorted_polygon_index_array[a]=tis[a].
tri;
563 unsigned count_to_render=1;
564 unsigned start_index=0;
565 unsigned node_id=tis[0].
idx;
566 for (
unsigned i=1;i<overlapping_polygon_count;++i) {
567 if (node_id!=tis[i].idx) {
568 SortingNodeStruct* state=overlapping_nodes[node_id];
585 if (count_to_render) {
586 SortingNodeStruct* state=overlapping_nodes[node_id];
597 for (node_id=0;node_id<overlapping_node_count;++node_id) {
598 SortingNodeStruct* state=overlapping_nodes[node_id];
602 overlapping_node_count=0;
603 overlapping_polygon_count=0;
604 overlapping_vertex_count=0;
625 Insert_To_Sorting_Pool(state);
632 clean_list.Add_Head(state);
638 Flush_Sorting_Pool();
643 total_sorting_vertices=0;
663 while ((head = sorted_list.Head ()) !=
NULL) {
664 sorted_list.Remove_Head ();
671 while ((head = clean_list.Head ()) !=
NULL) {
672 clean_list.Remove_Head ();
676 delete[] temp_index_array;
677 temp_index_array=
NULL;
678 temp_index_array_count=0;
690 unsigned short start_index,
691 unsigned short polygon_count,
692 unsigned short min_vertex_index,
693 unsigned short vertex_count,
694 unsigned short layerCount)
726 D3DXVECTOR4 transformed_vec;
743 if (sorted_list.Head()==sorted_list.Tail())
744 sorted_list.Add_Head(state);
751 if (!node) sorted_list.Add_Tail(state);
#define DEBUG_ASSERTCRASH(c, m)
#define DX8_RECORD_SORTING_RENDER(polys, verts)
#define W3DMPO_GLUE(ARGCLASS)
void Add_Head(DLNodeClass< T > *node)
void Insert_Before(DLNodeClass< T > *n)
int Get_Max_Textures_Per_Pass() const
static void Set_Vertex_Buffer(const VertexBufferClass *vb, unsigned stream=0)
static void Set_Texture(unsigned stage, TextureBaseClass *texture)
static void _Enable_Triangle_Draw(bool enable)
static const DX8Caps * Get_Current_Caps()
static bool _Is_Triangle_Draw_Enabled()
static void _Set_DX8_Transform(D3DTRANSFORMSTATETYPE transform, const Matrix4x4 &m)
static void Set_Index_Buffer(const IndexBufferClass *ib, unsigned short index_base_offset)
static void Draw_Triangles(unsigned buffer_type, unsigned short start_index, unsigned short polygon_count, unsigned short min_vertex_index, unsigned short vertex_count)
static void Set_Material(const VertexMaterialClass *material)
static void Set_DX8_Light(int index, D3DLIGHT8 *light)
static void Set_Shader(const ShaderClass &shader)
static void Get_Transform(D3DTRANSFORMSTATETYPE transform, Matrix4x4 &m)
static void Set_Transform(D3DTRANSFORMSTATETYPE transform, const Matrix4x4 &m)
static void Apply_Render_State_Changes()
static void Release_Render_State()
static void Get_Render_State(RenderStateStruct &state)
static void Set_Render_State(const RenderStateStruct &state)
static unsigned short Get_Default_Index_Count(void)
current size of dynamic index buffer
static void _Reset(bool frame_changed)
static unsigned short Get_Default_Vertex_Count(void)
current size of dynamic vertex buffer
static void _Reset(bool frame_changed)
unsigned short * index_buffer
unsigned short polygon_count
unsigned short start_index
Vector3 transformed_center
SphereClass bounding_sphere
unsigned short min_vertex_index
unsigned short vertex_count
RenderStateStruct sorting_state
static void Insert_Triangles(const SphereClass &bounding_sphere, unsigned short start_index, unsigned short polygon_count, unsigned short min_vertex_index, unsigned short vertex_count)
static void Insert_VolumeParticle(const SphereClass &bounding_sphere, unsigned short start_index, unsigned short polygon_count, unsigned short min_vertex_index, unsigned short vertex_count, unsigned short layerCount)
static void SetMinVertexBufferSize(unsigned val)
unsigned short Get_Vertex_Count() const
bool Get_Lighting() const
static bool Is_Sorting_Enabled(void)
int IndexBufferExceptionFunc(void)
const unsigned dynamic_fvf_type
@ BUFFER_TYPE_DYNAMIC_DX8
@ BUFFER_TYPE_DYNAMIC_SORTING
const unsigned MAX_VERTEX_STREAMS
#define REF_PTR_RELEASE(x)
bool operator==(const TempIndexStruct &l, const TempIndexStruct &r)
bool operator>=(const TempIndexStruct &l, const TempIndexStruct &r)
bool operator>(const TempIndexStruct &l, const TempIndexStruct &r)
void Release_Refs(SortingNodeStruct *state)
bool operator<(const TempIndexStruct &l, const TempIndexStruct &r)
bool operator<=(const TempIndexStruct &l, const TempIndexStruct &r)
unsigned vertex_buffer_types[MAX_VERTEX_STREAMS]
unsigned short index_base_offset
unsigned short vba_offset
unsigned short iba_offset
IndexBufferClass * index_buffer
TextureBaseClass * Textures[MAX_TEXTURE_STAGES]
unsigned index_buffer_type
VertexMaterialClass * material
VertexBufferClass * vertex_buffers[MAX_VERTEX_STREAMS]