📄 plasmaball.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> *\**************************************************************************//* ********************************************************************** * * plasmaball * * Demo application for showcasing Coin. * * Written by Morten Eriksen <mortene@coin3d.org>, partly based on * code by Peder Blekken <pederb@coin3d.org> and Marius Bugge Monsen * <mariusbu@stud.ntnu.no>. * ********************************************************************** */#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/events/SoKeyboardEvent.h>#include <Inventor/nodes/SoAntiSquish.h>#include <Inventor/nodes/SoCamera.h>#include <Inventor/nodes/SoCube.h>#include <Inventor/nodes/SoEventCallback.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoOrthographicCamera.h>#include <Inventor/nodes/SoRotor.h>#include <Inventor/nodes/SoScale.h>#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoShapeHints.h>#include <Inventor/nodes/SoSphere.h>#include <Inventor/nodes/SoSurroundScale.h>#include <Inventor/nodes/SoText2.h>#include <Inventor/nodes/SoText3.h>#include <Inventor/nodes/SoTexture2.h>#include <Inventor/nodes/SoTextureCoordinateEnvironment.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/sensors/SoTimerSensor.h>#include <math.h>#include <stdio.h>#include <stdlib.h> // exit()#include <time.h>#include <coinlogo.h>typedef struct{ unsigned char r,g,b,a;} myrgb;// yeah, statics :)static int * plasma;static unsigned int palette[256];static myrgb colors[256];static unsigned short rpos1, rpos2, rpos3, rpos4;static unsigned short gpos1, gpos2, gpos3, gpos4;static unsigned short bpos1, bpos2, bpos3, bpos4;static short adders[18] = { 1, 2, 13, 4, 25, 6, 17, 2, 9, 31, 11, 12, 3, 4, 11, 61, 17, 8 };voidevent_cb(void * userdata, SoEventCallback * node){ const SoEvent * event = node->getEvent();// if (SO_KEY_PRESS_EVENT(event, XXX)) {// }}static intrandom(int maxval){ return rand() % (maxval+1);}static voidredraw_plasma(unsigned char * buffer, SbVec2s size, int components){ int i,j; unsigned char color; unsigned short rtpos1,rtpos2, rtpos3,rtpos4; unsigned short gtpos1,gtpos2, gtpos3,gtpos4; unsigned short btpos1,btpos2, btpos3,btpos4; rtpos3 = rpos3; rtpos4 = rpos4; gtpos3 = gpos3; gtpos4 = gpos4; btpos3 = bpos3; btpos4 = bpos4; unsigned char * ptr = buffer; unsigned int col; for (i = 0; i < size[1]; i++) { rtpos1 = rpos1; rtpos2 = rpos2; gtpos1 = gpos1; gtpos2 = gpos2; btpos1 = bpos1; btpos2 = bpos2; for (j = 0; j < size[0]; j++) { color = plasma[rtpos1]+plasma[rtpos2]+plasma[rtpos3]+ plasma[rtpos4]+plasma[i%0xff]+plasma[j&0xff]; col = palette[color]; color = plasma[gtpos1]+plasma[gtpos2]+plasma[gtpos3]+ plasma[gtpos4]+plasma[i%0xff]+plasma[j&0xff]; col |= palette[color]<<8; color = plasma[btpos1]+plasma[btpos2]+plasma[btpos3]+ plasma[btpos4]+plasma[i%0xff]+plasma[j&0xff]; col |= palette[color]<<16; // Doesn't matter whether we're big-endian or not. *ptr++ = (col>>8)&0xff; *ptr++ = (col>>16)&0xff; *ptr++ = col>>24; *ptr++ = (col&0xff) < 224 ? 0 : 255; // XXX rtpos1 = rtpos1+adders[0]; rtpos2 = rtpos2+adders[1]; gtpos1 = gtpos1+adders[6]; gtpos2 = gtpos2+adders[7]; btpos1 = btpos1+adders[12]; btpos2 = btpos2+adders[13]; } rtpos3 = rtpos3+adders[2]; rtpos4 = rtpos4+adders[3]; gtpos3 = gtpos3+adders[8]; gtpos4 = gtpos4+adders[9]; btpos3 = btpos3+adders[14]; btpos4 = btpos4+adders[15]; } rpos1 = rpos1 + adders[4]; rpos3 = rpos3 + adders[5]; gpos1 = gpos1 + adders[10]; gpos3 = gpos3 + adders[11]; bpos1 = bpos1 + adders[16]; bpos3 = bpos3 + adders[17];}static voidsetup_plasma(void){ rpos1 = rand(); rpos2 = rand(); rpos3 = rand(); rpos4 = rand(); gpos1 = rand(); gpos2 = rand(); gpos3 = rand(); gpos4 = rand(); bpos1 = rand(); bpos2 = rand(); bpos3 = rand(); bpos4 = rand(); int i; plasma = new int[65536]; double inc0 = M_PI*2.0f / (256.0f*(random(128)+1)); double inc1 = M_PI*2.0f / (256.0f*(random(64)+1)); double inc2 = M_PI*2.0f / (256.0f*(random(32)+1)); double inc3 = M_PI*2.0f / (256.0f*(random(1)+1)); double v0, v1, v2, v3; v0 = v1 = v2 = v3 = 0.0; for (i = 0; i < 65536; i++) { plasma[i] = (int) ((sin(v0) + sin(v1) + sin(v2) + sin(v3)) * 32.0); v0 += inc0; v1 += inc1; v2 += inc2; v3 += inc3; } for (i = 0; i < 128; i++) { palette[i] = (i*2); palette[i+128] = (255-i*2); } double inc = M_PI / 256.0f; double val = 0.0; for (i = 0; i < 255; i++) { palette[i] = (unsigned int) (sin(val) * 127) + 128; val += inc; } for (i = 0; i < 64; i++) { colors[i].r = 255; colors[i].g = i*4; colors[i].b = 0; colors[i].a = 255; } for (i = 0; i < 64; i++) { colors[i+64].r = 255-i*4; colors[i+64].g = 255; colors[i+64].b = i*4; colors[i+64].a = 255; } for (i = 0; i < 64; i++) { colors[i+128].r = 0; colors[i+128].g = i*4; colors[i+128].b = 255; colors[i+128].a = 255; } for (i = 0; i < 64; i++) { colors[i+192].r = i*4; colors[i+192].g = 255 - i*4; colors[i+192].b = 255 - i*4; colors[i+192].a = 255; }}static voidtimersensorcallback(void * data, SoSensor *){ SoTexture2 * texnode = (SoTexture2*) data; SbVec2s size; int nc; unsigned char * image = texnode->image.startEditing(size, nc); redraw_plasma(image, size, nc); texnode->image.finishEditing();}intmain(int argc, char **argv){ // Initialize @WIDGET@ window = So@Gui@::init(argv[0]); if (window == NULL) exit(1); SoSeparator * root = new SoSeparator; root->ref(); SoEventCallback * cb = new SoEventCallback; cb->addEventCallback(SoKeyboardEvent::getClassTypeId(), event_cb, NULL); root->insertChild(cb, 0); srand(998648536); setup_plasma(); const SbVec2s PLASMADIMENSIONS(256, 256); SoSeparator * surroundsep = new SoSeparator; root->addChild(surroundsep); SoTexture2 * texnode = new SoTexture2; // FIXME: this causes a crash with SGI Inventor -- // investigate. 20010919 mortene. texnode->image.setValue(PLASMADIMENSIONS, 4, NULL); surroundsep->addChild(texnode); SoMaterial * mat = new SoMaterial; mat->diffuseColor = SbColor(0.8f, 0.8f, 0.8f); mat->specularColor = SbColor(1.0f, 1.0f, 1.0f); mat->shininess = 0.9f; surroundsep->addChild(mat); SoSurroundScale * ss = new SoSurroundScale; ss->numNodesUpToReset = 1; ss->numNodesUpToContainer = 2; surroundsep->addChild(ss); SoAntiSquish * antisquish = new SoAntiSquish; antisquish->sizing = SoAntiSquish::BIGGEST_DIMENSION; surroundsep->addChild(antisquish); SoSphere * sphere = new SoSphere; sphere->radius = 1.2f; surroundsep->addChild(sphere); SoSeparator * logosep = new SoSeparator; root->addChild(logosep); SoRotor * logorot = new SoRotor; logorot->rotation = SbRotation(SbVec3f(0, 1, 0), 1); logorot->speed = 0.2f; logosep->addChild(logorot); SoInput in; in.setBuffer((void *)COIN_LOGO, strlen(COIN_LOGO)); SoNode * logoroot; SbBool ok = SoDB::read(&in, logoroot); assert(ok && "COIN_LOGO import failed"); logosep->addChild(logoroot); // Timer sensor SoTimerSensor * texturetimer = new SoTimerSensor(timersensorcallback, texnode); texturetimer->setInterval(0.05); // FIXME: base on framerate. 20010817 mortene. texturetimer->schedule(); // Setting up the examiner viewer So@Gui@ExaminerViewer * viewer = new So@Gui@ExaminerViewer(window); viewer->setBackgroundColor( SbColor( 0.2f, 0.4f, 0.5f ) ); viewer->setSceneGraph(root); viewer->setTitle("Hairy plasma ball"); viewer->viewAll(); SoSeparator * instructsep = new SoSeparator; root->addChild(instructsep); SoOrthographicCamera * ocam = new SoOrthographicCamera; instructsep->addChild(ocam); SoTranslation * instructtrans = new SoTranslation;// instructtrans->translation = SbVec3f(); instructsep->addChild(instructtrans); SoText2 * instructions = new SoText2; const char * str[] = { "Click and drag with left mouse button to move sphere logo.", "", "Hit 'q' to exit demonstration." }; 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); 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 with SoWin. (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 + -