Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Background colour does not change on mac OS

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2018
    Posts
    5

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

    Code cpp:
    //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//
    Last edited by Dark Photon; 11-01-2018 at 05:55 AM.

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2018
    Posts
    5

    OpenGl Mac os Mojave

    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?

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,542

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2018
    Posts
    5
    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.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Quote Originally Posted by batman999 View Post
    It just gives me a lot of deprecation warnings while compiling it. Which is common on OS Mojave. But there are no errors.
    Just in case it wasn't clear, Dark Photon is referring to run-time errors reported by glGetError(), not compile-time errors.

    Quote Originally Posted by batman999 View Post
    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.
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2018
    Posts
    5
    Quote Originally Posted by GClements View Post
    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.
    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?

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Quote Originally Posted by batman999 View Post
    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?
    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.

  8. #8
    Junior Member Newbie
    Join Date
    Oct 2018
    Posts
    5
    Thanks a lot. I will try this.

  9. #9
    Junior Member Newbie
    Join Date
    Aug 2018
    Posts
    4
    Hi, I'm having the same issue the past week after Mojave update. I have tried all solution from :
    1. Change glFlush() into glutSwapBuffers() to flush without a call
    2. 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.
    3. I tried to resize the window using reshape, which eventually work but causing screen tearing
    4. I updated Xcode compiler for terminal to the latest version (Beta)

    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 !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •