problem with variables

Started by
2 comments, last by vpro 20 years, 6 months ago
i''ve prob with this: just save it to "cylinder.h" and init variable: "_obj cyl" and call in the draw_scene procedure "draw_obj(cyl)". it seems that integers defined in "void create_cylinder(_obj o)" arent assigned... im beginner help needed! struct _dot { GLfloat x,y,z; }; struct _tri { int first, second, third; }; struct _quad { int first, second, third, fourth; }; struct _obj { _dot dot[64]; _tri tri[64]; _quad quad[64]; int dots, tris, quads; }; void create_cylinder(_obj o) { o.dots=0; o.tris=0; o.quads=0; o.tris=1; o.tri[1].first=1; o.tri[1].second=2; o.tri[1].third=3; o.dot[1].x=-1; o.dot[1].y=-1; o.dot[1].z=1; o.dot[2].x=1; o.dot[2].y=-1; o.dot[2].z=1; o.dot[3].x=0; o.dot[3].y=1; o.dot[3].z=1; } void draw_obj(_obj o) { int c=1; _obj a_obj; create_cylinder(a_obj); glBegin(GL_TRIANGLES); glVertex3f( o.dot[o.tri[c].first].x, o.dot[o.tri[c].first].y, o.dot[o.tri[c].first].z ); glVertex3f( o.dot[o.tri[c].second].x, o.dot[o.tri[c].second].y, o.dot[o.tri[c].second].z ); glVertex3f( o.dot[o.tri[c].third].x, o.dot[o.tri[c].third].y, o.dot[o.tri[c].third].z ); // should be drawn: // glVertex3f( -1.0f, -1.0f, 1.0f ); // glVertex3f( 1.0f, -1.0f, 1.0f ); // glVertex3f( -0.0f, 1.0f, 1.0f ); glEnd(); }
Advertisement
Your function creates its own copy of "a_obj" - when it makes the changes in the function, they are specific to the functions copy of a_obj.

What you want to do is pass the function a pointer to a_obj. Change the function definition to:

void create_cylinder(_obj * o)

and when you call the function, do it thus:

create_cylinder(&a_obj);

Cheers,
Brendan.
You need to pass o (at least in create_cylinder) by reference, not by value. Use the & operator in the functions signature if using C++, otherwise you have to pass a pointer to _obj.
thanx it worked. why c++ is such headache. in pascal evrything was logical. eg. "void my_procedure() { ... }" f**ks up but
"void my_procedure(void) { ... }" works...

This topic is closed to new replies.

Advertisement