您现在的位置是:首页 >技术杂谈 >Visual Studio 2022 搭建GLFW OpenGL开发环境网站首页技术杂谈

Visual Studio 2022 搭建GLFW OpenGL开发环境

QMCY_jason 2024-06-03 00:00:02
简介Visual Studio 2022 搭建GLFW OpenGL开发环境

最近工作需要 需要写一个全景的视频播放器

网上搜了下大概解决方案是 ffmpeg+opengl

b站有很多视频  按照视频 搭建了OpenGL的开发环境

先去GLFW的网站下载 windows平台的库文件

为什么使用GLFW  因为GLFW是跨平台的   我下的是64位版本解压后有目录如下

 

 包含了动态库和静态库 这里我们使用静态库 mt那个是multithread 多线程版本的

An OpenGL library | GLFW

打开VS 新建一个控制台的新项目

添加一个main.cpp文件

添加以下代码

#include "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 */
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_TRIANGLES);

        glVertex2f(-0.5f,-0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        
        glEnd();


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

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

    glfwTerminate();
    return 0;
}

项目工程目录

 

 

 工作目录添加了一个3rd的目录用来存放第三方的库这里使用的是静态库glfw3.lib

设置工程属性 添加头文件目录 C/C++ 常规

 添加lib库目录 如下 链接器常规那里

 设置lib库文件名 这里主要是glfw3.lib 和 opengl32.lib

F5 直接编译运行  出来如下三角形窗口 到此opengl的环境就搭建好了

 

后来看到教程又说 上面的方法调用的opengl是比较老的版本  需要用到一个glew或者glad的东西来 用

去这里下载glew 直接选择编译好的二进制版本

The OpenGL Extension Wrangler Library download | SourceForge.net

解压过后如下图

还是使用静态库

 

引入头文件  之后的步骤和之前一致  修改下代码

#include "glew.h"
#include "glfw3.h"


#include   <iostream>

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

    if (glewInit() != GLEW_OK)
    {
        std::cout << "glewInit failed" << std::endl;
    }


    std::cout << glGetString(GL_VERSION);
     

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_TRIANGLES);

        glVertex2f(-0.5f,-0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        
        glEnd();


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

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

    glfwTerminate();
    return 0;
}

但是我试了 不管 加不加glew  打印的版本都是 如下

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。