00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <datagram.h>
00020 #include <datagramIterator.h>
00021 #include <bamReader.h>
00022 #include <bamWriter.h>
00023 #include <ioPtaDatagramShort.h>
00024 #include <ioPtaDatagramInt.h>
00025 #include <ioPtaDatagramLinMath.h>
00026 #include <graphicsStateGuardianBase.h>
00027
00028 #include "geomTri.h"
00029 #include "geomTrifan.h"
00030
00031 TypeHandle GeomTrifan::_type_handle;
00032
00033
00034
00035
00036
00037
00038 int GeomTrifan::
00039 get_num_tris() const {
00040 int numtris = 0;
00041 for (int i = 0; i < _numprims; i++) {
00042 numtris += _primlengths[i] - 2;
00043 }
00044 return numtris;
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 Geom *GeomTrifan::
00056 make_copy() const {
00057 return new GeomTrifan(*this);
00058 }
00059
00060
00061
00062
00063
00064
00065 void GeomTrifan::
00066 print_draw_immediate( void ) const
00067 {
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 }
00195
00196
00197
00198
00199
00200
00201 void GeomTrifan::
00202 draw_immediate(GraphicsStateGuardianBase *gsg, GeomContext *gc) {
00203 gsg->draw_trifan(this, gc);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212 Geom *GeomTrifan::
00213 explode() const {
00214 PTA_Vertexf coords=PTA_Vertexf::empty_array(0);
00215 PTA_Normalf normals=PTA_Normalf::empty_array(0);
00216 PTA_TexCoordf texcoords=PTA_TexCoordf::empty_array(0);
00217 PTA_Colorf colors=PTA_Colorf::empty_array(0);
00218
00219 VertexIterator vi = make_vertex_iterator();
00220 NormalIterator ni = make_normal_iterator();
00221 TexCoordIterator ti = make_texcoord_iterator();
00222 ColorIterator ci = make_color_iterator();
00223
00224
00225 if (get_binding(G_COLOR) == G_OVERALL) {
00226 colors.push_back(get_next_color(ci));
00227 }
00228 if (get_binding(G_NORMAL) == G_OVERALL) {
00229 normals.push_back(get_next_normal(ni));
00230 }
00231
00232 int num_tris = 0;
00233 int num_prims = get_num_prims();
00234
00235 for (int i = 0; i < num_prims; i++) {
00236
00237 Colorf per_trifan_color;
00238 Normalf per_trifan_normal;
00239 if (get_binding(G_COLOR) == G_PER_PRIM) {
00240 per_trifan_color = get_next_color(ci);
00241 }
00242 if (get_binding(G_NORMAL) == G_PER_PRIM) {
00243 per_trifan_normal = get_next_normal(ni);
00244 }
00245
00246 int num_verts = get_length(i);
00247 assert(num_verts >= 3);
00248
00249
00250
00251 Vertexf f3vertex[3];
00252 Normalf f3normal[3];
00253 Colorf f3color[3];
00254 TexCoordf f3texcoord[3];
00255
00256
00257
00258
00259 int v;
00260 for (v = 0; v <= 2; v += 2) {
00261 if (get_binding(G_COORD) == G_PER_VERTEX) {
00262 f3vertex[v] = get_next_vertex(vi);
00263 }
00264 if (get_binding(G_NORMAL) == G_PER_VERTEX) {
00265 f3normal[v] = get_next_normal(ni);
00266 }
00267 if (get_binding(G_TEXCOORD) == G_PER_VERTEX) {
00268 f3texcoord[v] = get_next_texcoord(ti);
00269 }
00270 if (get_binding(G_COLOR) == G_PER_VERTEX) {
00271 f3color[v] = get_next_color(ci);
00272 }
00273 }
00274
00275
00276
00277 for (v = 2; v < num_verts; v++) {
00278 num_tris++;
00279
00280 f3vertex[1] = f3vertex[2];
00281 f3normal[1] = f3normal[2];
00282 f3texcoord[1] = f3texcoord[2];
00283 f3color[1] = f3color[2];
00284
00285 if (get_binding(G_COLOR) == G_PER_PRIM) {
00286 colors.push_back(per_trifan_color);
00287 } else if (get_binding(G_COLOR) == G_PER_COMPONENT) {
00288 colors.push_back(get_next_color(ci));
00289 }
00290
00291 if (get_binding(G_NORMAL) == G_PER_PRIM) {
00292 normals.push_back(per_trifan_normal);
00293 } else if (get_binding(G_NORMAL) == G_PER_COMPONENT) {
00294 normals.push_back(get_next_normal(ni));
00295 }
00296
00297
00298 assert(get_binding(G_COORD) == G_PER_VERTEX);
00299 f3vertex[2] = get_next_vertex(vi);
00300 coords.push_back(f3vertex[0]);
00301 coords.push_back(f3vertex[1]);
00302 coords.push_back(f3vertex[2]);
00303
00304 if (get_binding(G_NORMAL) == G_PER_VERTEX) {
00305 f3normal[2] = get_next_normal(ni);
00306 normals.push_back(f3normal[0]);
00307 normals.push_back(f3normal[1]);
00308 normals.push_back(f3normal[2]);
00309 }
00310 if (get_binding(G_TEXCOORD) == G_PER_VERTEX) {
00311 f3texcoord[2] = get_next_texcoord(ti);
00312 texcoords.push_back(f3texcoord[0]);
00313 texcoords.push_back(f3texcoord[1]);
00314 texcoords.push_back(f3texcoord[2]);
00315 }
00316 if (get_binding(G_COLOR) == G_PER_VERTEX) {
00317 f3color[2] = get_next_color(ci);
00318 colors.push_back(f3color[0]);
00319 colors.push_back(f3color[1]);
00320 colors.push_back(f3color[2]);
00321 }
00322 }
00323 }
00324
00325 Geom *tris = new GeomTri;
00326 tris->set_coords(coords);
00327
00328 tris->set_normals(normals,
00329 get_binding(G_NORMAL) == G_PER_COMPONENT ?
00330 G_PER_PRIM : get_binding(G_NORMAL));
00331 tris->set_texcoords(texcoords, get_binding(G_TEXCOORD));
00332 tris->set_colors(colors,
00333 get_binding(G_COLOR) == G_PER_COMPONENT ?
00334 G_PER_PRIM : get_binding(G_COLOR));
00335 tris->set_num_prims(num_tris);
00336
00337 return tris;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 PTA_ushort GeomTrifan::
00356 get_tris() const {
00357 int num_tris = get_num_tris();
00358 PTA_ushort tris;
00359 tris.reserve(num_tris * 3);
00360
00361 int k = 0;
00362
00363 for (int i = 0; i < _numprims; i++) {
00364 ushort v0, v1, v2;
00365 if (_vindex.empty()) {
00366 v0 = k++;
00367 v1 = k++;
00368 } else {
00369 v0 = _vindex[k++];
00370 v1 = _vindex[k++];
00371 }
00372
00373 int len = _primlengths[i] - 2;
00374
00375 for (int j = 0; j < len; j++) {
00376 if (_vindex.empty()) {
00377 v2 = k++;
00378 } else {
00379 v2 = _vindex[k++];
00380 }
00381
00382 tris.push_back(v0);
00383 tris.push_back(v1);
00384 tris.push_back(v2);
00385
00386 v1 = v2;
00387 }
00388 }
00389
00390 nassertr((int)tris.size() == num_tris * 3, PTA_ushort());
00391 return tris;
00392 }
00393
00394
00395
00396
00397
00398
00399 TypedWritable* GeomTrifan::
00400 make_GeomTrifan(const FactoryParams ¶ms) {
00401 GeomTrifan *me = new GeomTrifan;
00402 DatagramIterator scan;
00403 BamReader *manager;
00404
00405 parse_params(params, scan, manager);
00406 me->fillin(scan, manager);
00407 me->make_dirty();
00408 me->config();
00409 return me;
00410 }
00411
00412
00413
00414
00415
00416
00417 void GeomTrifan::
00418 register_with_read_factory(void) {
00419 BamReader::get_factory()->register_factory(get_class_type(), make_GeomTrifan);
00420 }
00421