Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

OpenGL on OS X

  1. Jul 18, 2005 #1
    I'm having trouble getting the basic OpenGL app running properly. When it opens the window is entirely white when it should have a black background and white triangle.

    I will explain the code in bold bellow

    Code (Text):

    #include <OpenGL/gl.h>
    #include <OpenGL/glu.h>
    #include <GLUT/glut.h>

    int initGL(GLvoid);
    GLvoid drawScene(GLvoid);
    GLvoid resizeScene(int width, int height);

    int
    main(int argc, char *argv[])
    {
        const int WINDOW_HEIGHT = 300;
        const int WINDOW_WIDTH = 400;
       
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        glutInitWindowPosition(100, 100);
        glutCreateWindow(argv[0]);
       
        initGL();
       
        glutDisplayFunc(drawScene);
        glutReshapeFunc(resizeScene);
        [b]
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(0.0, 1.0, 1.0);
       glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
       glBegin(GL_POLYGON);
          glVertex2f(-0.5, -0.5);
          glVertex2f(-0.5, 0.5);
          glVertex2f(0.5, 0.5);
          glVertex2f(0.5, -0.5);
       glEnd();
       glFlush();
        [/b]
        glutMainLoop();
       
        return 0;
    }

    int
    initGL(GLvoid)
    {
        glShadeModel(GL_SMOOTH);
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
       
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
       
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
       
        return 0;
    }

    GLvoid
    resizeScene(int width, int height)
    {
        if (height == 0)
            height = 1;
           
        glViewport(0, 0, width, height);
       
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
       
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
       
        return;
    }

    GLvoid
    drawScene(GLvoid)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
       
        glTranslatef(-1.5f, 0.0f, -6.0f);
        glColor3f(1.0,0.0,0.0);
        glBegin(GL_TRIANGLES);
            glVertex3f(0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
        glEnd();
       
        return;
    }

     
    The code in bold, when added to the rest, produces a black background and white square like it's suppose to. I don't know if that tells you anything about the rest of my code or not...
     
  2. jcsd
  3. Jul 18, 2005 #2
    Argh! It seems that a silly glFlush(); fixed my problems. The code in bold above was from a book and gave me the hint :tongue:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: OpenGL on OS X
  1. Quick OS X Question (Replies: 1)

Loading...