Rafael
Rafael

Reputation: 55

glVertex3f and glVertex2fx displaying different results

I suspect it has to do with my conversion code:

vector3::operator float*() const
{
    // x, y, z are member floats
    float arr[3];
    arr[0] = x;
    arr[1] = y;
    arr[2] = z;
    return arr;
}

Then in another class I do:

glBegin(GL_POLYGON);
    glVertex3fv(origin); // wrong result
    //glVertex3f(origin.x, origin.y, origin.z); // good
    //glVertex3f(0.0, 0.0, 0.0); // also good
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
glEnd();

The problem is that the rectangle is stretched very far. I suspect it is because of the way I am passing the argument.

Upvotes: 0

Views: 173

Answers (1)

datenwolf
datenwolf

Reputation: 162194

You can't do this in C/C++ legally:

vector3::operator float*() const
{
    float arr[3];
    // ...
    return arr;
}

It invokes undefined behavior. When operator float*() returns, arr goes out of scope and the pointer returned becomes invalid.

Consider yourself lucky, that you've got no nasal demons ;-)

Upvotes: 2

Related Questions