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.
c++ arrays opengl casting
|
show 2 more comments
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
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
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
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
asked Nov 21 at 5:44
jinenofu
1
1
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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
132k10131234
132k10131234
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 |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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