gpt4 book ai didi

c++ - glewInit() 在 macOS 上失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:04 24 4
gpt4 key购买 nike

所以我有一段代码在 ubuntu 机器上运行良好,但在 xcode 或通过终端无法运行。我试图在 xcode 上运行它,但它在 main 上失败了:

“使用未声明的标识符 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;
}

我有 Lion 10.7.4 和 xCode 4.2.1

最佳答案

glewInit() 调用(当然还有 includes)在 MacOS 上不是必需的,因此您可以这样排除它:

#ifndef __APPLE__
glewInit();
#endif

与包含相同。

现在有未解析的符号。您必须包含 MacOSX 的 native GL header :

#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 Window 中的“扩展”。因此,只需将 OpenGL 和 GLUT 框架包含到您的 XCode 项目中,并希望它能够构建和运行。

关于c++ - glewInit() 在 macOS 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com