2012-06-26 47 views
5

所以我有一段代碼可以在ubuntu機器上正常運行,但是無法在xcode或終端上運行。我試圖在xcode上運行它,但它主要失敗:glewInit()在macOS上失敗

「使用未聲明的標識符glewInit;您的意思是glutInit?」 「功能調用的參數太少,預期2,有0」

代碼冗長是由我的教授編寫的,它運行在ubuntus上。但是有了這些錯誤,我認爲原因是......好的,被宣稱的標識符包含缺失。所以,谷歌搜索後,我想通了,glewInit是GLEW庫的一部分 - >所以我下載的代碼,並與下面安裝了它在我的機器上:

使 須藤-s 使安裝

這是成功的安裝到我的/ usr/include/GL中。現在,當我輸入xcode #include或只是#include時,編譯器會拋出glew.h未找到(儘管我可以在usr/include/GL中看到自己的文件)。

下面是代碼:

#include "include/Angel.h" 

// The rotation around z axis 
GLfloat Theta = 0.0; // Angle (in degrees) 
GLfloat step = 0.01; // Incremental 
GLuint locTheta; 
enum { CW = 0, CCW = 1}; 
int direction = CW; // Direction 

//Scale along x and y axes 
GLfloat ScaleFactor[2] = {1.0, 1.0}; 
GLuint locScale; 

const int NumPoints = 4; 
void init(); 
void display(void); 
void reshape(GLsizei w, GLsizei h); 
void keyboard(unsigned char key, int x, int y); 
void mouse(int button, int state, int x, int y); 
void idle(void); 
//---------------------------------------------------------------------------- 

// OpenGL initialization 
void init() 
{ 
    // Vertices of a unit square centered at origin, sides aligned with axes 
    vec4 points[] = { 
     vec4(-0.5, -0.5, 0, 1.0), //v1 
     vec4( 0.5, -0.5, 0, 1.0), //v2 
     vec4(-0.5, 0.5, 0, 1.0), //v3 
     vec4( 0.5, 0.5, 0, 1.0) //v4 
    }; 

    // RGBA colors 
    vec4 colors[] = { 
     vec4(1.0, 0.0, 0.0, 1.0), // red 
     vec4(0.0, 1.0, 0.0, 1.0), // green 
     vec4(0.0, 1.0, 0.0, 1.0), // green 
     vec4(0.0, 0.0, 1.0, 1.0), // blue 
    }; 

    // Create and initialize a buffer object 
    GLuint buffer; 
    glGenBuffers(1, &buffer); 
    glBindBuffer(GL_ARRAY_BUFFER, buffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW); 
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points); 
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors); 

    // Load shaders and use the resulting shader program 
    GLuint program = InitShader("vshader_rot.glsl", "fshader_rot.glsl"); 
    glUseProgram(program); 

    // set up vertex arrays 
    GLuint vPosition = glGetAttribLocation(program, "vPosition"); 
    glEnableVertexAttribArray(vPosition); 
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 
       BUFFER_OFFSET(0)); 

    GLuint vColor = glGetAttribLocation(program, "vColor"); 
    glEnableVertexAttribArray(vColor); 
    glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 
       BUFFER_OFFSET(sizeof(points))); 

    // The location of shader uniform variables 
    locTheta = glGetUniformLocation(program, "theta"); 
    locScale = glGetUniformLocation(program, "scale"); 

    glClearColor(1.0, 1.0, 1.0, 1.0); 
} 

//---------------------------------------------------------------------------- 

void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glUniform1f(locTheta, Theta); 
    glUniform2fv(locScale, 1, ScaleFactor); 

    glDrawArrays(GL_TRIANGLE_STRIP, 0, NumPoints); 

    glutSwapBuffers(); 
} 

//---------------------------------------------------------------------------- 
void reshape(GLsizei w, GLsizei h) 
{ 
    glViewport(0, 0, w, h); 

    // Scale the square to avoid stretching 
    if (w > h) ScaleFactor[0] = (float)h/w; 
    if (w < h) ScaleFactor[1] = (float)w/h; 
} 

//---------------------------------------------------------------------------- 

void keyboard(unsigned char key, int x, int y) 
{ 
    switch(key) { 
    case 033: // Escape Key 
    case 'q': case 'Q': 
     exit(EXIT_SUCCESS); 
     break; 
    } 
} 

//---------------------------------------------------------------------------- 

void mouse(int button, int state, int x, int y) 
{ 
    if (state == GLUT_DOWN) { 
     switch(button) 
     { 
     case GLUT_LEFT_BUTTON:  
      direction = CCW; 
      break; 
     case GLUT_RIGHT_BUTTON: 
      direction = CW; 
      break; 
     } 
    } 
} 

//---------------------------------------------------------------------------- 

void idle(void) 
{ 
    // Animate the rotation 
    if (direction == CW)  
     Theta += step; 
    else 
     Theta -= step; 

    if (Theta > 360.0) { 
     Theta -= 360.0; 
    } 

    glutPostRedisplay(); 
} 

//---------------------------------------------------------------------------- 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); 
    glutInitWindowSize(512, 512); 
    glutCreateWindow("Rotating Color Square"); 

    glewInit(); 
    init(); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutKeyboardFunc(keyboard); 
    glutMouseFunc(mouse); 
    glutIdleFunc(idle); 

    glutMainLoop(); 
    return 0; 
} 

我有獅子10.7.4和xcode的4.2.1

+0

你可以在Angel.h中顯示什麼。 – deebee

回答

17

glewInit()調用(以及當然包括)不是在MacOS必要,所以你可以這樣排除它:

#ifndef __APPLE__ 
glewInit(); 
#endif 

與包含相同。

現在與未解決的符號。你必須包括的MacOSX的本地GL標題:

#ifdef __APPLE__ 
# include <OpenGL/gl.h> 
# include <OpenGL/glext.h> 
#else /// your stuff for linux 
# include "GL/GL.h" 
.... whatever 
#endif 

OpenGL是一個核心技術OSX,而​​不是一個「擴展名」,如Linux的/ X窗口。因此,只需將OpenGL和GLUT框架包含到您的XCode項目中,並希望它能夠構建和工作。

+0

真棒,謝謝:)似乎修復) – user1039063

+0

是的,有時似乎很奇怪,在MacOS上的發展有點容易:)不要猶豫,upvote答案,順便說一句:) –

+0

甚至接受答案...; ) –