⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smiley.cpp.in

📁 学习 open inventor 的例子
💻 IN
字号:
/**************************************************************************\ * *  This file is part of a set of example programs for the Coin library. *  Copyright (C) 2000-2003 by Systems in Motion. All rights reserved. * *                   <URL:http://www.coin3d.org> * *  This sourcecode can be redistributed and/or modified under the *  terms of the GNU General Public License version 2 as published by *  the Free Software Foundation. See the file COPYING at the root *  directory of the distribution for more details. * *  As a special exception, all sourcecode of the demo examples can be *  used for any purpose for licensees of the Coin Professional *  Edition License, without the restrictions of the GNU GPL. See our *  web pages for information about how to acquire a Professional Edition *  License. * *  Systems in Motion, <URL:http://www.sim.no>, <mailto:support@sim.no> *\**************************************************************************//* ********************************************************************** * * smiley * * Demo application for showcasing Coin. * * Written by Marius Kintel <kintel@coin3d.org>. * ********************************************************************** */#if HAVE_CONFIG_H#include <config.h>#endif // HAVE_CONFIG_H#include <stdlib.h> // exit()#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoPointSet.h>#include <Inventor/nodes/SoDrawStyle.h>#include <Inventor/nodes/SoDirectionalLight.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoCoordinate3.h>#include <Inventor/nodes/SoComplexity.h>#include <Inventor/nodes/SoTransform.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoSphere.h>#include <Inventor/nodes/SoCylinder.h>#include <Inventor/nodes/SoIndexedFaceSet.h>#include <Inventor/nodes/SoCallback.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoNurbsSurface.h>#include <Inventor/nodes/SoQuadMesh.h>#include <Inventor/nodes/SoTexture2.h>#include <Inventor/nodes/SoTexture2Transform.h>#include <Inventor/nodes/SoText3.h>#include <Inventor/engines/SoCalculator.h>#include <Inventor/engines/SoCompose.h>#include <Inventor/engines/SoTimeCounter.h>#include <Inventor/@Gui@/So@Gui@.h>#include <Inventor/@Gui@/So@Gui@RenderArea.h>#include <Inventor/@Gui@/viewers/So@Gui@ExaminerViewer.h>#define STAR_INNER_RAD 0.25float starpts[11][3] = {  { 0.000, 0.000, 0.2},  { 0.951, 0.309, 0},  { 0.000, 1.000, 0},  {-0.951, 0.309, 0},  {-0.588,-0.809, 0},  { 0.588,-0.809, 0},  { 0.588*STAR_INNER_RAD, 0.809*STAR_INNER_RAD, 0},  {-0.588*STAR_INNER_RAD, 0.809*STAR_INNER_RAD, 0},  {-0.951*STAR_INNER_RAD,-0.309*STAR_INNER_RAD, 0},  { 0.000*STAR_INNER_RAD,-1.000*STAR_INNER_RAD, 0},  { 0.951*STAR_INNER_RAD,-0.309*STAR_INNER_RAD, 0},};int starindices[40] = {  0,1,6,-1,  0,6,2,-1,  0,2,7,-1,  0,7,3,-1,  0,3,8,-1,  0,8,4,-1,  0,4,9,-1,  0,9,5,-1,  0,5,10,-1,  0,10,1,-1};float planepts[4][3] = {  {5,-2.2,-5},  {5,-2.2,5},  {-5,-2.2,-5},  {-5,-2.2,5}};float pts[11*7][3] = {  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0,-0.5,0},  {0.102341,-0.5,0.102341},  {0.144732,-0.5,0},  {0.102341,-0.5,-0.102341},  {0,-0.5,-0.144732},  {-0.102341,-0.5,-0.102341},  {-0.144732,-0.5,0},  {-0.102341,-0.5,0.102341},  {0,-0.5,0.144732},  {0.102341,-0.5,0.102341},  {0.144732,-0.5,0},  {0.102341,-0.5,-0.102341},  {0.307024,-0.391806,0.307024},  {0.434197,-0.391806,0},  {0.307024,-0.391806,-0.307024},  {0,-0.391806,-0.434197},  {-0.307024,-0.391806,-0.307024},  {-0.434197,-0.391806,0},  {-0.307024,-0.391806,0.307024},  {0,-0.391806,0.434197},  {0.307024,-0.391806,0.307024},  {0.434197,-0.391806,0},  {0.307024,-0.391806,-0.307024},  {0.434197,0,0.434197},  {0.614047,0,0},  {0.434197,0,-0.434197},  {0,0,-0.614047},  {-0.434197,0,-0.434197},  {-0.614047,0,0},  {-0.434197,0,0.434197},  {0,0,0.614047},  {0.434197,0,0.434197},  {0.614047,0,0},  {0.434197,0,-0.434197},  {0.307024,0.391806,0.307024},  {0.434197,0.391806,0},  {0.307024,0.391806,-0.307024},  {0,0.391806,-0.434197},  {-0.307024,0.391806,-0.307024},  {-0.434197,0.391806,0},  {-0.307024,0.391806,0.307024},  {0,0.391806,0.434197},  {0.307024,0.391806,0.307024},  {0.434197,0.391806,0},  {0.307024,0.391806,-0.307024},  {0.102341,0.5,0.102341},  {0.144732,0.5,0},  {0.102341,0.5,-0.102341},  {0,0.5,-0.144732},  {-0.102341,0.5,-0.102341},  {-0.144732,0.5,0},  {-0.102341,0.5,0.102341},  {0,0.5,0.144732},  {0.102341,0.5,0.102341},  {0.144732,0.5,0},  {0.102341,0.5,-0.102341},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0},  {0,0.5,0}};// The knot vectorsfloat uknots[15] = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};float vknots[11] = {2, 2, 2, 2,  3, 4, 5, 6,  6, 6, 6};SoCoordinate3 *controlPts;SoTransform *trans;SoMaterial *material;voidsphericalConv(float r, float theta, float phi,               float& x, float& y, float& z){  x = r * sin(phi) * cos(theta);  z = r * sin(phi) * sin(theta);  y = r * cos(phi);}voidmakeSurface(SoSeparator *root){  SoSeparator *smileysep = new SoSeparator;  trans = new SoTransform;  SoSeparator *surfSep = new SoSeparator;  SoComplexity  *complexity = new SoComplexity;  controlPts = new SoCoordinate3;  material = new SoMaterial;  material->diffuseColor.setValue(1,1,0);  material->specularColor.setValue(1,1,1);  material->ambientColor.setValue(0.7,0.7,0);  SoNurbsSurface  *surface  = new SoNurbsSurface;  complexity->value = 1.0;    controlPts->point.setValues(0, 11*7, pts);  surface->numUControlPoints = 11;  surface->numVControlPoints = 7;  surface->uKnotVector.setValues(0,15, uknots);  surface->vKnotVector.setValues(0,11, vknots);    surfSep->addChild(complexity);  surfSep->addChild(controlPts);  surfSep->addChild(material);  surfSep->addChild(surface);    //Create eyes  SoSeparator *facesep = new SoSeparator;  SoMaterial *mat = new SoMaterial;  mat->diffuseColor.setValue(0,0,0);  mat->specularColor.setValue(0,0,0);  mat->ambientColor.setValue(0,0,0);  SoSeparator *eyessep = new SoSeparator;  SoTranslation *e1trans = new SoTranslation;  e1trans->translation.setValue(SbVec3f(-0.15,0.25,0.4));  SoSphere *e1 = new SoSphere;  e1->radius.setValue(0.05);  SoTranslation *e2trans = new SoTranslation;  e2trans->translation.setValue(SbVec3f(0.30,0,0));  SoSphere *e2 = new SoSphere;  e2->radius.setValue(0.05);  eyessep->addChild(mat);  eyessep->addChild(e1trans);  eyessep->addChild(e1);  eyessep->addChild(e2trans);  eyessep->addChild(e2);  //Create mouth  SoSeparator *msep = new SoSeparator;  SoCoordinate3 *mcoord = new SoCoordinate3;  SoIndexedFaceSet *msmile = new SoIndexedFaceSet;  int i,idx=0;  for (i=0;i<15;i++) {    msmile->coordIndex.set1Value(idx++,i);    msmile->coordIndex.set1Value(idx++,i+16);    msmile->coordIndex.set1Value(idx++,i+17);    msmile->coordIndex.set1Value(idx++,-1);    msmile->coordIndex.set1Value(idx++,i+1);    msmile->coordIndex.set1Value(idx++,i);    msmile->coordIndex.set1Value(idx++,i+17);    msmile->coordIndex.set1Value(idx++,-1);  }  msep->addChild(mat);  msep->addChild(mcoord);  msep->addChild(msmile);   float px,py,x,y,z;  for (i=0;i<16;i++) {    px = 35 * cos((190+10*i)*M_PI/180) + 90; //theta    py = 90 - 35 * sin((190+10*i)*M_PI/180)-20; //phi    sphericalConv(0.51,px*M_PI/180,py*M_PI/180,x,y,z);    mcoord->point.set1Value(i,x,y,z);  }  for (i=0;i<16;i++) {    px = 45 * cos((190+10*i)*M_PI/180) + 90; //theta    py = 90 - 45 * sin((190+10*i)*M_PI/180)-20; //phi    sphericalConv(0.51,px*M_PI/180,py*M_PI/180,x,y,z);    mcoord->point.set1Value(i+16,x,y,z);  }  // Create hat  SoSeparator *hatsep = new SoSeparator;  SoMaterial *hatmat = new SoMaterial;  hatmat->diffuseColor.setValue(0,0.2,0);  hatmat->specularColor.setValue(0,0.2,0);  hatmat->ambientColor.setValue(0,0.1,0);  SoTranslation *h1trans = new SoTranslation;  h1trans->translation.setValue(SbVec3f(0,0.5,0));  SoCylinder *h1 = new SoCylinder;  h1->parts.setValue(SoCylinder::SIDES|SoCylinder::TOP);  h1->radius.setValue(0.4);  h1->height.setValue(0.3);  SoTranslation *h2trans = new SoTranslation;  h2trans->translation.setValue(SbVec3f(0,-0.14,0.3));  SoCylinder *h2 = new SoCylinder;  h2->radius.setValue(0.35);  h2->height.setValue(0.02);  hatsep->addChild(hatmat);  hatsep->addChild(h1trans);  hatsep->addChild(h1);  hatsep->addChild(h2trans);  hatsep->addChild(h2);  SoSeparator *starsep = new SoSeparator;  SoMaterial *starmat = new SoMaterial;  starmat->diffuseColor.setValue(0.8,0,0);  starmat->specularColor.setValue(1,1,1);  starmat->ambientColor.setValue(0.3,0,0);  SoTransform *strans = new SoTransform;  strans->translation.setValue(SbVec3f(0,0.5,0.4));  strans->scaleFactor.setValue(SbVec3f(0.1,0.1,0.1));  SoCoordinate3 *starcoord = new SoCoordinate3;  starcoord->point.setValues(0,11,starpts);  SoIndexedFaceSet *star = new SoIndexedFaceSet;  star->coordIndex.setValues(0,40,starindices);  starsep->addChild(starmat);  starsep->addChild(strans);  starsep->addChild(starcoord);  starsep->addChild(star);  facesep->addChild(eyessep);  facesep->addChild(msep);  facesep->addChild(hatsep);  facesep->addChild(starsep);  SoSeparator *planesep = new SoSeparator;    SoComplexity *comple = new SoComplexity;  comple->textureQuality.setValue(0.1);    SoTexture2 *mapTex = new SoTexture2;  unsigned char texture[4];  texture[0]=texture[3]=0xff;  texture[1]=texture[2]=0x00;  mapTex->image.setValue(SbVec2s(2,2),1,texture);  SoTexture2Transform *mapTexTrans = new SoTexture2Transform;  mapTexTrans->scaleFactor.setValue(10,10);  SoCoordinate3 *planecoord = new SoCoordinate3;  planecoord->point.setValues(0,4,planepts);    SoQuadMesh *quadmesh = new SoQuadMesh;  quadmesh->verticesPerColumn.setValue(2);  quadmesh->verticesPerRow.setValue(2);  planesep->addChild(comple);  planesep->addChild(mapTex);  planesep->addChild(mapTexTrans);  planesep->addChild(planecoord);  planesep->addChild(quadmesh);  smileysep->addChild(trans);  smileysep->addChild(surfSep);  smileysep->addChild(facesep);  root->addChild(smileysep);  root->addChild(planesep);}voidframeCallback(void *userData, SoAction *action){  static SbTime * firsttime=NULL;  if (!action->isOfType(SoGLRenderAction::getClassTypeId())) return;  if (!firsttime) {    firsttime = new SbTime(SbTime::getTimeOfDay());  }  SbTime currtime = SbTime::getTimeOfDay();  float deltat = currtime.getMsecValue() - firsttime->getMsecValue();  float cosval = cos(360*deltat/2000*M_PI/180);  float newpos = cosval-1;  if (newpos>-1.2)    material->diffuseColor.setValue(1,1,0);  else    material->diffuseColor.setValue(1,(newpos+2)/0.8,0);    trans->translation.setValue(SbVec3f(0,newpos,0));  int numvals = controlPts->point.getNum();  SbVec3f *points = controlPts->point.startEditing();  for (int i=0;i<numvals;i++) {    if (pts[i][1]<-2.20-newpos)      points[i][1]=-2.20-newpos;    else       points[i][1]=pts[i][1];  }  controlPts->point.finishEditing();}int main(int argc, char *argv[]){  @WIDGET@ window = So@Gui@::init(argv[0]);  if (window == NULL) exit(1);  SoSeparator * root = new SoSeparator;  root->ref();  SoDirectionalLight *light = new SoDirectionalLight;  light->direction.setValue(-1,-1,-1);  root->addChild(light);  SoCamera *camera = new SoPerspectiveCamera;  root->addChild(camera);  makeSurface(root);    SoTimeCounter *timecount = new SoTimeCounter;  timecount->min.setValue(0);  timecount->max.setValue(359);  timecount->frequency.setValue(0.15);  SoCalculator *calc = new SoCalculator;  calc->expression.set1Value(0,"ta = -a*M_PI/180"); // Angle (radians)  calc->expression.set1Value(1,"oa = 10*cos(ta)");  calc->expression.set1Value(2,"ob = 10*sin(ta)");  calc->expression.set1Value(3,"oc = 2*M_PI-(ta-M_PI/2)");  calc->a.connectFrom(&timecount->output);  SoComposeVec3f *compose = new SoComposeVec3f;  compose->x.connectFrom(&calc->oa);  compose->z.connectFrom(&calc->ob);  compose->y.setValue(0);  SoComposeRotation *composer = new SoComposeRotation;  composer->axis.setValue(SbVec3f(0,1,0));  composer->angle.connectFrom(&calc->oc);    SoCallback *callback = new SoCallback;  callback->setCallback(frameCallback, NULL);  root->addChild(callback);//    So@Gui@ExaminerViewer * renderarea = new So@Gui@ExaminerViewer( window );  So@Gui@RenderArea * renderarea = new So@Gui@RenderArea( window );  camera->farDistance.setValue(1000);  camera->focalDistance.setValue(10);  camera->position.connectFrom(&compose->vector);  camera->orientation.connectFrom(&composer->rotation);  renderarea->setSceneGraph( root );  renderarea->setBackgroundColor( SbColor( 0.0f, 0.2f, 0.3f )  );  if (argc>1) {    renderarea->setTitle(argv[1]);    renderarea->setIconTitle(argv[1]);  }  renderarea->show();  So@Gui@::show(window);  So@Gui@::mainLoop();  delete renderarea;  root->unref();  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -