mac osx 使用glut

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LaineGates/article/details/80543134

mac osx 自带opengl,而且版本较高,通常来讲,使用自带的glut就可以解决问题。
PS:IDE可能找不到头文件,但编译器找得到
(需先安装XCode)
如下:

#include <GLUT/glut.h>
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
#include <iostream>
using namespace std;

// 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 draw()
{
        glClearColor(0.2, 0.3, 0.3, 1);
        glClear(GL_COLOR_BUFFER_BIT);

        //开始画一个三角形
        glBegin(GL_TRIANGLES);
        glColor3f(1, 0, 0); //Red
        glVertex3f(0, 1, 1);

        glColor3f(0, 1, 0); //Green
        glVertex3f(-1, -1, 0);

        glColor3f(0, 0, 1); //Blue
        glVertex3f(1, -1, 0);
        //结束一个画图步骤
        glEnd();
        return;

}

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

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

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

    draw();

    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_3_2_CORE_PROFILE | 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 );

    const GLubyte* name = glGetString(GL_VENDOR);//返回负责当前OpenGL实现厂商的名字
    const GLubyte* biaoshifu = glGetString(GL_RENDERER);//返回一个渲染器标识符,通常是个硬件平台
    const GLubyte* OpenGLVersion =glGetString(GL_VERSION);//返回当前OpenGL实现的版本号
   const  GLubyte* gluVersion=gluGetString(GLU_VERSION); //返回当前GLU工具库版本
   printf("OpenGL实现厂商的名字:%s\n", name);
   printf("渲染器标识符:%s\n", biaoshifu);
   printf("OOpenGL实现的版本号:%s\n",OpenGLVersion );
   printf("OGLU工具库版本:%s\n", gluVersion);


    glutMainLoop();
    return 0;
}

自己按网上帖子尝试了glfw和glew,使用固定pipline时,没有问题,一旦用到shader,就会出各种奇特的bug。看到这情况,果断回头用glut了。

编译参数:

g++ main.cpp -framework OpenGL -framework GLUT ...

另附几个不错的帖子:
mac + openGL 3.2+
OpenGL3.2+特性使用
assimp+OpenGL
mac+glew+glfw

猜你喜欢

转载自blog.csdn.net/LaineGates/article/details/80543134