Windows下使用OpenGL

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

最近项目上需要用OpenGL进行3D显示,特在Windows平台上安装OpenGL,并测试。

几个月前使用过OpenGL,但是这次从头来,还是出现了各种问题(因为换工作了,所以都得重新开始。。。)

这次我使用的是glfw,我认为单纯的glfw就可以运行,其实不是的,需要依赖gl.h,VS在安装时,默认是安装了OpenGL的,该头文件也有,但是所需要的静态库却是需要自己配置的,跟glfw3.lib放在同样的地方。

glfw的下载地址:https://www.glfw.org/download.html

上述地址提供了源码和release库。我最开始下载源码,然后用cmake进行编译成VS2015下的库函数,但是在使用的时候总报错,提示无法解析外部命令(生成库有问题!我还在继续查找根本原因,我的opencv、GLM用cmake都没出现,单单这个出现问题,有点奇葩。。。最后发现是没有添加opengl32.lib库,该库负责一些与Windows相关的函数调用),最后索性使用了release版的库和头文件(注意由于Windows下VS没有64位的,release版本需要下载32位的!)

在工程中将glfw的头文件包含进去,然后添加库目录,最后添加链接库glfw3.lib特别注意opengl32.lib也要放进去(该库的目录在VS中已经默认添加,所以不用管路径)。

测试效果如下:

以下是测试代码:

---------------------------

#include <iostream>
#include <windows.h>//不能缺少
#include <./glfw3.h>

int main()
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(480, 320, "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))
    {
        /* Draw a triangle */
        glBegin(GL_TRIANGLES);

        glColor3f(1.0, 0.0, 0.0);    // Red
        glVertex3f(0.0, 1.0, 0.0);

        glColor3f(0.0, 1.0, 0.0);    // Green
        glVertex3f(-1.0, -1.0, 0.0);

        glColor3f(0.0, 0.0, 1.0);    // Blue
        glVertex3f(1.0, -1.0, 0.0);

        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();


    std::cin.get();
    return 0;
}

------------------------------

猜你喜欢

转载自blog.csdn.net/qq_33810188/article/details/89501280
今日推荐