一个OpenGL小程序,为什么Mac下不能正常运行呢?
是我从老师那拷来的一个例子程序,是可以显示三个球体,模拟公转自转的小程序。系统OS X 10.6.4
我用Xcode,之前写一点更简单的OpenGL程序是可以运行且正常的。
这个程序没有报错,但打开的窗口却一片漆黑没有任何图形。
我试了下,在Windows的vc环境下是可以正常运行的。但在Mac下就没有图形出来。不知道为什么。。
求解……
以下是程序代码:
#include <glut/glut.h>
#include <math.h>
#define C_PI 3.14159265358979
GLdouble distanceSunToEarth = 70;
GLdouble distanceMoonToEarth = 35;
GLdouble gzAngleEarth = 0;
GLdouble gzAngleMoon = 0;
GLsizei zzAngle = 0;
int earthX = 70;
int earthY = 0;
int earthZ = 0;
int moonX = 70 + 35;
int moonY = 0;
int moonZ = 0;
void drawPlanet(int r, GLdouble x0, GLdouble y0, GLdouble z0 )
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(70.0 , 1.0, 0.0, 0.0);
glTranslatef(x0, y0, z0);
glRotatef(zzAngle, 0.0, 0.0, 1.0);
glutWireSphere(r, 30, 10);
glPopMatrix();
}
void init()
{
glClearColor(0.0, 0.0 , 0.0, 1.0);
glShadeModel(GL_FLAT);
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 70 + 100, 0.0, 0.0, 0.0, 0.0, 1.0, 70 + 100);
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'r':
gzAngleEarth += C_PI/180;
earthX = (GLdouble)70 * cos(gzAngleEarth);
earthY = 70 * sin(gzAngleEarth);
zzAngle = (zzAngle + 10)%360;
gzAngleMoon += C_PI/45;
moonX = earthX + 35 * cos(gzAngleMoon);
moonY = earthY + 35 * sin(gzAngleMoon);
glutPostRedisplay();
break;
case 's':
glutPostRedisplay();
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
//draw sun
drawPlanet(30, 0, 0, 0);
//draw earth
drawPlanet(20, earthX, earthY, earthZ);
//draw moon
drawPlanet(10, moonX, moonY, moonZ);
glutSwapBuffers();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(0, 0);
glutCreateWindow("Rotate1");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
//glutTimerFunc(100, timer, );
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}