src/primitives.cpp

changeset 363
75583c9f289d
parent 358
7885fa5b09c5
child 365
b3f6c93db2e4
equal deleted inserted replaced
362:344fe1da32c8 363:75583c9f289d
306 bool primitiveLoaderBusy() 306 bool primitiveLoaderBusy()
307 { 307 {
308 return g_primListerMutex; 308 return g_primListerMutex;
309 } 309 }
310 310
311 double radialPoint( int i, int divs, double ( *func )( double ))
312 {
313 return ( *func )(( i * 2 * pi ) / divs );
314 }
315
311 vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num ) 316 vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num )
312 { 317 {
313 vector<LDObject*> objs; 318 vector<LDObject*> objs;
314 319
315 for( int i = 0; i < segs; ++i ) 320 for( int i = 0; i < segs; ++i )
316 { 321 {
317 double x0 = cos( ( i * 2 * pi ) / divs ), 322 double x = radialPoint( i, divs, cos ),
318 x1 = cos( ( ( i + 1 ) * 2 * pi ) / divs ), 323 nextX = radialPoint( i + 1, divs, cos ),
319 z0 = sin( ( i * 2 * pi ) / divs ), 324 prevX = radialPoint(( i + 15 ) % 16, divs, cos ),
320 z1 = sin( ( ( i + 1 ) * 2 * pi ) / divs ); 325 z = radialPoint( i, divs, sin ),
321 326 nextZ = radialPoint( i + 1, divs, sin ),
322 LDObject* obj = null; 327 prevZ = radialPoint(( i + 15 ) % 16, divs, sin );
323 328
324 switch( type ) 329 switch( type )
325 { 330 {
326 case Circle: 331 case Circle:
327 { 332 {
328 vertex v0( x0, 0.0f, z0 ), 333 vertex v0( x, 0.0f, z ),
329 v1( x1, 0.0f, z1 ); 334 v1( nextX, 0.0f, nextZ );
330 335
331 LDLine* line = new LDLine; 336 LDLine* line = new LDLine;
332 line->setVertex( 0, v0 ); 337 line->setVertex( 0, v0 );
333 line->setVertex( 1, v1 ); 338 line->setVertex( 1, v1 );
334 line->setColor( edgecolor ); 339 line->setColor( edgecolor );
335 obj = line; 340 objs << line;
336 } 341 }
337 break; 342 break;
338 343
339 case Cylinder: 344 case Cylinder:
340 case Ring: 345 case Ring:
343 double x2, x3, z2, z3; 348 double x2, x3, z2, z3;
344 double y0, y1, y2, y3; 349 double y0, y1, y2, y3;
345 350
346 if( type == Cylinder ) 351 if( type == Cylinder )
347 { 352 {
348 x2 = x1; 353 x2 = nextX;
349 x3 = x0; 354 x3 = x;
350 z2 = z1; 355 z2 = nextZ;
351 z3 = z0; 356 z3 = z;
352 357
353 y0 = y1 = 0.0f; 358 y0 = y1 = 0.0f;
354 y2 = y3 = 1.0f; 359 y2 = y3 = 1.0f;
355 } 360 }
356 else 361 else
357 { 362 {
358 x2 = x1 * ( num + 1 ); 363 x2 = nextX * ( num + 1 );
359 x3 = x0 * ( num + 1 ); 364 x3 = x * ( num + 1 );
360 z2 = z1 * ( num + 1 ); 365 z2 = nextZ * ( num + 1 );
361 z3 = z0 * ( num + 1 ); 366 z3 = z * ( num + 1 );
362 367
363 x0 *= num; 368 x *= num;
364 x1 *= num; 369 nextX *= num;
365 z0 *= num; 370 z *= num;
366 z1 *= num; 371 nextZ *= num;
367 372
368 if( type == Ring ) 373 if( type == Ring )
369 y0 = y1 = y2 = y3 = 0.0f; 374 y0 = y1 = y2 = y3 = 0.0f;
370 else 375 else
371 { 376 {
372 y0 = y1 = 1.0f; 377 y0 = y1 = 1.0f;
373 y2 = y3 = 0.0f; 378 y2 = y3 = 0.0f;
374 } 379 }
375 } 380 }
376 381
377 vertex v0( x0, y0, z0 ), 382 vertex v0( x, y0, z ),
378 v1( x1, y1, z1 ), 383 v1( nextX, y1, nextZ ),
379 v2( x2, y2, z2 ), 384 v2( x2, y2, z2 ),
380 v3( x3, y3, z3 ); 385 v3( x3, y3, z3 );
381 386
382 LDQuad* quad = new LDQuad; 387 LDQuad* quad = new LDQuad;
383 quad->setColor( maincolor ); 388 quad->setColor( maincolor );
384 quad->setVertex( 0, v0 ); 389 quad->setVertex( 0, v0 );
385 quad->setVertex( 1, v1 ); 390 quad->setVertex( 1, v1 );
386 quad->setVertex( 2, v2 ); 391 quad->setVertex( 2, v2 );
387 quad->setVertex( 3, v3 ); 392 quad->setVertex( 3, v3 );
388 obj = quad; 393 objs << quad;
394
395 LDCondLine* cond = null;
396 if( type == Cylinder )
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;
389 } 408 }
390 break; 409 break;
391 410
392 case Disc: 411 case Disc:
393 case DiscNeg: 412 case DiscNeg:
396 415
397 if( type == Disc ) 416 if( type == Disc )
398 x2 = z2 = 0.0f; 417 x2 = z2 = 0.0f;
399 else 418 else
400 { 419 {
401 x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f; 420 x2 = ( x >= 0.0f ) ? 1.0f : -1.0f;
402 z2 = ( z0 >= 0.0f ) ? 1.0f : -1.0f; 421 z2 = ( z >= 0.0f ) ? 1.0f : -1.0f;
403 } 422 }
404 423
405 vertex v0( x0, 0.0f, z0 ), 424 vertex v0( x, 0.0f, z ),
406 v1( x1, 0.0f, z1 ), 425 v1( nextX, 0.0f, nextZ ),
407 v2( x2, 0.0f, z2 ); 426 v2( x2, 0.0f, z2 );
408 427
409 // Disc negatives need to go the other way around, otherwise 428 // Disc negatives need to go the other way around, otherwise
410 // they'll end up upside-down. 429 // they'll end up upside-down.
411 LDTriangle* seg = new LDTriangle; 430 LDTriangle* seg = new LDTriangle;
412 seg->setColor( maincolor ); 431 seg->setColor( maincolor );
413 seg->setVertex( type == Disc ? 0 : 2, v0 ); 432 seg->setVertex( type == Disc ? 0 : 2, v0 );
414 seg->setVertex( 1, v1 ); 433 seg->setVertex( 1, v1 );
415 seg->setVertex( type == Disc ? 2 : 0, v2 ); 434 seg->setVertex( type == Disc ? 2 : 0, v2 );
416 obj = seg; 435 objs << seg;
417 } 436 }
418 break; 437 break;
419 438
420 default: 439 default:
421 break; 440 break;
422 } 441 }
423
424 if( obj )
425 objs << obj;
426 } 442 }
427 443
428 return objs; 444 return objs;
429 } 445 }
430 446
433 // Not translated as primitives are in English. 449 // Not translated as primitives are in English.
434 return type == Circle ? "Circle" : 450 return type == Circle ? "Circle" :
435 type == Cylinder ? "Cylinder" : 451 type == Cylinder ? "Cylinder" :
436 type == Disc ? "Disc" : 452 type == Disc ? "Disc" :
437 type == DiscNeg ? "Disc Negative" : 453 type == DiscNeg ? "Disc Negative" :
438 type == Ring ? "Ring" : 454 type == Ring ? "Ring" : "Cone";
439 "Cone";
440 } 455 }
441 456
442 static const str g_radialNameRoots[] = 457 static const str g_radialNameRoots[] =
443 { 458 {
444 "edge", 459 "edge",

mercurial