OpenGL学习笔记(二):窗口与视口(glfw的使用、处理输入、扩展glfw为MFC子窗口)


现代OpenGL项目推荐使用GLFW + GLAD来配置使用OpenGL。配置环境:

  • VS2017
  • glfw-3.4
  • glad-opengl4.6-core

GLFW配置

下载源码 -> CMake生成VS2017项目 -> 编译生成glfw3dll.libglfw3.dll
在这里插入图片描述
将源码中的include/GLFW目录添加进附加包含目录
将生成的glfw3dll.lib添加进VS工程属性Linker(链接器)选项卡里的Input(输入)选项卡中

#include <GLFW/glfw3.h>

创建窗口

	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

	GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpengl", NULL, NULL);
	if (window == NULL) {
   
		std::cout << "Failed to create CLFW window" << std::endl;
		glfwTerminate();
		return;
	}

	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(mpWindow, framebuffer_size_callback);
	glfwSetCursorPosCallback(mpWindow, mouse_callback);
	glfwSetScrollCallback(mpWindow, scroll_callback);
	
	//GLAD
	/*if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cout << "Fail to initialize GLAD";
		return;
	}*/
	
	//渲染循环
	while (!glfwWindowShouldClose(window)) {
   
		// per-frame time logic
		float currentFrame = static_cast<float>(glfwGetTime());
		deltaTime = currentFrame - lastFrame;
		lastFrame = currentFrame;

		// input
		processInput(mpWindow);
			
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);	//状态设置
		glClear(GL_COLOR_BUFFER_BIT);	//状态使用
		// glfw:交换缓冲区和轮询IO事件(按键按下或释放,鼠标移动等)
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	//回收前面分配的GLFW资源
	glfwTerminate();
  • 初始化GLFW库

    int glfwInit(void);
    

    This function initializes the GLFW library. Before most GLFW functions can be used, GLFW must be initialized, and before an application terminates GLFW should be terminated in order to free any resources allocated during or after initialization.
    @reentrancy This function must not be called from a callback.
    @thread_safety This function must only be called from the main thread.

  • 终止GLFW库
    销毁窗口,回收已分配资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值