OpenGL环境搭建Windows+Mac+Linux

OpenGL环境搭建Windows+Mac+Linux

Mac平台下


 

下载列表:
GLFW
cmake

下载的GLFW解压缩 

OpenGL环境搭建Windows+Mac+Linux

然后安装cmake, 安装好cmake之后打开

OpenGL环境搭建Windows+Mac+Linux

1.browse source, 选择GLFW的源码根目录

2.browse build, 选择要生成的工程目录,最好是空文件夹

3.点击Configure

 OpenGL环境搭建Windows+Mac+Linux

选择Xcode, 然后点击Done

然后会出来这个画面

OpenGL环境搭建Windows+Mac+Linux

不要惊慌,再点击一次Configure应该就没有那么红了(主要是上面那么框中没有红得就可以了,下面那个一直存在红色)

4.然后点击Generate, 就生成工程了

OpenGL环境搭建Windows+Mac+Linux

双击GLFW.xcodeproj, XCode就会打开了

然后继续下面操作

OpenGL环境搭建Windows+Mac+Linux

选择Simple吧,然后运行就可以看到一个窗口了

我们可以在上面画一个三角形

#include <GLFW/glfw3.h>



int main(void)

{

    GLFWwindow* window;

    

    /* Initialize the library */

    if (!glfwInit())

        return -1;

    

    /* Create a windowed mode window and its OpenGL context */

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

    if (!window)

    {

        glfwTerminate();

        return -1;

    }

    

    /* Make the window's context current */

    glfwMakeContextCurrent(window);

    

    /* Loop until the user closes the window */

    while (!glfwWindowShouldClose(window))

    {

        /* Render here */

        

        glClearColor(1, 1, 1, 1);

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        

        static GLfloat vertexs[] = {

            -1, -1, 0,

            1, -1, 0,

            0, 1, 0

        };

        static GLubyte colors[] = {

            255,0,0,0,

            0,255,0,0,

            0,0,255,0,

        };

        

        glEnableClientState(GL_VERTEX_ARRAY);

        glEnableClientState(GL_COLOR_ARRAY);

        

        glVertexPointer(3, GL_FLOAT, 0, vertexs);

        glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);

        

        glDrawArrays(GL_TRIANGLES, 0, 3);

        

        /* Swap front and back buffers */

        glfwSwapBuffers(window);

        

        /* Poll for and process events */

        glfwPollEvents();

    }

    

    glfwTerminate();

    return 0;

}

OpenGL环境搭建Windows+Mac+Linux

 

windows平台


 

直接在GLFW网站上下载 [Windows pre-compiled binaries], 下载32位版比较保险
下载后解压看到如下文件:

OpenGL环境搭建Windows+Mac+Linux

将include\GLFW里.h文件加入C:\Program Files (x86)\Windows Kits\8.0\Include\um\gl

如果你用的是vs2012
将lib-msvc110里的glfw3.lib,glfw3dll.lib放到C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86
将lib-msvc110里的glfw3.dll放到C:\windows\System32

 

好了环境配置完毕!下面实战吧 打开vs2012创建一个win32 console的工程
然后创建一个main.cpp, 以后每个工程都要加这两句代码

#pragma comment(lib,"opengl32.lib")

#pragma comment(lib,"glfw3.lib")

下面我们绘制一个三角形吧!!

#pragma comment(lib,"opengl32.lib")

#pragma comment(lib,"glfw3.lib")

#include <gl/glfw3.h>

#include <gl/GL.h>



int main(void)

{

    GLFWwindow* window;



    /* Initialize the library */

    if (!glfwInit())

        return -1;



    /* Create a windowed mode window and its OpenGL context */

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

    if (!window)

    {

        glfwTerminate();

        return -1;

    }



    /* Make the window's context current */

    glfwMakeContextCurrent(window);



    /* Loop until the user closes the window */

    while (!glfwWindowShouldClose(window))

    {

        /* Render here */

        glClearColor(1,1,1,1);

        glClear(GL_COLOR_BUFFER_BIT);



        static GLfloat pvertexs[] = {

            -1, -1, 0,

            1, -1, 0,

            0, 1, 0

        };

        static GLfloat color[] = {

            1,0,0,1,

            0,1,0,1,

            0,0,1,1

        };

        glEnableClientState(GL_COLOR_ARRAY);

        glEnableClientState(GL_VERTEX_ARRAY);



        glVertexPointer(3, GL_FLOAT, 0, pvertexs);

        glColorPointer(4, GL_FLOAT, 0, color);



        glDrawArrays(GL_TRIANGLES, 0, 3);



        /* Swap front and back buffers */

        glfwSwapBuffers(window);



        /* Poll for and process events */

        glfwPollEvents();

    }



    glfwTerminate();

    return 0;

} 

效果图:

OpenGL环境搭建Windows+Mac+Linux

 

Linux平台


 

在Linux上安装也比较简单,首先还是安装cmake,这里安装的命令行的cmake(到了linux下不用命令行不显逼格)

然后安装 xorg-devlibglu1-mesa-dev,基于debian的linux(ubuntu,mint...)可以使用如下命令安装

sudo apt-get install xorg-dev

sudo apt-get install libglu1-mesa-dev

安装后将glfw解压缩到一个文件夹glfw中,然后选择一个你要生成文件的位置(空白文件夹)test

1.打开命令行,cd 到你的test文件夹中

cd '/home/luwei/Downloads/test'

2.使用cmake命令指向glfw文件夹生成文件

cmake '/home/luwei/Downloads/glfw'

3.输入编译命令,编译源码

make

现在生成的可执行文件都在test文件夹中了,生成位置对应glfw的文件目录结构,输入命令运行程序

./examples/simple 

 我们自己写个自己的OpenGL的程序吧

 找到glfw/examples/simple.c,使用你习惯的编辑器打开,然后写自己的OpenGL程序

 这里我还是使用上面的那段程序啦(注意引入的头文件路径不同了)

 写完之后,要重新make命令编译,在test文件夹下使用make命令编译,然后运行。

OpenGL环境搭建Windows+Mac+Linux

总结


哎!!!总算都出来了,OpenGL编程记住一大堆函数和它该用啥参数是让我很苦恼的事情,尤其是Linux下我用编辑器写的程序根本没有代码提示,没办法记性不好呀

 

你可能感兴趣的:(windows)