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.










share|improve this question







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 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












  • 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

















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.










share|improve this question







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 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












  • 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















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.










share|improve this question







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






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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 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












  • 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




















  • 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? 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












  • 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


















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














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 ;-)






share|improve this answer























  • 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 function glvertex that internally calls glVertex.
    – datenwolf
    Nov 22 at 7:34











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',
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
});


}
});






jinenofu is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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








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 ;-)






share|improve this answer























  • 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 function glvertex that internally calls glVertex.
    – datenwolf
    Nov 22 at 7:34















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 ;-)






share|improve this answer























  • 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 function glvertex that internally calls glVertex.
    – datenwolf
    Nov 22 at 7:34













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 ;-)






share|improve this answer














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 ;-)







share|improve this answer














share|improve this answer



share|improve this answer








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 function glvertex that internally calls glVertex.
    – datenwolf
    Nov 22 at 7:34


















  • 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 function glvertex that 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










jinenofu is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Sphinx de Gizeh

Dijon

Équipe cycliste