Background colour does not change on mac OS

Hello,

Can someone tell me what is wrong in my code below. My other project mates have the same exact code but it works fine for them. When i run this, i just get a black background and i cannot see if i’m drawing anything or not. They are using windows and I’m using mac.

//here is a part of my code//


//Draw the cicle with given parameters (size, color, position)
void drawCircle(float cx, float cy, float r, int num_segments) {
    glBegin(GL_LINE_LOOP);
    glColor3ub(colors[0],colors[1],colors[2]);
    glLineWidth(circleSize);
    for(int ii = 0; ii < num_segments; ii++) {
        float theta = 2.0f * 3.1415926f * (float)(ii) / (float)(num_segments);//get the current angle

        float x = r * cosf(theta);//calculate the x component
        float y = r * sinf(theta);//calculate the y component

        glVertex2f(x + cx, y + cy);//output vertex

    }
    glColor3f(1.0f, 1.0f, 1.0f);
    glEnd();
}

bool isInit = false;

void init() {
    glMatrixMode(GL_PROJECTION);
    float aspect = (float)windowWidth / (float)windowHeight;
    glOrtho(-aspect, aspect, -1, 1, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

bool pixelsLoaded = false;
GLuint pixelsTex;

void display() {
    if (!isInit) {
        init();
        isInit = true;
    }
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    if (reset) {
        glClear(GL_COLOR_BUFFER_BIT);
    }
    reset = false;

    if (menuMode) {
        if (!pixelsLoaded) {
            glGenTextures(1, &pixelsTex);
            glBindTexture(GL_TEXTURE_2D, pixelsTex);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, windowWidth, windowHeight, 0);
            glBindTexture(GL_TEXTURE_2D, 0);
            pixelsLoaded = true;
        }

        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1, 1, -1, 1, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glBindTexture(GL_TEXTURE_2D, pixelsTex);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(-1.0f, -1.0f);
        glTexCoord2i(0, 1); glVertex2i(-1.0f, 1.0f);
        glTexCoord2i(1, 1); glVertex2i(1.0f, 1.0f);
        glTexCoord2i(1, 0); glVertex2i(1.0f, -1.0f);
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        float aspect = (float)windowWidth / (float)windowHeight;
        glOrtho(-aspect, aspect, -1, 1, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    } else if (pixelsLoaded) {
        //reset texture
        pixelsLoaded = false;
    }


    bool redraw = callHeldInputs();

    /*glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
        glVertex2f(-squareSize[0] + offsetsUpRight[1], -squareSize[1] + offsetsUpRight[0]);
        glVertex2f(squareSize[0] + offsetsUpRight[1], -squareSize[1] + offsetsUpRight[0]);
        glVertex2f(squareSize[0] + offsetsUpRight[1], squareSize[1] + offsetsUpRight[0]);
        glVertex2f(-squareSize[0] + offsetsUpRight[1], squareSize[1] + offsetsUpRight[0]);
    glEnd();
    glColor3f(1.0f, 1.0f, 1.0f);*/
    drawCircle(0.0f + offsetsUpRight[1], 0.0f + offsetsUpRight[0], circleSize, 20);


    glFlush();
    glutPostRedisplay();
}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    screenWidth = glutGet(GLUT_SCREEN_WIDTH);
    screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
    if (argc > 1) {
        glutInitWindowSize(320, 320);
        moveSpeed = moveSpeed / 10;        
    } else {
        glutInitWindowSize(screenWidth, screenHeight);
    }
    glutCreateWindow("OpenGL Test");
    windowWidth = glutGet(GLUT_WINDOW_WIDTH);
    windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
    glutInitWindowPosition(50, 50);
    glutKeyboardFunc(readKeyboard);
    glutSpecialFunc(readSpecialKeyboard);
    glutSpecialUpFunc(readSpecialKeyboardUp);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

//end of code//

Ive learnt that there is something wrong with the mojave update of mac os. I’ve seen similar problems online with openGl after users have updated it to Mojave. But there are no working solutions for this.

Any suggestions?

Have you Checked for GL Errors?

It just gives me a lot of deprecation warnings while compiling it. Which is common on OS Mojave. But there are no errors.

Also, when i put a glClear(GL_COLOR_BUFFER_BIT); at the top of the display function, I do get a white background and i can see and move the circle I’m creating above. But then i cannot draw anything because obviously it will clear everytime i draw something.

Just in case it wasn’t clear, Dark Photon is referring to run-time errors reported by glGetError(), not compile-time errors.

Are you trying to draw on top of a previously-displayed frame? For that, you’d need to use framebuffer objects. The contents of the back buffer become undefined after a buffer swap. In some cases the contents will remain unchanged, but you can’t rely upon that.

[QUOTE=GClements;1292871]
Are you trying to draw on top of a previously-displayed frame? For that, you’d need to use framebuffer objects. The contents of the back buffer become undefined after a buffer swap. In some cases the contents will remain unchanged, but you can’t rely upon that.[/QUOTE]

Yes I’m keeping a frame and drawing on top it. So that it feels like drawing a line.
Can you please tell me how to use the framebuffer objects?

[QUOTE=batman999;1292872]Yes I’m keeping a frame and drawing on top it. So that it feels like drawing a line.
Can you please tell me how to use the framebuffer objects?[/QUOTE]
Create a FBO with glGenFramebuffers and glBindFramebuffer. Create a renderbuffer with glGenRenderbuffers and glBindRenderbuffer, create its storage with glRenderbufferStorage, bind it to the framebuffer with glFramebufferRenderbuffer.

Bind the FBO to GL_DRAW_FRAMEBUFFER, render, bind the FBO to GL_READ_FRAMEBUFFER, bind zero to GL_DRAW_FRAMEBUFFER (to make the default framebuffer the draw framebuffer), use glBlitFramebuffer to copy the FBO to the default framebuffer, swap buffers.

Framebuffer objects require OpenGL 3.0 or later. With earlier versions, off-screen rendering required the use of platform-specific extensions or relying upon the back buffer being preserved by a buffer swap (which depends upon the platform, hardware and driver). The alternative is to accumulate a list of primitives and render them all to update the frame.

Thanks a lot. I will try this.

Hi, I’m having the same issue the past week after Mojave update. I have tried all solution from :
[ol]
[li] Change glFlush() into glutSwapBuffers() to flush without a call[/li][li] Change glClear(GL_COLOR_BUFFER_BIT) into glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT) (this one causing even more warning). Also I tried to set the color into glClearColor(255,255,255,0) (white), but after clear color buffer, only black screen. So the glClear isn’t working.[/li][li] I tried to resize the window using reshape, which eventually work but causing screen tearing[/li][li] I updated Xcode compiler for terminal to the latest version (Beta)[/li][/ol]
This will not answer you code question, because probably there’s nothing wrong with your code. My code was also working perfectly on Sierra and then just black screen on Mojave. If you need quick fix, what I did was clean uninstall XCode 10 (which comes with Mojave update, you can find clean uninstall tutorial online), then from Apple Developer download XCode 9.41 (for Sierra) and also Command Tools 9.41 (for Sierra). Your program hopefully will work again, it works for me. Good luck !