我必须包含哪些库来访问GLUT / OpenGL(没有XCode的Mac)?

时间:2012-06-19 04:13:38

标签: c macos opengl glut

标题是问题的核心,但问题来自于我从SpaceSimulator.net的教程中获得的代码。我会在这里为你发布。

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

typedef struct {
    float x,y,z;
} vertex_type;

typedef struct {
    int a,b,c;
} polygon_type;

#define MAX_POLYGONS 2000
polygon_type polygon[MAX_POLYGONS];

#define MAX_VERTICES 2000
vertex_type vertex[MAX_VERTICES];

typedef struct {
    vertex_type vertex[MAX_VERTICES];
    polygon_type polygon[MAX_POLYGONS];
} obj_type,*obj_type_ptr;

int screen_width, screen_height, filling;
GLfloat rotation_x_increment, rotation_y_increment, rotation_z_increment;
GLfloat rotation_x, rotation_y, rotation_z;

obj_type cube =
{
    {
        -10,-10, 10, //vertex v0
         10,-10, 10, //vertex v1
         10,-10,-10, //vertex v2
        -10,-10,-10, //vertex v3
        -10, 10, 10, //vertex v4
         10, 10, 10, //vertex v5
         10, 10,-10, //vertex v6
        -10, 10,-10  //vertex v7
    },
    {
        0, 1, 4, //polygon v0,v1,v4
        1, 5, 4, //polygon v1,v5,v4
        1, 2, 5, //polygon v1,v2,v5
        2, 6, 5, //polygon v2,v6,v5
        2, 3, 6, //polygon v2,v3,v6
        3, 7, 6, //polygon v3,v7,v6
        3, 0, 7, //polygon v3,v0,v7
        0, 4, 7, //polygon v0,v4,v7
        4, 5, 7, //polygon v4,v5,v7
        5, 6, 7, //polygon v5,v6,v7
        3, 2, 0, //polygon v3,v2,v0
        2, 1, 0, //polygon v2,v1,v0
    }
};


void init(void)
{
   glClearColor(0.0, 0.0, 0.2, 0.0);
   glShadeModel(GL_SMOOTH);
   glViewport(0,0,screen_width,screen_height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);
   glEnable(GL_DEPTH_TEST);
   glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
}

void resize(int width, int height)
{
   screen_width=width; 
   screen_height=height; 
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glViewport(0,0,screen_width,screen_height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);
   glutPostRedisplay ();
}

void keyboard_s (int key, int x, int y)
{
   switch (key)
   {
      case GLUT_KEY_UP:
         rotation_x_increment = rotation_x_increment +0.005;
      break;
      case GLUT_KEY_DOWN:
         rotation_x_increment = rotation_x_increment -0.005;
      break;
      case GLUT_KEY_LEFT:
         rotation_y_increment = rotation_y_increment +0.005;
      break;
      case GLUT_KEY_RIGHT:
         rotation_y_increment = rotation_y_increment -0.005;
      break;
   }
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key)
   {
      case ' ':
         rotation_x_increment=0;
         rotation_y_increment=0;
         rotation_z_increment=0;
      break;
      case 'r': case 'R':
         if (filling==0)
         {
            glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
            filling=1;
         } 
         else 
         {
            glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
            filling=0;
         }
      break;
   }
}

void display(void)
{
   int l_index;
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0,0.0,-50);
   rotation_x = rotation_x + rotation_x_increment;
   rotation_y = rotation_y + rotation_y_increment;
   rotation_z = rotation_z + rotation_z_increment;
   if (rotation_x > 359) rotation_x = 0;
   if (rotation_y > 359) rotation_y = 0;
   if (rotation_z > 359) rotation_z = 0;
   glRotatef(rotation_x,1.0,0.0,0.0);
   glRotatef(rotation_y,0.0,1.0,0.0);
   glRotatef(rotation_z,0.0,0.0,1.0);
   glBegin(GL_TRIANGLES);
   for (l_index=0;l_index<12;l_index++)
   {
      glColor3f(1.0,0.0,0.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].a ].x, cube.vertex[ cube.polygon[l_index].a ].y, cube.vertex[ cube.polygon[l_index].a ].z);
      glColor3f(0.0,1.0,0.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].b ].x, cube.vertex[ cube.polygon[l_index].b ].y, cube.vertex[ cube.polygon[l_index].b ].z);
      glColor3f(0.0,0.0,1.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].c ].x, cube.vertex[ cube.polygon[l_index].c ].y, cube.vertex[ cube.polygon[l_index].c ].z);
   }
   glEnd();
   glFlush();
   glutSwapBuffers();
}

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screen_width,screen_height);
    glutInitWindowPosition(0,0);
    glutCreateWindow("www.spacesimulator.net - 3d engine tutorials: Tutorial 2");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc (resize);
    glutKeyboardFunc (keyboard);
    glutSpecialFunc (keyboard_s);
    init();
    glutMainLoop();
}

好的,所以这给了g ++的错误:

    Undefined symbols for architecture x86_64:
      "_glClear", referenced from:
          display()    in ccUTBKlR.o
          resize(int, int)in ccUTBKlR.o
... tons more of the same-style errors for every gl/glut function ...

附带问题:这是什么编译阶段?这是过去的语法错误,但没有完全编译。

所以我怀疑我所包含的库不正确,或者这些库的Mac版本有些不同;虽然我怀疑后者,因为GLUT应该是便携式的。

为了获得这些GLUT / OpenGL功能,我必须包含哪些库?我知道我必须包括'GLUT / OpenGL';我希望有一些更具体的东西。

另外,我真的有一个痛苦的地方从C / C ++转移到Objective-C和XCode,所以我希望有一个解决方案不涉及这个转换。

2 个答案:

答案 0 :(得分:9)

我迟到了,但我想为那些遇到同样问题的人省下一些努力。坦率地说,约翰威尔夫的回答并不清楚。

OpenGL和GLUT附带OS和Xcode安装。默认路径为/System/Library/Frameworks/OpenGL.framework/System/Library/Frameworks/GLUT.framework

您可以直接包含所需的标头,并使用以下链接器选项在命令行上编译代码:

gcc YOUR_CODE -framework OpenGL -framework GLUT

btw,“GLUT”在Windows上是“GL”。如果要避免跨平台问题,可以使用以下预处理器语句:

#if defined(__APPLE__) && defined(__MACH__)
#  include <GLUT/glut.h>
#else 
#  include <GL/glut.h>
#endif

答案 1 :(得分:2)

将OpenGL Framework添加到链接器选项。 GLUT不是OpenGL的一部分,必须单独安装。安装后,您也可以添加GLUT框架。