1
#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

void GraphiqueAffichage() {
    glClearColor(1.0, 1.0, 0.5, 0.5);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glFlush();
}

int main(int argc, const char * argv[]) {
    // insert code here...
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(480, 272);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutCreateWindow("Bonjour");
    glutDisplayFunc(GraphiqueAffichage);
    glutMainLoop();
    return 0;
}

Hello I am on a Mac using OS X 10.12, ans with this code, no window is displayed, is it normal ? Why ? Please help me. The compilation is correct, no error, build successful, but no window is created ! I tried this code that works with windows but I have a Mac and it does not work, how to make it working ?

2 Answers2

3

The compilation is correct, no error, build successful ...

but you get a list of errors when you run the program, right? "Successfully compiling" does not (alas) mean your code is correct.

Looking up the very first error message, it seems you forgot to call glutInit first:

int main(int argc, char * argv[]) {
    glutInit(&argc, argv);
    glutInitWindowPosition(10, 10);
    ...

(right where your code says, "insert code here"...)

man glutInit tells you why it failed as well:

glutInit will initialize the GLUT library and negotiate a session with the window system.

where "the window system" is Mac OS X.

In addition, your main is wrong. argv is not a const char * – with that const specifier, your compiler will yell at you.

With these changes, I get a nice yellow window – your glClearColor – and with the custom title "Bonjour".

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • yes, but it was not written in the book I bought, is it normal ? – johnathan Mhehdi Sep 17 '16 at 17:08
  • A note on http://linux.die.net/man/3/glutinit says this: "You really should always call this, even if you are a WIN32 user." The author of your book may not have been aware of this. Some toolchains may complain [`glutInit` is deprecated](http://stackoverflow.com/q/24095931/2564301); I guess that mainly depends on what version you are using. FWIW: your code, with changes, does not issue that specific warning on my system – Mac OS X 10.7.5, using `clang`. – Jongware Sep 17 '16 at 17:56
  • @johnathanMhehdi: I guess the question is, does your program work with this? If not, I'll have to look further into this. You may need to add what book your code came from. – Jongware Sep 18 '16 at 03:41
1

You need initializer glut

glutInit(&argc, argv);

in your main.

//#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

void GraphiqueAffichage() {
    glClearColor(1.0, 1.0, 0.5, 0.5);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glFlush();
}

int main(int argc, const char * argv[]) {
    // insert code here...
    glutInit(&argc, argv);
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(480, 272);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutCreateWindow("Bonjour");
    glutDisplayFunc(GraphiqueAffichage);
    glutMainLoop();
    return 0;
}
EdCornejo
  • 741
  • 13
  • 14