美文网首页
OpenGL环境搭建

OpenGL环境搭建

作者: 君幸食j | 来源:发表于2020-08-07 16:49 被阅读0次
  • 打开Xcode -> macOS -> App,新建一个Mac工程 1.png 2.png
  • 删掉文件:AppDelegate.h、AppDelegate.m、ViewController.h、ViewController.m、main.m 3.png
  • 拖入include文件夹和libGLTools.a到项目 4.png
  • 添加OpenGl.framework和GLUT.framework两个系统库 5.png
  • 在Bulid Settings输入Header Search path中拖入include文件生成路径 6.png
  • 创建main.cpp文件 7.png 8.png 9.png
  • 在main.cpp复制以下代码

    #include "GLTools.h"
    #include <glut/glut.h>
    
    GLBatch triangleBatch;
    GLShaderManager shaderManager;
    
    void ChangeSize(int w,int h)
    {
        glViewport(0,0, w, h);
    }
    
    void SetupRC()
    {
        glClearColor(0.0f,0.0f,1.0f,1.0f);
        
        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();
    }
    
    void RenderScene(void)
    {
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

        GLfloat vRed[] = {1.0f,0.0f,0.0f,1.0f};

        shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
        triangleBatch.Draw();

        glutSwapBuffers();
    }
 
    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 err = glewInit();
        if(GLEW_OK != err)
        {
            fprintf(stderr,"glew error:%s\n",glewGetErrorString(err));
            return 1;
        }

        SetupRC();
        glutMainLoop();
        return 0;
    
    }
  • 最后编译运行,效果如下 10.png

相关文章

网友评论

      本文标题:OpenGL环境搭建

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