SoCallbackAction

/*----------------------------------------------------------- * Using a callback for generated primitives. * A simple scene with a sphere is created. * A callback is used to write out the triangles that * form the sphere in the scene. *----------------------------------------------------------*/ #include <Inventor/SoDB.h> #include <Inventor/SoPrimitiveVertex.h> #include <Inventor/actions/SoCallbackAction.h> #include <Inventor/actions/SoWriteAction.h> #include <Inventor/nodes/SoDirectionalLight.h> #include <Inventor/nodes/SoMaterial.h> #include <Inventor/nodes/SoPerspectiveCamera.h> #include <Inventor/nodes/SoSeparator.h> #include <Inventor/nodes/SoSphere.h> #ifdef WIN32 # include "print.h" #endif // Function prototypes void printSpheres(SoNode *); int printHeaderCallback(void *, SoCallbackAction *, const SoNode *); void printTriangleCallback(void *, SoCallbackAction *, const SoPrimitiveVertex *, const SoPrimitiveVertex *, const SoPrimitiveVertex *); void printVertex(const SoPrimitiveVertex *); // // CODE FOR The Inventor Mentor STARTS HERE void printSpheres(SoNode *root) { SoCallbackAction myAction; //#ifdef WIN32 // myAction.addPreCallback(SoSphere::getClassTypeId(), // printHeaderCallback, NULL); //#else myAction.addPreCallback(SoSphere::getClassTypeId(), (SoCallbackAction::SoCallbackActionCB *)printHeaderCallback, NULL); //#endif myAction.addTriangleCallback(SoSphere::getClassTypeId(), printTriangleCallback, NULL); myAction.apply(root); } int printHeaderCallback(void *, SoCallbackAction *, const SoNode *node) { printf("/n Sphere "); // Print the node name (if it exists) and address if (! !node->getName()) printf("named /"%s/" ", node->getName().getString()); printf("at address %#x/n", node); return SoCallbackAction::CONTINUE; } void printTriangleCallback(void *, SoCallbackAction *, const SoPrimitiveVertex *vertex1, const SoPrimitiveVertex *vertex2, const SoPrimitiveVertex *vertex3) { printf("Triangle:/n"); printVertex(vertex1); printVertex(vertex2); printVertex(vertex3); } void printVertex(const SoPrimitiveVertex *vertex) { const SbVec3f &point = vertex->getPoint(); printf("/tCoords = (%g, %g, %g)/n", point[0], point[1], point[2]); const SbVec3f &normal = vertex->getNormal(); printf("/tNormal = (%g, %g, %g)/n", normal[0], normal[1], normal[2]); } // CODE FOR The Inventor Mentor ENDS HERE /// int main(int, char **) { // Initialize Inventor SoDB::init(); // Make a scene containing a red sphere SoSeparator *root = new SoSeparator; SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; SoMaterial *myMaterial = new SoMaterial; root->ref(); root->addChild(myCamera); root->addChild(new SoDirectionalLight); myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0); // Red root->addChild(myMaterial); root->addChild(new SoSphere); root->ref(); // Write out the triangles that form the sphere in the scene printSpheres(root); root->unref(); exit(1); // The usual exit(0) has been replaced with exit(1) to enable the console // window showing the output to stay on the screen until the user deletes it return 1; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值