📄 cubes.cpp.in
字号:
redmat->specularColor.setValue(1.0, 1.0, 1.0); bluemat->diffuseColor.setValue(0.0, 0.0, 1.0); bluemat->specularColor.setValue(1.0, 1.0, 1.0); tokenTrans->translation.setValue(0,4.75,0); SoSeparator *redsep; SoSeparator *bluesep; SoMaterial *redmats = new SoMaterial; redmats->diffuseColor.setValue(1.0, 0.0, 0.0); redmats->specularColor.setValue(1.0, 1.0, 1.0); board->addChild(redmats); redsep = new SoSeparator; board->addChild(redsep); SoMaterial *bluemats = new SoMaterial; bluemats->diffuseColor.setValue(0.0, 0.0, 1.0); bluemats->specularColor.setValue(1.0, 1.0, 1.0); board->addChild(bluemats); bluesep = new SoSeparator; board->addChild(bluesep); SoSwitch *sw; SbVec3f prevTrans(0,0,0); SbVec3f newTrans(0,0,0); for (int k=0;k<4;k++) for (int j=0;j<4;j++) for (int i=0;i<4;i++) { newTrans.setValue(i*2-3, k+0.75, -j*2+3); newTrans-=prevTrans; sw = new SoSwitch; redswitches[i+j*4+k*16] = sw; SoTranslation *tt = new SoTranslation; tt->translation.setValue(newTrans); redsep->addChild(tt); SoSphere *s = new SoSphere; s->radius.setValue(0.5); sw->addChild(s); redsep->addChild(sw); sw = new SoSwitch; blueswitches[i+j*4+k*16] = sw; tt = new SoTranslation; tt->translation.setValue(newTrans); bluesep->addChild(tt); s = new SoSphere; s->radius.setValue(0.5); sw->addChild(s); bluesep->addChild(sw); prevTrans+=newTrans; } timecounter = new SoTimeCounter; timecounter->frequency.setValue(2); timecounter->on.setValue(false); SoOnOff *onoff = new SoOnOff; onoff->toggle.connectFrom(&timecounter->syncOut); selectone = new SoSelectOne(SoMFInt32::getClassTypeId()); ((SoMFInt32 *)selectone->input)->set1Value(0,SO_SWITCH_NONE); ((SoMFInt32 *)selectone->input)->set1Value(1,SO_SWITCH_ALL); selectone->index.connectFrom(&onoff->isOn); return board;}SoSeparator *create_winnerboard(int i0, int i1, int i2, int i3){ SoSeparator *board = new SoSeparator; SoRotation *rot = new SoRotation; rot->rotation = SbRotation(SbVec3f(1,0,0),30*M_PI/180); board->addChild(rot); SoRotor *rotor = new SoRotor; rotor->rotation = SbRotation(SbVec3f(0, 1, 0), 1); rotor->speed = 0.1f; board->addChild(rotor); // Bottom plate SoMaterial *mat = new SoMaterial; mat->diffuseColor.setValue(0.44,0.20,0.11); mat->specularColor.setValue(0.71,0.33,0.18); mat->transparency.setValue(0.7); board->addChild(mat); SoCube *plate = new SoCube; plate->height.setValue(0.5); plate->width.setValue(10); plate->depth.setValue(10); board->addChild(plate); // Add cylinders SoSeparator *cylsep = new SoSeparator; board->addChild(cylsep); mat = new SoMaterial; mat->specularColor.setValue(1.0f, 0.0f, 0.0f); mat->emissiveColor.setValue(0.5f, 0.3f, 0.0f); mat->transparency.setValue(0.7); mat->shininess = 0.8f; cylsep->addChild(mat); SoCylinder *cyl = new SoCylinder; cyl->radius.setValue(0.05); cyl->height.setValue(4.25); SbVec3f move; move.setValue(-3,2.25,-3); for (int j=0;j<4;j++) { for (int i=0;i<4;i++) { SoTranslation *trans = new SoTranslation; trans->translation.setValue(move); cylsep->addChild(trans); cylsep->addChild(cyl); move.setValue(2,0,0); } move.setValue(-6,0,2); } SoMaterial *redmats = new SoMaterial; redmats->diffuseColor.setValue(1.0, 0.0, 0.0); redmats->specularColor.setValue(1.0, 1.0, 1.0); redmats->transparency.setValue(0.7); board->addChild(redmats); int idx[4] = {i0,i1,i2,i3}; SbVec3f prevTrans(0,0,0); SbVec3f newTrans(0,0,0); for (int a=0;a<4;a++) { int i = idx[a]&0x03; int j = (idx[a]&0x0c)>>2; int k = (idx[a]&0x30)>>4; newTrans.setValue(i*2-3, k+0.75, -j*2+3); newTrans-=prevTrans; SoTranslation *t0 = new SoTranslation; t0->translation.setValue(newTrans); board->addChild(t0); SoSphere *s = new SoSphere; s->radius.setValue(0.5); board->addChild(s); prevTrans+=newTrans; } return board;}// void draggerCB(void *data, SoDragger *dragger)// {// SoTranslate1Dragger *drg = (SoTranslate1Dragger *)dragger;// printf("Dragger: %f\n",drg->translation.getValue()[0]);// }SoSeparator *create_helpscene(){ SoSeparator *root = new SoSeparator; // Callback for keyboard events SoEventCallback *cb = new SoEventCallback; cb->addEventCallback(SoKeyboardEvent::getClassTypeId(), help_key_cb, NULL); root->addChild(cb); SoPerspectiveCamera *cam = new SoPerspectiveCamera; root->addChild(cam); SoDirectionalLight *light= new SoDirectionalLight; root->addChild(light); SoSeparator *boards = new SoSeparator; root->addChild(boards); SoTranslation *trans = new SoTranslation; trans->translation.setValue(0,7.5,0); boards->addChild(trans); SoSeparator *b1 = create_winnerboard(4,5,6,7); boards->addChild(b1); trans = new SoTranslation; trans->translation.setValue(15,0,0); boards->addChild(trans); b1 = create_winnerboard(5,21,37,53); boards->addChild(b1); trans = new SoTranslation; trans->translation.setValue(15,0,0); boards->addChild(trans); b1 = create_winnerboard(16,21,26,31); boards->addChild(b1); trans = new SoTranslation; trans->translation.setValue(-30,-15,0); boards->addChild(trans); b1 = create_winnerboard(8,25,42,59); boards->addChild(b1); trans = new SoTranslation; trans->translation.setValue(15,0,0); boards->addChild(trans); b1 = create_winnerboard(0,21,42,63); boards->addChild(b1); trans = new SoTranslation; trans->translation.setValue(15,0,0); boards->addChild(trans); b1 = create_winnerboard(44,45,46,47); boards->addChild(b1); float dist=50; cam->position.setValue(15,0,dist); cam->farDistance.setValue(1000.0f); cam->nearDistance.setValue(0.1f); cam->focalDistance.setValue(dist); // 2D instructions SoSeparator *instructsep = new SoSeparator; root->addChild(instructsep); // ...with its own camera SoOrthographicCamera *ocam = new SoOrthographicCamera; //FIXME: Uncommented is workaround for camera/text2 problem (gcc-2.96)// ocam->position.setValue(0,0,-10); ocam->viewportMapping.setValue(SoCamera::LEAVE_ALONE); ocam->nearDistance.setValue(0.1); ocam->farDistance.setValue(100); instructsep->addChild(ocam); const char *str[] = { "Instructions\n", "============\n", "\n", "1 - Start 1-player game (player vs. computer)\n", "2 - Start 2-player game\n", "Backspace - Undo last move\n", "Arrows/Mouse - Move token\n", "Enter/Left button - Place token\n", "Q - Quit\n", "\n", "Each player takes turn on placing a token of his color\n", "on the board. The token can be placed on any of the 16\n", "nails on the board. A placed token will drop down on the\n", "board on top of any tokens previously placed on the same nail.\n", "The maximum building height is 4 tokens, making the board a\n", "4x4x4 cube.\n", "The objective of the game is to obtain 4 tokens of your color\n", "in a row. Any constellation of tokens making up a straight line\n", "is a winner; diagonals both in-plane and in 3D are winners.\n", "\n", "This screen shows example winning combinations.\n", "\n", "(Press any key to return)\n", }; for (unsigned int i=0;i<sizeof(str)/sizeof(char*);i++) { out.out(str[i]); } out.render( ); // Add texture SoTexture2 *consol_texture = new SoTexture2; int xsize = out.getTextureXSize(); int ysize = out.getTextureYSize(); consol_texture->image.setValue(SbVec2s(xsize,ysize), 4, (const unsigned char *)out.getTexture()); instructsep->addChild(consol_texture); SoCoordinate3 * coord = new SoCoordinate3; instructsep->addChild(coord); float aspect = out.getAspectRatio(); ocam->aspectRatio.setValue(aspect); coord->point.set1Value(0, SbVec3f(-1.0*aspect, -1.0, 0)); coord->point.set1Value(1, SbVec3f( 1.0*aspect, -1.0, 0)); coord->point.set1Value(2, SbVec3f( 1.0*aspect, 1.0, 0)); coord->point.set1Value(3, SbVec3f(-1.0*aspect, 1.0, 0)); // Define the consol texture coordinates SoTextureCoordinate2 *tex_coord = new SoTextureCoordinate2; instructsep->addChild( tex_coord ); tex_coord->point.set1Value(0, SbVec2f(0.0f, 1.0f)); tex_coord->point.set1Value(1, SbVec2f(1.0f, 1.0f)); tex_coord->point.set1Value(2, SbVec2f(1.0f, 0.0f)); tex_coord->point.set1Value(3, SbVec2f(0.0f, 0.0f)); // Define texture coordinate bindings SoTextureCoordinateBinding * tex_bind = new SoTextureCoordinateBinding; instructsep->addChild(tex_bind); tex_bind->value.setValue(SoTextureCoordinateBinding::PER_VERTEX); // Define a face set SoFaceSet *face_set = new SoFaceSet; instructsep->addChild(face_set); face_set->numVertices.setValue(4); return root;}intmain(int argc, char **argv){ @WIDGET@ window = So@Gui@::init(argv[0]); if (window == NULL) exit(1); game = new GameLogic; SoSeparator *root = new SoSeparator; root->ref(); sceneswitch = new SoSwitch; root->addChild(sceneswitch); SoSeparator *gameroot = new SoSeparator; sceneswitch->addChild(gameroot); SoSeparator *helpscene = create_helpscene(); sceneswitch->addChild(helpscene); sceneswitch->whichChild.setValue(0); SoDirectionalLight *light= new SoDirectionalLight; gameroot->addChild(light); // Callback for keyboard events SoEventCallback *cb = new SoEventCallback; cb->addEventCallback(SoKeyboardEvent::getClassTypeId(), event_cb, NULL); cb->addEventCallback(SoLocation2Event::getClassTypeId(), mouse_event_cb, NULL); cb->addEventCallback(SoMouseButtonEvent::getClassTypeId(), button_event_cb, NULL); gameroot->insertChild(cb, 0); SoPerspectiveCamera *cam = new SoPerspectiveCamera; gameroot->addChild(cam); // Setup draggers SoSeparator *draggersep = new SoSeparator; gameroot->addChild(draggersep); SoTranslation *dtrans = new SoTranslation; dtrans->translation.setValue(-0.6,0,0); draggersep->addChild(dtrans); SoTranslate1Dragger *xdragger = new SoTranslate1Dragger; xdragger->translation.setValue(0.6,-3,10);// xdragger->addValueChangedCallback(draggerCB,NULL); draggersep->addChild(xdragger); SoRotation *drot = new SoRotation; drot->rotation.setValue(SbVec3f(0,0,1),-90*M_PI/180); draggersep->addChild(drot); dtrans = new SoTranslation; dtrans->translation.setValue(2.4,0,0); draggersep->addChild(dtrans); SoTranslate1Dragger *ydragger = new SoTranslate1Dragger; ydragger->translation.setValue(0.6,0.6,10);// ydragger->addValueChangedCallback(draggerCB,NULL); draggersep->addChild(ydragger); // Engines for connecting draggers to rotation nodes SoDecomposeVec3f *decomp = new SoDecomposeVec3f; decomp->ref(); decomp->vector.connectFrom(&xdragger->translation); SoComposeRotation *comprot = new SoComposeRotation; comprot->ref(); comprot->axis.setValue(0,1,0); comprot->angle.connectFrom(&decomp->x); SoDecomposeVec3f *ydecomp = new SoDecomposeVec3f; ydecomp->ref(); ydecomp->vector.connectFrom(&ydragger->translation); SoComposeRotation *ycomprot = new SoComposeRotation; ycomprot->ref(); ycomprot->axis.setValue(1,0,0); ycomprot->angle.connectFrom(&ydecomp->x); // Board SoSeparator *boardsep = create_board(&comprot->rotation,&ycomprot->rotation); gameroot->addChild(boardsep); // Sets up the examiner viewer viewer = new So@Gui@RenderArea(window);// viewer = new So@Gui@ExaminerViewer(window); viewer->setBackgroundColor( SbColor( 0.37f, 0.61f, 0.27f ) ); viewer->setSceneGraph(root); viewer->setTitle("Cubes");// viewer->setAnimationEnabled(false);// viewer->setViewing(false); viewer->setTransparencyType(SoGLRenderAction::BLEND); // Places the camera correctly float dist=20; cam->position.setValue(0,0,dist); cam->farDistance.setValue(1000.0f); cam->nearDistance.setValue(0.1f); cam->focalDistance.setValue(dist); // New camera for the Coin logo "legend" SoPerspectiveCamera *logocam = new SoPerspectiveCamera; gameroot->addChild(logocam); SoSeparator *logosep = new SoSeparator; gameroot->addChild(logosep); // To make it render in a custom viewport SoCallback *vpcb = new SoCallback(); vpcb->setCallback(setViewportCB, NULL); logosep->addChild(vpcb); SoRotor *logorot = new SoRotor; logorot->rotation = SbRotation(SbVec3f(0, 1, 0), 1); logorot->speed = 0.1f; logosep->addChild(logorot); SoTranslation *logotrans = new SoTranslation; logotrans->translation.setValue(100,0,0); SoMaterial *mat = new SoMaterial; mat->specularColor.setValue( 1.0f, 0.0f, 0.0f ); mat->emissiveColor.setValue( 0.5f, 0.3f, 0.0f ); mat->shininess = 0.8f; logosep->addChild(mat); SoInput in; in.setBuffer((void *)COIN_LOGO,strlen(COIN_LOGO)); SoNode *logo; SoDB::read(&in,logo); logosep->addChild(logo); // FIXME: Place camera manually? SbViewportRegion reg(100,100); logocam->viewAll(logosep,reg); // 2D instructions SoSeparator *instructsep = new SoSeparator; gameroot->addChild(instructsep); // ...with its own camera SoOrthographicCamera *ocam = new SoOrthographicCamera; //FIXME: Uncommented is workaround for camera/text2 problem (gcc-2.96)// ocam->orientation.setValue(SbRotation(SbVec3f(0,1,0),M_PI)); ocam->viewportMapping.setValue(SoCamera::LEAVE_ALONE); ocam->nearDistance.setValue(0.1); instructsep->addChild(ocam); SoTranslation *instructtrans = new SoTranslation; instructsep->addChild(instructtrans); SoText2 *instructions = new SoText2; const char *str[] = { "For instructions, press 'h'" }; instructions->string.setValues(0, sizeof(str) / sizeof(char *), str); instructions->justification = SoText2::LEFT; instructsep->addChild(instructions); SbViewVolume vv = ocam->getViewVolume(); SbVec3f p = vv.getPlanePoint(0.0f, SbVec2f(0.0f, 0.95f)); instructtrans->translation.setValue(p[0], p[1], 0); // Create path to intersection plane SoSearchAction search; search.setNode(isectfaceset); search.apply(gameroot); isectpath = new SoPath(*search.getPath()); isectpath->ref(); // Write scene to disk// SoOutput out;// out.openFile("test.iv");// SoWriteAction w(&out);// w.apply(gameroot);// out.closeFile(); // Get started// viewer->setDecoration(FALSE); viewer->show(); So@Gui@::show(window);#ifdef HAVE_SOCOMPONENT_SETFULLSCREEN // Must do this _after_ show() to avoid the "Start"-bar being // visible. (Bug!) (void)viewer->setFullScreen(TRUE);#endif // HAVE_SOCOMPONENT_SETFULLSCREEN So@Gui@::mainLoop(); delete viewer; root->unref(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -