Win10+Vs2022+opengl4.6+glad+glwf3+freetype2.12 开发环境搭建(二进制兼容版)

6 篇文章 0 订阅
4 篇文章 0 订阅

1. 安装vs2022
https://visualstudio.microsoft.com
只需要选择 Desktop development with C++ 组件,其它组件都不用选。

C++组件中要手动勾上C++ Clang tools和最新版的Windows SDK,其它的保持默认状态即可.


2. 打开vs2022, 创建一个新项目(C++,Windows, Console, Empty Project),创建同名的solution文件夹,这里是idealand
在solution文件夹中创建libs文件夹,用于存放第三方库

在源代码中添加main.cpp文件,cout<<"hello world"; 编译运行

这样项目属性中才能出现C/C++选项卡。


3. 设置glfw库,用于创建窗体
glfw官网:https://www.glfw.org
下载二进制库: https://github.com/glfw/glfw/releases/download/3.3.8/glfw-3.3.8.bin.WIN64.zip
解压后放到(solution)\libs\文件夹里面即可,文件结构如下:

在项目中添加glfw库:

项目属性 - C/C++ - additional include directories: $(SolutionDir)libs\glfw\include\;
项目属性 - Linker - additional library directories: $(SolutionDir)libs\glfw\lib-vc2022\;
项目属性 - Linker - input - additional dependencies: glfw3.lib;


4. OpenGL的库文件是opengl32.lib,它在安装windows SDK的时候已经安装好了,64bits的opengl库也叫opengl32.lib,这个是比较意外的。
在项目中添加opengl库:
项目属性 - Linker - input - additional dependencies: opengl32.lib;


5. GLAD是opengl functions的外包
打开glad官网:http://glad.dav1d.de/,选择C/C++, OpenGL, gl: version 4.6, profile: compatibility, 

点击 Generate,下载glad.zip文件,解压缩,放到(solution)\libs\里面,文件结构如下:

在项目中添加glad库:
项目属性 - C/C++ - additional include directories: $(SolutionDir)libs\glad\include\;$(SolutionDir)libs\glad\src\;

6. 修改项目属性为windows application

项目属性
Linker->System->SubSystem  (set it to Windows). 
Linker->Advanced->Entry Point  (set it to mainCRTStartup).

7. 在main.cpp中填入以下代码 

#include <glad/glad.h>// must be placed before glfw
#include <GLFW/glfw3.h>
#include <glad.c>



namespace idealand
{
namespace opengl
{
class Window
{
public:
GLFWwindow* win = 0;
int w = 0; int h = 0;
char* title = 0;

char* glsl_version = (char*)"#version 460";
int SwapInterval = 1;

static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);
}

static void setup_opengl_viewport(int width, int height)
{
glViewport(0, 0, width, height);
}

static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
setup_opengl_viewport(width, height);
}

Window(char* title, int w = 800, int h = 800, int OpenGLVersionMajor = 4, int OpenGLVersionMinor = 6) : title(title), w(w), h(h)
{
glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) { return; }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OpenGLVersionMajor); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OpenGLVersionMinor);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); // GLFW_OPENGL_COMPAT_PROFILE GLFW_OPENGL_CORE_PROFILE
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for mac os it's needed
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); // for debug

win = glfwCreateWindow(w, h, title, NULL, NULL); if (!win) { printf("Failed to create GLFW window\n"); close(-1); return; } glfwMakeContextCurrent(win);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { printf("Failed to initialize GLAD\n"); close(-1); return; }
setup_opengl_viewport(w, h);
glfwSwapInterval(SwapInterval); //must be used after current context set, the default is 0, which means show the new content once available, 1 is the prefered value
glfwSetFramebufferSizeCallback(win, Window::framebuffer_size_callback); glfwSetKeyCallback(win, key_callback);

//------------- opengl global settings ----------------
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA); glEnable(GL_BLEND); //glEnable(GL_DEPTH_TEST);

}
~Window()
{
close(0);
}

int close(int r)
{
if (win) glfwDestroyWindow(win); glfwTerminate(); return r;
}

};

void company()
{
int w = 800, h = 800;
opengl::Window win((char*)"Lines And Chars", w, h);
while (!glfwWindowShouldClose(win.win))
{
glClearColor(1.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); // 填充红色背景

glColor3f(0.0f, 0.0f, 1.0f); // 设置线的颜色为蓝色
glLineWidth(5.0f); // 设置线的宽度
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0.5, 0);
glEnd();

glColor3f(0.0f, 1.0f, 0.0f); // 设置线的颜色为绿色
glLineWidth(15.0f); // 设置线的宽度
glBegin(GL_LINES);
glVertex3f(-0.5, -0.5, 0);
glVertex3f(-0.3, 0.3, 0);
glEnd();

glFlush();

glfwSwapBuffers(win.win); // swap buffer to show new content
glfwPollEvents(); // response to user operations on the window, process the events by default handlers
// glfwWaitEvents(); // for static rendering content
}
}
}

}

int main()
{
idealand::opengl::company();
}

8. 编译运行


参考资料:
OpenGL的环境搭建(cmake+glfw+glew+vs2017)(2019年)
https://blog.csdn.net/NowSayHelloWorld/article/details/102635236

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiuzen

您的资助将帮助我创作更好的作品

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值