2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <glib/gslist.h>
25 #include "preferences.h"
26 #include "brush_primit.h"
29 std::set<Callback> g_patchTextureChangedCallbacks;
31 void Patch_addTextureChangedCallback(const Callback& callback)
33 g_patchTextureChangedCallbacks.insert(callback);
36 void Patch_textureChanged()
38 std::for_each(g_patchTextureChangedCallbacks.begin(), g_patchTextureChangedCallbacks.end(), CallbackInvoke());
42 Shader* PatchInstance::m_state_selpoint;
43 Shader* Patch::m_state_ctrl;
44 Shader* Patch::m_state_lattice;
45 EPatchType Patch::m_type;
48 std::size_t MAX_PATCH_WIDTH = 0;
49 std::size_t MAX_PATCH_HEIGHT = 0;
51 int g_PatchSubdivideThreshold = 4;
53 void BezierCurveTree_Delete(BezierCurveTree *pCurve)
57 BezierCurveTree_Delete(pCurve->left);
58 BezierCurveTree_Delete(pCurve->right);
63 std::size_t BezierCurveTree_Setup(BezierCurveTree *pCurve, std::size_t index, std::size_t stride)
67 if(pCurve->left && pCurve->right)
69 index = BezierCurveTree_Setup(pCurve->left, index, stride);
70 pCurve->index = index*stride;
72 index = BezierCurveTree_Setup(pCurve->right, index, stride);
76 pCurve->index = BEZIERCURVETREE_MAX_INDEX;
83 bool BezierCurve_IsCurved(BezierCurve *pCurve)
85 Vector3 vTemp(vector3_subtracted(pCurve->right, pCurve->left));
86 Vector3 v1(vector3_subtracted(pCurve->crd, pCurve->left));
87 Vector3 v2(vector3_subtracted(pCurve->right, pCurve->crd));
89 if(vector3_equal(v1, g_vector3_identity) || vector3_equal(vTemp, v1)) // return 0 if 1->2 == 0 or 1->2 == 1->3
92 vector3_normalise(v1);
93 vector3_normalise(v2);
94 if(vector3_equal(v1, v2))
98 const double width = vector3_length(v3);
99 vector3_scale(v3, 1.0 / width);
101 if(vector3_equal(v1, v3) && vector3_equal(v2, v3))
104 const double angle = acos(vector3_dot(v1, v2)) / c_pi;
106 const double index = width * angle;
108 if(index > static_cast<double>(g_PatchSubdivideThreshold))
113 void BezierInterpolate(BezierCurve *pCurve)
115 pCurve->left = vector3_mid(pCurve->left, pCurve->crd);
116 pCurve->right = vector3_mid(pCurve->crd, pCurve->right);
117 pCurve->crd = vector3_mid(pCurve->left, pCurve->right);
120 void BezierCurveTree_FromCurveList(BezierCurveTree *pTree, GSList *pCurveList)
122 GSList *pLeftList = 0;
123 GSList *pRightList = 0;
124 BezierCurve *pCurve, *pLeftCurve, *pRightCurve;
127 for (GSList *l = pCurveList; l; l = l->next)
129 pCurve = (BezierCurve *)(l->data);
130 if(bSplit || BezierCurve_IsCurved(pCurve))
133 pLeftCurve = new BezierCurve;
134 pRightCurve = new BezierCurve;
135 pLeftCurve->left = pCurve->left;
136 pRightCurve->right = pCurve->right;
137 BezierInterpolate(pCurve);
138 pLeftCurve->crd = pCurve->left;
139 pRightCurve->crd = pCurve->right;
140 pLeftCurve->right = pCurve->crd;
141 pRightCurve->left = pCurve->crd;
143 pLeftList = g_slist_prepend(pLeftList, pLeftCurve);
144 pRightList = g_slist_prepend(pRightList, pRightCurve);
148 if(pLeftList != 0 && pRightList != 0)
150 pTree->left = new BezierCurveTree;
151 pTree->right = new BezierCurveTree;
152 BezierCurveTree_FromCurveList(pTree->left, pLeftList);
153 BezierCurveTree_FromCurveList(pTree->right, pRightList);
157 for (l = pLeftList; l != 0; l = g_slist_next(l))
158 delete (BezierCurve*)l->data;
160 for (l = pRightList; l != 0; l = g_slist_next(l))
161 delete (BezierCurve*)l->data;
164 g_slist_free(pLeftList);
165 g_slist_free(pRightList);
175 int Patch::m_CycleCapIndex = 0;
178 void Patch::setDims (std::size_t w, std::size_t h)
182 ASSERT_MESSAGE(w <= MAX_PATCH_WIDTH, "patch too wide");
183 if(w > MAX_PATCH_WIDTH)
185 else if(w < MIN_PATCH_WIDTH)
190 ASSERT_MESSAGE(h <= MAX_PATCH_HEIGHT, "patch too tall");
191 if(h > MAX_PATCH_HEIGHT)
192 h = MAX_PATCH_HEIGHT;
193 else if(h < MIN_PATCH_HEIGHT)
194 h = MIN_PATCH_HEIGHT;
196 m_width = w; m_height = h;
198 if(m_width * m_height != m_ctrl.size())
200 m_ctrl.resize(m_width * m_height);
201 onAllocate(m_ctrl.size());
205 inline const Colour4b& colour_for_index(std::size_t i, std::size_t width)
207 return (i%2 || (i/width)%2) ? colour_inside : colour_corner;
210 void Patch::UpdateCachedData()
212 if(!m_width || !m_height)
214 BuildTesselationCurves(ROW);
215 BuildTesselationCurves(COL);
219 IndexBuffer ctrl_indices;
221 m_ctrl_vertices.clear();
222 m_lattice_indices.clear();
224 m_lattice_indices.reserve(((m_width * (m_height - 1)) + (m_height * (m_width - 1))) << 1);
225 ctrl_indices.reserve(m_ctrlTransformed.size());
227 UniqueVertexBuffer<PointVertex> inserter(m_ctrl_vertices);
228 for(iterator i = m_ctrlTransformed.begin(); i != m_ctrlTransformed.end(); ++i)
230 ctrl_indices.insert(inserter.insert(pointvertex_quantised(PointVertex(reinterpret_cast<const Vertex3f&>((*i).m_vertex), colour_for_index(i - m_ctrlTransformed.begin(), m_width)))));
234 for(IndexBuffer::iterator i = ctrl_indices.begin(); i != ctrl_indices.end(); ++i)
236 if(std::size_t(i - ctrl_indices.begin()) % m_width)
238 m_lattice_indices.insert(*(i - 1));
239 m_lattice_indices.insert(*i);
241 if(std::size_t(i - ctrl_indices.begin()) >= m_width)
243 m_lattice_indices.insert(*(i - m_width));
244 m_lattice_indices.insert(*i);
251 Array<RenderIndex>::iterator first = m_tess.m_indices.begin();
252 for(std::size_t s=0; s<m_tess.m_numStrips; s++)
254 Array<RenderIndex>::iterator last = first + m_tess.m_lenStrips;
256 for(Array<RenderIndex>::iterator i(first); i+2 != last; i += 2)
258 ArbitraryMeshTriangle_sumTangents(m_tess.m_vertices[*(i+0)], m_tess.m_vertices[*(i+1)], m_tess.m_vertices[*(i+2)]);
259 ArbitraryMeshTriangle_sumTangents(m_tess.m_vertices[*(i+2)], m_tess.m_vertices[*(i+1)], m_tess.m_vertices[*(i+3)]);
265 for(Array<ArbitraryMeshVertex>::iterator i = m_tess.m_vertices.begin(); i != m_tess.m_vertices.end(); ++i)
267 vector3_normalise(reinterpret_cast<Vector3&>((*i).tangent));
268 vector3_normalise(reinterpret_cast<Vector3&>((*i).bitangent));
276 void Patch::InvertMatrix()
280 PatchControlArray_invert(m_ctrl, m_width, m_height);
282 controlPointsChanged();
285 void Patch::TransposeMatrix()
290 Array<PatchControl> tmp(m_width * m_height);
291 copy_ctrl(tmp.data(), m_ctrl.data(), m_ctrl.data() + m_width * m_height);
293 PatchControlIter from = tmp.data();
294 for(std::size_t h = 0; h != m_height; ++h)
296 PatchControlIter to = m_ctrl.data() + h;
297 for(std::size_t w = 0; w != m_width; ++w, ++from, to += m_height)
305 std::size_t tmp = m_width;
310 controlPointsChanged();
313 void Patch::Redisperse(EMatrixMajor mt)
315 std::size_t w, h, width, height, row_stride, col_stride;
316 PatchControl* p1, * p2, * p3;
323 width = (m_width-1)>>1;
326 row_stride = m_width;
329 width = (m_height-1)>>1;
331 col_stride = m_width;
335 ERROR_MESSAGE("neither row-major nor column-major");
339 for(h=0;h<height;h++)
341 p1 = m_ctrl.data()+(h*row_stride);
346 p2->m_vertex = vector3_mid(p1->m_vertex, p3->m_vertex);
351 controlPointsChanged();
354 void Patch::InsertRemove(bool bInsert, bool bColumn, bool bFirst)
360 if(bColumn && (m_width + 2 <= MAX_PATCH_WIDTH))
361 InsertPoints(COL, bFirst);
362 else if(m_height + 2 <= MAX_PATCH_HEIGHT)
363 InsertPoints(ROW, bFirst);
367 if(bColumn && (m_width - 2 >= MIN_PATCH_WIDTH))
368 RemovePoints(COL, bFirst);
369 else if(m_height - 2 >= MIN_PATCH_HEIGHT)
370 RemovePoints(ROW, bFirst);
373 controlPointsChanged();
376 Patch* Patch::MakeCap(Patch* patch, EPatchCap eType, EMatrixMajor mt, bool bFirst)
378 std::size_t i, width, height;
391 ERROR_MESSAGE("neither row-major nor column-major");
395 Array<Vector3> p(width);
397 std::size_t nIndex = (bFirst) ? 0 : height-1;
400 for (i=0; i<width; i++)
402 p[(bFirst)?i:(width-1)-i] = ctrlAt(nIndex, i).m_vertex;
407 for (i=0; i<width; i++)
409 p[(bFirst)?i:(width-1)-i] = ctrlAt(i, nIndex).m_vertex;
413 patch->ConstructSeam(eType, p.data(), width);
417 void Patch::FlipTexture(int nAxis)
421 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
423 (*i).m_texcoord[nAxis] = -(*i).m_texcoord[nAxis];
426 controlPointsChanged();
429 void Patch::TranslateTexture(float s, float t)
433 s = -1 * s / m_state->getTexture().width;
434 t = t / m_state->getTexture().height;
436 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
438 (*i).m_texcoord[0] += s;
439 (*i).m_texcoord[1] += t;
442 controlPointsChanged();
445 void Patch::ScaleTexture(float s, float t)
449 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
451 (*i).m_texcoord[0] *= s;
452 (*i).m_texcoord[1] *= t;
455 controlPointsChanged();
458 void Patch::RotateTexture(float angle)
462 const float s = static_cast<float>(sin(degrees_to_radians(angle)));
463 const float c = static_cast<float>(cos(degrees_to_radians(angle)));
465 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
467 const float x = (*i).m_texcoord[0];
468 const float y = (*i).m_texcoord[1];
469 (*i).m_texcoord[0] = (x * c) - (y * s);
470 (*i).m_texcoord[1] = (y * c) + (x * s);
473 controlPointsChanged();
477 void Patch::SetTextureRepeat(float s, float t)
480 float si, ti, sc, tc;
485 si = s / (float)(m_width - 1);
486 ti = t / (float)(m_height - 1);
488 pDest = m_ctrl.data();
489 for (h=0, tc = 0.0f; h<m_height; h++, tc+=ti)
491 for (w=0, sc = 0.0f; w<m_width; w++, sc+=si)
493 pDest->m_texcoord[0] = sc;
494 pDest->m_texcoord[1] = tc;
499 controlPointsChanged();
503 void Patch::SetTextureInfo(texdef_t *pt)
505 if(pt->getShift()[0] || pt->getShift()[1])
506 TranslateTexture (pt->getShift()[0], pt->getShift()[1]);
507 else if(pt->getScale()[0] || pt->getScale()[1])
509 if(pt->getScale()[0] == 0.0f) pt->setScale(0, 1.0f);
510 if(pt->getScale()[1] == 0.0f) pt->setScale(1, 1.0f);
511 ScaleTexture (pt->getScale()[0], pt->getScale()[1]);
514 RotateTexture (pt->rotate);
518 inline int texture_axis(const Vector3& normal)
520 // axis dominance order: Z, X, Y
521 return (normal.x() >= normal.y()) ? (normal.x() > normal.z()) ? 0 : 2 : (normal.y() > normal.z()) ? 1 : 2;
524 void Patch::CapTexture()
526 const PatchControl& p1 = m_ctrl[m_width];
527 const PatchControl& p2 = m_ctrl[m_width*(m_height-1)];
528 const PatchControl& p3 = m_ctrl[(m_width*m_height)-1];
531 Vector3 normal(g_vector3_identity);
534 Vector3 tmp(vector3_cross(
535 vector3_subtracted(p2.m_vertex, m_ctrl[0].m_vertex),
536 vector3_subtracted(p3.m_vertex, m_ctrl[0].m_vertex)
538 if(!vector3_equal(tmp, g_vector3_identity))
540 vector3_add(normal, tmp);
544 Vector3 tmp(vector3_cross(
545 vector3_subtracted(p1.m_vertex, p3.m_vertex),
546 vector3_subtracted(m_ctrl[0].m_vertex, p3.m_vertex)
548 if(!vector3_equal(tmp, g_vector3_identity))
550 vector3_add(normal, tmp);
554 ProjectTexture(texture_axis(normal));
557 // uses longest parallel chord to calculate texture coords for each row/col
558 void Patch::NaturalTexture()
563 float fSize = (float)m_state->getTexture().width * Texdef_getDefaultTextureScale();
567 PatchControl* pWidth = m_ctrl.data();
568 for (std::size_t w=0; w<m_width; w++, pWidth++)
571 PatchControl* pHeight = pWidth;
572 for (std::size_t h=0; h<m_height; h++, pHeight+=m_width)
573 pHeight->m_texcoord[0] = static_cast<float>(tex);
580 PatchControl* pHeight = pWidth;
581 for (std::size_t h=0; h<m_height; h++, pHeight+=m_width)
583 Vector3 v(vector3_subtracted(pHeight->m_vertex, (pHeight+1)->m_vertex));
584 double length = tex + (vector3_length(v) / fSize);
585 if(fabs(length) > texBest) texBest = length;
594 float fSize = -(float)m_state->getTexture().height * Texdef_getDefaultTextureScale();
598 PatchControl* pHeight = m_ctrl.data();
599 for (std::size_t h=0; h<m_height; h++, pHeight+=m_width)
602 PatchControl* pWidth = pHeight;
603 for (std::size_t w=0; w<m_width; w++, pWidth++)
604 pWidth->m_texcoord[1] = static_cast<float>(tex);
611 PatchControl* pWidth = pHeight;
612 for (std::size_t w=0; w<m_width; w++, pWidth++)
614 Vector3 v(vector3_subtracted(pWidth->m_vertex, (pWidth+m_width)->m_vertex));
615 double length = tex + (vector3_length(v) / fSize);
616 if(fabs(length) > texBest) texBest = length;
624 controlPointsChanged();
631 void Patch::AccumulateBBox()
633 m_aabb_local = AABB();
635 for(PatchControlArray::iterator i = m_ctrlTransformed.begin(); i != m_ctrlTransformed.end(); ++i)
637 aabb_extend_by_point_safe(m_aabb_local, (*i).m_vertex);
644 void Patch::InsertPoints(EMatrixMajor mt, bool bFirst)
646 std::size_t width, height, row_stride, col_stride;
652 row_stride = m_width;
657 col_stride = m_width;
663 ERROR_MESSAGE("neither row-major nor column-major");
669 PatchControl* p1 = m_ctrl.data();
670 for(std::size_t w = 0; w != width; ++w, p1 += col_stride)
673 PatchControl* p2 = p1;
674 for(std::size_t h = 1; h < height; h += 2, p2 += 2 * row_stride)
676 if(0)//p2->m_selectable.isSelected())
689 PatchControl* p2 = p1;
690 for(std::size_t h = 0; h < height; h += 2, p2 += 2 * row_stride)
692 if(0)//p2->m_selectable.isSelected())
706 Array<PatchControl> tmp(m_ctrl);
708 std::size_t row_stride2, col_stride2;
712 setDims(m_width, m_height+2);
714 row_stride2 = m_width;
717 setDims(m_width+2, m_height);
718 col_stride2 = m_width;
722 ERROR_MESSAGE("neither row-major nor column-major");
747 for(std::size_t w = 0; w != width; ++w)
749 PatchControl* p1 = tmp.data() + (w*col_stride);
750 PatchControl* p2 = m_ctrl.data() + (w*col_stride2);
751 for(std::size_t h = 0; h != height; ++h, p2 += row_stride2, p1 += row_stride)
755 p2 += 2 * row_stride2;
760 p1 = tmp.data() + (w*col_stride+pos*row_stride);
761 p2 = m_ctrl.data() + (w*col_stride2+pos*row_stride2);
763 PatchControl* r2a = (p2+row_stride2);
764 PatchControl* r2b = (p2-row_stride2);
765 PatchControl* c2a = (p1-2*row_stride);
766 PatchControl* c2b = (p1-row_stride);
768 // set two new row points
769 *(p2+2*row_stride2) = *p1;
772 for(std::size_t i = 0; i != 3; ++i)
774 r2a->m_vertex[i] = float_mid(c2b->m_vertex[i], p1->m_vertex[i]);
776 r2b->m_vertex[i] = float_mid(c2a->m_vertex[i], c2b->m_vertex[i]);
778 p2->m_vertex[i] = float_mid(r2a->m_vertex[i], r2b->m_vertex[i]);
780 for(std::size_t i = 0; i != 2; ++i)
782 r2a->m_texcoord[i] = float_mid(c2b->m_texcoord[i], p1->m_texcoord[i]);
784 r2b->m_texcoord[i] = float_mid(c2a->m_texcoord[i], c2b->m_texcoord[i]);
786 p2->m_texcoord[i] = float_mid(r2a->m_texcoord[i], r2b->m_texcoord[i]);
791 void Patch::RemovePoints(EMatrixMajor mt, bool bFirst)
793 std::size_t width, height, row_stride, col_stride;
799 row_stride = m_width;
804 col_stride = m_width;
810 ERROR_MESSAGE("neither row-major nor column-major");
816 PatchControl* p1 = m_ctrl.data();
817 for(std::size_t w = 0; w != width; ++w, p1 += col_stride)
820 PatchControl* p2 = p1;
821 for(std::size_t h=1; h < height; h += 2, p2 += 2 * row_stride)
823 if(0)//p2->m_selectable.isSelected())
836 PatchControl* p2 = p1;
837 for(std::size_t h=0; h < height; h += 2, p2 += 2 * row_stride)
839 if(0)//p2->m_selectable.isSelected())
853 Array<PatchControl> tmp(m_ctrl);
855 std::size_t row_stride2, col_stride2;
859 setDims(m_width, m_height-2);
861 row_stride2 = m_width;
864 setDims(m_width-2, m_height);
865 col_stride2 = m_width;
869 ERROR_MESSAGE("neither row-major nor column-major");
888 else if(pos > height - 3)
897 for(std::size_t w = 0; w != width; w++)
899 PatchControl* p1 = tmp.data() + (w*col_stride);
900 PatchControl* p2 = m_ctrl.data() + (w*col_stride2);
901 for(std::size_t h = 0; h != height; ++h, p2 += row_stride2, p1 += row_stride)
905 p1 += 2 * row_stride2; h += 2;
910 p1 = tmp.data() + (w*col_stride+pos*row_stride);
911 p2 = m_ctrl.data() + (w*col_stride2+pos*row_stride2);
913 for(std::size_t i=0; i<3; i++)
915 (p2-row_stride2)->m_vertex[i] = ((p1+2*row_stride)->m_vertex[i]+(p1-2*row_stride)->m_vertex[i]) * 0.5f;
917 (p2-row_stride2)->m_vertex[i] = (p2-row_stride2)->m_vertex[i]+(2.0f * ((p1)->m_vertex[i]-(p2-row_stride2)->m_vertex[i]));
919 for(std::size_t i=0; i<2; i++)
921 (p2-row_stride2)->m_texcoord[i] = ((p1+2*row_stride)->m_texcoord[i]+(p1-2*row_stride)->m_texcoord[i]) * 0.5f;
923 (p2-row_stride2)->m_texcoord[i] = (p2-row_stride2)->m_texcoord[i]+(2.0f * ((p1)->m_texcoord[i]-(p2-row_stride2)->m_texcoord[i]));
928 void Patch::ConstructSeam(EPatchCap eType, Vector3* p, std::size_t width)
935 m_ctrl[0].m_vertex = p[0];
936 m_ctrl[1].m_vertex = p[1];
937 m_ctrl[2].m_vertex = p[1];
938 m_ctrl[3].m_vertex = p[1];
939 m_ctrl[4].m_vertex = p[1];
940 m_ctrl[5].m_vertex = p[1];
941 m_ctrl[6].m_vertex = p[2];
942 m_ctrl[7].m_vertex = p[1];
943 m_ctrl[8].m_vertex = p[1];
949 Vector3 p3(vector3_added(p[2], vector3_subtracted(p[0], p[1])));
950 m_ctrl[0].m_vertex = p3;
951 m_ctrl[1].m_vertex = p3;
952 m_ctrl[2].m_vertex = p[2];
953 m_ctrl[3].m_vertex = p3;
954 m_ctrl[4].m_vertex = p3;
955 m_ctrl[5].m_vertex = p[1];
956 m_ctrl[6].m_vertex = p3;
957 m_ctrl[7].m_vertex = p3;
958 m_ctrl[8].m_vertex = p[0];
963 Vector3 p5(vector3_mid(p[0], p[4]));
966 m_ctrl[0].m_vertex = p[0];
967 m_ctrl[1].m_vertex = p5;
968 m_ctrl[2].m_vertex = p[4];
969 m_ctrl[3].m_vertex = p[1];
970 m_ctrl[4].m_vertex = p[2];
971 m_ctrl[5].m_vertex = p[3];
972 m_ctrl[6].m_vertex = p[2];
973 m_ctrl[7].m_vertex = p[2];
974 m_ctrl[8].m_vertex = p[2];
980 m_ctrl[0].m_vertex = p[4];
981 m_ctrl[1].m_vertex = p[3];
982 m_ctrl[2].m_vertex = p[2];
983 m_ctrl[3].m_vertex = p[1];
984 m_ctrl[4].m_vertex = p[0];
985 m_ctrl[5].m_vertex = p[3];
986 m_ctrl[6].m_vertex = p[3];
987 m_ctrl[7].m_vertex = p[2];
988 m_ctrl[8].m_vertex = p[1];
989 m_ctrl[9].m_vertex = p[1];
990 m_ctrl[10].m_vertex = p[3];
991 m_ctrl[11].m_vertex = p[3];
992 m_ctrl[12].m_vertex = p[2];
993 m_ctrl[13].m_vertex = p[1];
994 m_ctrl[14].m_vertex = p[1];
999 std::size_t mid = (width - 1) >> 1;
1001 bool degenerate = (mid % 2) != 0;
1003 std::size_t newHeight = mid + (degenerate ? 2 : 1);
1005 setDims(3, newHeight);
1010 for(std::size_t i = width; i != width + 2; ++i)
1012 p[i] = p[width - 1];
1017 PatchControl* pCtrl = m_ctrl.data();
1018 for(std::size_t i = 0; i != m_height; ++i, pCtrl += m_width)
1020 pCtrl->m_vertex = p[i];
1024 PatchControl* pCtrl = m_ctrl.data() + 2;
1025 std::size_t h = m_height - 1;
1026 for(std::size_t i = 0; i != m_height; ++i, pCtrl += m_width)
1028 pCtrl->m_vertex = p[h + (h - i)];
1036 ERROR_MESSAGE("invalid patch-cap type");
1040 controlPointsChanged();
1043 void Patch::ProjectTexture(int nAxis)
1064 ERROR_MESSAGE("invalid axis");
1068 float fWidth = 1 / (m_state->getTexture().width * Texdef_getDefaultTextureScale());
1069 float fHeight = 1 / (m_state->getTexture().height * -Texdef_getDefaultTextureScale());
1071 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
1073 (*i).m_texcoord[0] = (*i).m_vertex[s] * fWidth;
1074 (*i).m_texcoord[1] = (*i).m_vertex[t] * fHeight;
1077 controlPointsChanged();
1080 void Patch::constructPlane(const AABB& aabb, int axis, std::size_t width, std::size_t height)
1082 setDims(width, height);
1087 case 2: x=0; y=1; z=2; break;
1088 case 1: x=0; y=2; z=1; break;
1089 case 0: x=1; y=2; z=0; break;
1091 ERROR_MESSAGE("invalid view-type");
1095 if(m_width < MIN_PATCH_WIDTH || m_width > MAX_PATCH_WIDTH) m_width = 3;
1096 if(m_height < MIN_PATCH_HEIGHT || m_height > MAX_PATCH_HEIGHT) m_height = 3;
1099 vStart[x] = aabb.origin[x] - aabb.extents[x];
1100 vStart[y] = aabb.origin[y] - aabb.extents[y];
1101 vStart[z] = aabb.origin[z];
1103 float xAdj = fabsf((vStart[x] - (aabb.origin[x] + aabb.extents[x])) / (float)(m_width - 1));
1104 float yAdj = fabsf((vStart[y] - (aabb.origin[y] + aabb.extents[y])) / (float)(m_height - 1));
1107 vTmp[z] = vStart[z];
1108 PatchControl* pCtrl = m_ctrl.data();
1111 for (std::size_t h=0; h<m_height; h++)
1114 for (std::size_t w=0; w<m_width; w++, ++pCtrl)
1116 pCtrl->m_vertex = vTmp;
1125 void Patch::ConstructPrefab(const AABB& aabb, EPatchPrefab eType, int axis, std::size_t width, std::size_t height)
1131 vPos[0] = vector3_subtracted(aabb.origin, aabb.extents);
1132 vPos[1] = aabb.origin;
1133 vPos[2] = vector3_added(aabb.origin, aabb.extents);
1138 constructPlane(aabb, axis, width, height);
1140 else if(eType == eSqCylinder
1141 || eType == eCylinder
1142 || eType == eDenseCylinder
1143 || eType == eVeryDenseCylinder
1145 || eType == eSphere)
1147 unsigned char *pIndex;
1148 unsigned char pCylIndex[] =
1162 PatchControl *pStart;
1165 case eSqCylinder: setDims(9, 3);
1166 pStart = m_ctrl.data();
1168 case eDenseCylinder:
1169 case eVeryDenseCylinder:
1172 pStart = m_ctrl.data() + 1;
1174 case eCone: setDims(9, 3);
1175 pStart = m_ctrl.data() + 1;
1179 pStart = m_ctrl.data() + (9+1);
1182 ERROR_MESSAGE("this should be unreachable");
1186 for(std::size_t h=0; h<3; h++, pStart+=9)
1189 PatchControl* pCtrl = pStart;
1190 for(std::size_t w=0; w<8; w++, pCtrl++)
1192 pCtrl->m_vertex[0] = vPos[pIndex[0]][0];
1193 pCtrl->m_vertex[1] = vPos[pIndex[1]][1];
1194 pCtrl->m_vertex[2] = vPos[h][2];
1203 PatchControl* pCtrl=m_ctrl.data();
1204 for(std::size_t h=0; h<3; h++, pCtrl+=9)
1206 pCtrl[8].m_vertex = pCtrl[0].m_vertex;
1210 case eDenseCylinder:
1211 case eVeryDenseCylinder:
1214 PatchControl* pCtrl=m_ctrl.data();
1215 for (std::size_t h=0; h<3; h++, pCtrl+=9)
1217 pCtrl[0].m_vertex = pCtrl[8].m_vertex;
1223 PatchControl* pCtrl=m_ctrl.data();
1224 for (std::size_t h=0; h<2; h++, pCtrl+=9)
1226 pCtrl[0].m_vertex = pCtrl[8].m_vertex;
1230 PatchControl* pCtrl=m_ctrl.data()+9*2;
1231 for (std::size_t w=0; w<9; w++, pCtrl++)
1233 pCtrl->m_vertex[0] = vPos[1][0];
1234 pCtrl->m_vertex[1] = vPos[1][1];
1235 pCtrl->m_vertex[2] = vPos[2][2];
1241 PatchControl* pCtrl=m_ctrl.data()+9;
1242 for (std::size_t h=0; h<3; h++, pCtrl+=9)
1244 pCtrl[0].m_vertex = pCtrl[8].m_vertex;
1248 PatchControl* pCtrl = m_ctrl.data();
1249 for (std::size_t w=0; w<9; w++, pCtrl++)
1251 pCtrl->m_vertex[0] = vPos[1][0];
1252 pCtrl->m_vertex[1] = vPos[1][1];
1253 pCtrl->m_vertex[2] = vPos[2][2];
1257 PatchControl* pCtrl = m_ctrl.data()+(9*4);
1258 for (std::size_t w=0; w<9; w++, pCtrl++)
1260 pCtrl->m_vertex[0] = vPos[1][0];
1261 pCtrl->m_vertex[1] = vPos[1][1];
1262 pCtrl->m_vertex[2] = vPos[2][2];
1266 ERROR_MESSAGE("this should be unreachable");
1270 else if (eType == eBevel)
1272 unsigned char *pIndex;
1273 unsigned char pBevIndex[] =
1282 PatchControl* pCtrl = m_ctrl.data();
1283 for(std::size_t h=0; h<3; h++)
1286 for(std::size_t w=0; w<3; w++, pIndex+=2, pCtrl++)
1288 pCtrl->m_vertex[0] = vPos[pIndex[0]][0];
1289 pCtrl->m_vertex[1] = vPos[pIndex[1]][1];
1290 pCtrl->m_vertex[2] = vPos[h][2];
1294 else if(eType == eEndCap)
1296 unsigned char *pIndex;
1297 unsigned char pEndIndex[] =
1308 PatchControl* pCtrl = m_ctrl.data();
1309 for(std::size_t h=0; h<3; h++)
1312 for(std::size_t w=0; w<5; w++, pIndex+=2, pCtrl++)
1314 pCtrl->m_vertex[0] = vPos[pIndex[0]][0];
1315 pCtrl->m_vertex[1] = vPos[pIndex[1]][1];
1316 pCtrl->m_vertex[2] = vPos[h][2];
1321 if(eType == eDenseCylinder)
1323 InsertRemove(true, false, true);
1326 if(eType == eVeryDenseCylinder)
1328 InsertRemove(true, false, false);
1329 InsertRemove(true, false, true);
1335 void Patch::RenderDebug(RenderStateFlags state) const
1337 for (std::size_t i = 0; i<m_tess.m_numStrips; i++)
1339 glBegin(GL_QUAD_STRIP);
1340 for (std::size_t j = 0; j<m_tess.m_lenStrips; j++)
1342 glNormal3fv(normal3f_to_array((m_tess.m_vertices.data() + m_tess.m_indices[i*m_tess.m_lenStrips+j])->normal));
1343 glTexCoord2fv(texcoord2f_to_array((m_tess.m_vertices.data() + m_tess.m_indices[i*m_tess.m_lenStrips+j])->texcoord));
1344 glVertex3fv(vertex3f_to_array((m_tess.m_vertices.data() + m_tess.m_indices[i*m_tess.m_lenStrips+j])->vertex));
1350 void RenderablePatchSolid::RenderNormals() const
1352 const std::size_t width = m_tess.m_numStrips+1;
1353 const std::size_t height = m_tess.m_lenStrips>>1;
1355 for(std::size_t i=0;i<width;i++)
1357 for(std::size_t j=0;j<height;j++)
1362 vertex3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->vertex),
1363 vector3_scaled(normal3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->normal), 8)
1366 glVertex3fv(vertex3f_to_array((m_tess.m_vertices.data() + (j*width+i))->vertex));
1367 glVertex3fv(&vNormal[0]);
1372 vertex3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->vertex),
1373 vector3_scaled(normal3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->tangent), 8)
1376 glVertex3fv(vertex3f_to_array((m_tess.m_vertices.data() + (j*width+i))->vertex));
1377 glVertex3fv(&vNormal[0]);
1382 vertex3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->vertex),
1383 vector3_scaled(normal3f_to_vector3((m_tess.m_vertices.data() + (j*width+i))->bitangent), 8)
1386 glVertex3fv(vertex3f_to_array((m_tess.m_vertices.data() + (j*width+i))->vertex));
1387 glVertex3fv(&vNormal[0]);
1394 #define DEGEN_0a 0x01
1395 #define DEGEN_1a 0x02
1396 #define DEGEN_2a 0x04
1397 #define DEGEN_0b 0x08
1398 #define DEGEN_1b 0x10
1399 #define DEGEN_2b 0x20
1401 #define AVERAGE 0x80
1404 unsigned int subarray_get_degen(PatchControlIter subarray, std::size_t strideU, std::size_t strideV)
1406 unsigned int nDegen = 0;
1407 const PatchControl* p1;
1408 const PatchControl* p2;
1412 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1416 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1419 p1 = subarray + strideV;
1421 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1425 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1428 p1 = subarray + (strideV << 1);
1430 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1434 if(vector3_equal(p1->m_vertex, p2->m_vertex))
1441 inline void deCasteljau3(const Vector3& P0, const Vector3& P1, const Vector3& P2, Vector3& P01, Vector3& P12, Vector3& P012)
1443 P01 = vector3_mid(P0, P1);
1444 P12 = vector3_mid(P1, P2);
1445 P012 = vector3_mid(P01, P12);
1448 inline void BezierInterpolate3( const Vector3& start, Vector3& left, Vector3& mid, Vector3& right, const Vector3& end )
1450 left = vector3_mid(start, mid);
1451 right = vector3_mid(mid, end);
1452 mid = vector3_mid(left, right);
1455 inline void BezierInterpolate2( const Vector2& start, Vector2& left, Vector2& mid, Vector2& right, const Vector2& end )
1457 left[0]= float_mid(start[0], mid[0]);
1458 left[1] = float_mid(start[1], mid[1]);
1459 right[0] = float_mid(mid[0], end[0]);
1460 right[1] = float_mid(mid[1], end[1]);
1461 mid[0] = float_mid(left[0], right[0]);
1462 mid[1] = float_mid(left[1], right[1]);
1466 inline Vector2& texcoord_for_index(Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1468 return reinterpret_cast<Vector2&>(vertices[index].texcoord);
1471 inline Vector3& vertex_for_index(Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1473 return reinterpret_cast<Vector3&>(vertices[index].vertex);
1476 inline Vector3& normal_for_index(Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1478 return reinterpret_cast<Vector3&>(vertices[index].normal);
1481 inline Vector3& tangent_for_index(Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1483 return reinterpret_cast<Vector3&>(vertices[index].tangent);
1486 inline Vector3& bitangent_for_index(Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1488 return reinterpret_cast<Vector3&>(vertices[index].bitangent);
1491 inline const Vector2& texcoord_for_index(const Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1493 return reinterpret_cast<const Vector2&>(vertices[index].texcoord);
1496 inline const Vector3& vertex_for_index(const Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1498 return reinterpret_cast<const Vector3&>(vertices[index].vertex);
1501 inline const Vector3& normal_for_index(const Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1503 return reinterpret_cast<const Vector3&>(vertices[index].normal);
1506 inline const Vector3& tangent_for_index(const Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1508 return reinterpret_cast<const Vector3&>(vertices[index].tangent);
1511 inline const Vector3& bitangent_for_index(const Array<ArbitraryMeshVertex>& vertices, std::size_t index)
1513 return reinterpret_cast<const Vector3&>(vertices[index].bitangent);
1516 #include "math/curve.h"
1518 inline PatchControl QuadraticBezier_evaluate(const PatchControl* firstPoint, double t)
1520 PatchControl result = { Vector3(0, 0, 0), Vector2(0, 0) };
1521 double denominator = 0;
1524 double weight = BernsteinPolynomial<Zero, Two>::apply(t);
1525 vector3_add(result.m_vertex, vector3_scaled(firstPoint[0].m_vertex, weight));
1526 vector2_add(result.m_texcoord, vector2_scaled(firstPoint[0].m_texcoord, weight));
1527 denominator += weight;
1530 double weight = BernsteinPolynomial<One, Two>::apply(t);
1531 vector3_add(result.m_vertex, vector3_scaled(firstPoint[1].m_vertex, weight));
1532 vector2_add(result.m_texcoord, vector2_scaled(firstPoint[1].m_texcoord, weight));
1533 denominator += weight;
1536 double weight = BernsteinPolynomial<Two, Two>::apply(t);
1537 vector3_add(result.m_vertex, vector3_scaled(firstPoint[2].m_vertex, weight));
1538 vector2_add(result.m_texcoord, vector2_scaled(firstPoint[2].m_texcoord, weight));
1539 denominator += weight;
1542 vector3_divide(result.m_vertex, denominator);
1543 vector2_divide(result.m_texcoord, denominator);
1547 inline Vector3 vector3_linear_interpolated(const Vector3& a, const Vector3& b, double t)
1549 return vector3_added(vector3_scaled(a, 1.0 - t), vector3_scaled(b, t));
1552 inline Vector2 vector2_linear_interpolated(const Vector2& a, const Vector2& b, double t)
1554 return vector2_added(vector2_scaled(a, 1.0 - t), vector2_scaled(b, t));
1557 void normalise_safe(Vector3& normal)
1559 if(!vector3_equal(normal, g_vector3_identity))
1561 vector3_normalise(normal);
1565 inline void QuadraticBezier_evaluate(const PatchControl& a, const PatchControl& b, const PatchControl& c, double t, PatchControl& point, PatchControl& left, PatchControl& right)
1567 left.m_vertex = vector3_linear_interpolated(a.m_vertex, b.m_vertex, t);
1568 left.m_texcoord = vector2_linear_interpolated(a.m_texcoord, b.m_texcoord, t);
1569 right.m_vertex = vector3_linear_interpolated(b.m_vertex, c.m_vertex, t);
1570 right.m_texcoord = vector2_linear_interpolated(b.m_texcoord, c.m_texcoord, t);
1571 point.m_vertex = vector3_linear_interpolated(left.m_vertex, right.m_vertex, t);
1572 point.m_texcoord = vector2_linear_interpolated(left.m_texcoord, right.m_texcoord, t);
1575 void Patch::TesselateSubMatrixFixed(ArbitraryMeshVertex* vertices, std::size_t strideX, std::size_t strideY, unsigned int nFlagsX, unsigned int nFlagsY, PatchControl* subMatrix[3][3])
1577 double incrementU = 1.0 / m_subdivisions_x;
1578 double incrementV = 1.0 / m_subdivisions_y;
1579 const std::size_t width = m_subdivisions_x + 1;
1580 const std::size_t height = m_subdivisions_y + 1;
1582 for(std::size_t i = 0; i != width; ++i)
1584 double tU = (i + 1 == width) ? 1 : i * incrementU;
1585 PatchControl pointX[3];
1586 PatchControl leftX[3];
1587 PatchControl rightX[3];
1588 QuadraticBezier_evaluate(*subMatrix[0][0], *subMatrix[0][1], *subMatrix[0][2], tU, pointX[0], leftX[0], rightX[0]);
1589 QuadraticBezier_evaluate(*subMatrix[1][0], *subMatrix[1][1], *subMatrix[1][2], tU, pointX[1], leftX[1], rightX[1]);
1590 QuadraticBezier_evaluate(*subMatrix[2][0], *subMatrix[2][1], *subMatrix[2][2], tU, pointX[2], leftX[2], rightX[2]);
1592 ArbitraryMeshVertex* p = vertices + i * strideX;
1593 for(std::size_t j = 0; j != height; ++j)
1595 if((j == 0 || j + 1 == height) && (i == 0 || i + 1 == width))
1600 double tV = (j + 1 == height) ? 1 : j * incrementV;
1602 PatchControl pointY[3];
1603 PatchControl leftY[3];
1604 PatchControl rightY[3];
1605 QuadraticBezier_evaluate(*subMatrix[0][0], *subMatrix[1][0], *subMatrix[2][0], tV, pointY[0], leftY[0], rightY[0]);
1606 QuadraticBezier_evaluate(*subMatrix[0][1], *subMatrix[1][1], *subMatrix[2][1], tV, pointY[1], leftY[1], rightY[1]);
1607 QuadraticBezier_evaluate(*subMatrix[0][2], *subMatrix[1][2], *subMatrix[2][2], tV, pointY[2], leftY[2], rightY[2]);
1612 QuadraticBezier_evaluate(pointX[0], pointX[1], pointX[2], tV, point, left, right);
1615 QuadraticBezier_evaluate(pointY[0], pointY[1], pointY[2], tU, point, up, down);
1617 vertex3f_to_vector3(p->vertex) = point.m_vertex;
1618 texcoord2f_to_vector2(p->texcoord) = point.m_texcoord;
1620 ArbitraryMeshVertex a, b, c;
1622 a.vertex = vertex3f_for_vector3(left.m_vertex);
1623 a.texcoord = texcoord2f_for_vector2(left.m_texcoord);
1624 b.vertex = vertex3f_for_vector3(right.m_vertex);
1625 b.texcoord = texcoord2f_for_vector2(right.m_texcoord);
1629 c.vertex = vertex3f_for_vector3(up.m_vertex);
1630 c.texcoord = texcoord2f_for_vector2(up.m_texcoord);
1634 c.vertex = vertex3f_for_vector3(down.m_vertex);
1635 c.texcoord = texcoord2f_for_vector2(down.m_texcoord);
1638 Vector3 normal = vector3_normalised(vector3_cross(right.m_vertex - left.m_vertex, up.m_vertex - down.m_vertex));
1640 Vector3 tangent, bitangent;
1641 ArbitraryMeshTriangle_calcTangents(a, b, c, tangent, bitangent);
1642 vector3_normalise(tangent);
1643 vector3_normalise(bitangent);
1645 if(((nFlagsX & AVERAGE) != 0 && i == 0) || ((nFlagsY & AVERAGE) != 0 && j == 0))
1647 normal3f_to_vector3(p->normal) = vector3_normalised(vector3_added(normal3f_to_vector3(p->normal), normal));
1648 normal3f_to_vector3(p->tangent) = vector3_normalised(vector3_added(normal3f_to_vector3(p->tangent), tangent));
1649 normal3f_to_vector3(p->bitangent) = vector3_normalised(vector3_added(normal3f_to_vector3(p->bitangent), bitangent));
1653 normal3f_to_vector3(p->normal) = normal;
1654 normal3f_to_vector3(p->tangent) = tangent;
1655 normal3f_to_vector3(p->bitangent) = bitangent;
1664 void Patch::TesselateSubMatrix( const BezierCurveTree *BX, const BezierCurveTree *BY,
1665 std::size_t offStartX, std::size_t offStartY,
1666 std::size_t offEndX, std::size_t offEndY,
1667 std::size_t nFlagsX, std::size_t nFlagsY,
1668 Vector3& left, Vector3& mid, Vector3& right,
1669 Vector2& texLeft, Vector2& texMid, Vector2& texRight,
1672 int newFlagsX, newFlagsY;
1675 Vector3 vertex_0_0, vertex_0_1, vertex_1_0, vertex_1_1, vertex_2_0, vertex_2_1;
1677 Vector2 texcoord_0_0, texcoord_0_1, texcoord_1_0, texcoord_1_1, texcoord_2_0, texcoord_2_1;
1682 BezierInterpolate2( texcoord_for_index(m_tess.m_vertices, offStartX + offStartY),
1684 texcoord_for_index(m_tess.m_vertices, BX->index + offStartY),
1686 texcoord_for_index(m_tess.m_vertices, offEndX + offStartY) );
1689 BezierInterpolate2( texcoord_for_index(m_tess.m_vertices, offStartX + offEndY),
1691 texcoord_for_index(m_tess.m_vertices, BX->index + offEndY),
1693 texcoord_for_index(m_tess.m_vertices, offEndX + offEndY) );
1697 BezierInterpolate2(texLeft,
1703 if(!BezierCurveTree_isLeaf(BY))
1705 texcoord_for_index(m_tess.m_vertices, BX->index + BY->index) = texTmp;
1709 if(!BezierCurveTree_isLeaf(BX->left))
1711 texcoord_for_index(m_tess.m_vertices, BX->left->index + offStartY) = texcoord_0_0;
1712 texcoord_for_index(m_tess.m_vertices, BX->left->index + offEndY) = texcoord_2_0;
1714 if(!BezierCurveTree_isLeaf(BY))
1716 texcoord_for_index(m_tess.m_vertices, BX->left->index + BY->index) = texcoord_1_0;
1719 if(!BezierCurveTree_isLeaf(BX->right))
1721 texcoord_for_index(m_tess.m_vertices, BX->right->index + offStartY) = texcoord_0_1;
1722 texcoord_for_index(m_tess.m_vertices, BX->right->index + offEndY) = texcoord_2_1;
1724 if(!BezierCurveTree_isLeaf(BY))
1726 texcoord_for_index(m_tess.m_vertices, BX->right->index + BY->index) = texcoord_1_1;
1733 BezierInterpolate3( vertex_for_index(m_tess.m_vertices, offStartX + offStartY),
1735 vertex_for_index(m_tess.m_vertices, BX->index + offStartY),
1737 vertex_for_index(m_tess.m_vertices, offEndX + offStartY) );
1740 BezierInterpolate3( vertex_for_index(m_tess.m_vertices, offStartX + offEndY),
1742 vertex_for_index(m_tess.m_vertices, BX->index + offEndY),
1744 vertex_for_index(m_tess.m_vertices, offEndX + offEndY) );
1749 BezierInterpolate3( left,
1755 if(!BezierCurveTree_isLeaf(BY))
1757 vertex_for_index(m_tess.m_vertices, BX->index + BY->index) = tmp;
1761 if(!BezierCurveTree_isLeaf(BX->left))
1763 vertex_for_index(m_tess.m_vertices, BX->left->index + offStartY) = vertex_0_0;
1764 vertex_for_index(m_tess.m_vertices, BX->left->index + offEndY) = vertex_2_0;
1766 if(!BezierCurveTree_isLeaf(BY))
1768 vertex_for_index(m_tess.m_vertices, BX->left->index + BY->index) = vertex_1_0;
1771 if(!BezierCurveTree_isLeaf(BX->right))
1773 vertex_for_index(m_tess.m_vertices, BX->right->index + offStartY) = vertex_0_1;
1774 vertex_for_index(m_tess.m_vertices, BX->right->index + offEndY) = vertex_2_1;
1776 if(!BezierCurveTree_isLeaf(BY))
1778 vertex_for_index(m_tess.m_vertices, BX->right->index + BY->index) = vertex_1_1;
1786 ArbitraryMeshVertex a, b, c;
1789 if(!(nFlagsX & DEGEN_0a) || !(nFlagsX & DEGEN_0b))
1791 tangentU = vector3_subtracted(vertex_0_1, vertex_0_0);
1792 a.vertex = vertex3f_for_vector3(vertex_0_0);
1793 a.texcoord = texcoord2f_for_vector2(texcoord_0_0);
1794 c.vertex = vertex3f_for_vector3(vertex_0_1);
1795 c.texcoord = texcoord2f_for_vector2(texcoord_0_1);
1797 else if(!(nFlagsX & DEGEN_1a) || !(nFlagsX & DEGEN_1b))
1799 tangentU = vector3_subtracted(vertex_1_1, vertex_1_0);
1800 a.vertex = vertex3f_for_vector3(vertex_1_0);
1801 a.texcoord = texcoord2f_for_vector2(texcoord_1_0);
1802 c.vertex = vertex3f_for_vector3(vertex_1_1);
1803 c.texcoord = texcoord2f_for_vector2(texcoord_1_1);
1807 tangentU = vector3_subtracted(vertex_2_1, vertex_2_0);
1808 a.vertex = vertex3f_for_vector3(vertex_2_0);
1809 a.texcoord = texcoord2f_for_vector2(texcoord_2_0);
1810 c.vertex = vertex3f_for_vector3(vertex_2_1);
1811 c.texcoord = texcoord2f_for_vector2(texcoord_2_1);
1816 if((nFlagsY & DEGEN_0a) && (nFlagsY & DEGEN_1a) && (nFlagsY & DEGEN_2a))
1818 tangentV = vector3_subtracted(vertex_for_index(m_tess.m_vertices, BX->index + offEndY), tmp);
1819 b.vertex = vertex3f_for_vector3(tmp);//m_tess.m_vertices[BX->index + offEndY].vertex;
1820 b.texcoord = texcoord2f_for_vector2(texTmp);//m_tess.m_vertices[BX->index + offEndY].texcoord;
1824 tangentV = vector3_subtracted(tmp, vertex_for_index(m_tess.m_vertices, BX->index + offStartY));
1825 b.vertex = vertex3f_for_vector3(tmp);//m_tess.m_vertices[BX->index + offStartY].vertex;
1826 b.texcoord = texcoord2f_for_vector2(texTmp); //m_tess.m_vertices[BX->index + offStartY].texcoord;
1830 Vector3 normal, s, t;
1831 ArbitraryMeshVertex& v = m_tess.m_vertices[offStartY + BX->index];
1832 Vector3& p = normal3f_to_vector3(v.normal);
1833 Vector3& ps = normal3f_to_vector3(v.tangent);
1834 Vector3& pt = normal3f_to_vector3(v.bitangent);
1838 normal = vector3_cross(tangentV, tangentU);
1842 normal = vector3_cross(tangentU, tangentV);
1844 normalise_safe(normal);
1846 ArbitraryMeshTriangle_calcTangents(a, b, c, s, t);
1850 if(nFlagsX & AVERAGE)
1852 p = vector3_normalised(vector3_added(p, normal));
1853 ps = vector3_normalised(vector3_added(ps, s));
1854 pt = vector3_normalised(vector3_added(pt, t));
1865 ArbitraryMeshVertex a, b, c;
1868 if(!(nFlagsX & DEGEN_2a) || !(nFlagsX & DEGEN_2b))
1870 tangentU = vector3_subtracted(vertex_2_1, vertex_2_0);
1871 a.vertex = vertex3f_for_vector3(vertex_2_0);
1872 a.texcoord = texcoord2f_for_vector2(texcoord_2_0);
1873 c.vertex = vertex3f_for_vector3(vertex_2_1);
1874 c.texcoord = texcoord2f_for_vector2(texcoord_2_1);
1876 else if(!(nFlagsX & DEGEN_1a) || !(nFlagsX & DEGEN_1b))
1878 tangentU = vector3_subtracted(vertex_1_1, vertex_1_0);
1879 a.vertex = vertex3f_for_vector3(vertex_1_0);
1880 a.texcoord = texcoord2f_for_vector2(texcoord_1_0);
1881 c.vertex = vertex3f_for_vector3(vertex_1_1);
1882 c.texcoord = texcoord2f_for_vector2(texcoord_1_1);
1886 tangentU = vector3_subtracted(vertex_0_1, vertex_0_0);
1887 a.vertex = vertex3f_for_vector3(vertex_0_0);
1888 a.texcoord = texcoord2f_for_vector2(texcoord_0_0);
1889 c.vertex = vertex3f_for_vector3(vertex_0_1);
1890 c.texcoord = texcoord2f_for_vector2(texcoord_0_1);
1895 if((nFlagsY & DEGEN_0b) && (nFlagsY & DEGEN_1b) && (nFlagsY & DEGEN_2b))
1897 tangentV = vector3_subtracted(tmp, vertex_for_index(m_tess.m_vertices, BX->index + offStartY));
1898 b.vertex = vertex3f_for_vector3(tmp);//m_tess.m_vertices[BX->index + offStartY].vertex;
1899 b.texcoord = texcoord2f_for_vector2(texTmp);//m_tess.m_vertices[BX->index + offStartY].texcoord;
1903 tangentV = vector3_subtracted(vertex_for_index(m_tess.m_vertices, BX->index + offEndY), tmp);
1904 b.vertex = vertex3f_for_vector3(tmp);//m_tess.m_vertices[BX->index + offEndY].vertex;
1905 b.texcoord = texcoord2f_for_vector2(texTmp);//m_tess.m_vertices[BX->index + offEndY].texcoord;
1908 ArbitraryMeshVertex& v = m_tess.m_vertices[offEndY+BX->index];
1909 Vector3& p = normal3f_to_vector3(v.normal);
1910 Vector3& ps = normal3f_to_vector3(v.tangent);
1911 Vector3& pt = normal3f_to_vector3(v.bitangent);
1915 p = vector3_cross(tangentV, tangentU);
1919 p = vector3_cross(tangentU, tangentV);
1923 ArbitraryMeshTriangle_calcTangents(a, b, c, ps, pt);
1930 newFlagsX = newFlagsY = 0;
1932 if((nFlagsX & DEGEN_0a) && (nFlagsX & DEGEN_0b))
1934 newFlagsX |= DEGEN_0a;
1935 newFlagsX |= DEGEN_0b;
1937 if((nFlagsX & DEGEN_1a) && (nFlagsX & DEGEN_1b))
1939 newFlagsX |= DEGEN_1a;
1940 newFlagsX |= DEGEN_1b;
1942 if((nFlagsX & DEGEN_2a) && (nFlagsX & DEGEN_2b))
1944 newFlagsX |= DEGEN_2a;
1945 newFlagsX |= DEGEN_2b;
1947 if((nFlagsY & DEGEN_0a) && (nFlagsY & DEGEN_1a) && (nFlagsY & DEGEN_2a))
1949 newFlagsY |= DEGEN_0a;
1950 newFlagsY |= DEGEN_1a;
1951 newFlagsY |= DEGEN_2a;
1953 if((nFlagsY & DEGEN_0b) && (nFlagsY & DEGEN_1b) && (nFlagsY & DEGEN_2b))
1955 newFlagsY |= DEGEN_0b;
1956 newFlagsY |= DEGEN_1b;
1957 newFlagsY |= DEGEN_2b;
1961 //if((nFlagsX & DEGEN_0a) && (nFlagsX & DEGEN_1a) && (nFlagsX & DEGEN_2a)) { newFlagsX |= DEGEN_0a; newFlagsX |= DEGEN_1a; newFlagsX |= DEGEN_2a; }
1962 //if((nFlagsX & DEGEN_0b) && (nFlagsX & DEGEN_1b) && (nFlagsX & DEGEN_2b)) { newFlagsX |= DEGEN_0b; newFlagsX |= DEGEN_1b; newFlagsX |= DEGEN_2b; }
1964 newFlagsX |= (nFlagsX & SPLIT);
1965 newFlagsX |= (nFlagsX & AVERAGE);
1967 if(!BezierCurveTree_isLeaf(BY))
1970 int nTemp = newFlagsY;
1972 if((nFlagsY & DEGEN_0a) && (nFlagsY & DEGEN_0b))
1974 newFlagsY |= DEGEN_0a;
1975 newFlagsY |= DEGEN_0b;
1977 newFlagsY |= (nFlagsY & SPLIT);
1978 newFlagsY |= (nFlagsY & AVERAGE);
1980 Vector3& p = vertex_for_index(m_tess.m_vertices, BX->index+BY->index);
1983 Vector2& p2 = texcoord_for_index(m_tess.m_vertices, BX->index+BY->index);
1986 TesselateSubMatrix( BY, BX->left,
1987 offStartY, offStartX,
1989 newFlagsY, newFlagsX,
1990 vertex_0_0, vertex_1_0, vertex_2_0,
1991 texcoord_0_0, texcoord_1_0, texcoord_2_0,
1999 if((nFlagsY & DEGEN_2a) && (nFlagsY & DEGEN_2b)) { newFlagsY |= DEGEN_2a; newFlagsY |= DEGEN_2b; }
2001 TesselateSubMatrix( BY, BX->right,
2002 offStartY, BX->index,
2004 newFlagsY, newFlagsX,
2005 vertex_0_1, vertex_1_1, vertex_2_1,
2006 texcoord_0_1, texcoord_1_1, texcoord_2_1,
2011 if(!BezierCurveTree_isLeaf(BX->left))
2013 TesselateSubMatrix( BX->left, BY,
2014 offStartX, offStartY,
2016 newFlagsX, newFlagsY,
2017 left, vertex_1_0, tmp,
2018 texLeft, texcoord_1_0, texTmp,
2022 if(!BezierCurveTree_isLeaf(BX->right))
2024 TesselateSubMatrix( BX->right, BY,
2025 BX->index, offStartY,
2027 newFlagsX, newFlagsY,
2028 tmp, vertex_1_1, right,
2029 texTmp, texcoord_1_1, texRight,
2036 void Patch::BuildTesselationCurves(EMatrixMajor major)
2038 std::size_t nArrayStride, length, cross, strideU, strideV;
2043 length = (m_width - 1) >> 1;
2050 BezierCurveTreeArray_deleteAll(m_tess.m_curveTreeU);
2055 nArrayStride = m_tess.m_nArrayWidth;
2056 length = (m_height - 1) >> 1;
2063 BezierCurveTreeArray_deleteAll(m_tess.m_curveTreeV);
2068 ERROR_MESSAGE("neither row-major nor column-major");
2072 Array<std::size_t> arrayLength(length);
2073 Array<BezierCurveTree*> pCurveTree(length);
2075 std::size_t nArrayLength = 1;
2079 for(Array<std::size_t>::iterator i = arrayLength.begin(); i != arrayLength.end(); ++i)
2081 *i = Array<std::size_t>::value_type((major == ROW) ? m_subdivisions_x : m_subdivisions_y);
2087 // create a list of the horizontal control curves in each column of sub-patches
2088 // adaptively tesselate each horizontal control curve in the list
2089 // create a binary tree representing the combined tesselation of the list
2090 for(std::size_t i = 0; i != length; ++i)
2092 PatchControl* p1 = m_ctrlTransformed.data() + (i * 2 * strideU);
2093 GSList* pCurveList = 0;
2094 for(std::size_t j = 0; j < cross; j += 2)
2096 PatchControl* p2 = p1+strideV;
2097 PatchControl* p3 = p2+strideV;
2099 // directly taken from one row of control points
2101 BezierCurve* pCurve = new BezierCurve;
2102 pCurve->crd = (p1+strideU)->m_vertex;
2103 pCurve->left = p1->m_vertex;
2104 pCurve->right = (p1+(strideU<<1))->m_vertex;
2105 pCurveList = g_slist_prepend(pCurveList, pCurve);
2113 // interpolated from three columns of control points
2115 BezierCurve* pCurve = new BezierCurve;
2116 pCurve->crd = vector3_mid((p1+strideU)->m_vertex, (p3+strideU)->m_vertex);
2117 pCurve->left = vector3_mid(p1->m_vertex, p3->m_vertex);
2118 pCurve->right = vector3_mid((p1+(strideU<<1))->m_vertex, (p3+(strideU<<1))->m_vertex);
2120 pCurve->crd = vector3_mid(pCurve->crd, (p2+strideU)->m_vertex);
2121 pCurve->left = vector3_mid(pCurve->left, p2->m_vertex);
2122 pCurve->right = vector3_mid(pCurve->right, (p2+(strideU<<1))->m_vertex);
2123 pCurveList = g_slist_prepend(pCurveList, pCurve);
2129 pCurveTree[i] = new BezierCurveTree;
2130 BezierCurveTree_FromCurveList(pCurveTree[i], pCurveList);
2131 for(GSList* l = pCurveList; l != 0; l = g_slist_next(l))
2133 delete static_cast<BezierCurve*>((*l).data);
2135 g_slist_free(pCurveList);
2137 // set up array indices for binary tree
2138 // accumulate subarray width
2139 arrayLength[i] = Array<std::size_t>::value_type(BezierCurveTree_Setup(pCurveTree[i], nArrayLength, nArrayStride) - (nArrayLength - 1));
2140 // accumulate total array width
2141 nArrayLength += arrayLength[i];
2148 m_tess.m_nArrayWidth = nArrayLength;
2149 std::swap(m_tess.m_arrayWidth, arrayLength);
2153 std::swap(m_tess.m_curveTreeU, pCurveTree);
2157 m_tess.m_nArrayHeight = nArrayLength;
2158 std::swap(m_tess.m_arrayHeight, arrayLength);
2162 std::swap(m_tess.m_curveTreeV, pCurveTree);
2168 inline void vertex_assign_ctrl(ArbitraryMeshVertex& vertex, const PatchControl& ctrl)
2170 vertex.vertex.x = ctrl.m_vertex[0];
2171 vertex.vertex.y = ctrl.m_vertex[1];
2172 vertex.vertex.z = ctrl.m_vertex[2];
2173 vertex.texcoord.s = ctrl.m_texcoord[0];
2174 vertex.texcoord.t = ctrl.m_texcoord[1];
2177 inline void vertex_clear_normal(ArbitraryMeshVertex& vertex)
2179 vertex.normal.x = 0;
2180 vertex.normal.y = 0;
2181 vertex.normal.z = 0;
2182 vertex.tangent.x = 0;
2183 vertex.tangent.y = 0;
2184 vertex.tangent.z = 0;
2185 vertex.bitangent.x = 0;
2186 vertex.bitangent.y = 0;
2187 vertex.bitangent.z = 0;
2190 inline void tangents_remove_degenerate(Vector3 tangents[6], Vector2 textureTangents[6], unsigned int flags)
2192 if(flags & DEGEN_0a)
2194 const std::size_t i =
2196 ? (flags & DEGEN_1a)
2197 ? (flags & DEGEN_1b)
2198 ? (flags & DEGEN_2a)
2204 tangents[0] = tangents[i];
2205 textureTangents[0] = textureTangents[i];
2207 if(flags & DEGEN_0b)
2209 const std::size_t i =
2211 ? (flags & DEGEN_1b)
2212 ? (flags & DEGEN_1a)
2213 ? (flags & DEGEN_2b)
2219 tangents[1] = tangents[i];
2220 textureTangents[1] = textureTangents[i];
2222 if(flags & DEGEN_2a)
2224 const std::size_t i =
2226 ? (flags & DEGEN_1a)
2227 ? (flags & DEGEN_1b)
2228 ? (flags & DEGEN_0a)
2234 tangents[4] = tangents[i];
2235 textureTangents[4] = textureTangents[i];
2237 if(flags & DEGEN_2b)
2239 const std::size_t i =
2241 ? (flags & DEGEN_1b)
2242 ? (flags & DEGEN_1a)
2243 ? (flags & DEGEN_0b)
2249 tangents[5] = tangents[i];
2250 textureTangents[5] = textureTangents[i];
2254 void bestTangents00(unsigned int degenerateFlags, double dot, double length, std::size_t& index0, std::size_t& index1)
2256 if(fabs(dot + length) < 0.001) // opposing direction = degenerate
2258 if(!(degenerateFlags & DEGEN_1a)) // if this tangent is degenerate we cannot use it
2263 else if(!(degenerateFlags & DEGEN_0b))
2274 else if(fabs(dot - length) < 0.001) // same direction = degenerate
2276 if(degenerateFlags & DEGEN_0b)
2289 void bestTangents01(unsigned int degenerateFlags, double dot, double length, std::size_t& index0, std::size_t& index1)
2291 if(fabs(dot - length) < 0.001) // same direction = degenerate
2293 if(!(degenerateFlags & DEGEN_1a)) // if this tangent is degenerate we cannot use it
2298 else if(!(degenerateFlags & DEGEN_2b))
2309 else if(fabs(dot + length) < 0.001) // opposing direction = degenerate
2311 if(degenerateFlags & DEGEN_2b)
2324 void bestTangents10(unsigned int degenerateFlags, double dot, double length, std::size_t& index0, std::size_t& index1)
2326 if(fabs(dot - length) < 0.001) // same direction = degenerate
2328 if(!(degenerateFlags & DEGEN_1b)) // if this tangent is degenerate we cannot use it
2333 else if(!(degenerateFlags & DEGEN_0a))
2344 else if(fabs(dot + length) < 0.001) // opposing direction = degenerate
2346 if(degenerateFlags & DEGEN_0a)
2359 void bestTangents11(unsigned int degenerateFlags, double dot, double length, std::size_t& index0, std::size_t& index1)
2361 if(fabs(dot + length) < 0.001) // opposing direction = degenerate
2363 if(!(degenerateFlags & DEGEN_1b)) // if this tangent is degenerate we cannot use it
2368 else if(!(degenerateFlags & DEGEN_2a))
2379 else if(fabs(dot - length) < 0.001) // same direction = degenerate
2381 if(degenerateFlags & DEGEN_2a)
2394 void Patch::accumulateVertexTangentSpace(std::size_t index, Vector3 tangentX[6], Vector3 tangentY[6], Vector2 tangentS[6], Vector2 tangentT[6], std::size_t index0, std::size_t index1)
2397 Vector3 normal(vector3_cross(tangentX[index0], tangentY[index1]));
2398 if(!vector3_equal(normal, g_vector3_identity))
2400 vector3_add(normal_for_index(m_tess.m_vertices, index), vector3_normalised(normal));
2405 ArbitraryMeshVertex a, b, c;
2406 a.vertex = Vertex3f(0, 0, 0);
2407 a.texcoord = TexCoord2f(0, 0);
2408 b.vertex = vertex3f_for_vector3(tangentX[index0]);
2409 b.texcoord = texcoord2f_for_vector2(tangentS[index0]);
2410 c.vertex = vertex3f_for_vector3(tangentY[index1]);
2411 c.texcoord = texcoord2f_for_vector2(tangentT[index1]);
2414 ArbitraryMeshTriangle_calcTangents(a, b, c, s, t);
2415 if(!vector3_equal(s, g_vector3_identity))
2417 vector3_add(tangent_for_index(m_tess.m_vertices, index), vector3_normalised(s));
2419 if(!vector3_equal(t, g_vector3_identity))
2421 vector3_add(bitangent_for_index(m_tess.m_vertices, index), vector3_normalised(t));
2426 void Patch::BuildVertexArray()
2428 const std::size_t strideU = 1;
2429 const std::size_t strideV = m_width;
2431 const std::size_t numElems = m_tess.m_nArrayWidth*m_tess.m_nArrayHeight; // total number of elements in vertex array
2433 const bool bWidthStrips = (m_tess.m_nArrayWidth >= m_tess.m_nArrayHeight); // decide if horizontal strips are longer than vertical
2436 // allocate vertex, normal, texcoord and primitive-index arrays
2437 m_tess.m_vertices.resize(numElems);
2438 m_tess.m_indices.resize(m_tess.m_nArrayWidth *2 * (m_tess.m_nArrayHeight - 1));
2440 // set up strip indices
2443 m_tess.m_numStrips = m_tess.m_nArrayHeight-1;
2444 m_tess.m_lenStrips = m_tess.m_nArrayWidth*2;
2446 for(std::size_t i=0; i<m_tess.m_nArrayWidth; i++)
2448 for(std::size_t j=0; j<m_tess.m_numStrips; j++)
2450 m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(j*m_tess.m_nArrayWidth+i);
2451 m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex((j+1)*m_tess.m_nArrayWidth+i);
2452 // reverse because radiant uses CULL_FRONT
2453 //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(j*m_tess.m_nArrayWidth+i);
2454 //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex((j+1)*m_tess.m_nArrayWidth+i);
2460 m_tess.m_numStrips = m_tess.m_nArrayWidth-1;
2461 m_tess.m_lenStrips = m_tess.m_nArrayHeight*2;
2463 for(std::size_t i=0; i<m_tess.m_nArrayHeight; i++)
2465 for(std::size_t j=0; j<m_tess.m_numStrips; j++)
2467 m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j);
2468 m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j+1);
2469 // reverse because radiant uses CULL_FRONT
2470 //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j);
2471 //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j+1);
2478 PatchControlIter pCtrl = m_ctrlTransformed.data();
2479 for(std::size_t j = 0, offStartY = 0; j+1 < m_height; j += 2, pCtrl += (strideU + strideV))
2481 // set up array offsets for this sub-patch
2482 const bool leafY = (m_patchDef3) ? false : BezierCurveTree_isLeaf(m_tess.m_curveTreeV[j>>1]);
2483 const std::size_t offMidY = (m_patchDef3) ? 0 : m_tess.m_curveTreeV[j>>1]->index;
2484 const std::size_t widthY = m_tess.m_arrayHeight[j>>1] * m_tess.m_nArrayWidth;
2485 const std::size_t offEndY = offStartY + widthY;
2487 for(std::size_t i = 0, offStartX = 0; i+1 < m_width; i += 2, pCtrl += (strideU << 1))
2489 const bool leafX = (m_patchDef3) ? false : BezierCurveTree_isLeaf(m_tess.m_curveTreeU[i>>1]);
2490 const std::size_t offMidX = (m_patchDef3) ? 0 : m_tess.m_curveTreeU[i>>1]->index;
2491 const std::size_t widthX = m_tess.m_arrayWidth[i>>1];
2492 const std::size_t offEndX = offStartX + widthX;
2494 PatchControl *subMatrix[3][3];
2495 subMatrix[0][0] = pCtrl;
2496 subMatrix[0][1] = subMatrix[0][0]+strideU;
2497 subMatrix[0][2] = subMatrix[0][1]+strideU;
2498 subMatrix[1][0] = subMatrix[0][0]+strideV;
2499 subMatrix[1][1] = subMatrix[1][0]+strideU;
2500 subMatrix[1][2] = subMatrix[1][1]+strideU;
2501 subMatrix[2][0] = subMatrix[1][0]+strideV;
2502 subMatrix[2][1] = subMatrix[2][0]+strideU;
2503 subMatrix[2][2] = subMatrix[2][1]+strideU;
2505 // assign on-patch control points to vertex array
2506 if(i == 0 && j == 0)
2508 vertex_clear_normal(m_tess.m_vertices[offStartX + offStartY]);
2510 vertex_assign_ctrl(m_tess.m_vertices[offStartX + offStartY], *subMatrix[0][0]);
2513 vertex_clear_normal(m_tess.m_vertices[offEndX + offStartY]);
2515 vertex_assign_ctrl(m_tess.m_vertices[offEndX + offStartY], *subMatrix[0][2]);
2518 vertex_clear_normal(m_tess.m_vertices[offStartX + offEndY]);
2520 vertex_assign_ctrl(m_tess.m_vertices[offStartX + offEndY], *subMatrix[2][0]);
2522 vertex_clear_normal(m_tess.m_vertices[offEndX + offEndY]);
2523 vertex_assign_ctrl(m_tess.m_vertices[offEndX + offEndY], *subMatrix[2][2]);
2527 // assign remaining control points to vertex array
2530 vertex_assign_ctrl(m_tess.m_vertices[offMidX + offStartY], *subMatrix[0][1]);
2531 vertex_assign_ctrl(m_tess.m_vertices[offMidX + offEndY], *subMatrix[2][1]);
2535 vertex_assign_ctrl(m_tess.m_vertices[offStartX + offMidY], *subMatrix[1][0]);
2536 vertex_assign_ctrl(m_tess.m_vertices[offEndX + offMidY], *subMatrix[1][2]);
2540 vertex_assign_ctrl(m_tess.m_vertices[offMidX + offMidY], *subMatrix[1][1]);
2545 // test all 12 edges for degeneracy
2546 unsigned int nFlagsX = subarray_get_degen(pCtrl, strideU, strideV);
2547 unsigned int nFlagsY = subarray_get_degen(pCtrl, strideV, strideU);
2548 Vector3 tangentX[6], tangentY[6];
2549 Vector2 tangentS[6], tangentT[6];
2551 // set up tangents for each of the 12 edges if they were not degenerate
2552 if(!(nFlagsX & DEGEN_0a))
2554 tangentX[0] = vector3_subtracted(subMatrix[0][1]->m_vertex, subMatrix[0][0]->m_vertex);
2555 tangentS[0] = vector2_subtracted(subMatrix[0][1]->m_texcoord, subMatrix[0][0]->m_texcoord);
2557 if(!(nFlagsX & DEGEN_0b))
2559 tangentX[1] = vector3_subtracted(subMatrix[0][2]->m_vertex, subMatrix[0][1]->m_vertex);
2560 tangentS[1] = vector2_subtracted(subMatrix[0][2]->m_texcoord, subMatrix[0][1]->m_texcoord);
2562 if(!(nFlagsX & DEGEN_1a))
2564 tangentX[2] = vector3_subtracted(subMatrix[1][1]->m_vertex, subMatrix[1][0]->m_vertex);
2565 tangentS[2] = vector2_subtracted(subMatrix[1][1]->m_texcoord, subMatrix[1][0]->m_texcoord);
2567 if(!(nFlagsX & DEGEN_1b))
2569 tangentX[3] = vector3_subtracted(subMatrix[1][2]->m_vertex, subMatrix[1][1]->m_vertex);
2570 tangentS[3] = vector2_subtracted(subMatrix[1][2]->m_texcoord, subMatrix[1][1]->m_texcoord);
2572 if(!(nFlagsX & DEGEN_2a))
2574 tangentX[4] = vector3_subtracted(subMatrix[2][1]->m_vertex, subMatrix[2][0]->m_vertex);
2575 tangentS[4] = vector2_subtracted(subMatrix[2][1]->m_texcoord, subMatrix[2][0]->m_texcoord);
2577 if(!(nFlagsX & DEGEN_2b))
2579 tangentX[5] = vector3_subtracted(subMatrix[2][2]->m_vertex, subMatrix[2][1]->m_vertex);
2580 tangentS[5] = vector2_subtracted(subMatrix[2][2]->m_texcoord, subMatrix[2][1]->m_texcoord);
2583 if(!(nFlagsY & DEGEN_0a))
2585 tangentY[0] = vector3_subtracted(subMatrix[1][0]->m_vertex, subMatrix[0][0]->m_vertex);
2586 tangentT[0] = vector2_subtracted(subMatrix[1][0]->m_texcoord, subMatrix[0][0]->m_texcoord);
2588 if(!(nFlagsY & DEGEN_0b))
2590 tangentY[1] = vector3_subtracted(subMatrix[2][0]->m_vertex, subMatrix[1][0]->m_vertex);
2591 tangentT[1] = vector2_subtracted(subMatrix[2][0]->m_texcoord, subMatrix[1][0]->m_texcoord);
2593 if(!(nFlagsY & DEGEN_1a))
2595 tangentY[2] = vector3_subtracted(subMatrix[1][1]->m_vertex, subMatrix[0][1]->m_vertex);
2596 tangentT[2] = vector2_subtracted(subMatrix[1][1]->m_texcoord, subMatrix[0][1]->m_texcoord);
2598 if(!(nFlagsY & DEGEN_1b))
2600 tangentY[3] = vector3_subtracted(subMatrix[2][1]->m_vertex, subMatrix[1][1]->m_vertex);
2601 tangentT[3] = vector2_subtracted(subMatrix[2][1]->m_texcoord, subMatrix[1][1]->m_texcoord);
2603 if(!(nFlagsY & DEGEN_2a))
2605 tangentY[4] = vector3_subtracted(subMatrix[1][2]->m_vertex, subMatrix[0][2]->m_vertex);
2606 tangentT[4] = vector2_subtracted(subMatrix[1][2]->m_texcoord, subMatrix[0][2]->m_texcoord);
2608 if(!(nFlagsY & DEGEN_2b))
2610 tangentY[5] = vector3_subtracted(subMatrix[2][2]->m_vertex, subMatrix[1][2]->m_vertex);
2611 tangentT[5] = vector2_subtracted(subMatrix[2][2]->m_texcoord, subMatrix[1][2]->m_texcoord);
2614 // set up remaining edge tangents by borrowing the tangent from the closest parallel non-degenerate edge
2615 tangents_remove_degenerate(tangentX, tangentS, nFlagsX);
2616 tangents_remove_degenerate(tangentY, tangentT, nFlagsY);
2620 std::size_t index = offStartX + offStartY;
2621 std::size_t index0 = 0;
2622 std::size_t index1 = 0;
2624 double dot = vector3_dot(tangentX[index0], tangentY[index1]);
2625 double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
2627 bestTangents00(nFlagsX, dot, length, index0, index1);
2629 accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
2634 std::size_t index = offEndX + offStartY;
2635 std::size_t index0 = 1;
2636 std::size_t index1 = 4;
2638 double dot = vector3_dot(tangentX[index0],tangentY[index1]);
2639 double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
2641 bestTangents10(nFlagsX, dot, length, index0, index1);
2643 accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
2648 std::size_t index = offStartX + offEndY;
2649 std::size_t index0 = 4;
2650 std::size_t index1 = 1;
2652 double dot = vector3_dot(tangentX[index0], tangentY[index1]);
2653 double length = vector3_length(tangentX[index1]) * vector3_length(tangentY[index1]);
2655 bestTangents01(nFlagsX, dot, length, index0, index1);
2657 accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
2662 std::size_t index = offEndX + offEndY;
2663 std::size_t index0 = 5;
2664 std::size_t index1 = 5;
2666 double dot = vector3_dot(tangentX[index0],tangentY[index1]);
2667 double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
2669 bestTangents11(nFlagsX, dot, length, index0, index1);
2671 accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
2674 //normalise normals that won't be accumulated again
2677 normalise_safe(normal_for_index(m_tess.m_vertices, offStartX + offStartY));
2678 normalise_safe(tangent_for_index(m_tess.m_vertices, offStartX + offStartY));
2679 normalise_safe(bitangent_for_index(m_tess.m_vertices, offStartX + offStartY));
2683 normalise_safe(normal_for_index(m_tess.m_vertices, offEndX + offStartY));
2684 normalise_safe(tangent_for_index(m_tess.m_vertices, offEndX + offStartY));
2685 normalise_safe(bitangent_for_index(m_tess.m_vertices, offEndX + offStartY));
2689 normalise_safe(normal_for_index(m_tess.m_vertices, offStartX + offEndY));
2690 normalise_safe(tangent_for_index(m_tess.m_vertices, offStartX + offEndY));
2691 normalise_safe(bitangent_for_index(m_tess.m_vertices, offStartX + offEndY));
2693 if(i+3 == m_width && j+3 == m_height)
2695 normalise_safe(normal_for_index(m_tess.m_vertices, offEndX + offEndY));
2696 normalise_safe(tangent_for_index(m_tess.m_vertices, offEndX + offEndY));
2697 normalise_safe(bitangent_for_index(m_tess.m_vertices, offEndX + offEndY));
2700 // set flags to average normals between shared edges
2709 // set flags to save evaluating shared edges twice
2713 // if the patch is curved.. tesselate recursively
2714 // use the relevant control curves for this sub-patch
2717 TesselateSubMatrixFixed(m_tess.m_vertices.data() + offStartX + offStartY, 1, m_tess.m_nArrayWidth, nFlagsX, nFlagsY, subMatrix);
2723 TesselateSubMatrix( m_tess.m_curveTreeU[i>>1], m_tess.m_curveTreeV[j>>1],
2724 offStartX, offStartY, offEndX, offEndY, // array offsets
2726 subMatrix[1][0]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[1][2]->m_vertex,
2727 subMatrix[1][0]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[1][2]->m_texcoord,
2732 TesselateSubMatrix( m_tess.m_curveTreeV[j>>1], m_tess.m_curveTreeU[i>>1],
2733 offStartY, offStartX, offEndY, offEndX, // array offsets
2735 subMatrix[0][1]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[2][1]->m_vertex,
2736 subMatrix[0][1]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[2][1]->m_texcoord,
2741 offStartX = offEndX;
2743 offStartY = offEndY;
2750 class PatchFilterWrapper : public Filter
2754 PatchFilter& m_filter;
2756 PatchFilterWrapper(PatchFilter& filter, bool invert) : m_invert(invert), m_filter(filter)
2759 void setActive(bool active)
2767 bool filter(const Patch& patch)
2769 return m_invert ^ m_filter.filter(patch);
2774 typedef std::list<PatchFilterWrapper> PatchFilters;
2775 PatchFilters g_patchFilters;
2777 void add_patch_filter(PatchFilter& filter, int mask, bool invert)
2779 g_patchFilters.push_back(PatchFilterWrapper(filter, invert));
2780 GlobalFilterSystem().addFilter(g_patchFilters.back(), mask);
2783 bool patch_filtered(Patch& patch)
2785 for(PatchFilters::iterator i = g_patchFilters.begin(); i != g_patchFilters.end(); ++i)
2787 if((*i).active() && (*i).filter(patch))