-void Patch::BuildVertexArray()
-{
- const std::size_t strideU = 1;
- const std::size_t strideV = m_width;
-
- const std::size_t numElems = m_tess.m_nArrayWidth*m_tess.m_nArrayHeight; // total number of elements in vertex array
-
- const bool bWidthStrips = (m_tess.m_nArrayWidth >= m_tess.m_nArrayHeight); // decide if horizontal strips are longer than vertical
-
-
- // allocate vertex, normal, texcoord and primitive-index arrays
- m_tess.m_vertices.resize(numElems);
- m_tess.m_indices.resize(m_tess.m_nArrayWidth *2 * (m_tess.m_nArrayHeight - 1));
-
- // set up strip indices
- if(bWidthStrips)
- {
- m_tess.m_numStrips = m_tess.m_nArrayHeight-1;
- m_tess.m_lenStrips = m_tess.m_nArrayWidth*2;
-
- for(std::size_t i=0; i<m_tess.m_nArrayWidth; i++)
- {
- for(std::size_t j=0; j<m_tess.m_numStrips; j++)
- {
- m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(j*m_tess.m_nArrayWidth+i);
- m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex((j+1)*m_tess.m_nArrayWidth+i);
- // reverse because radiant uses CULL_FRONT
- //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(j*m_tess.m_nArrayWidth+i);
- //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex((j+1)*m_tess.m_nArrayWidth+i);
- }
- }
- }
- else
- {
- m_tess.m_numStrips = m_tess.m_nArrayWidth-1;
- m_tess.m_lenStrips = m_tess.m_nArrayHeight*2;
-
- for(std::size_t i=0; i<m_tess.m_nArrayHeight; i++)
- {
- for(std::size_t j=0; j<m_tess.m_numStrips; j++)
- {
- m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j);
- 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);
- // reverse because radiant uses CULL_FRONT
- //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j);
- //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j+1);
-
- }
- }
- }
-
- {
- PatchControlIter pCtrl = m_ctrlTransformed.data();
- for(std::size_t j = 0, offStartY = 0; j+1 < m_height; j += 2, pCtrl += (strideU + strideV))
- {
- // set up array offsets for this sub-patch
- const bool leafY = (m_patchDef3) ? false : BezierCurveTree_isLeaf(m_tess.m_curveTreeV[j>>1]);
- const std::size_t offMidY = (m_patchDef3) ? 0 : m_tess.m_curveTreeV[j>>1]->index;
- const std::size_t widthY = m_tess.m_arrayHeight[j>>1] * m_tess.m_nArrayWidth;
- const std::size_t offEndY = offStartY + widthY;
-
- for(std::size_t i = 0, offStartX = 0; i+1 < m_width; i += 2, pCtrl += (strideU << 1))
- {
- const bool leafX = (m_patchDef3) ? false : BezierCurveTree_isLeaf(m_tess.m_curveTreeU[i>>1]);
- const std::size_t offMidX = (m_patchDef3) ? 0 : m_tess.m_curveTreeU[i>>1]->index;
- const std::size_t widthX = m_tess.m_arrayWidth[i>>1];
- const std::size_t offEndX = offStartX + widthX;
-
- PatchControl *subMatrix[3][3];
- subMatrix[0][0] = pCtrl;
- subMatrix[0][1] = subMatrix[0][0]+strideU;
- subMatrix[0][2] = subMatrix[0][1]+strideU;
- subMatrix[1][0] = subMatrix[0][0]+strideV;
- subMatrix[1][1] = subMatrix[1][0]+strideU;
- subMatrix[1][2] = subMatrix[1][1]+strideU;
- subMatrix[2][0] = subMatrix[1][0]+strideV;
- subMatrix[2][1] = subMatrix[2][0]+strideU;
- subMatrix[2][2] = subMatrix[2][1]+strideU;
-
- // assign on-patch control points to vertex array
- if(i == 0 && j == 0)
- {
- vertex_clear_normal(m_tess.m_vertices[offStartX + offStartY]);
- }
- vertex_assign_ctrl(m_tess.m_vertices[offStartX + offStartY], *subMatrix[0][0]);
- if(j == 0)
- {
- vertex_clear_normal(m_tess.m_vertices[offEndX + offStartY]);
- }
- vertex_assign_ctrl(m_tess.m_vertices[offEndX + offStartY], *subMatrix[0][2]);
- if(i == 0)
- {
- vertex_clear_normal(m_tess.m_vertices[offStartX + offEndY]);
- }
- vertex_assign_ctrl(m_tess.m_vertices[offStartX + offEndY], *subMatrix[2][0]);
-
- vertex_clear_normal(m_tess.m_vertices[offEndX + offEndY]);
- vertex_assign_ctrl(m_tess.m_vertices[offEndX + offEndY], *subMatrix[2][2]);
-
- if(!m_patchDef3)
- {
- // assign remaining control points to vertex array
- if(!leafX)
- {
- vertex_assign_ctrl(m_tess.m_vertices[offMidX + offStartY], *subMatrix[0][1]);
- vertex_assign_ctrl(m_tess.m_vertices[offMidX + offEndY], *subMatrix[2][1]);
- }
- if(!leafY)
- {
- vertex_assign_ctrl(m_tess.m_vertices[offStartX + offMidY], *subMatrix[1][0]);
- vertex_assign_ctrl(m_tess.m_vertices[offEndX + offMidY], *subMatrix[1][2]);
-
- if(!leafX)
- {
- vertex_assign_ctrl(m_tess.m_vertices[offMidX + offMidY], *subMatrix[1][1]);
- }
- }
- }
-
- // test all 12 edges for degeneracy
- unsigned int nFlagsX = subarray_get_degen(pCtrl, strideU, strideV);
- unsigned int nFlagsY = subarray_get_degen(pCtrl, strideV, strideU);
- Vector3 tangentX[6], tangentY[6];
- Vector2 tangentS[6], tangentT[6];
-
- // set up tangents for each of the 12 edges if they were not degenerate
- if(!(nFlagsX & DEGEN_0a))
- {
- tangentX[0] = vector3_subtracted(subMatrix[0][1]->m_vertex, subMatrix[0][0]->m_vertex);
- tangentS[0] = vector2_subtracted(subMatrix[0][1]->m_texcoord, subMatrix[0][0]->m_texcoord);
- }
- if(!(nFlagsX & DEGEN_0b))
- {
- tangentX[1] = vector3_subtracted(subMatrix[0][2]->m_vertex, subMatrix[0][1]->m_vertex);
- tangentS[1] = vector2_subtracted(subMatrix[0][2]->m_texcoord, subMatrix[0][1]->m_texcoord);
- }
- if(!(nFlagsX & DEGEN_1a))
- {
- tangentX[2] = vector3_subtracted(subMatrix[1][1]->m_vertex, subMatrix[1][0]->m_vertex);
- tangentS[2] = vector2_subtracted(subMatrix[1][1]->m_texcoord, subMatrix[1][0]->m_texcoord);
- }
- if(!(nFlagsX & DEGEN_1b))
- {
- tangentX[3] = vector3_subtracted(subMatrix[1][2]->m_vertex, subMatrix[1][1]->m_vertex);
- tangentS[3] = vector2_subtracted(subMatrix[1][2]->m_texcoord, subMatrix[1][1]->m_texcoord);
- }
- if(!(nFlagsX & DEGEN_2a))
- {
- tangentX[4] = vector3_subtracted(subMatrix[2][1]->m_vertex, subMatrix[2][0]->m_vertex);
- tangentS[4] = vector2_subtracted(subMatrix[2][1]->m_texcoord, subMatrix[2][0]->m_texcoord);
- }
- if(!(nFlagsX & DEGEN_2b))
- {
- tangentX[5] = vector3_subtracted(subMatrix[2][2]->m_vertex, subMatrix[2][1]->m_vertex);
- tangentS[5] = vector2_subtracted(subMatrix[2][2]->m_texcoord, subMatrix[2][1]->m_texcoord);
- }
-
- if(!(nFlagsY & DEGEN_0a))
- {
- tangentY[0] = vector3_subtracted(subMatrix[1][0]->m_vertex, subMatrix[0][0]->m_vertex);
- tangentT[0] = vector2_subtracted(subMatrix[1][0]->m_texcoord, subMatrix[0][0]->m_texcoord);
- }
- if(!(nFlagsY & DEGEN_0b))
- {
- tangentY[1] = vector3_subtracted(subMatrix[2][0]->m_vertex, subMatrix[1][0]->m_vertex);
- tangentT[1] = vector2_subtracted(subMatrix[2][0]->m_texcoord, subMatrix[1][0]->m_texcoord);
- }
- if(!(nFlagsY & DEGEN_1a))
- {
- tangentY[2] = vector3_subtracted(subMatrix[1][1]->m_vertex, subMatrix[0][1]->m_vertex);
- tangentT[2] = vector2_subtracted(subMatrix[1][1]->m_texcoord, subMatrix[0][1]->m_texcoord);
- }
- if(!(nFlagsY & DEGEN_1b))
- {
- tangentY[3] = vector3_subtracted(subMatrix[2][1]->m_vertex, subMatrix[1][1]->m_vertex);
- tangentT[3] = vector2_subtracted(subMatrix[2][1]->m_texcoord, subMatrix[1][1]->m_texcoord);
- }
- if(!(nFlagsY & DEGEN_2a))
- {
- tangentY[4] = vector3_subtracted(subMatrix[1][2]->m_vertex, subMatrix[0][2]->m_vertex);
- tangentT[4] = vector2_subtracted(subMatrix[1][2]->m_texcoord, subMatrix[0][2]->m_texcoord);
- }
- if(!(nFlagsY & DEGEN_2b))
- {
- tangentY[5] = vector3_subtracted(subMatrix[2][2]->m_vertex, subMatrix[1][2]->m_vertex);
- tangentT[5] = vector2_subtracted(subMatrix[2][2]->m_texcoord, subMatrix[1][2]->m_texcoord);
- }
-
- // set up remaining edge tangents by borrowing the tangent from the closest parallel non-degenerate edge
- tangents_remove_degenerate(tangentX, tangentS, nFlagsX);
- tangents_remove_degenerate(tangentY, tangentT, nFlagsY);
-
- {
- // x=0, y=0
- std::size_t index = offStartX + offStartY;
- std::size_t index0 = 0;
- std::size_t index1 = 0;
-
- double dot = vector3_dot(tangentX[index0], tangentY[index1]);
- double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
-
- bestTangents00(nFlagsX, dot, length, index0, index1);
-
- accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
- }
-
- {
- // x=1, y=0
- std::size_t index = offEndX + offStartY;
- std::size_t index0 = 1;
- std::size_t index1 = 4;
-
- double dot = vector3_dot(tangentX[index0],tangentY[index1]);
- double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
-
- bestTangents10(nFlagsX, dot, length, index0, index1);
-
- accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
- }
-
- {
- // x=0, y=1
- std::size_t index = offStartX + offEndY;
- std::size_t index0 = 4;
- std::size_t index1 = 1;
-
- double dot = vector3_dot(tangentX[index0], tangentY[index1]);
- double length = vector3_length(tangentX[index1]) * vector3_length(tangentY[index1]);
-
- bestTangents01(nFlagsX, dot, length, index0, index1);
-
- accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
- }
-
- {
- // x=1, y=1
- std::size_t index = offEndX + offEndY;
- std::size_t index0 = 5;
- std::size_t index1 = 5;
-
- double dot = vector3_dot(tangentX[index0],tangentY[index1]);
- double length = vector3_length(tangentX[index0]) * vector3_length(tangentY[index1]);
-
- bestTangents11(nFlagsX, dot, length, index0, index1);
-
- accumulateVertexTangentSpace(index, tangentX, tangentY, tangentS, tangentT, index0, index1);
- }
-
- //normalise normals that won't be accumulated again
- if(i!=0 || j!=0)
- {
- normalise_safe(normal_for_index(m_tess.m_vertices, offStartX + offStartY));
- normalise_safe(tangent_for_index(m_tess.m_vertices, offStartX + offStartY));
- normalise_safe(bitangent_for_index(m_tess.m_vertices, offStartX + offStartY));
- }
- if(i+3 == m_width)
- {
- normalise_safe(normal_for_index(m_tess.m_vertices, offEndX + offStartY));
- normalise_safe(tangent_for_index(m_tess.m_vertices, offEndX + offStartY));
- normalise_safe(bitangent_for_index(m_tess.m_vertices, offEndX + offStartY));
- }
- if(j+3 == m_height)
- {
- normalise_safe(normal_for_index(m_tess.m_vertices, offStartX + offEndY));
- normalise_safe(tangent_for_index(m_tess.m_vertices, offStartX + offEndY));
- normalise_safe(bitangent_for_index(m_tess.m_vertices, offStartX + offEndY));
- }
- if(i+3 == m_width && j+3 == m_height)
- {
- normalise_safe(normal_for_index(m_tess.m_vertices, offEndX + offEndY));
- normalise_safe(tangent_for_index(m_tess.m_vertices, offEndX + offEndY));
- normalise_safe(bitangent_for_index(m_tess.m_vertices, offEndX + offEndY));
- }
-
- // set flags to average normals between shared edges
- if(j != 0)
- {
- nFlagsX |= AVERAGE;
- }
- if(i != 0)
- {
- nFlagsY |= AVERAGE;
- }
- // set flags to save evaluating shared edges twice
- nFlagsX |= SPLIT;
- nFlagsY |= SPLIT;
-
- // if the patch is curved.. tesselate recursively
- // use the relevant control curves for this sub-patch
- if(m_patchDef3)
- {
- TesselateSubMatrixFixed(m_tess.m_vertices.data() + offStartX + offStartY, 1, m_tess.m_nArrayWidth, nFlagsX, nFlagsY, subMatrix);
- }
- else
- {
- if(!leafX)
- {
- TesselateSubMatrix( m_tess.m_curveTreeU[i>>1], m_tess.m_curveTreeV[j>>1],
- offStartX, offStartY, offEndX, offEndY, // array offsets
- nFlagsX, nFlagsY,
- subMatrix[1][0]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[1][2]->m_vertex,
- subMatrix[1][0]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[1][2]->m_texcoord,
- false );
- }
- else if(!leafY)
- {
- TesselateSubMatrix( m_tess.m_curveTreeV[j>>1], m_tess.m_curveTreeU[i>>1],
- offStartY, offStartX, offEndY, offEndX, // array offsets
- nFlagsY, nFlagsX,
- subMatrix[0][1]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[2][1]->m_vertex,
- subMatrix[0][1]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[2][1]->m_texcoord,
- true );
- }
- }
-
- offStartX = offEndX;
- }
- offStartY = offEndY;
- }
- }
+void Patch::BuildVertexArray(){
+ const std::size_t strideU = 1;
+ const std::size_t strideV = m_width;
+
+ const std::size_t numElems = m_tess.m_nArrayWidth * m_tess.m_nArrayHeight; // total number of elements in vertex array
+
+ const bool bWidthStrips = ( m_tess.m_nArrayWidth >= m_tess.m_nArrayHeight ); // decide if horizontal strips are longer than vertical
+
+
+ // allocate vertex, normal, texcoord and primitive-index arrays
+ m_tess.m_vertices.resize( numElems );
+ m_tess.m_indices.resize( m_tess.m_nArrayWidth * 2 * ( m_tess.m_nArrayHeight - 1 ) );
+
+ // set up strip indices
+ if ( bWidthStrips ) {
+ m_tess.m_numStrips = m_tess.m_nArrayHeight - 1;
+ m_tess.m_lenStrips = m_tess.m_nArrayWidth * 2;
+
+ for ( std::size_t i = 0; i < m_tess.m_nArrayWidth; i++ )
+ {
+ for ( std::size_t j = 0; j < m_tess.m_numStrips; j++ )
+ {
+ m_tess.m_indices[( j * m_tess.m_lenStrips ) + i * 2] = RenderIndex( j * m_tess.m_nArrayWidth + i );
+ m_tess.m_indices[( j * m_tess.m_lenStrips ) + i * 2 + 1] = RenderIndex( ( j + 1 ) * m_tess.m_nArrayWidth + i );
+ // reverse because radiant uses CULL_FRONT
+ //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(j*m_tess.m_nArrayWidth+i);
+ //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex((j+1)*m_tess.m_nArrayWidth+i);
+ }
+ }
+ }
+ else
+ {
+ m_tess.m_numStrips = m_tess.m_nArrayWidth - 1;
+ m_tess.m_lenStrips = m_tess.m_nArrayHeight * 2;
+
+ for ( std::size_t i = 0; i < m_tess.m_nArrayHeight; i++ )
+ {
+ for ( std::size_t j = 0; j < m_tess.m_numStrips; j++ )
+ {
+ m_tess.m_indices[( j * m_tess.m_lenStrips ) + i * 2] = RenderIndex( ( ( m_tess.m_nArrayHeight - 1 ) - i ) * m_tess.m_nArrayWidth + j );
+ 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 );
+ // reverse because radiant uses CULL_FRONT
+ //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2+1] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j);
+ //m_tess.m_indices[(j*m_tess.m_lenStrips)+i*2] = RenderIndex(((m_tess.m_nArrayHeight-1)-i)*m_tess.m_nArrayWidth+j+1);
+
+ }
+ }
+ }
+
+ {
+ PatchControlIter pCtrl = m_ctrlTransformed.data();
+ for ( std::size_t j = 0, offStartY = 0; j + 1 < m_height; j += 2, pCtrl += ( strideU + strideV ) )
+ {
+ // set up array offsets for this sub-patch
+ const bool leafY = ( m_patchDef3 ) ? false : BezierCurveTree_isLeaf( m_tess.m_curveTreeV[j >> 1] );
+ const std::size_t offMidY = ( m_patchDef3 ) ? 0 : m_tess.m_curveTreeV[j >> 1]->index;
+ const std::size_t widthY = m_tess.m_arrayHeight[j >> 1] * m_tess.m_nArrayWidth;
+ const std::size_t offEndY = offStartY + widthY;
+
+ for ( std::size_t i = 0, offStartX = 0; i + 1 < m_width; i += 2, pCtrl += ( strideU << 1 ) )
+ {
+ const bool leafX = ( m_patchDef3 ) ? false : BezierCurveTree_isLeaf( m_tess.m_curveTreeU[i >> 1] );
+ const std::size_t offMidX = ( m_patchDef3 ) ? 0 : m_tess.m_curveTreeU[i >> 1]->index;
+ const std::size_t widthX = m_tess.m_arrayWidth[i >> 1];
+ const std::size_t offEndX = offStartX + widthX;
+
+ PatchControl *subMatrix[3][3];
+ subMatrix[0][0] = pCtrl;
+ subMatrix[0][1] = subMatrix[0][0] + strideU;
+ subMatrix[0][2] = subMatrix[0][1] + strideU;
+ subMatrix[1][0] = subMatrix[0][0] + strideV;
+ subMatrix[1][1] = subMatrix[1][0] + strideU;
+ subMatrix[1][2] = subMatrix[1][1] + strideU;
+ subMatrix[2][0] = subMatrix[1][0] + strideV;
+ subMatrix[2][1] = subMatrix[2][0] + strideU;
+ subMatrix[2][2] = subMatrix[2][1] + strideU;
+
+ // assign on-patch control points to vertex array
+ if ( i == 0 && j == 0 ) {
+ vertex_clear_normal( m_tess.m_vertices[offStartX + offStartY] );
+ }
+ vertex_assign_ctrl( m_tess.m_vertices[offStartX + offStartY], *subMatrix[0][0] );
+ if ( j == 0 ) {
+ vertex_clear_normal( m_tess.m_vertices[offEndX + offStartY] );
+ }
+ vertex_assign_ctrl( m_tess.m_vertices[offEndX + offStartY], *subMatrix[0][2] );
+ if ( i == 0 ) {
+ vertex_clear_normal( m_tess.m_vertices[offStartX + offEndY] );
+ }
+ vertex_assign_ctrl( m_tess.m_vertices[offStartX + offEndY], *subMatrix[2][0] );
+
+ vertex_clear_normal( m_tess.m_vertices[offEndX + offEndY] );
+ vertex_assign_ctrl( m_tess.m_vertices[offEndX + offEndY], *subMatrix[2][2] );
+
+ if ( !m_patchDef3 ) {
+ // assign remaining control points to vertex array
+ if ( !leafX ) {
+ vertex_assign_ctrl( m_tess.m_vertices[offMidX + offStartY], *subMatrix[0][1] );
+ vertex_assign_ctrl( m_tess.m_vertices[offMidX + offEndY], *subMatrix[2][1] );
+ }
+ if ( !leafY ) {
+ vertex_assign_ctrl( m_tess.m_vertices[offStartX + offMidY], *subMatrix[1][0] );
+ vertex_assign_ctrl( m_tess.m_vertices[offEndX + offMidY], *subMatrix[1][2] );
+
+ if ( !leafX ) {
+ vertex_assign_ctrl( m_tess.m_vertices[offMidX + offMidY], *subMatrix[1][1] );
+ }
+ }
+ }
+
+ // test all 12 edges for degeneracy
+ unsigned int nFlagsX = subarray_get_degen( pCtrl, strideU, strideV );
+ unsigned int nFlagsY = subarray_get_degen( pCtrl, strideV, strideU );
+ Vector3 tangentX[6], tangentY[6];
+ Vector2 tangentS[6], tangentT[6];
+
+ // set up tangents for each of the 12 edges if they were not degenerate
+ if ( !( nFlagsX & DEGEN_0a ) ) {
+ tangentX[0] = vector3_subtracted( subMatrix[0][1]->m_vertex, subMatrix[0][0]->m_vertex );
+ tangentS[0] = vector2_subtracted( subMatrix[0][1]->m_texcoord, subMatrix[0][0]->m_texcoord );
+ }
+ if ( !( nFlagsX & DEGEN_0b ) ) {
+ tangentX[1] = vector3_subtracted( subMatrix[0][2]->m_vertex, subMatrix[0][1]->m_vertex );
+ tangentS[1] = vector2_subtracted( subMatrix[0][2]->m_texcoord, subMatrix[0][1]->m_texcoord );
+ }
+ if ( !( nFlagsX & DEGEN_1a ) ) {
+ tangentX[2] = vector3_subtracted( subMatrix[1][1]->m_vertex, subMatrix[1][0]->m_vertex );
+ tangentS[2] = vector2_subtracted( subMatrix[1][1]->m_texcoord, subMatrix[1][0]->m_texcoord );
+ }
+ if ( !( nFlagsX & DEGEN_1b ) ) {
+ tangentX[3] = vector3_subtracted( subMatrix[1][2]->m_vertex, subMatrix[1][1]->m_vertex );
+ tangentS[3] = vector2_subtracted( subMatrix[1][2]->m_texcoord, subMatrix[1][1]->m_texcoord );
+ }
+ if ( !( nFlagsX & DEGEN_2a ) ) {
+ tangentX[4] = vector3_subtracted( subMatrix[2][1]->m_vertex, subMatrix[2][0]->m_vertex );
+ tangentS[4] = vector2_subtracted( subMatrix[2][1]->m_texcoord, subMatrix[2][0]->m_texcoord );
+ }
+ if ( !( nFlagsX & DEGEN_2b ) ) {
+ tangentX[5] = vector3_subtracted( subMatrix[2][2]->m_vertex, subMatrix[2][1]->m_vertex );
+ tangentS[5] = vector2_subtracted( subMatrix[2][2]->m_texcoord, subMatrix[2][1]->m_texcoord );
+ }
+
+ if ( !( nFlagsY & DEGEN_0a ) ) {
+ tangentY[0] = vector3_subtracted( subMatrix[1][0]->m_vertex, subMatrix[0][0]->m_vertex );
+ tangentT[0] = vector2_subtracted( subMatrix[1][0]->m_texcoord, subMatrix[0][0]->m_texcoord );
+ }
+ if ( !( nFlagsY & DEGEN_0b ) ) {
+ tangentY[1] = vector3_subtracted( subMatrix[2][0]->m_vertex, subMatrix[1][0]->m_vertex );
+ tangentT[1] = vector2_subtracted( subMatrix[2][0]->m_texcoord, subMatrix[1][0]->m_texcoord );
+ }
+ if ( !( nFlagsY & DEGEN_1a ) ) {
+ tangentY[2] = vector3_subtracted( subMatrix[1][1]->m_vertex, subMatrix[0][1]->m_vertex );
+ tangentT[2] = vector2_subtracted( subMatrix[1][1]->m_texcoord, subMatrix[0][1]->m_texcoord );
+ }
+ if ( !( nFlagsY & DEGEN_1b ) ) {
+ tangentY[3] = vector3_subtracted( subMatrix[2][1]->m_vertex, subMatrix[1][1]->m_vertex );
+ tangentT[3] = vector2_subtracted( subMatrix[2][1]->m_texcoord, subMatrix[1][1]->m_texcoord );
+ }
+ if ( !( nFlagsY & DEGEN_2a ) ) {
+ tangentY[4] = vector3_subtracted( subMatrix[1][2]->m_vertex, subMatrix[0][2]->m_vertex );
+ tangentT[4] = vector2_subtracted( subMatrix[1][2]->m_texcoord, subMatrix[0][2]->m_texcoord );
+ }
+ if ( !( nFlagsY & DEGEN_2b ) ) {
+ tangentY[5] = vector3_subtracted( subMatrix[2][2]->m_vertex, subMatrix[1][2]->m_vertex );
+ tangentT[5] = vector2_subtracted( subMatrix[2][2]->m_texcoord, subMatrix[1][2]->m_texcoord );
+ }
+
+ // set up remaining edge tangents by borrowing the tangent from the closest parallel non-degenerate edge
+ tangents_remove_degenerate( tangentX, tangentS, nFlagsX );
+ tangents_remove_degenerate( tangentY, tangentT, nFlagsY );
+
+ {
+ // x=0, y=0
+ std::size_t index = offStartX + offStartY;
+ std::size_t index0 = 0;
+ std::size_t index1 = 0;
+
+ double dot = vector3_dot( tangentX[index0], tangentY[index1] );
+ double length = vector3_length( tangentX[index0] ) * vector3_length( tangentY[index1] );
+
+ bestTangents00( nFlagsX, dot, length, index0, index1 );
+
+ accumulateVertexTangentSpace( index, tangentX, tangentY, tangentS, tangentT, index0, index1 );
+ }
+
+ {
+ // x=1, y=0
+ std::size_t index = offEndX + offStartY;
+ std::size_t index0 = 1;
+ std::size_t index1 = 4;
+
+ double dot = vector3_dot( tangentX[index0],tangentY[index1] );
+ double length = vector3_length( tangentX[index0] ) * vector3_length( tangentY[index1] );
+
+ bestTangents10( nFlagsX, dot, length, index0, index1 );
+
+ accumulateVertexTangentSpace( index, tangentX, tangentY, tangentS, tangentT, index0, index1 );
+ }
+
+ {
+ // x=0, y=1
+ std::size_t index = offStartX + offEndY;
+ std::size_t index0 = 4;
+ std::size_t index1 = 1;
+
+ double dot = vector3_dot( tangentX[index0], tangentY[index1] );
+ double length = vector3_length( tangentX[index1] ) * vector3_length( tangentY[index1] );
+
+ bestTangents01( nFlagsX, dot, length, index0, index1 );
+
+ accumulateVertexTangentSpace( index, tangentX, tangentY, tangentS, tangentT, index0, index1 );
+ }
+
+ {
+ // x=1, y=1
+ std::size_t index = offEndX + offEndY;
+ std::size_t index0 = 5;
+ std::size_t index1 = 5;
+
+ double dot = vector3_dot( tangentX[index0],tangentY[index1] );
+ double length = vector3_length( tangentX[index0] ) * vector3_length( tangentY[index1] );
+
+ bestTangents11( nFlagsX, dot, length, index0, index1 );
+
+ accumulateVertexTangentSpace( index, tangentX, tangentY, tangentS, tangentT, index0, index1 );
+ }
+
+ //normalise normals that won't be accumulated again
+ if ( i != 0 || j != 0 ) {
+ normalise_safe( normal_for_index( m_tess.m_vertices, offStartX + offStartY ) );
+ normalise_safe( tangent_for_index( m_tess.m_vertices, offStartX + offStartY ) );
+ normalise_safe( bitangent_for_index( m_tess.m_vertices, offStartX + offStartY ) );
+ }
+ if ( i + 3 == m_width ) {
+ normalise_safe( normal_for_index( m_tess.m_vertices, offEndX + offStartY ) );
+ normalise_safe( tangent_for_index( m_tess.m_vertices, offEndX + offStartY ) );
+ normalise_safe( bitangent_for_index( m_tess.m_vertices, offEndX + offStartY ) );
+ }
+ if ( j + 3 == m_height ) {
+ normalise_safe( normal_for_index( m_tess.m_vertices, offStartX + offEndY ) );
+ normalise_safe( tangent_for_index( m_tess.m_vertices, offStartX + offEndY ) );
+ normalise_safe( bitangent_for_index( m_tess.m_vertices, offStartX + offEndY ) );
+ }
+ if ( i + 3 == m_width && j + 3 == m_height ) {
+ normalise_safe( normal_for_index( m_tess.m_vertices, offEndX + offEndY ) );
+ normalise_safe( tangent_for_index( m_tess.m_vertices, offEndX + offEndY ) );
+ normalise_safe( bitangent_for_index( m_tess.m_vertices, offEndX + offEndY ) );
+ }
+
+ // set flags to average normals between shared edges
+ if ( j != 0 ) {
+ nFlagsX |= AVERAGE;
+ }
+ if ( i != 0 ) {
+ nFlagsY |= AVERAGE;
+ }
+ // set flags to save evaluating shared edges twice
+ nFlagsX |= SPLIT;
+ nFlagsY |= SPLIT;
+
+ // if the patch is curved.. tesselate recursively
+ // use the relevant control curves for this sub-patch
+ if ( m_patchDef3 ) {
+ TesselateSubMatrixFixed( m_tess.m_vertices.data() + offStartX + offStartY, 1, m_tess.m_nArrayWidth, nFlagsX, nFlagsY, subMatrix );
+ }
+ else
+ {
+ if ( !leafX ) {
+ TesselateSubMatrix( m_tess.m_curveTreeU[i >> 1], m_tess.m_curveTreeV[j >> 1],
+ offStartX, offStartY, offEndX, offEndY, // array offsets
+ nFlagsX, nFlagsY,
+ subMatrix[1][0]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[1][2]->m_vertex,
+ subMatrix[1][0]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[1][2]->m_texcoord,
+ false );
+ }
+ else if ( !leafY ) {
+ TesselateSubMatrix( m_tess.m_curveTreeV[j >> 1], m_tess.m_curveTreeU[i >> 1],
+ offStartY, offStartX, offEndY, offEndX, // array offsets
+ nFlagsY, nFlagsX,
+ subMatrix[0][1]->m_vertex, subMatrix[1][1]->m_vertex, subMatrix[2][1]->m_vertex,
+ subMatrix[0][1]->m_texcoord, subMatrix[1][1]->m_texcoord, subMatrix[2][1]->m_texcoord,
+ true );
+ }
+ }
+
+ offStartX = offEndX;
+ }
+ offStartY = offEndY;
+ }
+ }
+}
+
+
+Vector3 getAverageNormal(const Vector3& normal1, const Vector3& normal2)
+{
+ // Beware of normals with 0 length
+ if ( vector3_length_squared( normal1 ) == 0 ) return normal2;
+ if ( vector3_length_squared( normal2 ) == 0 ) return normal1;
+
+ // Both normals have length > 0
+ //Vector3 n1 = vector3_normalised( normal1 );
+ //Vector3 n2 = vector3_normalised( normal2 );
+
+ // Get the angle bisector
+ if( vector3_length_squared( normal1 + normal2 ) == 0 ) return normal1;
+
+ Vector3 normal = vector3_normalised (normal1 + normal2);
+
+ // Now calculate the length correction out of the angle
+ // of the two normals
+ /* float factor = cos(n1.angle(n2) * 0.5); */
+ float factor = (float) vector3_dot( normal1, normal2 );
+ if ( factor > 1.0 ) factor = 1;
+ if ( factor < -1.0 ) factor = -1;
+ factor = acos( factor );
+
+ factor = cos( factor * 0.5 );
+
+ // Check for div by zero (if the normals are antiparallel)
+ // and stretch the resulting normal, if necessary
+ if (factor != 0)
+ {
+ normal /= factor;
+ }
+
+ return normal;
+}
+
+void Patch::createThickenedOpposite(const Patch& sourcePatch,
+ const float thickness,
+ const int axis,
+ bool& no12,
+ bool& no34)
+{
+ // Clone the dimensions from the other patch
+ setDims(sourcePatch.getWidth(), sourcePatch.getHeight());
+
+ // Also inherit the tesselation from the source patch
+ //setFixedSubdivisions(sourcePatch.subdivionsFixed(), sourcePatch.getSubdivisions());
+
+ // Copy the shader from the source patch
+ SetShader(sourcePatch.GetShader());
+
+ // if extrudeAxis == 0,0,0 the patch is extruded along its vertex normals
+ Vector3 extrudeAxis(0,0,0);
+
+ switch (axis) {
+ case 0: // X-Axis
+ extrudeAxis = Vector3(1,0,0);
+ break;
+ case 1: // Y-Axis
+ extrudeAxis = Vector3(0,1,0);
+ break;
+ case 2: // Z-Axis
+ extrudeAxis = Vector3(0,0,1);
+ break;
+ default:
+ // Default value already set during initialisation
+ break;
+ }
+
+ //check if certain seams are required + cycling in normals calculation is needed
+ //( endpoints != startpoints ) - not a cylinder or something
+ for (std::size_t col = 0; col < m_width; col++){
+ if( vector3_length_squared( sourcePatch.ctrlAt( 0, col ).m_vertex - sourcePatch.ctrlAt( m_height - 1, col ).m_vertex ) > 0.1f ){
+ //globalOutputStream() << "yes12.\n";
+ no12 = false;
+ break;
+ }
+ }
+ for (std::size_t row = 0; row < m_height; row++){
+ if( vector3_length_squared( sourcePatch.ctrlAt( row, 0 ).m_vertex - sourcePatch.ctrlAt( row, m_width - 1 ).m_vertex ) > 0.1f ){
+ no34 = false;
+ //globalOutputStream() << "yes34.\n";
+ break;
+ }
+ }
+
+ for (std::size_t col = 0; col < m_width; col++)
+ {
+ for (std::size_t row = 0; row < m_height; row++)
+ {
+ // The current control vertex on the other patch
+ const PatchControl& curCtrl = sourcePatch.ctrlAt(row, col);
+
+ Vector3 normal;
+
+ // Are we extruding along vertex normals (i.e. extrudeAxis == 0,0,0)?
+ if (extrudeAxis == Vector3(0,0,0))
+ {
+ // The col tangents (empty if 0,0,0)
+ Vector3 colTangent[2] = { Vector3(0,0,0), Vector3(0,0,0) };
+
+ // Are we at the beginning/end of the row? + not cylinder
+ if ( (col == 0 || col == m_width - 1) && !no34 )
+ {
+ // Get the next col index
+ std::size_t nextCol = (col == m_width - 1) ? (col - 1) : (col + 1);
+
+ const PatchControl& colNeighbour = sourcePatch.ctrlAt(row, nextCol);
+
+ // One available tangent
+ colTangent[0] = colNeighbour.m_vertex - curCtrl.m_vertex;
+ // Reverse it if we're at the end of the column
+ colTangent[0] *= (col == m_width - 1) ? -1 : +1;
+ //normalize
+ if ( vector3_length_squared( colTangent[0] ) != 0 ) vector3_normalise( colTangent[0] );
+ }
+ // We are in between, two tangents can be calculated
+ else
+ {
+ // Take two neighbouring vertices that should form a line segment
+ std::size_t nextCol, prevCol;
+ if( col == 0 ){
+ nextCol = col+1;
+ prevCol = m_width-2;
+ }
+ else if( col == m_width - 1 ){
+ nextCol = 1;
+ prevCol = col-1;
+ }
+ else{
+ nextCol = col+1;
+ prevCol = col-1;
+ }
+ const PatchControl& neighbour1 = sourcePatch.ctrlAt(row, nextCol);
+ const PatchControl& neighbour2 = sourcePatch.ctrlAt(row, prevCol);
+
+
+ // Calculate both available tangents
+ colTangent[0] = neighbour1.m_vertex - curCtrl.m_vertex;
+ colTangent[1] = neighbour2.m_vertex - curCtrl.m_vertex;
+
+ // Reverse the second one
+ colTangent[1] *= -1;
+
+ //normalize b4 stuff
+ if ( vector3_length_squared( colTangent[0] ) != 0 ) vector3_normalise( colTangent[0] );
+ if ( vector3_length_squared( colTangent[1] ) != 0 ) vector3_normalise( colTangent[1] );
+
+ // Cull redundant tangents (parallel)
+ if ( vector3_length_squared( colTangent[1] + colTangent[0] ) == 0 ||
+ vector3_length_squared( colTangent[1] - colTangent[0] ) == 0 ){
+ colTangent[1] = Vector3(0,0,0);
+ }
+ }
+
+ // Calculate the tangent vectors to the next row
+ Vector3 rowTangent[2] = { Vector3(0,0,0), Vector3(0,0,0) };
+
+ // Are we at the beginning or the end?
+ if ( (row == 0 || row == m_height - 1) && !no12 )
+ {
+ // Yes, only calculate one row tangent
+ // Get the next row index
+ std::size_t nextRow = (row == m_height - 1) ? (row - 1) : (row + 1);
+
+ const PatchControl& rowNeighbour = sourcePatch.ctrlAt(nextRow, col);
+
+ // First tangent
+ rowTangent[0] = rowNeighbour.m_vertex - curCtrl.m_vertex;
+ // Reverse it accordingly
+ rowTangent[0] *= (row == m_height - 1) ? -1 : +1;
+ //normalize
+ if ( vector3_length_squared( rowTangent[0] ) != 0 ) vector3_normalise( rowTangent[0] );
+ }
+ else
+ {
+ // Two tangents to calculate
+ std::size_t nextRow, prevRow;
+ if( row == 0 ){
+ nextRow = row+1;
+ prevRow = m_height-2;
+ }
+ else if( row == m_height - 1 ){
+ nextRow = 1;
+ prevRow = row-1;
+ }
+ else{
+ nextRow = row+1;
+ prevRow = row-1;
+ }
+ const PatchControl& rowNeighbour1 = sourcePatch.ctrlAt(nextRow, col);
+ const PatchControl& rowNeighbour2 = sourcePatch.ctrlAt(prevRow, col);
+
+ // First tangent
+ rowTangent[0] = rowNeighbour1.m_vertex - curCtrl.m_vertex;
+ rowTangent[1] = rowNeighbour2.m_vertex - curCtrl.m_vertex;
+
+ // Reverse the second one
+ rowTangent[1] *= -1;
+
+ //normalize b4 stuff
+ if ( vector3_length_squared( rowTangent[0] ) != 0 ) vector3_normalise( rowTangent[0] );
+ if ( vector3_length_squared( rowTangent[1] ) != 0 ) vector3_normalise( rowTangent[1] );
+
+ // Cull redundant tangents (parallel)
+ if ( vector3_length_squared( rowTangent[1] + rowTangent[0] ) == 0 ||
+ vector3_length_squared( rowTangent[1] - rowTangent[0] ) == 0 ){
+ rowTangent[1] = Vector3(0,0,0);
+ }
+ }
+
+
+ //clean parallel pairs...
+ if ( vector3_length_squared( rowTangent[0] + colTangent[0] ) == 0 ||
+ vector3_length_squared( rowTangent[0] - colTangent[0] ) == 0 ){
+ rowTangent[0] = Vector3(0,0,0);
+ }
+ if ( vector3_length_squared( rowTangent[1] + colTangent[1] ) == 0 ||
+ vector3_length_squared( rowTangent[1] - colTangent[1] ) == 0 ){
+ rowTangent[1] = Vector3(0,0,0);
+ }
+ if ( vector3_length_squared( rowTangent[0] + colTangent[1] ) == 0 ||
+ vector3_length_squared( rowTangent[0] - colTangent[1] ) == 0 ){
+ colTangent[1] = Vector3(0,0,0);
+ }
+ if ( vector3_length_squared( rowTangent[1] + colTangent[0] ) == 0 ||
+ vector3_length_squared( rowTangent[1] - colTangent[0] ) == 0 ){
+ rowTangent[1] = Vector3(0,0,0);
+ }
+
+ //clean dummies
+ if ( vector3_length_squared( colTangent[0] ) == 0 ){
+ colTangent[0] = colTangent[1];
+ colTangent[1] = Vector3(0,0,0);
+ }
+ if ( vector3_length_squared( rowTangent[0] ) == 0 ){
+ rowTangent[0] = rowTangent[1];
+ rowTangent[1] = Vector3(0,0,0);
+ }
+ if( vector3_length_squared( rowTangent[0] ) == 0 || vector3_length_squared( colTangent[0] ) == 0 ){
+ normal = extrudeAxis;
+
+ }
+ else{
+ // If two column + two row tangents are available, take the length-corrected average
+ if ( ( fabs( colTangent[1][0] ) + fabs( colTangent[1][1] ) + fabs( colTangent[1][2] ) ) > 0 &&
+ ( fabs( rowTangent[1][0] ) + fabs( rowTangent[1][1] ) + fabs( rowTangent[1][2] ) ) > 0 )
+ {
+ // Two column normals to calculate
+ Vector3 normal1 = vector3_normalised( vector3_cross( rowTangent[0], colTangent[0] ) );
+ Vector3 normal2 = vector3_normalised( vector3_cross( rowTangent[1], colTangent[1] ) );
+
+ normal = getAverageNormal(normal1, normal2);
+ /*globalOutputStream() << "0\n";
+ globalOutputStream() << normal1 << "\n";
+ globalOutputStream() << normal2 << "\n";
+ globalOutputStream() << normal << "\n";*/
+
+ }
+ // If two column tangents are available, take the length-corrected average
+ else if ( ( fabs( colTangent[1][0] ) + fabs( colTangent[1][1] ) + fabs( colTangent[1][2] ) ) > 0)
+ {
+ // Two column normals to calculate
+ Vector3 normal1 = vector3_normalised( vector3_cross( rowTangent[0], colTangent[0] ) );
+ Vector3 normal2 = vector3_normalised( vector3_cross( rowTangent[0], colTangent[1] ) );
+
+ normal = getAverageNormal(normal1, normal2);
+ /*globalOutputStream() << "1\n";
+ globalOutputStream() << normal1 << "\n";
+ globalOutputStream() << normal2 << "\n";
+ globalOutputStream() << normal << "\n";*/
+
+ }
+ else
+ {
+ // One column tangent available, maybe we have a second rowtangent?
+ if ( ( fabs( rowTangent[1][0] ) + fabs( rowTangent[1][1] ) + fabs( rowTangent[1][2] ) ) > 0)
+ {
+ // Two row normals to calculate
+ Vector3 normal1 = vector3_normalised( vector3_cross( rowTangent[0], colTangent[0] ) );
+ Vector3 normal2 = vector3_normalised( vector3_cross( rowTangent[1], colTangent[0] ) );
+
+ normal = getAverageNormal(normal1, normal2);
+ /*globalOutputStream() << "2\n";
+ globalOutputStream() << rowTangent[0] << "\n";
+ globalOutputStream() << colTangent[0] << "\n";
+ globalOutputStream() << vector3_cross( rowTangent[0], colTangent[0]) << "\n";
+ globalOutputStream() << normal1 << "\n";
+ globalOutputStream() << normal2 << "\n";
+ globalOutputStream() << normal << "\n";*/
+
+ }
+ else
+ {
+ if ( vector3_length_squared( vector3_cross( rowTangent[0], colTangent[0] ) ) > 0 ){
+ normal = vector3_normalised( vector3_cross( rowTangent[0], colTangent[0] ) );
+ /*globalOutputStream() << "3\n";
+ globalOutputStream() << (float)vector3_length_squared( vector3_cross( rowTangent[0], colTangent[0] ) ) << "\n";
+ globalOutputStream() << normal << "\n";*/
+ }
+ else{
+ normal = extrudeAxis;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ // Take the predefined extrude direction instead
+ normal = extrudeAxis;
+ }
+
+ // Store the new coordinates into this patch at the current coords
+ ctrlAt(row, col).m_vertex = curCtrl.m_vertex + normal*thickness;
+
+ // Clone the texture cooordinates of the source patch
+ ctrlAt(row, col).m_texcoord = curCtrl.m_texcoord;
+ }
+ }
+
+ // Notify the patch about the change
+ controlPointsChanged();
+}
+
+void Patch::createThickenedWall(const Patch& sourcePatch,
+ const Patch& targetPatch,
+ const int wallIndex)
+{
+ // Copy the shader from the source patch
+ SetShader(sourcePatch.GetShader());
+
+ // The start and end control vertex indices
+ int start = 0;
+ int end = 0;
+ // The increment (incr = 1 for the "long" edge, incr = width for the "short" edge)
+ int incr = 1;
+
+ // These are the target dimensions of this wall
+ // The width is depending on which edge is "seamed".
+ int cols = 0;
+ int rows = 3;
+
+ int sourceWidth = static_cast<int>(sourcePatch.getWidth());
+ int sourceHeight = static_cast<int>(sourcePatch.getHeight());
+/*
+ bool sourceTesselationFixed = sourcePatch.subdivionsFixed();
+ Subdivisions sourceTesselationX(sourcePatch.getSubdivisions().x(), 1);
+ Subdivisions sourceTesselationY(sourcePatch.getSubdivisions().y(), 1);
+*/
+ // Determine which of the four edges have to be connected
+ // and calculate the start, end & stepsize for the following loop
+ switch (wallIndex) {
+ case 0:
+ cols = sourceWidth;
+ start = 0;
+ end = sourceWidth - 1;
+ incr = 1;
+ //setFixedSubdivisions(sourceTesselationFixed, sourceTesselationX);
+ break;
+ case 1:
+ cols = sourceWidth;
+ start = sourceWidth * (sourceHeight-1);
+ end = sourceWidth*sourceHeight - 1;
+ incr = 1;
+ //setFixedSubdivisions(sourceTesselationFixed, sourceTesselationX);
+ break;
+ case 2:
+ cols = sourceHeight;
+ start = 0;
+ end = sourceWidth*(sourceHeight-1);
+ incr = sourceWidth;
+ //setFixedSubdivisions(sourceTesselationFixed, sourceTesselationY);
+ break;
+ case 3:
+ cols = sourceHeight;
+ start = sourceWidth - 1;
+ end = sourceWidth*sourceHeight - 1;
+ incr = sourceWidth;
+ //setFixedSubdivisions(sourceTesselationFixed, sourceTesselationY);
+ break;
+ }
+
+ setDims(cols, rows);
+
+ const PatchControlArray& sourceCtrl = sourcePatch.getControlPoints();
+ const PatchControlArray& targetCtrl = targetPatch.getControlPoints();
+
+ int col = 0;
+ // Now go through the control vertices with these calculated stepsize
+ for (int idx = start; idx <= end; idx += incr, col++) {
+ Vector3 sourceCoord = sourceCtrl[idx].m_vertex;
+ Vector3 targetCoord = targetCtrl[idx].m_vertex;
+ Vector3 middleCoord = (sourceCoord + targetCoord) / 2;
+
+ // Now assign the vertex coordinates
+ ctrlAt(0, col).m_vertex = sourceCoord;
+ ctrlAt(1, col).m_vertex = middleCoord;
+ ctrlAt(2, col).m_vertex = targetCoord;
+ }
+
+ if (wallIndex == 0 || wallIndex == 3) {
+ InvertMatrix();
+ }
+
+ // Notify the patch about the change
+ controlPointsChanged();
+
+ // Texture the patch "naturally"
+ NaturalTexture();