Reputation: 2386
I am currently trying to learn both C++ and OpenGL, and I am beginning with the following code example:
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}
It says here that this code can be compiled on MacOS using the following command:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
So I adapt the command in the following way:
gcc -Wno-deprecated-declarations -o simple simple.cpp -framework GLUT -framework OpenGL -framework Carbon
This seems to work and creates an executable called simple
.
I am told that this code should generate a white square on a black background as follows:
However, when I try to run this file from the terminal using ./simple
, the program runs continuously but nothing happens (that is, no window is generated at all), and so I have to terminate the program from the terminal.
Did I do something wrong here, or is this expected for the given code on MacOS?
To see what would happen, I tried to use the code presented in the aforementioned guide:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutDisplayFunc(display);
glutMainLoop();
}
As the guide says, this is "a simple OpenGL program that does nothing".
I compile it as follows:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
This compiles fine. However, when I try to run the executable, I get the following error:
GLUT Fatal API Usage: main loop entered with no windows created.
zsh: abort ./hello
According to this error message, the program was terminated because no windows were created. However, as I said, my simple.cpp program does not terminate (I have to forcefully terminate it), and it is written to create windows. So I'm guessing this means that windows are created in simple.cpp, but for some reason they just don't show up on MacOS? Can anyone else confirm this? Is the issue perhaps that blank windows don't show up on MacOS, and you need to include other graphical elements in order to make the window show?
The problem is that my understanding is that glutCreateWindow("simple");
in simple.cpp
is supposed to create a window with "simple" as its title, so I don't understand why it isn't working.
Thanks to derhass's answer, I was able to make it work:
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Upvotes: 1
Views: 905
Reputation: 45322
YOu did not include the call to glutInit()
into your simple.cpp
example. You can't call any other GLUT commands before this intialization, and doing so results in undefined behavior.
Upvotes: 2