📄 chart.cpp.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> *\**************************************************************************//* ********************************************************************** * * chart * * Demo application for showcasing Coin. * * Written by Lars J. Aas <larsa@coin3d.org>. * Additional copyright (C) 2001, Lars J. Aas. * ********************************************************************** */ /* * TODO: * - improve pick-plane generation, so it's not up/down movement that * matters * - encapsulate faceset in chart node with these fields: * SFInt32 width, SFInt32 depth, MFFloat level * and honor Material node and MaterialBinding element * - make width/height command line options with defaults * - reorder topfaces coordinates so the 4 coords for one face are in * sequence */// FIXME: there are several bugs when running on top of SGI// Inventor. Investigate. 20010919 mortene.#if HAVE_CONFIG_H#include <config.h>#endif // HAVE_CONFIG_H#include <Inventor/@Gui@/So@Gui@.h>#include <Inventor/@Gui@/viewers/So@Gui@ExaminerViewer.h>#include <Inventor/actions/SoSearchAction.h>#include <Inventor/actions/SoGLRenderAction.h>#include <Inventor/actions/SoHandleEventAction.h>#include <Inventor/actions/SoRayPickAction.h>#include <Inventor/nodes/SoCoordinate3.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoCallback.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoScale.h>#include <Inventor/nodes/SoRotor.h>#include <Inventor/nodes/SoNormal.h>#include <Inventor/nodes/SoCamera.h>#include <Inventor/nodes/SoIndexedFaceSet.h>#include <Inventor/nodes/SoAsciiText.h>#include <Inventor/nodes/SoText2.h>#include <Inventor/events/SoMouseButtonEvent.h>#include <Inventor/events/SoLocation2Event.h>#include <Inventor/details/SoFaceDetail.h>#include <Inventor/details/SoPointDetail.h>#include <Inventor/SoPickedPoint.h>#include <Inventor/SoPath.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <assert.h>#include <math.h>/* to facilitate picking on 0-level bars * picking is not affected by the PolygonOffset node */#ifndef FLOAT_EPSILON#define FLOAT_EPSILON 1.0e-4f#endifSoNode * initSceneGraph(int WIDTH, int DEPTH);void startLevelEditing(void);void setLevel(int WIDTH, int DEPTH, int x, int y, float level);void finishLevelEditing(void);void event_cb(void * userdata, SoAction * action);static So@Gui@ExaminerViewer * viewer = NULL;extern char scene_iv[];/* ********************************************************************** */// graph config [to come]/* ********************************************************************** */intmain( int argc, char ** argv ){ setbuf( stderr, NULL ); setbuf( stdout, NULL ); @WIDGET@ win = So@Gui@::init(argv[0]); srand(SbTime::getTimeOfDay().getMsecValue()); viewer = new So@Gui@ExaminerViewer(win); viewer->setViewing(false); int x, y, w = 12, d = 12; SoNode * scene = initSceneGraph( w, d ); viewer->setSceneGraph(scene); viewer->show(); So@Gui@::show(win); SoCamera * camera = viewer->getCamera(); camera->orientation.setValue( SbRotation(SbVec3f(1, 0, 0), (M_PI/3.0f)) * SbRotation(SbVec3f(0, 0, 1), -(M_PI/4.0f)) ); viewer->viewAll(); So@Gui@::mainLoop(); delete viewer; return 0;}/* ********************************************************************** */static SoNode * scene = NULL;SoNode *findNode( const char * name ){ static SoSearchAction * searcher = NULL; if ( ! searcher ) searcher = new SoSearchAction; searcher->setName(SbName(name)); searcher->setFind(SoSearchAction::NAME); searcher->setInterest(SoSearchAction::FIRST); searcher->apply(scene); if ( ! searcher->isFound() ) return NULL; return searcher->getPath()->getTail();}SoNode *initSceneGraph( int WIDTH, int DEPTH){ SoInput * in = new SoInput; in->setBuffer(scene_iv, strlen(scene_iv)); if ( ! SoDB::read(in, scene) ) { delete in; return NULL; } delete in; scene->ref(); SoCoordinate3 * points = (SoCoordinate3 *) findNode("points"); assert(points != NULL); SoMaterial * materials = (SoMaterial *) findNode("materials"); assert(materials != NULL); SoIndexedFaceSet * topfaces = (SoIndexedFaceSet *) findNode("topfaces"); assert(topfaces != NULL); SoIndexedFaceSet * sidefaces = (SoIndexedFaceSet *) findNode("sidefaces"); assert(sidefaces != NULL); SoCallback * hook = (SoCallback *) findNode("hook"); assert(hook != NULL); do { // set up materials int i; SbColor * colors = new SbColor[ WIDTH + DEPTH ]; for ( i = 0; i < WIDTH; i++ ) colors[i].setValue( 1.0f, 0.0f, float(i) / float(WIDTH-1) ); for ( i = 1; i < DEPTH; i++ ) colors[WIDTH + i - 1].setValue( float(DEPTH-1-i)/float(DEPTH-1), 0.0f, 1.0f); colors[WIDTH + DEPTH - 1].setValue( 0.0f, 1.0f, 1.0f); materials->diffuseColor.setValues(0, WIDTH + DEPTH, colors); delete [] colors; } while ( 0 ); do { // set up geometry points // the base geometry points float coords[][3] = { { -1.0f, -1.0f, -FLOAT_EPSILON }, { float(DEPTH) + 1.0f, -1.0f, -FLOAT_EPSILON }, { float(DEPTH) + 1.0f, float(WIDTH) + 1.0f, -FLOAT_EPSILON }, { -1.0f, float(WIDTH) + 1.0f, -FLOAT_EPSILON }, { -1.0f, -1.0f, -0.2f }, { float(DEPTH) + 1.0f, -1.0f, -0.2f }, { float(DEPTH) + 1.0f, float(WIDTH) + 1.0f, -0.2f }, { -1.0f, float(WIDTH) + 1.0f, -0.2f } }; points->point.setValues(0, 8, coords); int i, j; const int numvalues = (WIDTH + 1) * (DEPTH + 1) + WIDTH * DEPTH * 4; float * values = new float [ numvalues * 3 ]; float * ptr = values; // the base points for ( j = 0; j <= DEPTH; j++ ) { for ( i = 0; i <= WIDTH; i++ ) { *ptr++ = 1.0f * float(i); // x *ptr++ = 1.0f * float(j); // y *ptr++ = 0.0f; // floor } } // the top-face points for ( j = 0; j < (DEPTH*2); j++ ) { for ( i = 0; i < (WIDTH*2); i++ ) { *ptr++ = 1.0f * float( ((i+1)>>1) ); // x *ptr++ = 1.0f * float( ((j+1)>>1) ); // y *ptr++ = 1.0f; // height level } } points->point.setValues(8, numvalues, (float(*)[3]) values); delete [] values; } while ( 0 ); do { // set up topfaces int i, j; const int offset = 8 + (WIDTH+1) * (DEPTH+1); int * indexvalues, * iptr, * materialindex, * mptr, * normalindex, * nptr; indexvalues = new int [ 6 * WIDTH * DEPTH ]; iptr = indexvalues; materialindex = new int [ WIDTH * DEPTH + 1 ]; normalindex = new int [ WIDTH * DEPTH + 1 ]; for ( j = 0; j < DEPTH; j++ ) { for ( i = 0; i < WIDTH; i++ ) { *iptr++ = offset + ((2 * j) * WIDTH * 2) + (i * 2); *iptr++ = offset + ((2 * j) * WIDTH * 2) + (i * 2) + 1; *iptr++ = offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2) + 1; *iptr++ = offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2); *iptr++ = offset + ((2 * j) * WIDTH * 2) + (i * 2); *iptr++ = -1; materialindex[(j*WIDTH)+i] = i + j; normalindex[(j*WIDTH)+i] = 4; } } materialindex[WIDTH*DEPTH] = -1; normalindex[WIDTH*DEPTH] = -1; topfaces->coordIndex.setValues(0, 6 * WIDTH * DEPTH, indexvalues); topfaces->materialIndex.setValues(0, WIDTH * DEPTH + 1, materialindex); topfaces->normalIndex.setValues(0, WIDTH * DEPTH + 1, normalindex); delete [] indexvalues; delete [] materialindex; delete [] normalindex; } while ( 0 ); do { // set up sidefaces const int offset = (WIDTH+1) * (DEPTH+1); int * indexvalues, * iptr, * materialindex, * mptr, * normalindex, * nptr; int i, j; indexvalues = new int [ 6 * WIDTH * DEPTH * 4 ]; iptr = indexvalues; materialindex = new int [ WIDTH * DEPTH * 4 + 1 ]; normalindex = new int [ WIDTH * DEPTH * 4 + 1 ]; mptr = materialindex; nptr = normalindex; for ( j = 0; j < DEPTH; j++ ) { for ( i = 0; i < WIDTH; i++ ) { // south *iptr++ = 8 + (j * (WIDTH+1)) + i; *iptr++ = 8 + (j * (WIDTH+1)) + i + 1; *iptr++ = 8 + (j * WIDTH * 4) + (i * 2) + 1 + offset; *iptr++ = 8 + (j * WIDTH * 4) + (i * 2) + offset; *iptr++ = 8 + (j * (WIDTH+1)) + i; *iptr++ = -1; // west *iptr++ = 8 + (j * (WIDTH+1)) + i + 1; *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i + 1; *iptr++ = 8 + offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2) + 1; *iptr++ = 8 + offset + ((2 * j) * WIDTH * 2) + (i * 2) + 1; *iptr++ = 8 + (j * (WIDTH+1)) + i + 1; *iptr++ = -1; // north *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i + 1; *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i; *iptr++ = 8 + offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2) + 0; *iptr++ = 8 + offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2) + 1; *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i + 1; *iptr++ = -1; // east *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i + 0; *iptr++ = 8 + (j * (WIDTH+1)) + i + 0; *iptr++ = 8 + offset + ((2 * j) * WIDTH * 2) + (i * 2) + 0; *iptr++ = 8 + offset + (((2 * j) + 1) * WIDTH * 2) + (i * 2) + 0; *iptr++ = 8 + ((j+1) * (WIDTH+1)) + i + 0; *iptr++ = -1; *mptr++ = i + j; *mptr++ = i + j; *mptr++ = i + j; *mptr++ = i + j; *nptr++ = 3; *nptr++ = 0; *nptr++ = 2; *nptr++ = 1; } } *mptr++ = -1; sidefaces->coordIndex.setValues(0, 6 * WIDTH * DEPTH * 4, indexvalues); sidefaces->materialIndex.setValues(0, WIDTH * DEPTH * 4 + 1, materialindex); sidefaces->normalIndex.setValues(0, WIDTH * DEPTH * 4 + 1, normalindex); delete [] indexvalues; delete [] materialindex; delete [] normalindex; } while ( 0 ); hook->setCallback(event_cb); scene->unrefNoDelete(); return scene;}static SbVec3f * vertices;voidstartLevelEditing(void){ SoCoordinate3 * points = (SoCoordinate3 *) findNode("points"); vertices = points->point.startEditing();}voidfinishLevelEditing(void){ SoCoordinate3 * points = (SoCoordinate3 *) findNode("points"); points->point.finishEditing();}voidsetLevel( int WIDTH, int DEPTH, int x, int y, float level ){ SoCoordinate3 * points = (SoCoordinate3 *) findNode("points"); const int offset = 8 + (WIDTH + 1) * (DEPTH + 1); const int seidx = offset + y * WIDTH * 4 + x * 2; const int swidx = offset + y * WIDTH * 4 + x * 2 + 1; const int neidx = offset + (2 * y + 1) * WIDTH * 2 + x * 2; const int nwidx = offset + (2 * y + 1) * WIDTH * 2 + x * 2 + 1; SbVec3f * vertices = points->point.startEditing(); vertices[seidx][2] = level; vertices[swidx][2] = level; vertices[neidx][2] = level; vertices[nwidx][2] = level; points->point.finishEditing();}floatgetLevel( int WIDTH, int DEPTH, int x, int y ){ SoCoordinate3 * points = (SoCoordinate3 *) findNode("points"); const int offset = 8 + (WIDTH + 1) * (DEPTH + 1); const int seidx = offset + y * WIDTH * 4 + x * 2; SbVec3f se = points->point[seidx]; return se[2];}voidmark( int WIDTH, int DEPTH, int x, int y, SbBool selected ){ SoIndexedFaceSet * topfaces = (SoIndexedFaceSet *) findNode("topfaces"); SoIndexedFaceSet * sidefaces = (SoIndexedFaceSet *) findNode("sidefaces"); if ( selected ) { topfaces->materialIndex.set1Value( (y * WIDTH) + x, WIDTH + DEPTH - 1 ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4, WIDTH + DEPTH - 1 ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 1, WIDTH + DEPTH - 1 ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 2, WIDTH + DEPTH - 1 ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 3, WIDTH + DEPTH - 1 ); } else { topfaces->materialIndex.set1Value( (y * WIDTH) + x, x + y ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4, x + y ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 1, x + y ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 2, x + y ); sidefaces->materialIndex.set1Value( ((y * WIDTH) + x) * 4 + 3, x + y ); }}static SbBool demomode = TRUE;static SbBool attached = FALSE;static SoRayPickAction * picker = NULL;static SbPlane * pickplane = NULL;static int column = 0;static int row = 0;static float offset = 0.0f;static SbTime demochange(0,0);static SbBool animateddemo = FALSE; // not implementedstatic int mode = 0;static SbVec2s lastviewport = SbVec2s(-1,-1);void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -