Ilmari Heikkinen
2/2/2006 12:23:00 PM
On 2/1/06, anne001 <anne@wjh.harvard.edu> wrote:> Typically, data like info in ruby would be stored in an object? So I> should create an object for each body part, with in this case a method> made of those two lines?>> The other problem with this program is that it draws the upper arm, and> then draws the fore arm at the end of the upper arm. So whatever> rotation the arm went through the forearm will stay attached to the> arm. But with a whole body, this way of drawing does not generalise. It> seems to me that ruby's object oriented hierarchies would be perfect> for this.The usual way is to have a scenegraph, which is a tree that describesthe scene you're drawing. You then attach models and transformationsand material information to the nodes in the graph. When you draw it, therenderer (for example) traverses the graph depth-first, calling the statesetting functions and drawing functions. Other way would be to collectthe scene objects into a list and collapse their transforms into worldcoordinates and then draw the list.This way you can e.g. create a room object for the scene root, attachsome box objects to it, and put some books in the boxes. Now whenyou move a box, all the books in it move too.Here's some pseudocode:room = Room.newboxes = (1..3).each{ box = Box.new(:position => random_position) box.attach StackOfBooks.new box}room.attach(*boxes)renderer.draw(room)# hmm, one box too manyroom.detach(boxes.last)renderer.draw(room)The renderer#draw could be something like:def draw(scene) clear_frame setup_default_state setup_camera setup_lights draw_object(scene) swap_buffersenddef draw_object stack_state{ transform(obj) set_material(obj) draw_geometry(obj) obj.children.each{|c| draw_object(c) } }end> This program seems to redraw the whole figure from scratch, even if> only the forearm was changed. I wonder if the computation could be> saved so only the part changed is redrawn, but it does not sound like> it, because push and pop suggests a giant laundry list which it would> not be easy to access in organised pieces.With OpenGL, it's going to be a lot slower to figure out what parts of theframe have changed and need updating, than just clearing the frame anddrawing everything again. There are some situations where this doesn'tapply (like if there's a huge background scene in the horizon; it can bejust drawn to a texture once and the texture then used as an impostor),but generally it's not something to worry about.HTH,Ilmari