简体   繁体   中英

Why won't this OpenGL program open a window?

I am currently trying to learn both C++ and OpenGL, and I am beginning with the following code example:

simple.cpp

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


EDIT1

To see what would happen, I tried to use the code presented in the aforementioned guide :

hello.c

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


EDIT2

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.


EDIT3

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM