can robot.rb be written a ruby way?

A

anne001

This site gives the c code from robot.c in its
robot-framework-1.3.tar.gz file
http://yallara.cs.rmit.edu.au/~aholkner/Interactive3D/

and this site gives a ruby code for the same program in robot.rb in its
folder sample in version 0.32g
http://www2.giganet.net/~yoshi/

The c program is structured as follows:
void init(void)
void display(void)
void reshape (int w, int h)
void keyboard (unsigned char key, int x, int y)
int main(int argc, char** argv)
{ ...
/* Tell GLUT to create and display the window, with the given title
*/
glutCreateWindow("Robot Arm Demo");

/* Call our own init function to set up the background color and
shading
* model. */
init ();

/* Tell GLUT where each of our functions are. We are passing in the
name
* of each function, which it will then call as required. */
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);

/* All initialisation is finished, tell GLUT to run the application
forever
* (or until we tell it to quit) */
glutMainLoop();
....}

and the ruby program is set up as
def init
display = Proc.new {
reshape = Proc.new {|w, h|
keyboard = Proc.new {|key, x, y|
...
GLUT.CreateWindow($0);
init();
GLUT.DisplayFunc(display);
GLUT.ReshapeFunc(reshape);
GLUT.KeyboardFunc(keyboard);
GLUT.MainLoop();
....
I was surprised to find that robot.rb is a litteral translation of
robot.c. No objects talking to each other here!

On the other hand, GLUT has a function ReshapeFunc which requires a
function, and so reshape is going to be a function, etc, so there may
not be a lot of room for ruby like programming.

Is it the case here, that because it is calling opengl and GLUT code,
the structure is going to have to be "C" like, and not "Object OOP"
like? Or is there a way of writing robot.rb a more ruby like way?
 
A

anne001

Actually, maybe it is display which I would like to see more ruby like

display = Proc.new {
GL.Clear(GL::COLOR_BUFFER_BIT);
GL.PushMatrix();
GL.Translate(-1.0, 0.0, 0.0);
GL.Rotate($shoulder, 0.0, 0.0, 1.0);
GL.Translate(1.0, 0.0, 0.0);
GL.PushMatrix();
GL.Scale(2.0, 0.4, 1.0);
GLUT.WireCube (1.0);
GL.PopMatrix();

GL.Translate(1.0, 0.0, 0.0);
GL.Rotate($elbow, 0.0, 0.0, 1.0);
GL.Translate(1.0, 0.0, 0.0);
GL.PushMatrix();
GL.Scale(2.0, 0.4, 1.0);
GLUT.WireCube(1.0);
GL.PopMatrix();

GL.PopMatrix();
GLUT.SwapBuffers();
}
In this simple example, the arms are represented by a scaled cube.
so the basic drawing takes two lines
GL.Scale(2.0, 0.4, 1.0);
GLUT.WireCube(1.0);

But the figures I have seen on the internet have a long list of parts,
for ex
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21391512.html

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.

So in the object Arm, I should create a forearmobject, and in
forearmobject, I should create a hand object. The method draw arm
object would draw the arm, and proceed to call the draw forearm method,
which would draw the forearm, and call the draw hand method.

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.

If this is the case, I just need to figure out who is at the top of the
hierarchy, probably the hip, and build the thing.

are those thoughts on the right track for a ruby way version of
robot.rb?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top