0

I am trying to pass an array containing an X, Y, and Z coordinate (type float) to the function glTranslatef() however I can't figure out a way to accomplish this.

As an example of what I am trying to accomplish;

float xyz[3] = {3,2,1};
glTranslatef(xyz);

I get the following error whenever I try to attempt something like this

cannot convert ‘float*’ to ‘GLfloat {aka float}’ for argument ‘1’ to ‘void glTranslatef(GLfloat, GLfloat, GLfloat)’

I have tried searching all over google but I couldn't find what I was looking for.

3
  • So, you're looking for a C++ equivalent to Python's func(*args) syntax? Commented Feb 11, 2012 at 23:08
  • 1
    Do not use ALL UPPERCASE names, except for macros, where you should use them. Commented Feb 11, 2012 at 23:13
  • @ Alf P. Steinbach That's not my real code, it's just an example Commented Feb 11, 2012 at 23:25

4 Answers 4

5

glTranslatef takes three float arguments, not one array argument. That's the end of it.

float XYZ[3] = {3,2,1};
glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

If you're really desperate you can unpack it with a macro:

#define UNPACK_TRI_ARRAY(ar) ar[0], ar[1], ar[2]

float XYZ[3] = {3,2,1};
glTranslatef(UNPACK_TRI_ARRAY(XYZ));

But once you get to that point, you have to ask yourself why.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I just remember in some point in time (I don't even remember what language I was using) being able to accomplish this without having to repetitively and separately define the location in the array.
@MarkData: Maybe you were thinking about glVertexfv, glNormalfv, etc. which indeed take a vector/array.
Indeed. Unfortunately there is no v variant for glTranslatef.
2

Try like this:

glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml

Prototype:

void glTranslatef(GLfloat   x,
                  GLfloat   y,
                  GLfloat   z);

Comments

0
glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

Comments

0

AFAIK, I'd just define an overload for the function taking a const float * and then pass array elements to the original three-arguments (X,Y,Z) function.

1 Comment

Why not a const float (&)[3]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.