最近工作需要 需要写一个全景的视频播放器
网上搜了下大概解决方案是 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 打印的版本都是 如下
20230509 更新增加GLAD内容
根据中文的文档内容
创建窗口 - LearnOpenGL CN (learnopengl-cn.github.io)
进入GLAD官网
https://glad.dav1d.de
我选择最新的4.6版本 opengl core模式
点击Generate之后 生成了一个zip包 下载这个压缩包
新建一个空的项目
添加对应的文件 和 依赖项 之后 main.cpp改成如下代码
#include "glad/glad.h"
#include "glfw3.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
能够正常显示如下窗口了
文章来源:https://www.toymoban.com/news/detail-435037.html
文章来源地址https://www.toymoban.com/news/detail-435037.html
到了这里,关于Visual Studio 2022 搭建GLFW OpenGL开发环境的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!