00001
00002
00003
00004
00005 #include "ps2gl/gblock.h"
00006
00007
00008
00009
00010
00011 bool
00012 CGeometryBlock::SameDataFormat()
00013 {
00014 bool same = true;
00015
00016 if ( AreVerticesValid != AreNewVerticesValid
00017 || AreNormalsValid != AreNewNormalsValid
00018 || AreTexCoordsValid != AreNewTexCoordsValid
00019 || AreColorsValid != AreNewColorsValid )
00020 same = false;
00021 else if ( AreNewVerticesValid && WordsPerVertex != WordsPerNewVertex )
00022 same = false;
00023 else if ( AreNewNormalsValid && WordsPerNormal != WordsPerNewNormal )
00024 same = false;
00025 else if ( AreNewTexCoordsValid && WordsPerTexCoord != WordsPerNewTexCoord )
00026 same = false;
00027 else if ( AreNewColorsValid && WordsPerColor != WordsPerNewColor )
00028 same = false;
00029
00030 return same;
00031 }
00032
00033 bool
00034 CGeometryBlock::MergeNew()
00035 {
00036 bool success = true;
00037
00038 mAssert( IsPending() );
00039 mErrorIf( NumNewVertices == 0, "Trying to merge geometry with no vertices!" );
00040
00041 if ( NewPrimType != PrimType
00042 || ! SameDataFormat()
00043 || NumStrips == kMaxNumStrips
00044 )
00045 success = false;
00046 else {
00047 bool merged = false;
00048
00049 TotalVertices += NumNewVertices;
00050
00051
00052
00053
00054
00055 int stripLength = GetStripLength(NumStrips-1);
00056 if ( (float*)Vertices[NumStrips-1] + WordsPerVertex*stripLength
00057 == (float*)NewVertices
00058 && ( ! AreNormalsValid
00059 || (float*)Normals[NumStrips-1] + WordsPerNormal*stripLength
00060 == (float*)NewNormals )
00061 && ( ! AreTexCoordsValid
00062 || (float*)TexCoords[NumStrips-1] + WordsPerTexCoord*stripLength
00063 == (float*)NewTexCoords )
00064 && ( ! AreColorsValid
00065 || (float*)Colors[NumStrips-1] + WordsPerColor*stripLength
00066 == (float*)NewColors )
00067 )
00068 {
00069
00070
00071 if ( PrimType != GL_LINE_STRIP
00072 && PrimType != GL_TRIANGLE_STRIP
00073 && PrimType != GL_QUAD_STRIP ) {
00074 merged = true;
00075 StripLengths[NumStrips-1] += (unsigned int)NumNewVertices;
00076 }
00077 else {
00078 StripLengths[NumStrips-1] |= kContinueFlag;
00079 }
00080 }
00081
00082 if ( ! merged ) {
00083
00084 Vertices[NumStrips] = NewVertices;
00085 Normals[NumStrips] = NewNormals;
00086 TexCoords[NumStrips] = NewTexCoords;
00087 Colors[NumStrips] = NewColors;
00088
00089 StripLengths[NumStrips] = (unsigned int)NumNewVertices;
00090 }
00091
00092 if ( ! merged )
00093 NumStrips++;
00094
00095 ResetNew();
00096 }
00097
00098 return success;
00099 }
00100
00101 void
00102 CGeometryBlock::MakeNewValuesCurrent()
00103 {
00104 CommitPrimType();
00105
00106 NumStrips = 0;
00107
00108 Vertices[NumStrips] = NewVertices;
00109 Normals[NumStrips] = NewNormals;
00110 TexCoords[NumStrips] = NewTexCoords;
00111 Colors[NumStrips] = NewColors;
00112
00113 TotalVertices = NumNewVertices;
00114 StripLengths[NumStrips] = (unsigned int)NumNewVertices;
00115
00116 WordsPerVertex = WordsPerNewVertex;
00117 WordsPerNormal = WordsPerNewNormal;
00118 WordsPerTexCoord = WordsPerNewTexCoord;
00119 WordsPerColor = WordsPerNewColor;
00120
00121 AreVerticesValid = AreNewVerticesValid;
00122 AreNormalsValid = AreNewNormalsValid;
00123 AreTexCoordsValid = AreNewTexCoordsValid;
00124 AreColorsValid = AreNewColorsValid;
00125
00126 NumStrips = 1;
00127 }
00128
00129 void
00130 CGeometryBlock::ResetNew()
00131 {
00132 NewPrimType = GL_INVALID_VALUE;
00133 NumNewVertices = NumNewNormals = NumNewTexCoords = NumNewColors = 0;
00134
00135 NewVertices = NewNormals = NewTexCoords = NewColors = NULL;
00136 WordsPerNewVertex = WordsPerNewNormal = WordsPerNewTexCoord = WordsPerNewColor = 0;
00137 AreNewVerticesValid = AreNewNormalsValid = AreNewTexCoordsValid = AreNewColorsValid = false;
00138 }
00139
00140 void
00141 CGeometryBlock::ResetCurStrip()
00142 {
00143 Vertices[NumStrips] = Normals[NumStrips] = NULL;
00144 TexCoords[NumStrips] = Colors[NumStrips] = NULL;
00145 StripLengths[NumStrips] = 0;
00146 }
00147
00148 void
00149 CGeometryBlock::Reset()
00150 {
00151 TotalVertices = 0;
00152 WordsPerVertex = WordsPerNormal = WordsPerTexCoord = WordsPerColor = 0;
00153 AreVerticesValid = AreNormalsValid = AreTexCoordsValid = AreColorsValid = false;
00154 PrimType = GL_INVALID_VALUE;
00155 NumVertsPerPrim = NumVertsToRestartStrip = -1;
00156 NumStrips = 0;
00157 ResetCurStrip();
00158 ResetNew();
00159 }
00160
00161 void
00162 CGeometryBlock::CommitPrimType()
00163 {
00164 if ( PrimType != NewPrimType ) {
00165 switch (NewPrimType) {
00166 case GL_POINTS:
00167 NumVertsPerPrim = 1;
00168 NumVertsToRestartStrip = 0;
00169 break;
00170 case GL_LINE_STRIP:
00171 NumVertsPerPrim = 1;
00172 NumVertsToRestartStrip = 1;
00173 break;
00174 case GL_TRIANGLE_STRIP:
00175 case GL_TRIANGLE_FAN:
00176 case GL_QUAD_STRIP:
00177 case GL_POLYGON:
00178 NumVertsPerPrim = 1;
00179 NumVertsToRestartStrip = 2;
00180 break;
00181 case GL_LINES:
00182 NumVertsPerPrim = 2;
00183 NumVertsToRestartStrip = 0;
00184 break;
00185 case GL_TRIANGLES:
00186 NumVertsPerPrim = 3;
00187 NumVertsToRestartStrip = 0;
00188 break;
00189 case GL_QUADS:
00190 NumVertsPerPrim = 4;
00191 NumVertsToRestartStrip = 0;
00192 break;
00193 default:
00194 mError( "Shouldn't get here" );
00195 }
00196
00197 PrimType = NewPrimType;
00198 }
00199 }
00200