<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://eliowang.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 16:21:01 GMT</lastBuildDate><atom:link href="https://eliowang.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The GLAD library in OpenGL developing]]></title><description><![CDATA[Platform compatible
Rendering is not an easy job, it needs both software and hardware support. Unfortunately, there is more than one software/hardware renderer vendor on earth.
For hardware
OpenGL tries to unify different device driver interfaces. It...]]></description><link>https://eliowang.hashnode.dev/the-glad-library-in-opengl-developing</link><guid isPermaLink="true">https://eliowang.hashnode.dev/the-glad-library-in-opengl-developing</guid><category><![CDATA[openGL]]></category><dc:creator><![CDATA[ElioWang]]></dc:creator><pubDate>Wed, 13 Dec 2023 17:59:29 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-platform-compatible">Platform compatible</h2>
<p>Rendering is not an easy job, it needs both software and hardware support. Unfortunately, there is more than one software/hardware renderer vendor on earth.</p>
<h3 id="heading-for-hardware">For hardware</h3>
<p>OpenGL tries to unify different device driver interfaces. It is <strong>a standard rather than a library</strong>, so OpenGL never provides the implementation for rendering. GPU vendors offer the implementation. Any program that needs to use OpenGL has to load implementation dynamically, writing some dynamic linking code like this:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">typedef</span> <span class="hljs-title">void</span> <span class="hljs-params">(*GENBUFFERS)</span> <span class="hljs-params">(GLsizei, GLuint*)</span></span>;
GENBUFFERS glGenBuffers = (GENBUFFERS)wglGetProcAddress(<span class="hljs-string">"glGenBuffers"</span>);
<span class="hljs-comment">// ... invoke wglGetProcAddress per API</span>
</code></pre>
<p>Oh, tremendously boring...</p>
<h3 id="heading-for-software">For software</h3>
<p>Unfortunately again, different OS provides different interfaces to do that dynamic linking. For APIs, Windows provides <code>wglGetProcAddress</code>, Linux provides <code>glXGetProcAddress</code>, and MacOS provides <code>NSGLGetProcAddress</code>. The code above copies and modifies every time fitting to different systems.</p>
<p>Even worse, some OpenGL versions may cause <code>wglGetProcAddress</code> to fail on Windows. (<a target="_blank" href="https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions">OpenGL wiki</a>)</p>
<h3 id="heading-glad-there-is-glad">Glad there is glad</h3>
<p><a target="_blank" href="https://github.com/Dav1dde/glad">glad</a>, also alternatives like <a target="_blank" href="https://github.com/nigels-com/glew">glew</a>, <a target="_blank" href="https://github.com/kallisti5/glee">glee</a>, are used to simplify the checking and linking routine. Now use <code>gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)</code> in a nutshell to make life easier.</p>
<blockquote>
<p><a target="_blank" href="https://github.com/glfw/glfw">glfw</a> is a multi-platform window/event manager library.</p>
</blockquote>
<h2 id="heading-use-glad-without-glfw-on-windows">Use glad without glfw on windows</h2>
<p>Using <a target="_blank" href="https://github.com/glfw/glfw">glfw</a> to initialize glad is a popular way. But for curiosity, can we use <code>wglGetProcAddress</code> to initialize glad?</p>
<p>The answer is yes but not directly. You cannot directly write code like this:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">if</span> (!gladLoadGLLoader((GLADloadproc)wglGetProcAddress)) {
    ...
}
</code></pre>
<p><a target="_blank" href="https://community.khronos.org/t/glad-without-glfw/105017">Someone failed already</a>. The reason is described <a target="_blank" href="https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions">here</a> -- the dynamic linking interface is not available to <code>wglGetProcAddress</code> in <code>OpenGL32.DLL</code>. The right way is to mix the <code>wglGetProcAddress</code> and <code>GetProcAddress</code> like this:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> *<span class="hljs-title">GetAnyGLFuncAddress</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *name)</span>
</span>{
    <span class="hljs-keyword">void</span> *p = (<span class="hljs-keyword">void</span> *)wglGetProcAddress(name);
    <span class="hljs-keyword">if</span> (p == <span class="hljs-number">0</span> ||
       (p == (<span class="hljs-keyword">void</span>*)<span class="hljs-number">0x1</span>) || (p == (<span class="hljs-keyword">void</span>*)<span class="hljs-number">0x2</span>) || (p == (<span class="hljs-keyword">void</span>*)<span class="hljs-number">0x3</span>) ||
       (p == (<span class="hljs-keyword">void</span>*)<span class="hljs-number">-1</span>) )
    {
        HMODULE <span class="hljs-keyword">module</span> = LoadLibraryA(<span class="hljs-string">"opengl32.dll"</span>);
        p = (<span class="hljs-keyword">void</span> *)GetProcAddress(<span class="hljs-keyword">module</span>, name);
    }

    <span class="hljs-keyword">return</span> p;
}
<span class="hljs-keyword">if</span> (!gladLoadGLLoader((GLADloadproc)GetAnyGLFuncAddress)) {
    ...
}
</code></pre>
<p>This code works well with the <a target="_blank" href="https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_%28WGL%29">Windows OpenGL Context</a>. Full code snippet is available <a target="_blank" href="https://gist.github.com/AdjWang/808422011aee43312f87b15b69e238c6">here</a>.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><p><a target="_blank" href="https://learnopengl.com/Introduction">Learn OpenGL</a></p>
</li>
<li><p><a target="_blank" href="http://Open.GL">Open.GL</a> <a target="_blank" href="https://open.gl/context#Onemorething">tutorial - Context creation</a></p>
</li>
<li><p><a target="_blank" href="https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions">OpenGL wiki - Load OpenGL Functions</a></p>
</li>
<li><p><a target="_blank" href="https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_%28WGL%29">OpenGL wiki - Creating an OpenGL Context (WGL)</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>