src/primitives.cpp

changeset 365
b3f6c93db2e4
parent 363
75583c9f289d
child 377
271d1da66b7e
equal deleted inserted replaced
364:7c3af0a6f8ab 365:b3f6c93db2e4
314 } 314 }
315 315
316 vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num ) 316 vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num )
317 { 317 {
318 vector<LDObject*> objs; 318 vector<LDObject*> objs;
319 vector<int> condLineSegs;
319 320
320 for( int i = 0; i < segs; ++i ) 321 for( int i = 0; i < segs; ++i )
321 { 322 {
322 double x = radialPoint( i, divs, cos ), 323 double x0 = radialPoint( i, divs, cos ),
323 nextX = radialPoint( i + 1, divs, cos ), 324 x1 = radialPoint( i + 1, divs, cos ),
324 prevX = radialPoint(( i + 15 ) % 16, divs, cos ), 325 z0 = radialPoint( i, divs, sin ),
325 z = radialPoint( i, divs, sin ), 326 z1 = radialPoint( i + 1, divs, sin );
326 nextZ = radialPoint( i + 1, divs, sin ),
327 prevZ = radialPoint(( i + 15 ) % 16, divs, sin );
328 327
329 switch( type ) 328 switch( type )
330 { 329 {
331 case Circle: 330 case Circle:
332 { 331 {
333 vertex v0( x, 0.0f, z ), 332 vertex v0( x0, 0.0f, z0 ),
334 v1( nextX, 0.0f, nextZ ); 333 v1( x1, 0.0f, z1 );
335 334
336 LDLine* line = new LDLine; 335 LDLine* line = new LDLine;
337 line->setVertex( 0, v0 ); 336 line->setVertex( 0, v0 );
338 line->setVertex( 1, v1 ); 337 line->setVertex( 1, v1 );
339 line->setColor( edgecolor ); 338 line->setColor( edgecolor );
348 double x2, x3, z2, z3; 347 double x2, x3, z2, z3;
349 double y0, y1, y2, y3; 348 double y0, y1, y2, y3;
350 349
351 if( type == Cylinder ) 350 if( type == Cylinder )
352 { 351 {
353 x2 = nextX; 352 x2 = x1;
354 x3 = x; 353 x3 = x0;
355 z2 = nextZ; 354 z2 = z1;
356 z3 = z; 355 z3 = z0;
357 356
358 y0 = y1 = 0.0f; 357 y0 = y1 = 0.0f;
359 y2 = y3 = 1.0f; 358 y2 = y3 = 1.0f;
360 } 359 }
361 else 360 else
362 { 361 {
363 x2 = nextX * ( num + 1 ); 362 x2 = x1 * ( num + 1 );
364 x3 = x * ( num + 1 ); 363 x3 = x0 * ( num + 1 );
365 z2 = nextZ * ( num + 1 ); 364 z2 = z1 * ( num + 1 );
366 z3 = z * ( num + 1 ); 365 z3 = z0 * ( num + 1 );
367 366
368 x *= num; 367 x0 *= num;
369 nextX *= num; 368 x1 *= num;
370 z *= num; 369 z0 *= num;
371 nextZ *= num; 370 z1 *= num;
372 371
373 if( type == Ring ) 372 if( type == Ring )
374 y0 = y1 = y2 = y3 = 0.0f; 373 y0 = y1 = y2 = y3 = 0.0f;
375 else 374 else
376 { 375 {
377 y0 = y1 = 1.0f; 376 y0 = y1 = 1.0f;
378 y2 = y3 = 0.0f; 377 y2 = y3 = 0.0f;
379 } 378 }
380 } 379 }
381 380
382 vertex v0( x, y0, z ), 381 vertex v0( x0, y0, z0 ),
383 v1( nextX, y1, nextZ ), 382 v1( x1, y1, z1 ),
384 v2( x2, y2, z2 ), 383 v2( x2, y2, z2 ),
385 v3( x3, y3, z3 ); 384 v3( x3, y3, z3 );
386 385
387 LDQuad* quad = new LDQuad; 386 LDQuad* quad = new LDQuad;
388 quad->setColor( maincolor ); 387 quad->setColor( maincolor );
389 quad->setVertex( 0, v0 ); 388 quad->setVertex( 0, v0 );
390 quad->setVertex( 1, v1 ); 389 quad->setVertex( 1, v1 );
391 quad->setVertex( 2, v2 ); 390 quad->setVertex( 2, v2 );
392 quad->setVertex( 3, v3 ); 391 quad->setVertex( 3, v3 );
392
393 if( type == Cylinder )
394 quad->invert();
395
393 objs << quad; 396 objs << quad;
394 397
395 LDCondLine* cond = null; 398 if( type == Cylinder || type == Cone )
396 if( type == Cylinder ) 399 condLineSegs << i;
397 {
398 cond = new LDCondLine;
399 cond->setColor( edgecolor );
400 cond->setVertex( 0, v0 );
401 cond->setVertex( 1, v3 );
402 cond->setVertex( 2, vertex( nextX, 0.0f, nextZ ));
403 cond->setVertex( 3, vertex( prevX, 0.0f, prevZ ));
404 }
405
406 if( cond )
407 objs << cond;
408 } 400 }
409 break; 401 break;
410 402
411 case Disc: 403 case Disc:
412 case DiscNeg: 404 case DiscNeg:
415 407
416 if( type == Disc ) 408 if( type == Disc )
417 x2 = z2 = 0.0f; 409 x2 = z2 = 0.0f;
418 else 410 else
419 { 411 {
420 x2 = ( x >= 0.0f ) ? 1.0f : -1.0f; 412 x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f;
421 z2 = ( z >= 0.0f ) ? 1.0f : -1.0f; 413 z2 = ( z0 >= 0.0f ) ? 1.0f : -1.0f;
422 } 414 }
423 415
424 vertex v0( x, 0.0f, z ), 416 vertex v0( x0, 0.0f, z0 ),
425 v1( nextX, 0.0f, nextZ ), 417 v1( x1, 0.0f, z1 ),
426 v2( x2, 0.0f, z2 ); 418 v2( x2, 0.0f, z2 );
427 419
428 // Disc negatives need to go the other way around, otherwise 420 // Disc negatives need to go the other way around, otherwise
429 // they'll end up upside-down. 421 // they'll end up upside-down.
430 LDTriangle* seg = new LDTriangle; 422 LDTriangle* seg = new LDTriangle;
437 break; 429 break;
438 430
439 default: 431 default:
440 break; 432 break;
441 } 433 }
434 }
435
436 // If this is not a full circle, we need a conditional line at the other
437 // end, too.
438 if( segs < divs && condLineSegs.size() != 0 )
439 condLineSegs << segs;
440
441 for( int i : condLineSegs )
442 {
443 vertex v0( radialPoint( i, divs, cos ), 0.0f, radialPoint( i, divs, sin )),
444 v1,
445 v2( radialPoint( i + 1, divs, cos ), 0.0f, radialPoint( i + 1, divs, sin )),
446 v3( radialPoint( i - 1, divs, cos ), 0.0f, radialPoint( i - 1, divs, sin ));
447
448 if( type == Cylinder )
449 v1 = vertex( v0[X], 1.0f, v0[Z] );
450 elif( type == Cone )
451 {
452 v1 = vertex( v0[X] * ( num + 1 ), 0.0f, v0[Z] * ( num + 1 ));
453 v0[X] *= num;
454 v0[Y] = 1.0f;
455 v0[Z] *= num;
456 }
457
458 LDCondLine* line = new LDCondLine;
459 line->setColor( edgecolor );
460 line->setVertex( 0, v0 );
461 line->setVertex( 1, v1 );
462 line->setVertex( 2, v2 );
463 line->setVertex( 3, v3 );
464 objs << line;
442 } 465 }
443 466
444 return objs; 467 return objs;
445 } 468 }
446 469

mercurial