美文网首页
在MAC上配置OpenGL

在MAC上配置OpenGL

作者: 丁勒个东 | 来源:发表于2020-08-16 17:54 被阅读0次

    准备工作

    配置

    • 打开Xcode -> macOS -> Cocoa Application 创建Mac项目;
    • 添加OpenGl.framework GLUT.framework 两个系统库;
    • 添加CLTools.h,glew.h,在Bulid Settings中 Header Search path 选项添加CLTool.h 和glew.h 的路径;
    • 拖动libGLTools.a静态库到工程的frameworks文件夹中.
    • 删除AppDelegate.h /AppDelegate.m /main.m/ViewController.h/ViewController.m,创建main.cpp文件

    测试

    复制代码到main.cpp文件中

    #include "GLShaderManager.h"
    #include "GLTools.h"
    #include <GLUT/GLUT.h>
    
    GLShaderManager shaderManager;
    GLBatch triangleBatch;
    
    void changeSize(int w,int h)
    {
        glViewport(0, 0, w, h);
    }
    
    void RenderScene(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
        GLfloat vRed[] = {1.0,0.0,0.0,1.0f};
        shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
        triangleBatch.Draw();
        glutSwapBuffers();
    }
    
    void setupRC()
    {
        glClearColor(0.98f, 0.40f, 0.7f, 1);
        shaderManager.InitializeStockShaders();
        GLfloat vVerts[] = {
            -0.5f,0.0f,0.0f,
            0.5f,0.0f,0.0f,
            0.0f,0.5f,0.0f
        };
        triangleBatch.Begin(GL_TRIANGLES, 3);
        triangleBatch.CopyVertexData3f(vVerts);
        triangleBatch.End();
    }
    
    int main(int argc,char *argv[])
    {
        gltSetWorkingDirectory(argv[0]);
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL);
        glutInitWindowSize(800, 600);
        glutCreateWindow("Triangle");
        glutReshapeFunc(changeSize);
        glutDisplayFunc(RenderScene);
        GLenum status = glewInit();
        if (GLEW_OK != status) {
            printf("GLEW Error:%s\n",glewGetErrorString(status));
            return 1;
            
        }
        setupRC();
        glutMainLoop();
        return  0;
    }
    
    

    运行,出现以下结果则证明成功


    相关文章

      网友评论

          本文标题:在MAC上配置OpenGL

          本文链接:https://www.haomeiwen.com/subject/crxxjktx.html