我是JSL编程和Opengl的新手,并且想知道隐藏对象。如果我最初在场景中有多个球体,我希望用户能够使用滑块指定场景中的球体数量,并相应地更新它。有没有办法隐藏或删除对象?
显示列表没有自己的任何逻辑(用于隐藏/显示);您需要重建显示列表以显示您想要的内容。
sphere = Scene Display List();
sphere << sphere( .15, 15, 15 );
spheres = Scene Display List();
makespheres = Function( {n},
spheres << clear;
For( i = 1, i < n, i++,
spheres << color( i / n, 1 - i / n, .9 ); // .1,.1,.1 = almost black .9,.9,.9 = almost white
spheres << pushmatrix;
spheres << translate( (i - (n / 2)) / 3, 0, 0 );
spheres << calllist( sphere );
spheres << popmatrix;
);
);
Scene = Scene Box( 600, 600 );
Scene << backgroundcolor( 0 ); // black=0, gray=1, white=2
Scene << showarcball( always );
nspheres = 14;
makespheres( nspheres );
New Window( "x",
Scene,
Slider Box(
1,
20,
nspheres,
makespheres( nspheres );
scene << update;
)
);
Scene << perspective( 45, .2, 20 );
Scene << Translate( 0.0, 0.0, -4.5 );
Scene << arcball( spheres, 1.5 );
Scene << update;
滑块控制球体的数量
这使用了几层被调用的显示列表。最小的列表“球体”只包含一个球体,但可能包含一个更复杂的物体。“spheres”列表是滑块移动时重建的列表;保持它尽可能简单,因此当滑块移动时,JSL循环不必比所需更加努力。反过来,“球体”从主显示列表中调用(通过弧形球)。另一件需要注意的事情是:被叫列表不会将被转换(等)保持在被叫列表的本地。如果被调用列表转换对象并且未撤消该转换,则在完成被调用列表时,转换仍然有效。这就是使用pushmatrix和popmatrix的原因。