痛苦面具:learnOpenGL系列,HelloWindow,主要是顺序问题 - Lemon-GPU - D1h.Net第一号博客
返回主页

Lemon-GPU

痛苦面具:learnOpenGL系列,HelloWindow,主要是顺序问题

属于是非常困惑了,为啥初始化的顺序一定是这个样子:

  • 首先是include的东西,必须是先glad再是glfw,然后整个最基本的代码也得是按照一定的顺序来初始化
  • 首先是glfw的init,然后是创建窗口,然后是诡异的glfwMakeContextCurrent,然后是glfwSetFramebufferSizeCallback,然后才是去load这个gladloadGLLoader,这个参数又是个啥意思,这个renderloop居然还意外的好懂了。
  • 还有一个,glfw和glad到底是个啥啊

一个个来吧,哎
那第一个问题很好解决,原文都给了

Be sure to include GLAD before GLFW. The include file for GLAD includes the required OpenGL headers behind the scenes (like GL/gl.h) so be sure to include GLAD before other header files that require OpenGL (like GLFW).

来个deepl

请确保在GLFW之前包含GLAD。GLAD的include文件包括幕后所需的OpenGL头文件(如GL/gl.h),所以一定要在其他需要OpenGL的头文件(如GLFW)之前包括GLAD。

看了眼翻译还挺准确的
然后是第二个问题,这个啊,万能的StackOverflow已经给了:为啥我得用个OpenGL函数Loader

摘抄一下第一个答案:

OpenGL functions (core or extension) must be loaded at runtime, dynamically, whenever the function in question is not part of the platforms original OpenGL ABI (application binary interface).
For Windows the ABI covers is OpenGL-1.1
For Linux the ABI covers OpenGL-1.2 (there's no official OpenGL ABI for other *nixes, but they usually require OpenGL-1.2 as well)
For MacOS X the OpenGL version available and with it the ABI is defined by the OS version.
This leads to the following rules:
In Windows you're going to need a function loader for pretty much everything, except single textured, shaderless, fixed function drawing; it may be possible to load further functionality, but this is not a given.
In Linux you're going to need a function loader for pretty much everything, except basic multitextured with just the basic texenv modes, shaderless, fixed function drawing; it may be possible to load further functionality, but this is not a given.
In MacOS X you don't need a function loader at all, but the OpenGL features you can use are strictly determined by the OS version, either you have it, or you don't.
The difference between core OpenGL functions and extensions is, that core functions are found in the OpenGL specification, while extensions are functionality that may or may be not available in addition to what the OpenGL version available provides.
Both extensions and newer version core functions are loaded through the same mechanism.

deepl上

OpenGL函数(核心或扩展)必须在运行时动态加载,只要该函数不属于平台的原始OpenGL ABI(应用程序二进制接口)。
对于Windows来说,ABI包括OpenGL-1.1。
对于Linux,ABI包括OpenGL-1.2(其他*nixes没有官方的OpenGL ABI,但它们通常也需要OpenGL-1.2)。
对于MacOS X来说,可用的OpenGL版本和ABI是由操作系统版本定义的。
这就导致了以下的规则。
在Windows中,除了单一纹理的、无着色器的、固定功能的绘图,你需要一个函数加载器来实现几乎所有的功能;也许有可能加载更多的功能,但这并不是必然。
在Linux中,除了基本的多纹理和基本的texenv模式,无着色器,固定功能的绘制,你需要一个函数加载器来实现几乎所有的功能;也许可以加载更多的功能,但这并不是必然。
在MacOS X中,你根本不需要函数加载器,但你可以使用的OpenGL功能是由操作系统版本严格决定的,要么你有,要么你没有。
OpenGL核心功能和扩展功能的区别在于,核心功能可以在OpenGL规范中找到,而扩展功能是在现有的OpenGL版本提供的功能之外,可能有也可能没有。
扩展和较新版本的核心功能都是通过相同的机制加载的。

自己改了点用语。不过还是没解决。Loader到底是啥?
我推荐点东西好了,看完也就就差不多了

这样就把第二第三解决了。说的详细点,其实就是glfw是关注整个渲染流程里面比较外围的东西,渲染和显示解耦合了(个人认为),glad就是reload的函数指针,把driver的东西封装抽象了。
其实还是很蒙蔽。

冒着被舍友打屎的风险,我继续来bb了
关键的函数其实是glfwMakeContextCurrent,只有让window在当前这个程序所在的进程之后,我们才能load一下函数指针什么的,剩下的就是设置状态转移而已。你可以试一下让load在current之前,会失败。这个函数本身应该还有点side-effect的。

This function makes the OpenGL or OpenGL ES context of the specified window current on the calling thread. A context must only be made current on a single thread at a time and each thread can have only a single current context at a time.
When moving a context between threads, you must make it non-current on the old thread before making it current on the new one.
By default, making a context non-current implicitly forces a pipeline flush. On machines that support GL_KHR_context_flush_control, you can control whether a context performs this flush by setting the GLFW_CONTEXT_RELEASE_BEHAVIOR hint.
The specified window must have an OpenGL or OpenGL ES context. Specifying a window without a context will generate a GLFW_NO_WINDOW_CONTEXT error.

posted @ 2021-09-16 23:49  Lemon-GPU  阅读(9)  评论(0编辑  收藏  举报
Copyright © 2021 Lemon-GPU
Powered by .NET 6 on Kubernetes

问答 28u iTmz.Net 3q科技 A8团队1 A8团队2 A8团队3 A8备