1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
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. May 16, 2007 #1
    I'm having some trouble on OS X following a tutorial that wasn't really meant for this language-OS combination.

    I'm using Xcode and the GLUT+OpenGL frameworks to compile the following code. [it's supposed to open a window and display a white square and triangle on a black backdrop.]:
    Code (Text):

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

    #define kWindowWidth    800
    #define kWindowHeight   300

    GLvoid InitGL(GLvoid);
    GLvoid DrawGLScene(GLvoid);
    GLvoid ReSizeGLScene(int Width, int Height);

    int main(int argc, char** argv)
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize (kWindowWidth, kWindowHeight);
        glutInitWindowPosition (100, 100);
        glutCreateWindow (argv[0]);




        return 0;

    GLvoid ReSizeGLScene(int width, int height)             // Resize And Initialize The GL Window
        if (height==0)                              // Prevent A Divide By Zero By
            height=1;                           // Making Height Equal One

        glViewport(0, 0, width, height);   
            glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
        glLoadIdentity();                           // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window

        glMatrixMode(GL_MODELVIEW);                     // Select The Modelview Matrix
        glLoadIdentity();                           // Reset The Modelview Matrix

    GLvoid InitGL(GLvoid)                               // All Setup For OpenGL Goes Here
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);      
        glClearDepth(1.0f);                         // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
    GLvoid DrawGLScene(GLvoid)                              // Here's Where We Do All The Drawing
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
        glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
            glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
            glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
            glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
        glEnd();                            // Finished Drawing The Triangle

        glBegin(GL_QUADS);                      // Draw A Quad
            glVertex3f(-1.0f, 1.0f, 0.0f);              // Top Left
            glVertex3f( 1.0f, 1.0f, 0.0f);              // Top Right
            glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
            glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
        glEnd();                            // Done Drawing The Quad
    ...I get a window but it's white with nothing in it. When I resize the window to particular sizes I can see the black background and square only but obviously something is still wrong...

    The only code I can find is written in Objective-C and uses some NextStep stuff.

    NeHe tutotirlas:
  2. jcsd
  3. May 17, 2007 #2
    For the DrawGLScene(GLvoid) before the glBegin(...) commands put glColor3f(1.0, 1.0, 1.0). You might want to change the colors if you feel like.

    However I didn't find a resonable sollution yet. I didn't find what may be wrong yet too. You can try that and see if it works though.
  4. May 17, 2007 #3
    Haha I found it. I don't know why I didn't notice. At the end of the redraw function DrawGLScene(GLvoid) add glutSwapBuffers(). and you might also want the glColorf() too. Hope that works. I didn't look at the tutorial yet, I trust you copyed and pasted the code correctly.
  5. May 17, 2007 #4
    Thanks a lot, it works now.

    I was actually missing a translate command too, so that the square and triangle weren't on top of eachother.
    Last edited: May 17, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?