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 "geomTristrip.h"
00030
00031 TypeHandle GeomTristrip::_type_handle;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 Geom *GeomTristrip::
00043 make_copy() const {
00044 return new GeomTristrip(*this);
00045 }
00046
00047
00048
00049
00050
00051
00052 void GeomTristrip::print_draw_immediate( void ) const
00053 {
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
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
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 }
00269
00270
00271
00272
00273
00274
00275 void GeomTristrip::draw_immediate(GraphicsStateGuardianBase *gsg, GeomContext *gc) {
00276 gsg->draw_tristrip(this, gc);
00277 }
00278
00279
00280
00281
00282
00283
00284 int
00285 GeomTristrip::get_num_tris() const {
00286 int numtris = 0;
00287 for (int i = 0; i < _numprims; i++) {
00288 numtris += _primlengths[i] - 2;
00289 }
00290 return numtris;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299 Geom *GeomTristrip::
00300 explode() const {
00301 PTA_Vertexf coords=PTA_Vertexf::empty_array(0);
00302 PTA_Normalf normals=PTA_Normalf::empty_array(0);
00303 PTA_TexCoordf texcoords=PTA_TexCoordf::empty_array(0);
00304 PTA_Colorf colors=PTA_Colorf::empty_array(0);
00305
00306 VertexIterator vi = make_vertex_iterator();
00307 NormalIterator ni = make_normal_iterator();
00308 TexCoordIterator ti = make_texcoord_iterator();
00309 ColorIterator ci = make_color_iterator();
00310
00311
00312 if (get_binding(G_COLOR) == G_OVERALL) {
00313 colors.push_back(get_next_color(ci));
00314 }
00315 if (get_binding(G_NORMAL) == G_OVERALL) {
00316 normals.push_back(get_next_normal(ni));
00317 }
00318
00319 int num_tris = 0;
00320 int num_prims = get_num_prims();
00321
00322 for (int i = 0; i < num_prims; i++) {
00323
00324 Colorf per_tristrip_color;
00325 Normalf per_tristrip_normal;
00326 if (get_binding(G_COLOR) == G_PER_PRIM) {
00327 per_tristrip_color = get_next_color(ci);
00328 }
00329 if (get_binding(G_NORMAL) == G_PER_PRIM) {
00330 per_tristrip_normal = get_next_normal(ni);
00331 }
00332
00333 int num_verts = get_length(i);
00334 assert(num_verts >= 3);
00335
00336
00337
00338 Vertexf f3vertex[3];
00339 Normalf f3normal[3];
00340 Colorf f3color[3];
00341 TexCoordf f3texcoord[3];
00342
00343
00344
00345
00346
00347 int v;
00348 for (v = 0; v <= 2; v += 2) {
00349 if (get_binding(G_COORD) == G_PER_VERTEX) {
00350 f3vertex[v] = get_next_vertex(vi);
00351 }
00352 if (get_binding(G_NORMAL) == G_PER_VERTEX) {
00353 f3normal[v] = get_next_normal(ni);
00354 }
00355 if (get_binding(G_TEXCOORD) == G_PER_VERTEX) {
00356 f3texcoord[v] = get_next_texcoord(ti);
00357 }
00358 if (get_binding(G_COLOR) == G_PER_VERTEX) {
00359 f3color[v] = get_next_color(ci);
00360 }
00361 }
00362
00363
00364
00365 for (v = 2; v < num_verts; v++) {
00366 num_tris++;
00367
00368 if (get_binding(G_COLOR) == G_PER_PRIM) {
00369 colors.push_back(per_tristrip_color);
00370 } else if (get_binding(G_COLOR) == G_PER_COMPONENT) {
00371 colors.push_back(get_next_color(ci));
00372 }
00373
00374 if (get_binding(G_NORMAL) == G_PER_PRIM) {
00375 normals.push_back(per_tristrip_normal);
00376 } else if (get_binding(G_NORMAL) == G_PER_COMPONENT) {
00377 normals.push_back(get_next_normal(ni));
00378 }
00379
00380 if ((v & 1) == 0) {
00381
00382 f3vertex[1] = f3vertex[2];
00383 f3normal[1] = f3normal[2];
00384 f3texcoord[1] = f3texcoord[2];
00385 f3color[1] = f3color[2];
00386 } else {
00387
00388 f3vertex[0] = f3vertex[2];
00389 f3normal[0] = f3normal[2];
00390 f3texcoord[0] = f3texcoord[2];
00391 f3color[0] = f3color[2];
00392 }
00393
00394
00395 if (get_binding(G_COORD) == G_PER_VERTEX) {
00396 f3vertex[2] = get_next_vertex(vi);
00397 coords.push_back(f3vertex[0]);
00398 coords.push_back(f3vertex[1]);
00399 coords.push_back(f3vertex[2]);
00400 }
00401 if (get_binding(G_NORMAL) == G_PER_VERTEX) {
00402 f3normal[2] = get_next_normal(ni);
00403 normals.push_back(f3normal[0]);
00404 normals.push_back(f3normal[1]);
00405 normals.push_back(f3normal[2]);
00406 }
00407 if (get_binding(G_TEXCOORD) == G_PER_VERTEX) {
00408 f3texcoord[2] = get_next_texcoord(ti);
00409 texcoords.push_back(f3texcoord[0]);
00410 texcoords.push_back(f3texcoord[1]);
00411 texcoords.push_back(f3texcoord[2]);
00412 }
00413 if (get_binding(G_COLOR) == G_PER_VERTEX) {
00414 f3color[2] = get_next_color(ci);
00415 colors.push_back(f3color[0]);
00416 colors.push_back(f3color[1]);
00417 colors.push_back(f3color[2]);
00418 }
00419 }
00420 }
00421
00422 Geom *tris = new GeomTri;
00423 tris->set_coords(coords);
00424 tris->set_normals(normals,
00425 get_binding(G_NORMAL) == G_PER_COMPONENT ?
00426 G_PER_PRIM : get_binding(G_NORMAL));
00427 tris->set_texcoords(texcoords, get_binding(G_TEXCOORD));
00428 tris->set_colors(colors,
00429 get_binding(G_COLOR) == G_PER_COMPONENT ?
00430 G_PER_PRIM : get_binding(G_COLOR));
00431 tris->set_num_prims(num_tris);
00432
00433 return tris;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 PTA_ushort GeomTristrip::
00452 get_tris() const {
00453 int num_tris = get_num_tris();
00454 PTA_ushort tris;
00455 tris.reserve(num_tris * 3);
00456
00457 int k = 0;
00458
00459 for (int i = 0; i < _numprims; i++) {
00460 ushort v0, v1, v2;
00461 if (_vindex.empty()) {
00462 v0 = k++;
00463 v1 = k++;
00464 } else {
00465 v0 = _vindex[k++];
00466 v1 = _vindex[k++];
00467 }
00468
00469 bool even = true;
00470 int len = _primlengths[i];
00471
00472 for (int j = 2; j < len; j++) {
00473 if (_vindex.empty()) {
00474 v2 = k++;
00475 } else {
00476 v2 = _vindex[k++];
00477 }
00478
00479 even = !even;
00480
00481 if (even) {
00482 ushort vtmp = v1;
00483 v1 = v2;
00484 v2 = vtmp;
00485 }
00486
00487 tris.push_back(v0);
00488 tris.push_back(v1);
00489 tris.push_back(v2);
00490
00491 if (even) {
00492 v0 = v2;
00493 } else {
00494 v0 = v1;
00495 v1 = v2;
00496 }
00497 }
00498 }
00499
00500 nassertr((int)tris.size() == num_tris * 3, PTA_ushort());
00501 return tris;
00502 }
00503
00504
00505
00506
00507
00508
00509 TypedWritable* GeomTristrip::
00510 make_GeomTristrip(const FactoryParams ¶ms) {
00511 GeomTristrip *me = new GeomTristrip;
00512 DatagramIterator scan;
00513 BamReader *manager;
00514
00515 parse_params(params, scan, manager);
00516 me->fillin(scan, manager);
00517 me->make_dirty();
00518 me->config();
00519 return me;
00520 }
00521
00522
00523
00524
00525
00526
00527 void GeomTristrip::
00528 register_with_read_factory(void) {
00529 BamReader::get_factory()->register_factory(get_class_type(), make_GeomTristrip);
00530 }