doom3-bfg/neo/idlib/geometry/Surface_Patch.cpp

826 lines
18 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
/*
=================
idSurface_Patch::SetSize
=================
*/
void idSurface_Patch::SetSize( int patchWidth, int patchHeight )
{
if( patchWidth < 1 || patchWidth > maxWidth )
{
idLib::common->FatalError( "idSurface_Patch::SetSize: invalid patchWidth" );
2012-11-26 18:58:24 +00:00
}
if( patchHeight < 1 || patchHeight > maxHeight )
{
idLib::common->FatalError( "idSurface_Patch::SetSize: invalid patchHeight" );
2012-11-26 18:58:24 +00:00
}
width = patchWidth;
height = patchHeight;
verts.SetNum( width * height );
}
/*
=================
idSurface_Patch::PutOnCurve
Expects an expanded patch.
=================
*/
void idSurface_Patch::PutOnCurve()
{
2012-11-26 18:58:24 +00:00
int i, j;
idDrawVert prev, next;
2012-11-26 18:58:24 +00:00
assert( expanded == true );
// put all the approximating points on the curve
for( i = 0; i < width; i++ )
{
for( j = 1; j < height; j += 2 )
{
LerpVert( verts[j * maxWidth + i], verts[( j + 1 )*maxWidth + i], prev );
LerpVert( verts[j * maxWidth + i], verts[( j - 1 )*maxWidth + i], next );
LerpVert( prev, next, verts[j * maxWidth + i] );
2012-11-26 18:58:24 +00:00
}
}
for( j = 0; j < height; j++ )
{
for( i = 1; i < width; i += 2 )
{
LerpVert( verts[j * maxWidth + i], verts[j * maxWidth + i + 1], prev );
LerpVert( verts[j * maxWidth + i], verts[j * maxWidth + i - 1], next );
LerpVert( prev, next, verts[j * maxWidth + i] );
2012-11-26 18:58:24 +00:00
}
}
}
/*
================
idSurface_Patch::ProjectPointOntoVector
================
*/
void idSurface_Patch::ProjectPointOntoVector( const idVec3& point, const idVec3& vStart, const idVec3& vEnd, idVec3& vProj )
{
2012-11-26 18:58:24 +00:00
idVec3 pVec, vec;
2012-11-26 18:58:24 +00:00
pVec = point - vStart;
vec = vEnd - vStart;
vec.Normalize();
// project onto the directional vector for this segment
vProj = vStart + ( pVec * vec ) * vec;
2012-11-26 18:58:24 +00:00
}
/*
================
idSurface_Patch::RemoveLinearColumnsRows
Expects an expanded patch.
================
*/
void idSurface_Patch::RemoveLinearColumnsRows()
{
2012-11-26 18:58:24 +00:00
int i, j, k;
float len, maxLength;
idVec3 proj, dir;
2012-11-26 18:58:24 +00:00
assert( expanded == true );
for( j = 1; j < width - 1; j++ )
{
2012-11-26 18:58:24 +00:00
maxLength = 0;
for( i = 0; i < height; i++ )
{
idSurface_Patch::ProjectPointOntoVector( verts[i * maxWidth + j].xyz,
verts[i * maxWidth + j - 1].xyz, verts[i * maxWidth + j + 1].xyz, proj );
dir = verts[i * maxWidth + j].xyz - proj;
2012-11-26 18:58:24 +00:00
len = dir.LengthSqr();
if( len > maxLength )
{
2012-11-26 18:58:24 +00:00
maxLength = len;
}
}
if( maxLength < Square( 0.2f ) )
{
2012-11-26 18:58:24 +00:00
width--;
for( i = 0; i < height; i++ )
{
for( k = j; k < width; k++ )
{
verts[i * maxWidth + k] = verts[i * maxWidth + k + 1];
2012-11-26 18:58:24 +00:00
}
}
j--;
}
}
for( j = 1; j < height - 1; j++ )
{
2012-11-26 18:58:24 +00:00
maxLength = 0;
for( i = 0; i < width; i++ )
{
idSurface_Patch::ProjectPointOntoVector( verts[j * maxWidth + i].xyz,
verts[( j - 1 )*maxWidth + i].xyz, verts[( j + 1 )*maxWidth + i].xyz, proj );
dir = verts[j * maxWidth + i].xyz - proj;
2012-11-26 18:58:24 +00:00
len = dir.LengthSqr();
if( len > maxLength )
{
2012-11-26 18:58:24 +00:00
maxLength = len;
}
}
if( maxLength < Square( 0.2f ) )
{
2012-11-26 18:58:24 +00:00
height--;
for( i = 0; i < width; i++ )
{
for( k = j; k < height; k++ )
{
verts[k * maxWidth + i] = verts[( k + 1 ) * maxWidth + i];
2012-11-26 18:58:24 +00:00
}
}
j--;
}
}
}
/*
================
idSurface_Patch::ResizeExpanded
================
*/
void idSurface_Patch::ResizeExpanded( int newHeight, int newWidth )
{
2012-11-26 18:58:24 +00:00
int i, j;
2012-11-26 18:58:24 +00:00
assert( expanded == true );
if( newHeight <= maxHeight && newWidth <= maxWidth )
{
2012-11-26 18:58:24 +00:00
return;
}
if( newHeight * newWidth > maxHeight * maxWidth )
{
2012-11-26 18:58:24 +00:00
verts.SetNum( newHeight * newWidth );
}
// space out verts for new height and width
for( j = maxHeight - 1; j >= 0; j-- )
{
for( i = maxWidth - 1; i >= 0; i-- )
{
verts[j * newWidth + i] = verts[j * maxWidth + i];
2012-11-26 18:58:24 +00:00
}
}
maxHeight = newHeight;
maxWidth = newWidth;
}
/*
================
idSurface_Patch::Collapse
================
*/
void idSurface_Patch::Collapse()
{
2012-11-26 18:58:24 +00:00
int i, j;
if( !expanded )
{
idLib::common->FatalError( "idSurface_Patch::Collapse: patch not expanded" );
2012-11-26 18:58:24 +00:00
}
expanded = false;
if( width != maxWidth )
{
for( j = 0; j < height; j++ )
{
for( i = 0; i < width; i++ )
{
verts[j * width + i] = verts[j * maxWidth + i];
2012-11-26 18:58:24 +00:00
}
}
}
verts.SetNum( width * height );
}
/*
================
idSurface_Patch::Expand
================
*/
void idSurface_Patch::Expand()
{
2012-11-26 18:58:24 +00:00
int i, j;
if( expanded )
{
idLib::common->FatalError( "idSurface_Patch::Expand: patch alread expanded" );
2012-11-26 18:58:24 +00:00
}
expanded = true;
verts.SetNum( maxWidth * maxHeight );
if( width != maxWidth )
{
for( j = height - 1; j >= 0; j-- )
{
for( i = width - 1; i >= 0; i-- )
{
verts[j * maxWidth + i] = verts[j * width + i];
2012-11-26 18:58:24 +00:00
}
}
}
}
/*
============
idSurface_Patch::LerpVert
============
*/
void idSurface_Patch::LerpVert( const idDrawVert& a, const idDrawVert& b, idDrawVert& out ) const
{
2012-11-26 18:58:24 +00:00
out.xyz[0] = 0.5f * ( a.xyz[0] + b.xyz[0] );
out.xyz[1] = 0.5f * ( a.xyz[1] + b.xyz[1] );
out.xyz[2] = 0.5f * ( a.xyz[2] + b.xyz[2] );
out.SetNormal( ( a.GetNormal() + b.GetNormal() ) * 0.5f );
out.SetTexCoord( ( a.GetTexCoord() + b.GetTexCoord() ) * 0.5f );
}
/*
=================
idSurface_Patch::GenerateNormals
Handles all the complicated wrapping and degenerate cases
Expects a Not expanded patch.
=================
*/
#define COPLANAR_EPSILON 0.1f
void idSurface_Patch::GenerateNormals()
{
2012-11-26 18:58:24 +00:00
int i, j, k, dist;
idVec3 norm;
idVec3 sum;
int count;
idVec3 base;
idVec3 delta;
int x, y;
idVec3 around[8], temp;
bool good[8];
bool wrapWidth, wrapHeight;
static int neighbors[8][2] =
{
{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, { -1, -1}, { -1, 0}, { -1, 1}
2012-11-26 18:58:24 +00:00
};
2012-11-26 18:58:24 +00:00
assert( expanded == false );
2012-11-26 18:58:24 +00:00
//
// if all points are coplanar, set all normals to that plane
//
idVec3 extent[3];
float offset;
extent[0] = verts[width - 1].xyz - verts[0].xyz;
extent[1] = verts[( height - 1 ) * width + width - 1].xyz - verts[0].xyz;
extent[2] = verts[( height - 1 ) * width].xyz - verts[0].xyz;
2012-11-26 18:58:24 +00:00
norm = extent[0].Cross( extent[1] );
if( norm.LengthSqr() == 0.0f )
{
2012-11-26 18:58:24 +00:00
norm = extent[0].Cross( extent[2] );
if( norm.LengthSqr() == 0.0f )
{
2012-11-26 18:58:24 +00:00
norm = extent[1].Cross( extent[2] );
}
}
2012-11-26 18:58:24 +00:00
// wrapped patched may not get a valid normal here
if( norm.Normalize() != 0.0f )
{
2012-11-26 18:58:24 +00:00
offset = verts[0].xyz * norm;
for( i = 1; i < width * height; i++ )
{
2012-11-26 18:58:24 +00:00
float d = verts[i].xyz * norm;
if( idMath::Fabs( d - offset ) > COPLANAR_EPSILON )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == width * height )
{
2012-11-26 18:58:24 +00:00
// all are coplanar
for( i = 0; i < width * height; i++ )
{
2012-11-26 18:58:24 +00:00
verts[i].SetNormal( norm );
}
return;
}
}
2012-11-26 18:58:24 +00:00
// check for wrapped edge cases, which should smooth across themselves
wrapWidth = false;
for( i = 0; i < height; i++ )
{
delta = verts[i * width].xyz - verts[i * width + width - 1].xyz;
if( delta.LengthSqr() > Square( 1.0f ) )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == height )
{
2012-11-26 18:58:24 +00:00
wrapWidth = true;
}
2012-11-26 18:58:24 +00:00
wrapHeight = false;
for( i = 0; i < width; i++ )
{
delta = verts[i].xyz - verts[( height - 1 ) * width + i].xyz;
if( delta.LengthSqr() > Square( 1.0f ) )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == width )
{
2012-11-26 18:58:24 +00:00
wrapHeight = true;
}
for( i = 0; i < width; i++ )
{
for( j = 0; j < height; j++ )
{
2012-11-26 18:58:24 +00:00
count = 0;
base = verts[j * width + i].xyz;
for( k = 0; k < 8; k++ )
{
2012-11-26 18:58:24 +00:00
around[k] = vec3_origin;
good[k] = false;
for( dist = 1; dist <= 3; dist++ )
{
2012-11-26 18:58:24 +00:00
x = i + neighbors[k][0] * dist;
y = j + neighbors[k][1] * dist;
if( wrapWidth )
{
if( x < 0 )
{
2012-11-26 18:58:24 +00:00
x = width - 1 + x;
}
else if( x >= width )
{
2012-11-26 18:58:24 +00:00
x = 1 + x - width;
}
}
if( wrapHeight )
{
if( y < 0 )
{
2012-11-26 18:58:24 +00:00
y = height - 1 + y;
}
else if( y >= height )
{
2012-11-26 18:58:24 +00:00
y = 1 + y - height;
}
}
if( x < 0 || x >= width || y < 0 || y >= height )
{
2012-11-26 18:58:24 +00:00
break; // edge of patch
}
temp = verts[y * width + x].xyz - base;
if( temp.Normalize() == 0.0f )
{
2012-11-26 18:58:24 +00:00
continue; // degenerate edge, get more dist
}
else
{
2012-11-26 18:58:24 +00:00
good[k] = true;
around[k] = temp;
break; // good edge
}
}
}
2012-11-26 18:58:24 +00:00
sum = vec3_origin;
for( k = 0; k < 8; k++ )
{
if( !good[k] || !good[( k + 1 ) & 7] )
{
2012-11-26 18:58:24 +00:00
continue; // didn't get two points
}
norm = around[( k + 1 ) & 7].Cross( around[k] );
if( norm.Normalize() == 0.0f )
{
2012-11-26 18:58:24 +00:00
continue;
}
sum += norm;
count++;
}
if( count == 0 )
{
2012-11-26 18:58:24 +00:00
//idLib::common->Printf("bad normal\n");
count = 1;
}
sum.Normalize();
verts[j * width + i].SetNormal( sum );
}
}
}
/*
=================
idSurface_Patch::GenerateIndexes
=================
*/
void idSurface_Patch::GenerateIndexes()
{
2012-11-26 18:58:24 +00:00
int i, j, v1, v2, v3, v4, index;
indexes.SetNum( ( width - 1 ) * ( height - 1 ) * 2 * 3 );
2012-11-26 18:58:24 +00:00
index = 0;
for( i = 0; i < width - 1; i++ )
{
for( j = 0; j < height - 1; j++ )
{
2012-11-26 18:58:24 +00:00
v1 = j * width + i;
v2 = v1 + 1;
v3 = v1 + width + 1;
v4 = v1 + width;
indexes[index++] = v1;
indexes[index++] = v3;
indexes[index++] = v2;
indexes[index++] = v1;
indexes[index++] = v4;
indexes[index++] = v3;
}
}
2012-11-26 18:58:24 +00:00
GenerateEdgeIndexes();
}
/*
===============
idSurface_Patch::SampleSinglePatchPoint
===============
*/
void idSurface_Patch::SampleSinglePatchPoint( const idDrawVert ctrl[3][3], float u, float v, idDrawVert* out ) const
{
2012-11-26 18:58:24 +00:00
float vCtrl[3][8];
int vPoint;
int axis;
2012-11-26 18:58:24 +00:00
// find the control points for the v coordinate
for( vPoint = 0; vPoint < 3; vPoint++ )
{
for( axis = 0; axis < 8; axis++ )
{
2012-11-26 18:58:24 +00:00
float a, b, c;
float qA, qB, qC;
if( axis < 3 )
{
2012-11-26 18:58:24 +00:00
a = ctrl[0][vPoint].xyz[axis];
b = ctrl[1][vPoint].xyz[axis];
c = ctrl[2][vPoint].xyz[axis];
}
else if( axis < 6 )
{
a = ctrl[0][vPoint].GetNormal()[axis - 3];
b = ctrl[1][vPoint].GetNormal()[axis - 3];
c = ctrl[2][vPoint].GetNormal()[axis - 3];
}
else
{
a = ctrl[0][vPoint].GetTexCoord()[axis - 6];
b = ctrl[1][vPoint].GetTexCoord()[axis - 6];
c = ctrl[2][vPoint].GetTexCoord()[axis - 6];
2012-11-26 18:58:24 +00:00
}
qA = a - 2.0f * b + c;
qB = 2.0f * b - 2.0f * a;
qC = a;
vCtrl[vPoint][axis] = qA * u * u + qB * u + qC;
}
}
2012-11-26 18:58:24 +00:00
// interpolate the v value
for( axis = 0; axis < 8; axis++ )
{
2012-11-26 18:58:24 +00:00
float a, b, c;
float qA, qB, qC;
2012-11-26 18:58:24 +00:00
a = vCtrl[0][axis];
b = vCtrl[1][axis];
c = vCtrl[2][axis];
qA = a - 2.0f * b + c;
qB = 2.0f * b - 2.0f * a;
qC = a;
if( axis < 3 )
{
2012-11-26 18:58:24 +00:00
out->xyz[axis] = qA * v * v + qB * v + qC;
}
else if( axis < 6 )
{
2012-11-26 18:58:24 +00:00
idVec3 tempNormal = out->GetNormal();
tempNormal[axis - 3] = qA * v * v + qB * v + qC;
2012-11-26 18:58:24 +00:00
out->SetNormal( tempNormal );
//out->normal[axis-3] = qA * v * v + qB * v + qC;
}
else
{
2012-11-26 18:58:24 +00:00
idVec2 tempST = out->GetTexCoord();
tempST[axis - 6] = qA * v * v + qB * v + qC;
2012-11-26 18:58:24 +00:00
out->SetTexCoord( tempST );
}
}
}
/*
===================
idSurface_Patch::SampleSinglePatch
===================
*/
void idSurface_Patch::SampleSinglePatch( const idDrawVert ctrl[3][3], int baseCol, int baseRow, int width, int horzSub, int vertSub, idDrawVert* outVerts ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
float u, v;
2012-11-26 18:58:24 +00:00
horzSub++;
vertSub++;
for( i = 0; i < horzSub; i++ )
{
for( j = 0; j < vertSub; j++ )
{
u = ( float ) i / ( horzSub - 1 );
v = ( float ) j / ( vertSub - 1 );
SampleSinglePatchPoint( ctrl, u, v, &outVerts[( ( baseRow + j ) * width ) + i + baseCol] );
2012-11-26 18:58:24 +00:00
}
}
}
/*
=================
idSurface_Patch::SubdivideExplicit
=================
*/
void idSurface_Patch::SubdivideExplicit( int horzSubdivisions, int vertSubdivisions, bool genNormals, bool removeLinear )
{
2012-11-26 18:58:24 +00:00
int i, j, k, l;
idDrawVert sample[3][3];
int outWidth = ( ( width - 1 ) / 2 * horzSubdivisions ) + 1;
int outHeight = ( ( height - 1 ) / 2 * vertSubdivisions ) + 1;
idDrawVert* dv = new( TAG_IDLIB_SURFACE ) idDrawVert[ outWidth * outHeight ];
2012-11-26 18:58:24 +00:00
// generate normals for the control mesh
if( genNormals )
{
2012-11-26 18:58:24 +00:00
GenerateNormals();
}
2012-11-26 18:58:24 +00:00
int baseCol = 0;
for( i = 0; i + 2 < width; i += 2 )
{
2012-11-26 18:58:24 +00:00
int baseRow = 0;
for( j = 0; j + 2 < height; j += 2 )
{
for( k = 0; k < 3; k++ )
{
for( l = 0; l < 3; l++ )
{
sample[k][l] = verts[( ( j + l ) * width ) + i + k ];
2012-11-26 18:58:24 +00:00
}
}
SampleSinglePatch( sample, baseCol, baseRow, outWidth, horzSubdivisions, vertSubdivisions, dv );
baseRow += vertSubdivisions;
}
baseCol += horzSubdivisions;
}
verts.SetNum( outWidth * outHeight );
for( i = 0; i < outWidth * outHeight; i++ )
{
2012-11-26 18:58:24 +00:00
verts[i] = dv[i];
}
2012-11-26 18:58:24 +00:00
delete[] dv;
2012-11-26 18:58:24 +00:00
width = maxWidth = outWidth;
height = maxHeight = outHeight;
expanded = false;
if( removeLinear )
{
2012-11-26 18:58:24 +00:00
Expand();
RemoveLinearColumnsRows();
Collapse();
}
2012-11-26 18:58:24 +00:00
// normalize all the lerped normals
if( genNormals )
{
2012-11-26 18:58:24 +00:00
idVec3 tempNormal;
for( i = 0; i < width * height; i++ )
{
tempNormal = verts[i].GetNormal();
2012-11-26 18:58:24 +00:00
tempNormal.Normalize();
verts[i].SetNormal( tempNormal );
}
}
2012-11-26 18:58:24 +00:00
GenerateIndexes();
}
/*
=================
idSurface_Patch::Subdivide
=================
*/
void idSurface_Patch::Subdivide( float maxHorizontalError, float maxVerticalError, float maxLength, bool genNormals )
{
2012-11-26 18:58:24 +00:00
int i, j, k, l;
idDrawVert prev, next, mid;
idVec3 prevxyz, nextxyz, midxyz;
idVec3 delta;
float maxHorizontalErrorSqr, maxVerticalErrorSqr, maxLengthSqr;
2012-11-26 18:58:24 +00:00
// generate normals for the control mesh
if( genNormals )
{
2012-11-26 18:58:24 +00:00
GenerateNormals();
}
2012-11-26 18:58:24 +00:00
maxHorizontalErrorSqr = Square( maxHorizontalError );
maxVerticalErrorSqr = Square( maxVerticalError );
maxLengthSqr = Square( maxLength );
2012-11-26 18:58:24 +00:00
Expand();
2012-11-26 18:58:24 +00:00
// horizontal subdivisions
for( j = 0; j + 2 < width; j += 2 )
{
2012-11-26 18:58:24 +00:00
// check subdivided midpoints against control points
for( i = 0; i < height; i++ )
{
for( l = 0; l < 3; l++ )
{
prevxyz[l] = verts[i * maxWidth + j + 1].xyz[l] - verts[i * maxWidth + j ].xyz[l];
nextxyz[l] = verts[i * maxWidth + j + 2].xyz[l] - verts[i * maxWidth + j + 1].xyz[l];
midxyz[l] = ( verts[i * maxWidth + j ].xyz[l] + verts[i * maxWidth + j + 1].xyz[l] * 2.0f +
verts[i * maxWidth + j + 2].xyz[l] ) * 0.25f;
2012-11-26 18:58:24 +00:00
}
if( maxLength > 0.0f )
{
2012-11-26 18:58:24 +00:00
// if the span length is too long, force a subdivision
if( prevxyz.LengthSqr() > maxLengthSqr || nextxyz.LengthSqr() > maxLengthSqr )
{
2012-11-26 18:58:24 +00:00
break;
}
}
// see if this midpoint is off far enough to subdivide
delta = verts[i * maxWidth + j + 1].xyz - midxyz;
if( delta.LengthSqr() > maxHorizontalErrorSqr )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == height )
{
2012-11-26 18:58:24 +00:00
continue; // didn't need subdivision
}
if( width + 2 >= maxWidth )
{
2012-11-26 18:58:24 +00:00
ResizeExpanded( maxHeight, maxWidth + 4 );
}
2012-11-26 18:58:24 +00:00
// insert two columns and replace the peak
width += 2;
for( i = 0; i < height; i++ )
{
idSurface_Patch::LerpVert( verts[i * maxWidth + j ], verts[i * maxWidth + j + 1], prev );
idSurface_Patch::LerpVert( verts[i * maxWidth + j + 1], verts[i * maxWidth + j + 2], next );
2012-11-26 18:58:24 +00:00
idSurface_Patch::LerpVert( prev, next, mid );
for( k = width - 1; k > j + 3; k-- )
{
verts[i * maxWidth + k] = verts[i * maxWidth + k - 2];
2012-11-26 18:58:24 +00:00
}
verts[i * maxWidth + j + 1] = prev;
verts[i * maxWidth + j + 2] = mid;
verts[i * maxWidth + j + 3] = next;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// back up and recheck this set again, it may need more subdivision
j -= 2;
}
2012-11-26 18:58:24 +00:00
// vertical subdivisions
for( j = 0; j + 2 < height; j += 2 )
{
2012-11-26 18:58:24 +00:00
// check subdivided midpoints against control points
for( i = 0; i < width; i++ )
{
for( l = 0; l < 3; l++ )
{
prevxyz[l] = verts[( j + 1 ) * maxWidth + i].xyz[l] - verts[j * maxWidth + i].xyz[l];
nextxyz[l] = verts[( j + 2 ) * maxWidth + i].xyz[l] - verts[( j + 1 ) * maxWidth + i].xyz[l];
midxyz[l] = ( verts[j * maxWidth + i].xyz[l] + verts[( j + 1 ) * maxWidth + i].xyz[l] * 2.0f +
verts[( j + 2 ) * maxWidth + i].xyz[l] ) * 0.25f;
2012-11-26 18:58:24 +00:00
}
if( maxLength > 0.0f )
{
2012-11-26 18:58:24 +00:00
// if the span length is too long, force a subdivision
if( prevxyz.LengthSqr() > maxLengthSqr || nextxyz.LengthSqr() > maxLengthSqr )
{
2012-11-26 18:58:24 +00:00
break;
}
}
// see if this midpoint is off far enough to subdivide
delta = verts[( j + 1 ) * maxWidth + i].xyz - midxyz;
if( delta.LengthSqr() > maxVerticalErrorSqr )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == width )
{
2012-11-26 18:58:24 +00:00
continue; // didn't need subdivision
}
if( height + 2 >= maxHeight )
{
2012-11-26 18:58:24 +00:00
ResizeExpanded( maxHeight + 4, maxWidth );
}
2012-11-26 18:58:24 +00:00
// insert two columns and replace the peak
height += 2;
for( i = 0; i < width; i++ )
{
LerpVert( verts[j * maxWidth + i], verts[( j + 1 )*maxWidth + i], prev );
LerpVert( verts[( j + 1 )*maxWidth + i], verts[( j + 2 )*maxWidth + i], next );
2012-11-26 18:58:24 +00:00
LerpVert( prev, next, mid );
for( k = height - 1; k > j + 3; k-- )
{
verts[k * maxWidth + i] = verts[( k - 2 ) * maxWidth + i];
2012-11-26 18:58:24 +00:00
}
verts[( j + 1 )*maxWidth + i] = prev;
verts[( j + 2 )*maxWidth + i] = mid;
verts[( j + 3 )*maxWidth + i] = next;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// back up and recheck this set again, it may need more subdivision
j -= 2;
}
2012-11-26 18:58:24 +00:00
PutOnCurve();
2012-11-26 18:58:24 +00:00
RemoveLinearColumnsRows();
2012-11-26 18:58:24 +00:00
Collapse();
2012-11-26 18:58:24 +00:00
// normalize all the lerped normals
if( genNormals )
{
2012-11-26 18:58:24 +00:00
idVec3 tempNormal;
for( i = 0; i < width * height; i++ )
{
2012-11-26 18:58:24 +00:00
tempNormal = verts[i].GetNormal();
tempNormal.Normalize();
verts[i].SetNormal( tempNormal );
}
}
2012-11-26 18:58:24 +00:00
GenerateIndexes();
}