| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
_______________________________________________________________________ |
| 39 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 40 |
| |
| 41 |
| $Revision: 1.2 $ |
| 42 |
| |
| 43 |
| Classes : TsField |
| 44 |
| |
| 45 |
| Author : Dave Immel |
| 46 |
| |
| 47 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 48 |
_______________________________________________________________________ |
| 49 |
*/ |
| 50 |
|
| 51 |
//--------------------------- Include ---------------------------------- |
| 52 |
#include <cstdlib> |
| 53 |
#include <cstdio> |
| 54 |
#include <cmath> |
| 55 |
#include <sys/types.h> |
| 56 |
#include <Xm/Xm.h> |
| 57 |
#include <Xm/Form.h> |
| 58 |
#include <Inventor/SbLinear.h> |
| 59 |
#include <Inventor/SoPath.h> |
| 60 |
#include <Inventor/SoDB.h> |
| 61 |
#include <Inventor/events/SoKeyboardEvent.h> |
| 62 |
#include <Inventor/nodes/SoAnnotation.h> |
| 63 |
#include <Inventor/nodes/SoBaseColor.h> |
| 64 |
#include <Inventor/nodes/SoCallback.h> |
| 65 |
#include <Inventor/nodes/SoCamera.h> |
| 66 |
#include <Inventor/nodes/SoColorIndex.h> |
| 67 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 68 |
#include <Inventor/nodes/SoEventCallback.h> |
| 69 |
#include <Inventor/nodes/SoFont.h> |
| 70 |
#include <Inventor/nodes/SoGroup.h> |
| 71 |
#include <Inventor/nodes/SoTransform.h> |
| 72 |
#include <Inventor/nodes/SoTranslation.h> |
| 73 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 74 |
#include <Inventor/nodes/SoLightModel.h> |
| 75 |
#include <Inventor/nodes/SoDrawStyle.h> |
| 76 |
#include <Inventor/nodes/SoMaterial.h> |
| 77 |
#include <Inventor/nodes/SoResetTransform.h> |
| 78 |
#include <Inventor/nodes/SoSeparator.h> |
| 79 |
#include <Inventor/nodes/SoSwitch.h> |
| 80 |
#include <Inventor/nodes/SoText2.h> |
| 81 |
#include <Inventor/nodes/SoIndexedFaceSet.h> |
| 82 |
#include <Inventor/nodes/SoIndexedLineSet.h> |
| 83 |
#include <Inventor/nodes/SoNormal.h> |
| 84 |
#include <Inventor/nodes/SoNormalBinding.h> |
| 85 |
#include <Inventor/nodes/SoBaseColor.h> |
| 86 |
#include <Inventor/sensors/SoAlarmSensor.h> |
| 87 |
#include <Inventor/sensors/SoDataSensor.h> |
| 88 |
#include <Inventor/sensors/SoTimerSensor.h> |
| 89 |
#include "../../samples/common/InventorLogo.h" |
| 90 |
#include "TsPieces.h" |
| 91 |
#include "TsField.h" |
| 92 |
#include "TsViewer.h" |
| 93 |
#include "MotifHelp.h" |
| 94 |
|
| 95 |
#define FLD_VERTICAL_SHIFT -10.0 |
| 96 |
#define FLD_TOTAL_ROWS 12 |
| 97 |
#define FLD_CAM_Y 35.0 |
| 98 |
#define FLD_CAM_Z 20.0 |
| 99 |
|
| 100 |
#define DROP_RATE 20.0 |
| 101 |
#define POINTS_PER_SKILL_LEVEL 10000 |
| 102 |
|
| 103 |
#define NUM_HELP_LINES 21 |
| 104 |
static char *helpMessage[] = { |
| 105 |
"Random pieces fall to the green floor. Maneuver them to completely", |
| 106 |
"fill in the grid. Use the ARROW KEYS to translate the piece forward", |
| 107 |
"and backward on the grid. Rotate the piece as follows:", |
| 108 |
" ", |
| 109 |
" D KEY rotates the piece clockwise around the falling axis,", |
| 110 |
" F KEY rolls the piece over on its RIGHT side,", |
| 111 |
" S KEY rolls the piece over on its LEFT side,", |
| 112 |
" C KEY rolls the piece TOWARDS you,", |
| 113 |
" E KEY rolls the piece AWAY from you.", |
| 114 |
" ", |
| 115 |
"The game is over when no more pieces can fall.", |
| 116 |
"Press <Enter> key to start a new game.", |
| 117 |
"The H key displays this message. Press <Enter> to resume the game.", |
| 118 |
" ", |
| 119 |
"Points are awarded when pieces land on the floor and when rows", |
| 120 |
"are completed. Use the <SPACEBAR> key to drop a piece before it", |
| 121 |
"lands. More points are awarded for dropping the piece from a", |
| 122 |
"higher place. More points are awarded when more than one row is", |
| 123 |
"completed at the same time. Advance to the next level for every", |
| 124 |
"10,000 points scored. Pieces fall faster at each higher level.", |
| 125 |
"More difficult pieces fall at the higher levels." |
| 126 |
|
| 127 |
}; |
| 128 |
|
| 129 |
// |
| 130 |
// This orientation table is used to define each of 24 possible orientations |
| 131 |
// for a piece in three dimensions. For each orientation, a rotation about |
| 132 |
// the X, Y, or Z axis will result in a different orientation. This table |
| 133 |
// stores the destination orientation for each orientation resulting from a |
| 134 |
// rotation about each of the 3 axes. Refer to the README file for a |
| 135 |
// description of each orientation. |
| 136 |
// |
| 137 |
static int orientationTable[24][3] = { |
| 138 |
{ 4, 16, 1}, |
| 139 |
{ 5, 17, 2}, |
| 140 |
{ 6, 18, 3}, |
| 141 |
{ 7, 19, 0}, |
| 142 |
{ 8, 5, 21}, |
| 143 |
{ 11, 6, 22}, |
| 144 |
{ 10, 7, 23}, |
| 145 |
{ 9, 4, 20}, |
| 146 |
{ 12, 22, 9}, |
| 147 |
{ 13, 21, 10}, |
| 148 |
{ 14, 20, 11}, |
| 149 |
{ 15, 23, 8}, |
| 150 |
{ 0, 13, 17}, |
| 151 |
{ 3, 14, 16}, |
| 152 |
{ 2, 15, 19}, |
| 153 |
{ 1, 12, 18}, |
| 154 |
{ 19, 10, 5}, |
| 155 |
{ 16, 9, 6}, |
| 156 |
{ 17, 8, 7}, |
| 157 |
{ 18, 11, 4}, |
| 158 |
{ 21, 0, 15}, |
| 159 |
{ 22, 1, 14}, |
| 160 |
{ 23, 2, 13}, |
| 161 |
{ 20, 3, 12}}; |
| 162 |
|
| 163 |
//////////////////////////////////////////////////////////////////////// |
| 164 |
// |
| 165 |
// Public constructor - build the widget right now |
| 166 |
// |
| 167 |
TsField::TsField( |
| 168 |
int res, |
| 169 |
Widget parent, |
| 170 |
const char *name, |
| 171 |
SbBool buildInsideParent) |
| 172 |
: SoXtComponent( |
| 173 |
parent, |
| 174 |
name, |
| 175 |
buildInsideParent) |
| 176 |
// |
| 177 |
//////////////////////////////////////////////////////////////////////// |
| 178 |
{ |
| 179 |
gameStarted = FALSE; |
| 180 |
isGameOver = TRUE; |
| 181 |
isDropping = FALSE; |
| 182 |
currentPiece = NULL; |
| 183 |
otherBlocks = NULL; |
| 184 |
totalHeight = FLD_TOTAL_ROWS; |
| 185 |
currentHeight = 0; |
| 186 |
resolution = res; |
| 187 |
block = NULL; |
| 188 |
fieldTable = NULL; |
| 189 |
skillLevel = 0; |
| 190 |
pointsEarned = 0; |
| 191 |
numRemovals = 0; |
| 192 |
pointsNeeded = POINTS_PER_SKILL_LEVEL; |
| 193 |
totalScore = 0; |
| 194 |
helpDisplayed = TRUE; |
| 195 |
gamePaused = FALSE; |
| 196 |
trickleOn = FALSE; |
| 197 |
shiftOn = FALSE; |
| 198 |
removalOn = FALSE; |
| 199 |
numPieceTypes = 3; |
| 200 |
|
| 201 |
// |
| 202 |
// Skill rates 2 is not faster because that is the level in which |
| 203 |
// the more complex pieces are added. The level displayed to the user |
| 204 |
// is one greater than the index in the skillRates array. |
| 205 |
// |
| 206 |
skillRates[0] = 1.4; |
| 207 |
skillRates[1] = 1.7; |
| 208 |
skillRates[2] = 1.7; |
| 209 |
skillRates[3] = 2.0; |
| 210 |
skillRates[4] = 2.3; |
| 211 |
skillRates[5] = 2.6; |
| 212 |
skillRates[6] = 2.9; |
| 213 |
currentRate = skillRates[skillLevel]; |
| 214 |
currentFraction = 2.0; |
| 215 |
|
| 216 |
Widget w = buildWidget(parent, name); |
| 217 |
setBaseWidget(w); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
//////////////////////////////////////////////////////////////////////// |
| 222 |
// |
| 223 |
// Description: |
| 224 |
// Destructor for TsField. |
| 225 |
// |
| 226 |
// Use: public |
| 227 |
|
| 228 |
TsField::~TsField() |
| 229 |
|
| 230 |
// |
| 231 |
//////////////////////////////////////////////////////////////////////// |
| 232 |
{ |
| 233 |
} |
| 234 |
|
| 235 |
//////////////////////////////////////////////////////////////////////// |
| 236 |
// |
| 237 |
// Description: |
| 238 |
// This routine builds the widget of the game. |
| 239 |
// |
| 240 |
// Use: public |
| 241 |
|
| 242 |
Widget |
| 243 |
TsField::buildWidget( |
| 244 |
Widget parent, |
| 245 |
const char * name ) |
| 246 |
|
| 247 |
// |
| 248 |
//////////////////////////////////////////////////////////////////////// |
| 249 |
{ |
| 250 |
viewer = new TsViewer(parent, name); |
| 251 |
|
| 252 |
// Create all of the widgets for the game and put them in a form widget |
| 253 |
Widget widget = XmCreateForm( parent, "form", NULL, 0 ); |
| 254 |
Widget viewerWidget = viewer->getWidget(); |
| 255 |
|
| 256 |
viewer->setSize( SbVec2s( 660, 990 ) ); |
| 257 |
SoDirectionalLight *dirLgt = viewer->getHeadlight(); |
| 258 |
dirLgt->intensity = 1.0; |
| 259 |
|
| 260 |
XtManageChild (viewerWidget); |
| 261 |
|
| 262 |
ARG_VARS(10); |
| 263 |
|
| 264 |
RESET_ARGS(); |
| 265 |
ADD_LEFT_FORM(0); |
| 266 |
ADD_TOP_FORM(0); |
| 267 |
ADD_BOTTOM_FORM(0); |
| 268 |
XtSetValues(viewerWidget, ARGS); |
| 269 |
|
| 270 |
if (block == NULL) |
| 271 |
buildBlock(); |
| 272 |
|
| 273 |
return widget; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
//////////////////////////////////////////////////////////////////////// |
| 278 |
// |
| 279 |
// Set the Inventor logo on the screen. |
| 280 |
// |
| 281 |
static void |
| 282 |
logoCB(void *, SoAction *action) |
| 283 |
{ |
| 284 |
if (action->isOfType(SoGLRenderAction::getClassTypeId())) { |
| 285 |
static int pushedViewport = 0; |
| 286 |
if (!pushedViewport) { |
| 287 |
pushedViewport = 1; |
| 288 |
glPushAttrib(GL_VIEWPORT_BIT); |
| 289 |
glViewport(0, 0, 80, 80); |
| 290 |
} |
| 291 |
else { |
| 292 |
pushedViewport = 0; |
| 293 |
glPopAttrib(); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
//////////////////////////////////////////////////////////////////////// |
| 299 |
// |
| 300 |
// Description: |
| 301 |
// This routine initializes the geometry of the playing field and |
| 302 |
// the set of pieces. |
| 303 |
// |
| 304 |
// Use: public |
| 305 |
|
| 306 |
void |
| 307 |
TsField::initGame() |
| 308 |
|
| 309 |
// Values Returned: |
| 310 |
// path to root node of game field |
| 311 |
// |
| 312 |
//////////////////////////////////////////////////////////////////////// |
| 313 |
{ |
| 314 |
SoBaseColor *gridColor; |
| 315 |
SoSeparator *gridRoot; |
| 316 |
SoMaterial *fallingMaterial; |
| 317 |
TsEll *ell; |
| 318 |
TsTee *tee; |
| 319 |
TsZee *zee; |
| 320 |
TsLTri *ltri; |
| 321 |
TsMTri *mtri; |
| 322 |
TsRTri *rtri; |
| 323 |
int i, j, k; |
| 324 |
|
| 325 |
// |
| 326 |
// Initialize the sensors that will be used to trickle the current piece |
| 327 |
// down, remove the completed rows, and shift the remaining rows down |
| 328 |
// |
| 329 |
trickleSensor = new SoTimerSensor( TsField::tricklePiece, (void *)this ); |
| 330 |
removalSensor = new SoAlarmSensor( TsField::removeRows, (void *)this ); |
| 331 |
shiftSensor = new SoTimerSensor( TsField::shiftRows, (void *)this ); |
| 332 |
trickleSensor->setInterval( SbTime( .03 ) ); |
| 333 |
shiftSensor->setInterval( SbTime( .03 ) ); |
| 334 |
|
| 335 |
// |
| 336 |
// Allocate and initialize the table of boolean values for each position |
| 337 |
// of the field. This table will be used for intersection calculations, |
| 338 |
// as it specifies which positions in the field are occupied by a block. |
| 339 |
// |
| 340 |
fieldTable = new SbBool[(totalHeight+4)*resolution*resolution]; |
| 341 |
for (i=0; i<(totalHeight+4)*resolution*resolution; i++) |
| 342 |
fieldTable[i] = 0; |
| 343 |
|
| 344 |
// |
| 345 |
// Create a list of translation nodes that will be used to position |
| 346 |
// the blocks that make up each piece. Pieces are defined as a set |
| 347 |
// of indices into a 27-cube. This table contains 27 translations for |
| 348 |
// each position of this "Standard Cube". |
| 349 |
// |
| 350 |
SoTranslation *tmpTrans; |
| 351 |
for (i=0; i<3; i++) |
| 352 |
for (j=0; j<3; j++) |
| 353 |
for (k=0; k<3; k++) |
| 354 |
{ |
| 355 |
tmpTrans = new SoTranslation; |
| 356 |
tmpTrans->translation.setValue (2. * k, 2. * i, -2. * j); |
| 357 |
standardCube.append (tmpTrans); |
| 358 |
} |
| 359 |
|
| 360 |
// |
| 361 |
// Build the scene graph that represents the whole field of play. |
| 362 |
// The root of this graph will be given to the viewer. The graph |
| 363 |
// contains a child for handling play events, the floor of the field, |
| 364 |
// a child for the currently falling piece, and a child for all of |
| 365 |
// the static pieces gathering on the floor. |
| 366 |
// |
| 367 |
fieldRoot = new SoSeparator; |
| 368 |
fieldRoot->ref(); |
| 369 |
SoEventCallback *eventNode = new SoEventCallback; |
| 370 |
fieldRoot->addChild (eventNode); |
| 371 |
createFloorAndGrid(); |
| 372 |
fieldRoot->addChild (floor); |
| 373 |
|
| 374 |
// Setup the callback functions used for receiving play events |
| 375 |
eventNode->addEventCallback (SoKeyboardEvent::getClassTypeId(), |
| 376 |
TsField::handleKeyboard, (void *)this); |
| 377 |
|
| 378 |
// |
| 379 |
// Create the root node for the falling piece. An instance of the grid |
| 380 |
// will fall with the piece. Set up a Y translation node for the piece |
| 381 |
// and the grid, an XZ translation node for user control, a material |
| 382 |
// for the falling piece, and the falling piece. |
| 383 |
// |
| 384 |
fallingRoot = new SoSeparator(); |
| 385 |
pieceYTranslation = new SoTranslation(); |
| 386 |
pieceXZTranslation = new SoTranslation(); |
| 387 |
gridRoot = new SoSeparator(); |
| 388 |
gridTranslation = new SoTranslation(); |
| 389 |
fallingMaterial = new SoMaterial(); |
| 390 |
gridColor = new SoBaseColor(); |
| 391 |
|
| 392 |
fallingRoot->addChild (pieceYTranslation); |
| 393 |
fallingRoot->addChild (gridRoot); |
| 394 |
fallingRoot->addChild (pieceXZTranslation); |
| 395 |
fallingRoot->addChild (fallingMaterial); |
| 396 |
fallingRoot->ref(); |
| 397 |
gridRoot->addChild (gridTranslation); |
| 398 |
gridRoot->addChild (gridColor); |
| 399 |
gridRoot->addChild (grid); |
| 400 |
|
| 401 |
gridColor->rgb.setValue (0.9, 0.1, 0.1); |
| 402 |
gridTranslation->translation.setValue (0.0, 0.0, 0.0); |
| 403 |
fallingMaterial->ambientColor.setValue (1.0, 1.0, 1.0); |
| 404 |
fallingMaterial->diffuseColor.setValue (1.0, 1.0, 1.0); |
| 405 |
fallingMaterial->specularColor.setValue (1.0, 1.0, 1.0); |
| 406 |
fallingMaterial->shininess.setValue (20.0); |
| 407 |
fallingMaterial->ref(); |
| 408 |
|
| 409 |
// |
| 410 |
// Create three translation nodes that will be used to animate remaining |
| 411 |
// rows downward after rows have been removed. |
| 412 |
// |
| 413 |
for (i=0; i<3; i++) |
| 414 |
removalTranslation.append (new SoTranslation()); |
| 415 |
|
| 416 |
// |
| 417 |
// Create the scene graph for the static pieces sitting on the floor. |
| 418 |
// Each level of the playing field will be rendered by a different color |
| 419 |
// and at a different vertical location. As pieces fall and stop at |
| 420 |
// various levels, the blocks that make up the pieces will be distributed |
| 421 |
// to these levels. |
| 422 |
// For each vertical level of the playing field, create a separator node |
| 423 |
// for it, along with a node for Y translation and a material node |
| 424 |
// Initialize the material and Y translation. Blocks from the pieces |
| 425 |
// stopping at the levels will be added to the levelBlocks group. |
| 426 |
// |
| 427 |
otherBlocks = new SoGroup(totalHeight); |
| 428 |
fieldRoot->addChild (otherBlocks); |
| 429 |
{ |
| 430 |
SoSeparator *level; |
| 431 |
SoTranslation *levelXform; |
| 432 |
SoMaterial *levelMtl; |
| 433 |
float mtlValue; |
| 434 |
|
| 435 |
for (i=0; i<totalHeight; i++) |
| 436 |
{ |
| 437 |
level = new SoSeparator(); |
| 438 |
levelXform = new SoTranslation(); |
| 439 |
levelMtl = new SoMaterial(); |
| 440 |
otherBlocks->addChild (level); |
| 441 |
level->renderCaching = SoSeparator::ON; |
| 442 |
level->addChild (levelXform); |
| 443 |
level->addChild (levelMtl); |
| 444 |
levelGroup.append(level); |
| 445 |
|
| 446 |
levelXform->translation.setValue (0.0, (float)(2*i), 0.0); |
| 447 |
mtlValue = (float)(i%4)/3.0; |
| 448 |
levelMtl->ambientColor.setValue (mtlValue, mtlValue, 1.0); |
| 449 |
levelMtl->diffuseColor.setValue (mtlValue, mtlValue, 1.0); |
| 450 |
levelMtl->specularColor.setValue (mtlValue, mtlValue, 1.0); |
| 451 |
levelMtl->shininess.setValue (30.0); |
| 452 |
levelMaterial.append(levelMtl); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
// |
| 457 |
// Create a list of groups for storing the XZ location of each |
| 458 |
// position on the floor along with an instance of a block. This |
| 459 |
// will be used to place blocks on the levels after a piece has |
| 460 |
// stopped falling. |
| 461 |
// |
| 462 |
{ |
| 463 |
SoSeparator *tblk; |
| 464 |
SoTranslation *XZTrans; |
| 465 |
|
| 466 |
for (i=0; i<resolution; i++) |
| 467 |
for (j=0; j<resolution; j++) |
| 468 |
{ |
| 469 |
tblk = new SoSeparator(2); |
| 470 |
XZTrans = new SoTranslation; |
| 471 |
|
| 472 |
tblk->addChild (XZTrans); |
| 473 |
tblk->addChild (block); |
| 474 |
XZTrans->translation.setValue ((float)(-resolution+2*j), |
| 475 |
0.0, (float)(resolution-2*i)); |
| 476 |
staticBlock.append (tblk); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// |
| 481 |
// Create one of each piece and store pointers to them in a list |
| 482 |
// to be used during the game. |
| 483 |
// |
| 484 |
ell = new TsEll(); |
| 485 |
tee = new TsTee(); |
| 486 |
zee = new TsZee(); |
| 487 |
ltri = new TsLTri(); |
| 488 |
mtri = new TsMTri(); |
| 489 |
rtri = new TsRTri(); |
| 490 |
pieceList.append (ell); |
| 491 |
pieceList.append (tee); |
| 492 |
pieceList.append (zee); |
| 493 |
pieceList.append (mtri); |
| 494 |
pieceList.append (ltri); |
| 495 |
pieceList.append (rtri); |
| 496 |
|
| 497 |
// |
| 498 |
// Create the scene graph that will represent the falling piece. |
| 499 |
// The root of the piece will have a child for each block of the piece. |
| 500 |
// Each block will be rendered with a switch node pointing to the |
| 501 |
// correct standardCube translation and an instance to the generic |
| 502 |
// block. Add the first three block to the piece, all pieces have at |
| 503 |
// least three blocks. |
| 504 |
// |
| 505 |
fallingPiece = new SoSeparator(4); |
| 506 |
fallingPiece->ref(); |
| 507 |
|
| 508 |
SoSeparator *tmpSep; |
| 509 |
SoSwitch *tmpSwitch; |
| 510 |
for (i=0; i<4; i++) |
| 511 |
{ |
| 512 |
tmpSep = new SoSeparator(2); |
| 513 |
tmpSwitch = new SoSwitch(27); |
| 514 |
tmpSep->addChild (tmpSwitch); |
| 515 |
tmpSep->addChild (block); |
| 516 |
pieceChild.append (tmpSep); |
| 517 |
for (j=0; j<27; j++) |
| 518 |
tmpSwitch->addChild(standardCube[j]); |
| 519 |
} |
| 520 |
fallingPiece->addChild (pieceChild[0]); |
| 521 |
fallingPiece->addChild (pieceChild[1]); |
| 522 |
fallingPiece->addChild (pieceChild[2]); |
| 523 |
fallingRoot->addChild (fallingPiece); |
| 524 |
|
| 525 |
// Create a material and transform node for use in removing rows |
| 526 |
removalMaterial = new SoMaterial(); |
| 527 |
removalMaterial->ambientColor.setValue (1.0, 0.75, 0.0); |
| 528 |
removalMaterial->diffuseColor.setValue (1.0, 0.75, 0.0); |
| 529 |
removalMaterial->specularColor.setValue (1.0, 0.75, 0.0); |
| 530 |
removalMaterial->shininess.setValue (50.0); |
| 531 |
removalMaterial->ref(); |
| 532 |
|
| 533 |
// |
| 534 |
// Create a subgraph that will be used to render the score, level |
| 535 |
// as annontation. |
| 536 |
// |
| 537 |
overRoot = new SoSeparator; |
| 538 |
SoSeparator *scoreRoot = new SoSeparator; |
| 539 |
SoFont *overFont = new SoFont; |
| 540 |
SoLightModel *overModel = new SoLightModel; |
| 541 |
SoColorIndex *overColor = new SoColorIndex; |
| 542 |
SoText2 *overText = new SoText2; |
| 543 |
SoTranslation *overXl1 = new SoTranslation; |
| 544 |
SoTranslation *overXl2 = new SoTranslation; |
| 545 |
SbColor *colorMap = new SbColor[4]; |
| 546 |
|
| 547 |
colorMap[0].setValue(1.0, 0.0, 0.0); |
| 548 |
colorMap[1].setValue(0.7, 0.7, 0.7); |
| 549 |
colorMap[2].setValue(0.0, 0.0, 1.0); |
| 550 |
colorMap[3].setValue(1.0, 1.0, 1.0); |
| 551 |
overModel->model = SoLightModel::BASE_COLOR; |
| 552 |
overFont->size = 20; |
| 553 |
overColor->index = 1; |
| 554 |
overXl1->translation.setValue(SbVec3f(0.4, 0.9, 0.0)); |
| 555 |
overXl2->translation.setValue(SbVec3f(0.3, 0.0, 0.0)); |
| 556 |
overText->string.set1Value(0, SbString("Level:")); |
| 557 |
overText->string.set1Value(1, SbString("Score:")); |
| 558 |
overScore = new SoText2; |
| 559 |
overScore->string.set1Value(0, SbString("1")); |
| 560 |
overScore->string.set1Value(1, SbString("0")); |
| 561 |
scoreRoot->addChild(overFont); |
| 562 |
scoreRoot->addChild(overColor); |
| 563 |
scoreRoot->addChild(overXl1); |
| 564 |
scoreRoot->addChild(overText); |
| 565 |
scoreRoot->addChild(overXl2); |
| 566 |
scoreRoot->addChild(overScore); |
| 567 |
overRoot->addChild(overModel); |
| 568 |
overRoot->addChild(scoreRoot); |
| 569 |
|
| 570 |
// Build the scene graph for the help message and attach it to |
| 571 |
// the overRoot. |
| 572 |
buildHelpMessage(overRoot); |
| 573 |
|
| 574 |
// |
| 575 |
// Add Inventor logo to overlay planes |
| 576 |
// |
| 577 |
SoSeparator *logo = NULL; |
| 578 |
SoInput in; |
| 579 |
in.setBuffer((void *)ivLogo, ivLogoSize); // samples/common/InventorLogo.h |
| 580 |
logo = SoDB::readAll(&in); |
| 581 |
logo->ref(); |
| 582 |
in.closeFile(); |
| 583 |
|
| 584 |
SoCallback *cb = new SoCallback; // sets GL viewport |
| 585 |
cb->setCallback(logoCB); |
| 586 |
logo->insertChild(cb, 0); |
| 587 |
logo->addChild(cb); // Will pop viewport |
| 588 |
|
| 589 |
SoColorIndex *logoColor = new SoColorIndex; // Logo color |
| 590 |
logoColor->index = 3; |
| 591 |
logo->insertChild(logoColor, 0); |
| 592 |
overRoot->addChild(logo); |
| 593 |
|
| 594 |
|
| 595 |
// Initialize the viewer for the field |
| 596 |
viewer->setAutoClipping (FALSE); |
| 597 |
viewer->setSceneGraph( fieldRoot ); |
| 598 |
viewer->setOverlaySceneGraph( overRoot ); |
| 599 |
viewer->setOverlayColorMap(0, 4, colorMap); |
| 600 |
SoCamera *gameCamera = viewer->getCamera(); |
| 601 |
gameCamera->nearDistance = 0.6; |
| 602 |
gameCamera->farDistance = 60.0; |
| 603 |
gameCamera->position.setValue( 0.0, FLD_CAM_Y, FLD_CAM_Z ); |
| 604 |
gameCamera->orientation.setValue (SbVec3f(-1.0, 0.0, 0.0), 0.7); |
| 605 |
gameCamera->focalDistance = FLD_CAM_Z / cosf (0.7); |
| 606 |
gameCamera->aspectRatio = 0.65; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
//////////////////////////////////////////////////////////////////////// |
| 612 |
// |
| 613 |
// Description: |
| 614 |
// Start a new game. |
| 615 |
// |
| 616 |
// Use: public |
| 617 |
|
| 618 |
void |
| 619 |
TsField::startNewGame() |
| 620 |
|
| 621 |
// |
| 622 |
//////////////////////////////////////////////////////////////////////// |
| 623 |
{ |
| 624 |
SoSeparator *lg; |
| 625 |
int level, n, i; |
| 626 |
|
| 627 |
// Unschedule any sensors. |
| 628 |
trickleSensor->unschedule(); |
| 629 |
removalSensor->unschedule(); |
| 630 |
shiftSensor->unschedule(); |
| 631 |
|
| 632 |
// |
| 633 |
// Restore the field to a consistent state. Make sure any removal |
| 634 |
// materials and removal translations are not in the scene graph. |
| 635 |
// Make sure the currently falling piece is attached. |
| 636 |
// |
| 637 |
for (i=0; i<numRemovals; i++) |
| 638 |
{ |
| 639 |
lg = (SoSeparator *)levelGroup[removalList[i]]; |
| 640 |
if (lg->findChild (removalMaterial) != -1) |
| 641 |
lg->replaceChild (removalMaterial, levelMaterial[removalList[i]]); |
| 642 |
if (otherBlocks->findChild(removalTranslation[i]) != -1) |
| 643 |
otherBlocks->replaceChild (removalTranslation[i], lg); |
| 644 |
} |
| 645 |
if (fieldRoot->findChild (fallingRoot) == -1) |
| 646 |
fieldRoot->insertChild (fallingRoot, 2); |
| 647 |
|
| 648 |
// For each level, remove all of the pieces on that level |
| 649 |
for (level=0; level<totalHeight; level++) |
| 650 |
{ |
| 651 |
lg = (SoSeparator *)levelGroup[level]; |
| 652 |
n = lg->getNumChildren(); |
| 653 |
for (i=0; i<(n-2); i++) |
| 654 |
lg->removeChild(2); |
| 655 |
} |
| 656 |
|
| 657 |
// Clear all the values in the fieldTable |
| 658 |
for (i=0; i<((totalHeight+4)*resolution*resolution); i++) |
| 659 |
fieldTable[i] = 0; |
| 660 |
|
| 661 |
// Initialize game variables |
| 662 |
isGameOver = FALSE; |
| 663 |
isDropping = FALSE; |
| 664 |
currentHeight = 0; |
| 665 |
skillLevel = 0; |
| 666 |
currentRate = skillRates[skillLevel]; |
| 667 |
currentFraction = 2.0; |
| 668 |
pointsNeeded = POINTS_PER_SKILL_LEVEL; |
| 669 |
totalScore = 0; |
| 670 |
numPieceTypes = 3; |
| 671 |
|
| 672 |
// Reset the scoreboare |
| 673 |
overScore->string.set1Value(0, SbString("1")); |
| 674 |
overScore->string.set1Value(1, SbString("0")); |
| 675 |
|
| 676 |
// Add the first piece |
| 677 |
addPiece(); |
| 678 |
viewer->saveHomePosition(); |
| 679 |
} |
| 680 |
|
| 681 |
//////////////////////////////////////////////////////////////////////// |
| 682 |
// |
| 683 |
// Description: |
| 684 |
// Pause the game. |
| 685 |
// |
| 686 |
// Use: public |
| 687 |
|
| 688 |
void |
| 689 |
TsField::pauseGame() |
| 690 |
|
| 691 |
// |
| 692 |
//////////////////////////////////////////////////////////////////////// |
| 693 |
{ |
| 694 |
gamePaused = TRUE; |
| 695 |
|
| 696 |
// Unschedule any sensors saving which was on. |
| 697 |
if (trickleSensor->isScheduled()) { |
| 698 |
trickleOn = TRUE; |
| 699 |
trickleSensor->unschedule(); |
| 700 |
} |
| 701 |
if (removalSensor->isScheduled()) { |
| 702 |
removalOn = TRUE; |
| 703 |
removalSensor->unschedule(); |
| 704 |
} |
| 705 |
if (shiftSensor->isScheduled()) { |
| 706 |
shiftOn = TRUE; |
| 707 |
shiftSensor->unschedule(); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
//////////////////////////////////////////////////////////////////////// |
| 712 |
// |
| 713 |
// Description: |
| 714 |
// Resume the game. |
| 715 |
// |
| 716 |
// Use: public |
| 717 |
|
| 718 |
void |
| 719 |
TsField::resumeGame() |
| 720 |
|
| 721 |
// |
| 722 |
//////////////////////////////////////////////////////////////////////// |
| 723 |
{ |
| 724 |
gamePaused = FALSE; |
| 725 |
|
| 726 |
// Reschedule any sensors. |
| 727 |
wallClock.setToTimeOfDay(); |
| 728 |
if (trickleOn) { |
| 729 |
trickleOn = FALSE; |
| 730 |
trickleSensor->schedule(); |
| 731 |
} |
| 732 |
if (removalOn) { |
| 733 |
removalOn = FALSE; |
| 734 |
removalSensor->schedule(); |
| 735 |
} |
| 736 |
if (shiftOn) { |
| 737 |
shiftOn = FALSE; |
| 738 |
shiftSensor->schedule(); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
//////////////////////////////////////////////////////////////////////// |
| 744 |
// |
| 745 |
// Description: |
| 746 |
// This routine adds a piece to the playing field. |
| 747 |
// |
| 748 |
// Use: public |
| 749 |
|
| 750 |
void |
| 751 |
TsField::addPiece() |
| 752 |
|
| 753 |
// |
| 754 |
//////////////////////////////////////////////////////////////////////// |
| 755 |
{ |
| 756 |
double rnum; |
| 757 |
int pieceNum, pieceOrientation; |
| 758 |
int i; |
| 759 |
|
| 760 |
rnum = drand48(); |
| 761 |
pieceNum = (int)(rnum * (float)numPieceTypes); |
| 762 |
rnum = drand48(); |
| 763 |
pieceOrientation = (int)(rnum*24.0); |
| 764 |
|
| 765 |
currentPiece = (TsPiece *)pieceList[pieceNum]; |
| 766 |
|
| 767 |
// |
| 768 |
// Construct the scene graph for the new piece by adding children |
| 769 |
// to the fallingPiece separator for each block in the new piece. |
| 770 |
// Add or remove a 4th child depending on the number of blocks in the |
| 771 |
// new piece. |
| 772 |
// |
| 773 |
currentPiece->setOrientation (pieceOrientation); |
| 774 |
numPieceBlocks = currentPiece->getPosition (piecePositions); |
| 775 |
|
| 776 |
if (fallingPiece->getNumChildren() == 4) |
| 777 |
{ |
| 778 |
if (numPieceBlocks == 3) |
| 779 |
fallingPiece->removeChild (3); |
| 780 |
} |
| 781 |
else if (numPieceBlocks == 4) |
| 782 |
fallingPiece->addChild (pieceChild[3]); |
| 783 |
|
| 784 |
// |
| 785 |
// Set the switch nodes for each block to point to the correct translation |
| 786 |
// in the standardCube. Also, find the lowest block and position the |
| 787 |
// grid below it. |
| 788 |
// |
| 789 |
SoSwitch *tmpSwitch; |
| 790 |
int lowest = 26; |
| 791 |
for (i=0; i<numPieceBlocks; i++) |
| 792 |
{ |
| 793 |
tmpSwitch = (SoSwitch *)((SoGroup *)(pieceChild[i]))->getChild(0); |
| 794 |
tmpSwitch->whichChild = (int32_t)piecePositions[i]; |
| 795 |
if (piecePositions[i] < lowest) |
| 796 |
lowest = piecePositions[i]; |
| 797 |
} |
| 798 |
gridTranslation->translation.setValue (0.0, 2.0*(lowest/9), 0.0); |
| 799 |
|
| 800 |
// |
| 801 |
// Set the beginning position of the standard cube. |
| 802 |
// |
| 803 |
currentFraction = 2.0; |
| 804 |
currentOrientation = pieceOrientation; |
| 805 |
standardPosition[TS_X_AXIS] = 1; |
| 806 |
standardPosition[TS_Y_AXIS] = totalHeight; |
| 807 |
standardPosition[TS_Z_AXIS] = 1; |
| 808 |
|
| 809 |
pieceYTranslation->translation.setValue ( |
| 810 |
0.0, (float)(2*totalHeight) + currentFraction, 0.0); |
| 811 |
pieceXZTranslation->translation.setValue ( |
| 812 |
(float)(-resolution+2*1.0), 0.0, (float)(resolution-2*1.0)); |
| 813 |
|
| 814 |
// |
| 815 |
// Start a clock going to time how long it takes a piece to fall. |
| 816 |
// This time will be used to help keep score. |
| 817 |
// |
| 818 |
fallingClock.setToTimeOfDay(); |
| 819 |
|
| 820 |
// |
| 821 |
// Restart the timer sensor for this piece |
| 822 |
// |
| 823 |
wallClock.setToTimeOfDay(); |
| 824 |
trickleSensor->schedule(); |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
//////////////////////////////////////////////////////////////////////// |
| 829 |
// |
| 830 |
// Description: |
| 831 |
// This routine rotates the piece around the given axis by a |
| 832 |
// given amount. |
| 833 |
// |
| 834 |
// Use: public |
| 835 |
|
| 836 |
void |
| 837 |
TsField::rotation( int axis, int amount ) |
| 838 |
|
| 839 |
// |
| 840 |
//////////////////////////////////////////////////////////////////////// |
| 841 |
{ |
| 842 |
int newOrientation; |
| 843 |
|
| 844 |
if (isDropping) return; |
| 845 |
|
| 846 |
// Use the orientationTable to find a new orientation. |
| 847 |
newOrientation = orientationTable[currentOrientation][axis]; |
| 848 |
|
| 849 |
if (amount < 0) |
| 850 |
{ |
| 851 |
// |
| 852 |
// Since the orientation table only stores positive rotations, |
| 853 |
// a negative rotation is made by three successive positive rotations. |
| 854 |
// So make two more positive rotations. |
| 855 |
// |
| 856 |
newOrientation = orientationTable[newOrientation][axis]; |
| 857 |
newOrientation = orientationTable[newOrientation][axis]; |
| 858 |
} |
| 859 |
|
| 860 |
rotatePiece (newOrientation); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
//////////////////////////////////////////////////////////////////////// |
| 865 |
// |
| 866 |
// Description: |
| 867 |
// Rotate the current piece into the given orientation. Checks |
| 868 |
// against the boundaries of the field and the stack of blocks must |
| 869 |
// be made before the piece can actually be rotated. |
| 870 |
// |
| 871 |
// Use: public |
| 872 |
|
| 873 |
void |
| 874 |
TsField::rotatePiece( int newOrientation ) |
| 875 |
|
| 876 |
// |
| 877 |
//////////////////////////////////////////////////////////////////////// |
| 878 |
{ |
| 879 |
int localPos[4], fieldPos[4]; |
| 880 |
int i; |
| 881 |
|
| 882 |
// Get the block positions of the current piece given the new orientation |
| 883 |
(void)currentPiece->getPosition (newOrientation, localPos); |
| 884 |
|
| 885 |
// Check the piece against the field boundaries. |
| 886 |
if (!(validPosition (numPieceBlocks, localPos, standardPosition))) return; |
| 887 |
|
| 888 |
// Check the piece against the stack of static blocks. |
| 889 |
getFieldPositions (numPieceBlocks, localPos, fieldPos, standardPosition); |
| 890 |
if (collision (numPieceBlocks, fieldPos)) return; |
| 891 |
|
| 892 |
// Set the switch nodes for the current piece based on the new positions |
| 893 |
// Adjust the grid so it is at the bottom of the piece. |
| 894 |
SoSwitch *tmpSwitch; |
| 895 |
int lowest = 26; |
| 896 |
for (i=0; i<numPieceBlocks; i++) |
| 897 |
{ |
| 898 |
tmpSwitch = (SoSwitch *)((SoGroup *)(pieceChild[i]))->getChild(0); |
| 899 |
tmpSwitch->whichChild = (int32_t)localPos[i]; |
| 900 |
if (localPos[i] < lowest) |
| 901 |
lowest = localPos[i]; |
| 902 |
} |
| 903 |
gridTranslation->translation.setValue (0.0, 2.0*(lowest/9), 0.0); |
| 904 |
|
| 905 |
currentPiece->setOrientation (newOrientation); |
| 906 |
currentOrientation = newOrientation; |
| 907 |
} |
| 908 |
|
| 909 |
//////////////////////////////////////////////////////////////////////// |
| 910 |
// |
| 911 |
// Description: |
| 912 |
// This routine translates the piece along the given axis by the |
| 913 |
// given amount. |
| 914 |
// |
| 915 |
// Use: public |
| 916 |
|
| 917 |
void |
| 918 |
TsField::translation( int axis, int amount ) |
| 919 |
|
| 920 |
// |
| 921 |
//////////////////////////////////////////////////////////////////////// |
| 922 |
{ |
| 923 |
int newStandard[3]; |
| 924 |
int localPos[4], fieldPos[4]; |
| 925 |
|
| 926 |
if (isDropping) return; |
| 927 |
|
| 928 |
const SbVec3f &xlate = pieceXZTranslation->translation.getValue (); |
| 929 |
SbVec3f newXlate( xlate[0], xlate[1], xlate[2] ); |
| 930 |
|
| 931 |
// Translate the standard cube position. |
| 932 |
newStandard[TS_X_AXIS] = standardPosition[TS_X_AXIS]; |
| 933 |
newStandard[TS_Y_AXIS] = standardPosition[TS_Y_AXIS]; |
| 934 |
newStandard[TS_Z_AXIS] = standardPosition[TS_Z_AXIS]; |
| 935 |
newStandard[axis] += amount; |
| 936 |
|
| 937 |
// Get the block positions of the current piece |
| 938 |
(void)currentPiece->getPosition (localPos); |
| 939 |
|
| 940 |
// Check the piece against the field boundaries at the new position of |
| 941 |
// the standard cube. |
| 942 |
if (!(validPosition (numPieceBlocks, localPos, newStandard))) return; |
| 943 |
|
| 944 |
// Check the piece against the stack of static blocks. |
| 945 |
getFieldPositions (numPieceBlocks, localPos, fieldPos, newStandard); |
| 946 |
if (collision (numPieceBlocks, fieldPos)) return; |
| 947 |
|
| 948 |
// |
| 949 |
// The piece may be translated. Set the new position of the standard |
| 950 |
// cube and modify the XZ translation node. |
| 951 |
// |
| 952 |
standardPosition[axis] += amount; |
| 953 |
if (axis == TS_Z_AXIS) |
| 954 |
newXlate[axis] += -2.0 * (float)amount; |
| 955 |
else |
| 956 |
newXlate[axis] += 2.0 * (float)amount; |
| 957 |
pieceXZTranslation->translation.setValue (newXlate); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
//////////////////////////////////////////////////////////////////////// |
| 962 |
// |
| 963 |
// Description: |
| 964 |
// This routine drops the piece as the result of the spacebar |
| 965 |
// being pressed. |
| 966 |
// |
| 967 |
// Use: private |
| 968 |
|
| 969 |
void |
| 970 |
TsField::dropPiece() |
| 971 |
|
| 972 |
// |
| 973 |
//////////////////////////////////////////////////////////////////////// |
| 974 |
{ |
| 975 |
if (isDropping) return; |
| 976 |
|
| 977 |
isDropping = TRUE; |
| 978 |
saveRate = currentRate; |
| 979 |
currentRate = DROP_RATE; |
| 980 |
|
| 981 |
// Award some points based on where the piece was dropped from. |
| 982 |
pointsEarned = standardPosition[TS_Y_AXIS] * 2; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
//////////////////////////////////////////////////////////////////////// |
| 987 |
// |
| 988 |
// Description: |
| 989 |
// This routine adds points to the score and checks to see if |
| 990 |
// the skill level should be incremented. |
| 991 |
// |
| 992 |
// Use: private |
| 993 |
|
| 994 |
void |
| 995 |
TsField::addPoints( int points ) |
| 996 |
|
| 997 |
// |
| 998 |
//////////////////////////////////////////////////////////////////////// |
| 999 |
{ |
| 1000 |
char tmpStr[12]; |
| 1001 |
|
| 1002 |
totalScore += points * 10; |
| 1003 |
sprintf(tmpStr, "%d", totalScore); |
| 1004 |
overScore->string.set1Value(1, SbString(tmpStr)); |
| 1005 |
|
| 1006 |
pointsNeeded -= points * 10; |
| 1007 |
if ((pointsNeeded < 0) && (skillLevel < 7)) { |
| 1008 |
skillLevel++; |
| 1009 |
pointsNeeded += POINTS_PER_SKILL_LEVEL; |
| 1010 |
currentRate = skillRates[skillLevel]; |
| 1011 |
sprintf(tmpStr, "%d", skillLevel+1); |
| 1012 |
overScore->string.set1Value(0, SbString(tmpStr)); |
| 1013 |
if (skillLevel == 1) |
| 1014 |
numPieceTypes = 4; |
| 1015 |
else if (skillLevel == 2) |
| 1016 |
numPieceTypes = 6; |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
//////////////////////////////////////////////////////////////////////// |
| 1021 |
// |
| 1022 |
// Description: |
| 1023 |
// This routine creates the data for the floor. It consists of a |
| 1024 |
// grid and a polygon. |
| 1025 |
// |
| 1026 |
// Use: private |
| 1027 |
|
| 1028 |
void |
| 1029 |
TsField::createFloorAndGrid() |
| 1030 |
|
| 1031 |
// |
| 1032 |
//////////////////////////////////////////////////////////////////////// |
| 1033 |
{ |
| 1034 |
SoCoordinate3 *gridCoords; |
| 1035 |
SoMaterial *floorMtl; |
| 1036 |
SoIndexedLineSet *floorGrid; |
| 1037 |
SoTranslation *floorXform; |
| 1038 |
SoNormal *floorNormal; |
| 1039 |
SbVec3f *points, tcoord; |
| 1040 |
float val1, val2; |
| 1041 |
int32_t *indices; |
| 1042 |
int i, ct; |
| 1043 |
|
| 1044 |
floor = new SoSeparator(); |
| 1045 |
grid = new SoSeparator(); |
| 1046 |
floor->ref(); |
| 1047 |
grid->ref(); |
| 1048 |
|
| 1049 |
// |
| 1050 |
// Build the set of points describing the grid, and put them in a |
| 1051 |
// SoCoordinate3 node. |
| 1052 |
// |
| 1053 |
points = new SbVec3f[4*resolution]; |
| 1054 |
tcoord.setValue ((float)(-resolution), 0.0, (float)(resolution)); |
| 1055 |
for (i=0; i<=resolution; i++) |
| 1056 |
{ |
| 1057 |
points[i] = tcoord; |
| 1058 |
tcoord[0] += 2.0; |
| 1059 |
} |
| 1060 |
tcoord[0] = (float)(-resolution); |
| 1061 |
tcoord[2] = (float)resolution - 2.0; |
| 1062 |
val1 = (float)(-resolution); |
| 1063 |
val2 = (float)resolution; |
| 1064 |
for (i=0; i<(2*(resolution-1));) |
| 1065 |
{ |
| 1066 |
points[resolution+1+i++] = tcoord; |
| 1067 |
tcoord[0] = val2; |
| 1068 |
points[resolution+1+i++] = tcoord; |
| 1069 |
tcoord[0] = val1; |
| 1070 |
tcoord[2] -= 2.0; |
| 1071 |
} |
| 1072 |
for (i=3*resolution-1; i<4*resolution; i++) |
| 1073 |
{ |
| 1074 |
points[i] = tcoord; |
| 1075 |
tcoord[0] += 2.0; |
| 1076 |
} |
| 1077 |
gridCoords = new SoCoordinate3(); |
| 1078 |
gridCoords->point.setValues (0, 4*resolution, points); |
| 1079 |
grid->addChild (gridCoords); |
| 1080 |
delete[] points; |
| 1081 |
|
| 1082 |
// |
| 1083 |
// Create nodes representing the color and linewidth of the grid |
| 1084 |
// |
| 1085 |
SoBaseColor *gridColor = new SoBaseColor(); |
| 1086 |
SoDrawStyle *gridStyle = new SoDrawStyle(); |
| 1087 |
SoLightModel *lightModel = new SoLightModel(); |
| 1088 |
grid->addChild (gridStyle); |
| 1089 |
grid->addChild (lightModel); |
| 1090 |
gridColor->rgb.setValue (0.8, 0.8, 0.8); |
| 1091 |
gridStyle->style = SoDrawStyle::LINES; |
| 1092 |
gridStyle->lineWidth = 4; |
| 1093 |
lightModel->model = SoLightModel::BASE_COLOR; |
| 1094 |
|
| 1095 |
// |
| 1096 |
// Create one normal pointing up to be used by both the grid and the |
| 1097 |
// polygon. |
| 1098 |
// |
| 1099 |
tcoord.setValue (0.0, 1.0, 0.0); |
| 1100 |
floorNormal = new SoNormal(); |
| 1101 |
floorNormal->vector.setValues (0, 1, &tcoord); |
| 1102 |
grid->addChild (floorNormal); |
| 1103 |
|
| 1104 |
// |
| 1105 |
// create an SoIndexedLineSet node and fill it with indices into the |
| 1106 |
// coordinate list. |
| 1107 |
// |
| 1108 |
indices = new int32_t[(resolution+1)*2*3]; |
| 1109 |
indices[0] = 0; |
| 1110 |
indices[1] = resolution; |
| 1111 |
indices[2] = -1; |
| 1112 |
ct = 3; |
| 1113 |
for (i=resolution+1; i<3*resolution-1;) |
| 1114 |
{ |
| 1115 |
indices[ct++] = i++; |
| 1116 |
indices[ct++] = i++; |
| 1117 |
indices[ct++] = -1; |
| 1118 |
} |
| 1119 |
indices[ct++] = 3*resolution-1; |
| 1120 |
indices[ct++] = 4*resolution-1; |
| 1121 |
indices[ct++] = -1; |
| 1122 |
for (i=0; i<=resolution; i++) |
| 1123 |
{ |
| 1124 |
indices[ct++] = i; |
| 1125 |
indices[ct++] = i + 3*resolution-1; |
| 1126 |
indices[ct++] = -1; |
| 1127 |
} |
| 1128 |
floorGrid = new SoIndexedLineSet(); |
| 1129 |
floorGrid->coordIndex.setValues (0, ct, indices); |
| 1130 |
grid->addChild (floorGrid); |
| 1131 |
|
| 1132 |
// |
| 1133 |
// create the material and transform for the floor polygon. The polygon |
| 1134 |
// lowered slightly beneath the grid, so no coplanar anomalies will exist. |
| 1135 |
// |
| 1136 |
floor->addChild (gridColor); |
| 1137 |
floor->addChild (grid); |
| 1138 |
floorMtl = new SoMaterial(); |
| 1139 |
floorMtl->ambientColor.setValue (0.07, 0.5, 0.1); |
| 1140 |
floorMtl->diffuseColor.setValue (0.07, 0.5, 0.1); |
| 1141 |
floorMtl->specularColor.setValue (0.07, 0.5, 0.1); |
| 1142 |
floorMtl->shininess.setValue (10.0); |
| 1143 |
floorXform = new SoTranslation(); |
| 1144 |
floorXform->translation.setValue (0.0, -0.03, 0.0); |
| 1145 |
floor->addChild (floorMtl); |
| 1146 |
floor->addChild (floorXform); |
| 1147 |
|
| 1148 |
// Create 4 coordinates for the floor; |
| 1149 |
points = new SbVec3f[4]; |
| 1150 |
tcoord.setValue ((float)(-resolution), 0.0, (float)(resolution)); |
| 1151 |
points[0] = tcoord; |
| 1152 |
tcoord[0] = (float)resolution; |
| 1153 |
points[1] = tcoord; |
| 1154 |
tcoord[2] = (float)-resolution; |
| 1155 |
points[2] = tcoord; |
| 1156 |
tcoord[0] = (float)-resolution; |
| 1157 |
points[3] = tcoord; |
| 1158 |
SoCoordinate3 *floorCoords = new SoCoordinate3(); |
| 1159 |
floorCoords->point.setValues (0, 4, points); |
| 1160 |
floor->addChild (floorCoords); |
| 1161 |
floor->addChild (floorNormal); |
| 1162 |
SoNormalBinding *normBind = new SoNormalBinding; |
| 1163 |
floor->addChild (normBind); |
| 1164 |
normBind->value = SoNormalBinding::OVERALL; |
| 1165 |
delete[] points; |
| 1166 |
|
| 1167 |
// |
| 1168 |
// create an SoIndexedFaceSet node and fill it with indices into |
| 1169 |
// the coordinate list. |
| 1170 |
// |
| 1171 |
indices[0] = 0; |
| 1172 |
indices[1] = 1; |
| 1173 |
indices[2] = 2; |
| 1174 |
indices[3] = 3; |
| 1175 |
SoIndexedFaceSet *floorPoly = new SoIndexedFaceSet (); |
| 1176 |
floorPoly->coordIndex.setValues (0, 4, indices); |
| 1177 |
floor->addChild (floorPoly); |
| 1178 |
} |
| 1179 |
|
| 1180 |
//////////////////////////////////////////////////////////////////////// |
| 1181 |
// |
| 1182 |
// Description: |
| 1183 |
// This routine builds the scene graph which renders the help |
| 1184 |
// message. |
| 1185 |
// |
| 1186 |
// Use: private |
| 1187 |
|
| 1188 |
void |
| 1189 |
TsField::buildHelpMessage(SoSeparator *root) |
| 1190 |
|
| 1191 |
// |
| 1192 |
//////////////////////////////////////////////////////////////////////// |
| 1193 |
{ |
| 1194 |
SoFont *helpFont = new SoFont; |
| 1195 |
SoColorIndex *helpColor = new SoColorIndex; |
| 1196 |
SoText2 *helpText = new SoText2; |
| 1197 |
SoTranslation *helpXfm = new SoTranslation; |
| 1198 |
|
| 1199 |
helpRoot = new SoSeparator; |
| 1200 |
helpRoot->ref(); |
| 1201 |
|
| 1202 |
helpGroup = new SoGroup; |
| 1203 |
helpGroup->ref(); |
| 1204 |
helpRoot->addChild(helpGroup); |
| 1205 |
|
| 1206 |
helpFont->size = 20; |
| 1207 |
helpColor->index = 3; |
| 1208 |
helpXfm->translation.setValue(SbVec3f(-0.9, 0.7, 0.0)); |
| 1209 |
for (int i=0; i<NUM_HELP_LINES; i++) |
| 1210 |
helpText->string.set1Value(i, SbString(helpMessage[i])); |
| 1211 |
helpGroup->addChild(helpFont); |
| 1212 |
helpGroup->addChild(helpColor); |
| 1213 |
helpGroup->addChild(helpXfm); |
| 1214 |
helpGroup->addChild(helpText); |
| 1215 |
|
| 1216 |
root->addChild(helpRoot); |
| 1217 |
} |