最近工作需要 需要寫一個(gè)全景的視頻播放器
網(wǎng)上搜了下大概解決方案是 ffmpeg+opengl
b站有很多視頻? 按照視頻 搭建了OpenGL的開發(fā)環(huán)境
先去GLFW的網(wǎng)站下載 windows平臺(tái)的庫(kù)文件
為什么使用GLFW? 因?yàn)镚LFW是跨平臺(tái)的? ?我下的是64位版本解壓后有目錄如下
?
?包含了動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù) 這里我們使用靜態(tài)庫(kù) mt那個(gè)是multithread 多線程版本的
An OpenGL library | GLFW
打開VS 新建一個(gè)控制臺(tái)的新項(xiàng)目
添加一個(gè)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;
}
項(xiàng)目工程目錄
?
?
?工作目錄添加了一個(gè)3rd的目錄用來(lái)存放第三方的庫(kù)這里使用的是靜態(tài)庫(kù)glfw3.lib
設(shè)置工程屬性 添加頭文件目錄 C/C++ 常規(guī)
?添加lib庫(kù)目錄 如下 鏈接器常規(guī)那里
?設(shè)置lib庫(kù)文件名 這里主要是glfw3.lib 和 opengl32.lib
F5 直接編譯運(yùn)行? 出來(lái)如下三角形窗口 到此opengl的環(huán)境就搭建好了
?
后來(lái)看到教程又說(shuō) 上面的方法調(diào)用的opengl是比較老的版本? 需要用到一個(gè)glew或者glad的東西來(lái) 用
去這里下載glew 直接選擇編譯好的二進(jìn)制版本
The OpenGL Extension Wrangler Library download | SourceForge.net
解壓過(guò)后如下圖
還是使用靜態(tài)庫(kù)
引入頭文件? 之后的步驟和之前一致? 修改下代碼
#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內(nèi)容
根據(jù)中文的文檔內(nèi)容
創(chuàng)建窗口 - LearnOpenGL CN (learnopengl-cn.github.io)
進(jìn)入GLAD官網(wǎng)
https://glad.dav1d.de
?我選擇最新的4.6版本 opengl core模式
點(diǎn)擊Generate之后? 生成了一個(gè)zip包 下載這個(gè)壓縮包
新建一個(gè)空的項(xiàng)目?
添加對(duì)應(yīng)的文件 和 依賴項(xiàng) 之后? 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);
}
能夠正常顯示如下窗口了?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-435037.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-435037.html
到了這里,關(guān)于Visual Studio 2022 搭建GLFW OpenGL開發(fā)環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!