glVertex3f and glVertex2fx displaying different results
up vote
0
down vote
favorite
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.
c++ arrays opengl casting
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 2 more comments
up vote
0
down vote
favorite
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.
c++ arrays opengl casting
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
4
float arr[3];is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.
– Rabbid76
Nov 21 at 5:55
What isorigin?glVertex3fvexpects a pointer to 3 contiguousGLfloats. Are you sure thatoriginhas a type which grants this? What type does it actually have?
– Scheff
Nov 21 at 7:18
I assume, you want to callglVertexfor your DIY typevector3? How about an "overload"? E.g.void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); }Then, you could do e.g.vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)
– Scheff
Nov 21 at 7:21
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources ofvector3it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.
– Bartek Banachewicz
Nov 21 at 8:49
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
c++ arrays opengl casting
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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.
c++ arrays opengl casting
c++ arrays opengl casting
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 21 at 5:44
jinenofu
1
1
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
jinenofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
4
float arr[3];is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.
– Rabbid76
Nov 21 at 5:55
What isorigin?glVertex3fvexpects a pointer to 3 contiguousGLfloats. Are you sure thatoriginhas a type which grants this? What type does it actually have?
– Scheff
Nov 21 at 7:18
I assume, you want to callglVertexfor your DIY typevector3? How about an "overload"? E.g.void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); }Then, you could do e.g.vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)
– Scheff
Nov 21 at 7:21
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources ofvector3it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.
– Bartek Banachewicz
Nov 21 at 8:49
|
show 2 more comments
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
4
float arr[3];is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.
– Rabbid76
Nov 21 at 5:55
What isorigin?glVertex3fvexpects a pointer to 3 contiguousGLfloats. Are you sure thatoriginhas a type which grants this? What type does it actually have?
– Scheff
Nov 21 at 7:18
I assume, you want to callglVertexfor your DIY typevector3? How about an "overload"? E.g.void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); }Then, you could do e.g.vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)
– Scheff
Nov 21 at 7:21
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources ofvector3it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.
– Bartek Banachewicz
Nov 21 at 8:49
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
4
4
float arr[3]; is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.– Rabbid76
Nov 21 at 5:55
float arr[3]; is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.– Rabbid76
Nov 21 at 5:55
What is
origin? glVertex3fv expects a pointer to 3 contiguous GLfloats. Are you sure that origin has a type which grants this? What type does it actually have?– Scheff
Nov 21 at 7:18
What is
origin? glVertex3fv expects a pointer to 3 contiguous GLfloats. Are you sure that origin has a type which grants this? What type does it actually have?– Scheff
Nov 21 at 7:18
I assume, you want to call
glVertex for your DIY type vector3? How about an "overload"? E.g. void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); } Then, you could do e.g. vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)– Scheff
Nov 21 at 7:21
I assume, you want to call
glVertex for your DIY type vector3? How about an "overload"? E.g. void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); } Then, you could do e.g. vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)– Scheff
Nov 21 at 7:21
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources of
vector3 it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.– Bartek Banachewicz
Nov 21 at 8:49
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources of
vector3 it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.– Bartek Banachewicz
Nov 21 at 8:49
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
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 ;-)
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member functionglvertexthat internally calls glVertex.
– datenwolf
Nov 22 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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 ;-)
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member functionglvertexthat internally calls glVertex.
– datenwolf
Nov 22 at 7:34
add a comment |
up vote
2
down vote
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 ;-)
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member functionglvertexthat internally calls glVertex.
– datenwolf
Nov 22 at 7:34
add a comment |
up vote
2
down vote
up vote
2
down vote
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 ;-)
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 ;-)
edited Nov 21 at 14:53
answered Nov 21 at 11:33
datenwolf
131k9128231
131k9128231
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member functionglvertexthat internally calls glVertex.
– datenwolf
Nov 22 at 7:34
add a comment |
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member functionglvertexthat internally calls glVertex.
– datenwolf
Nov 22 at 7:34
So what should I do?
– jinenofu
Nov 22 at 0:05
So what should I do?
– jinenofu
Nov 22 at 0:05
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
For one (that's OpenGL related): Don't use immediate mode (anything that makes use of glBegin/glEnd). It's outdated for over 20 years. But if you really want to go down that road and insist on using the …v versions, then put your x, y, z members at contiguous locations in your class, or use an array there and return a pointer to that. However this will require, that you don't use that pointer, when the class instance goes out of scope.
– datenwolf
Nov 22 at 7:33
@jinenofu: You could also introduce a new class member function
glvertex that internally calls glVertex.– datenwolf
Nov 22 at 7:34
@jinenofu: You could also introduce a new class member function
glvertex that internally calls glVertex.– datenwolf
Nov 22 at 7:34
add a comment |
jinenofu is a new contributor. Be nice, and check out our Code of Conduct.
jinenofu is a new contributor. Be nice, and check out our Code of Conduct.
jinenofu is a new contributor. Be nice, and check out our Code of Conduct.
jinenofu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405898%2fglvertex3f-and-glvertex2fx-displaying-different-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi @jinenofu please provide a Minimum, Complete and Verifiable example see: stackoverflow.com/help/mcve
– AdaRaider
Nov 21 at 5:51
4
float arr[3];is a local variable in a method. You return a pointer to local data. The data go out of scope (are lost) after the function has terminated. Read a basic C++ or C tutorial.– Rabbid76
Nov 21 at 5:55
What is
origin?glVertex3fvexpects a pointer to 3 contiguousGLfloats. Are you sure thatoriginhas a type which grants this? What type does it actually have?– Scheff
Nov 21 at 7:18
I assume, you want to call
glVertexfor your DIY typevector3? How about an "overload"? E.g.void glVertex(const vector3 &v) { glVertex3f(v.x, v.y, v.z); }Then, you could do e.g.vector3 origin(1, 2, 3); glVertex(origin);. (I made some assumptions about your code but I guess you got the idea.)– Scheff
Nov 21 at 7:21
This is a C++ problem, not an OpenGL one. You're returning an address to a temporary. UB. Without seeing the sources of
vector3it's hard to answer definitely, but the recommendation I'd give is the same - ditch your own class and just use GLM. There are apparently more ways to write a vec3 badly than to do it correctly.– Bartek Banachewicz
Nov 21 at 8:49