灯光:
当使用缺省光照模式时(Phong 模式),在可以观察物体之前,场景中必须包含至少一个灯光节点。在执行渲染动作期间,如果遍历遇到了场景中的灯光节点,渲染将开启这个灯光节点.
灯光效果是累积的。每当向图形场景中增加一个灯光节点时,场景就会变得亮一些。OpenInventor 能开启的最大灯光数依赖于系统当前OpenGL 的具体实现。
SoLight类有三个子类:
SoPointLight:星星,在给定的3D空间位置上,均匀地向四周放射光线。
SoDirectionalLight :均匀地按照一个方向放射光线,发出的光线都是平行的。
SoSpotLight:发出的光线是从空间中某一点开始,沿着一个主方向进行照射的。就像舞台上的聚光灯那样,
它的灯光形状是一个以灯光所在位置为顶点的圆锥体光柱。
key code:
SoSeparator *root = new SoSeparator;
root->ref();
// Add a directional light 均匀地按照一个方向放射光线,发出的光线都是平行的。
SoDirectionalLight *myDirLight = new SoDirectionalLight;
myDirLight->direction.setValue(0, -1, -1);
myDirLight->color.setValue(1, 0, 0);
root->addChild(myDirLight);
// Put the shuttle and the light below a transform separator.
// A transform separator pushes and pops the transformation
// just like a separator node, but other aspects of the state
// are not pushed and popped. So the shuttle's translation
// will affect only the light. But the light will shine on
// the rest of the scene.
SoTransformSeparator *myTransformSeparator =
new SoTransformSeparator;
root->addChild(myTransformSeparator);
// A shuttle node translates back and forth between the two
// fields translation0 and translation1.
// This moves the light. 在两个变换中周期切换
SoShuttle *myShuttle = new SoShuttle;
myTransformSeparator->addChild(myShuttle);
myShuttle->translation0.setValue(-2, -1, 3);
myShuttle->translation1.setValue( 1, 2, -3);
// Add the point light below the transformSeparator
//星星,在给定的3D空间位置上,均匀地向四周放射光线。
SoPointLight *myPointLight = new SoPointLight;
myTransformSeparator->addChild(myPointLight);
myPointLight->color.setValue(0, 1, 0);
root->addChild(new SoCone);//场景物体
SoWinExaminerViewer *myViewer =
new SoWinExaminerViewer(myWindow);
myViewer->setSceneGraph(root);
myViewer->setTitle("Lights");
myViewer->setHeadlight(FALSE);
myViewer->show();
SoWin::show(myWindow);
SoWin::mainLoop();
return 0;
}