No polygons displayed when porting code from Windows to OS X

I’m having trouble with a program that I am porting from Windows to OS X. I have two programs to port. The first one ported fine, and displays the same in Windows and OS X with the same source code (except the glut includes).

However, my second program won’t display any polygons. It is as if the lights are just turned off, and all I can see is my background clear color. I can’t seem to figure it out and it is driving me crazy. I have chopped everything down to a very short chunk of code in my attempts to troubleshoot the issue.

I’m hoping that someone here could take a look at my code, and see if I am missing something. In Windows, this code will display a simple rectangular box, that is very short. In OS X, nothing.

If someone would be so kind as to look this over and let me know, it would be greatly appreciated. This is the code that works fine in Windows, but not in OS X. I attempted to post my code in this thread, but the form rejected the submission. I see that people have posted short segments of code. Is there a way to do it? Mine is about 400 lines.

Thank you

Put it up on pastebin and supply a link.

Thank you. Here is the pastebin code (the forum rejects the URL)
hcM7SRkh

OK, I got it chopped down to just a simple sphere, and the forum seems to allow it. This code displays the sphere in Windows, but not in OS X. Any ideas as to what I am missing?


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>

// Windows
#include <GL/glut.h>

// OS X
//#include <GLUT/glut.h>
//#include <OpenGL/gl.h>

using namespace std;

#define PI 3.14159  // sin cosine cosine sin 3.14159!
static GLfloat white[] = {1.0,1.0,1.0,1.0};

int win = 0;
static GLfloat lpos1[] = {0.0,10.0,10.0,1.0};
static GLdouble cpos[3];
static GLdouble lookpos[3];
static float alpha = 2 * PI;
static float beta = PI/25.0;
static float camera_distance = 15;
static int ww, hh;


void draw()
{	
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glColor3f(1.0,0.0,0.0);
	glutSolidSphere(1,50,50);
}
           
    bool init()
    {
        glClearColor(0.2,0.2,0.2,0.0);
        
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_SMOOTH);
        glDepthRange( 0.0, 1.0);
        
        lookpos[0] = 0.0;
        lookpos[1] = 2.0;
        lookpos[2] = 0.0;
        
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
        
        glEnable(GL_LIGHTING);
        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
        glEnable(GL_LIGHT1);

        return true;
    }

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        
        cpos[0] = camera_distance * cos(beta) * sin(alpha);
        cpos[1] = camera_distance * sin(beta);
        cpos[2] = camera_distance * cos(beta) * cos(alpha);
        
        gluLookAt(cpos[0],cpos[1],cpos[2], lookpos[0],lookpos[1],lookpos[2], 0.0,1.0,0.0);
        
        //Draw light sphere*****************
        glLightfv(GL_LIGHT1,GL_POSITION,lpos1);
        glLightfv(GL_LIGHT1,GL_DIFFUSE, white);
        glLightfv(GL_LIGHT1,GL_SPECULAR, white);
        
        glPushMatrix();
		draw();
        glPopMatrix();
        
        glFlush();
        glutSwapBuffers();
    }
           
    void resize(int w, int h)
    {
        ww = w;
        hh = h;
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, w, h);
        gluPerspective(45.0f, 1.0f * w / h, 1.0f, -100.0f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
           
    void keyboard(unsigned char key, int x, int y)
    {
        switch(key)
        {
            case 27 :
                exit(1);
                break;
        }
    }
                 
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        
        glutInitWindowPosition(50, 50);
        glutInitWindowSize(800, 600);
        
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        
        win = glutCreateWindow("test");
               
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutReshapeFunc(resize);
        
        if (!init())
            return 1;
        
        glutMainLoop();
        
        return 0;
    }
             

Here’s how to debug your problem:

  1. get OpenGL Profiler (it’s no longer included by default with Xcode. Go to developer.apple.com, log into Mac Dev Center, View All Downloads, get the most recent “Graphics Tools for Xcode”.)
  2. run OpenGL Profiler. In the Breakpoints window, click “Break on GL error”, then launch your app with it.
  3. you’ll break at glFrustum with GL_INVALID_VALUE. You can see the backtrace with the line number of your code causing the problem.
  4. look at your code:
    gluPerspective(45.0f, 1.0f * w / h, 1.0f, -100.0f);
  5. read the man page for gluPerspective and glFrustum.

… unrelated, but you’re attempting to use a depth buffer without allocating one in glutInitDisplayMode()

I followed your steps, and I was able to confirm the issue exactly as you described. I can’t thank you enough. Not only did you solve my initial issue, you showed me an excellent debugging tool, and even pointed out a future area of concern.

Thank you, thank you, thank you!