<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shadoweaver</title>
    <description>The latest articles on DEV Community by Shadoweaver (@shadoweaver).</description>
    <link>https://dev.to/shadoweaver</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F38786%2F2a89e120-2b45-4cff-908f-228233bb582e.jpeg</url>
      <title>DEV Community: Shadoweaver</title>
      <link>https://dev.to/shadoweaver</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shadoweaver"/>
    <language>en</language>
    <item>
      <title>Building standalone OpenGL application without Xcode, with bundled separate shader file</title>
      <dc:creator>Shadoweaver</dc:creator>
      <pubDate>Mon, 07 May 2018 16:27:47 +0000</pubDate>
      <link>https://dev.to/shadoweaver/building-standalone-opengl-application-without-xcode-with-bundled-separate-shader-file-229a</link>
      <guid>https://dev.to/shadoweaver/building-standalone-opengl-application-without-xcode-with-bundled-separate-shader-file-229a</guid>
      <description>&lt;h4&gt;
  
  
  Background
&lt;/h4&gt;

&lt;p&gt;I have a "Hello Rectangle" project which runs as expected when invoked in Terminal.app. But I'm having trouble creating a standalone macOS bundle out of it. &lt;/p&gt;

&lt;p&gt;Let's just say using full Xcode is out of question. I'm &lt;em&gt;very&lt;/em&gt; tight on disk space and I'm not doing iOS dev.&lt;br&gt;
Instead I'm using brewed toolchain, including &lt;code&gt;gcc-8&lt;/code&gt;, &lt;code&gt;glfw&lt;/code&gt; and &lt;code&gt;glew&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code base looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src
├── include
│   ├── draw.hpp
│   └── public.hpp
├── resc
│   ├── rect.frag
│   └── rect.vert
├── draw.cpp
├── init-shader.cpp
└── window.cpp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The actual compile command is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ g++-8 -lglew -lglfw -framework OpenGL -o &amp;lt;binary&amp;gt; &amp;lt;sources&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Currently, after &lt;code&gt;make&lt;/code&gt;, the binary can be invoked in terminal, providing both shader files are present in the same directory. The file reader in &lt;code&gt;init-shader.cpp&lt;/code&gt; accepts just filename without preceding path.&lt;/p&gt;

&lt;h4&gt;
  
  
  Problem
&lt;/h4&gt;

&lt;p&gt;I don't quite want to expose shader files, and if I read this &lt;a href="https://stackoverflow.com/a/20443864"&gt;SO answer&lt;/a&gt; right, it suggests either you pack shader files in final release (what I'm trying), or you hard code them in other sources (even more undesired). Plus, I want to make sure when I distribute it as a standalone &lt;code&gt;.app&lt;/code&gt; bundle, it can run out of box (as long as lowest OpenGL version requirement is met) instead of having to get all brewed dependencies.&lt;/p&gt;

&lt;p&gt;I read through this &lt;a href="https://stackoverflow.com/questions/1596945/building-osx-app-bundle"&gt;SO question&lt;/a&gt;, attempted the operations mentioned, namely:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build with the &lt;code&gt;-headerpad_max_install_names&lt;/code&gt; flag.&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;Info.plist&lt;/code&gt; and structre the directory as macOS dictates.&lt;/li&gt;
&lt;li&gt;Find all dylibs needed using &lt;code&gt;otool -L&lt;/code&gt; on executable, and recursively use &lt;code&gt;otool -L&lt;/code&gt; on them to avoid DLL hell (for a want of macOS-specific name), exclude what Apple have guaranteed, and copy them to &lt;code&gt;a.app/Contents/Frameworks&lt;/code&gt;. Four of them are found in my case, all in Homebrew's cellar:

&lt;ul&gt;
&lt;li&gt;libglfw.3.dylib
&lt;/li&gt;
&lt;li&gt;libGLEW.2.1.dylib&lt;/li&gt;
&lt;li&gt;libstdc++.6.dylib&lt;/li&gt;
&lt;li&gt;libgcc_s.1.dylib &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;install_name_tool -change&lt;/code&gt; to modify the reference to dylibs in binary file to those copied.&lt;/li&gt;
&lt;li&gt;Wrap the entry point in &lt;code&gt;Info.plist&lt;/code&gt; into a shell script so that working directory is right, wherever it's invoked. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Terminal didn't complain all the way through. But it won't run, whether using &lt;code&gt;open -a a.app&lt;/code&gt; or directly invoke the actual binary or that tiny script.&lt;/p&gt;

&lt;p&gt;And I found this &lt;a href="http://seaneshbaugh.com/posts/creating-an-opengl-4-1-program-with-glew-and-glfw-in-xcode"&gt;blog post&lt;/a&gt; which implies I also need to invoke &lt;code&gt;install_name_tool -id&lt;/code&gt; between (2) and (3), and &lt;code&gt;codesign&lt;/code&gt; them between (3) and (4).&lt;/p&gt;

&lt;p&gt;But it still won't run.&lt;/p&gt;

&lt;p&gt;In the former case, the shader linker says shader programs are corrupted, but compilation will succeed, but compilation log is empty; in the latter case the app crashes and &lt;code&gt;dyld&lt;/code&gt; complains.&lt;/p&gt;

&lt;p&gt;I suspect the former is mostly right, since when I blindly change that folder name from standard &lt;code&gt;Contents&lt;/code&gt; to &lt;code&gt;content&lt;/code&gt; (or any other name), it will run if I invoke the binary from terminal, and even when I &lt;code&gt;brew unlink&lt;/code&gt; &lt;code&gt;glew&lt;/code&gt; and &lt;code&gt;glfw&lt;/code&gt;, suggesting the binary knows where to look up dylibs and shader files. &lt;/p&gt;

&lt;p&gt;What did I miss? &lt;/p&gt;

&lt;h4&gt;
  
  
  Probing
&lt;/h4&gt;

&lt;p&gt;At this time my release dir looked like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Contents
├── Frameworks
│   ├── libGLEW.2.1.dylib
│   ├── libgcc_s.1.dylib
│   ├── libglfw.3.dylib
│   └── libstdc++.6.dylib
├── MacOS
│   ├── hello-rect    &amp;lt;- the actual binary
│   ├── launcher.sh   &amp;lt;- entry wrapper for working dir as the above link suggests
│   ├── rect.frag
│   └── rect.vert
└── Info.plist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Because the unbundled version obviously looks nowhere but &lt;code&gt;cwd&lt;/code&gt;, I thought in bundles they would do the same. Later on Google lead me to believe that I need &lt;code&gt;CoreFoundation&lt;/code&gt; API to correctly read files in bundle, and it murdered my day. I learned that there should be a &lt;code&gt;Resources&lt;/code&gt; folder in bundle, and then added CF codes, which for some reason always give segfault 11. I even brought up &lt;a href="https://stackoverflow.com/q/50214471/4652262"&gt;one new question&lt;/a&gt; on SO. &lt;/p&gt;

&lt;h4&gt;
  
  
  The real caveat
&lt;/h4&gt;

&lt;p&gt;By pure chance I saw yet another &lt;a href="https://stackoverflow.com/questions/17001488/osx-bundle-throwing-exception-only-when-app-in-macos-folder-and-resources-in-res"&gt;SO question&lt;/a&gt; which finally saved my day. &lt;strong&gt;Turns out, if GLFW sees &lt;code&gt;Resources&lt;/code&gt; in bundle, it changes working dir to that dir!&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;So the take away is this: structure the release folder like following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Contents
├── Frameworks
│   └── &amp;lt;dylibs&amp;gt;
├── MacOS
│   ├── hello-rect
│   └── launcher.sh
├── Resources
│   └── &amp;lt;shader files&amp;gt;
└── Info.plist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, walk through the previous list, without &lt;code&gt;install_name_tool -id&lt;/code&gt; or &lt;code&gt;codesign&lt;/code&gt;, and we're done. &lt;/p&gt;

&lt;p&gt;One final question: is &lt;code&gt;make&lt;/code&gt; suitable for wrapping all these into a script?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How I learned to compile example from GLFW3 on macOS</title>
      <dc:creator>Shadoweaver</dc:creator>
      <pubDate>Thu, 29 Mar 2018 11:49:32 +0000</pubDate>
      <link>https://dev.to/shadoweaver/how-i-learned-to-compile-example-from-glfw3-on-macos-5bfc</link>
      <guid>https://dev.to/shadoweaver/how-i-learned-to-compile-example-from-glfw3-on-macos-5bfc</guid>
      <description>&lt;p&gt;I'm just starting to learn graphics.&lt;/p&gt;

&lt;p&gt;Where it all began: the CG textbook we currently use (Hearn, Baker, Carithers) uses OpenGL 2.0 (using things like GLUT), while on my system (macOS 10.12.6 @ rMBP 13” late 2013), it’s simply deprecated as per compiler warning. Also I read that modern OpenGL does things differently and handsomely, so why not just go 4.1 (up to my system’s highest support)? (I even attempted Vulkan, but it’s too new for my system.)&lt;/p&gt;

&lt;p&gt;I learned that I should use &lt;a href="http://www.glfw.org/"&gt;GLFW&lt;/a&gt; to interact with desktop window manager, and &lt;a href="https://glew.sourceforge.io/"&gt;GLEW&lt;/a&gt; to manage OpenGL extensions. On macOS they are readily &lt;a href="https://brew.sh/"&gt;brew&lt;/a&gt;ed:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install glfw --with-examples --with-test
$ brew install glew
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;but I was at lost when I tried to compile &lt;code&gt;simple.c&lt;/code&gt; in GLFW examples. &lt;code&gt;linmath.h&lt;/code&gt; was readily &lt;a href="https://github.com/datenwolf/linmath.h"&gt;found&lt;/a&gt;, but what was I supposed to do with &lt;a href="http://glad.dav1d.de/"&gt;GLAD&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Apparently I can directly compile some even simpler code, as found in &lt;a href="http://seaneshbaugh.com/posts/creating-an-opengl-4-1-program-with-glew-and-glfw-in-xcode"&gt;this post&lt;/a&gt;, which didn’t use GLAD as in &lt;code&gt;simple.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I didn’t install the full Xcode, but used brewed &lt;code&gt;gcc-7&lt;/code&gt;. It took me some time to figure out that I should compile the above code with&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ g++-7 -lglfw -lglew -framework OpenGL [filename]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and it runs fine.&lt;/p&gt;

&lt;p&gt;And today I stumbled upon &lt;a href="https://learnopengl.com/Getting-started/Hello-Triangle"&gt;learn OpenGL&lt;/a&gt; which shows how to set up GLAD. I should download a certain version of it, put the headers into &lt;code&gt;/usr/include&lt;/code&gt;, and also compile and link the accompanying C code. Following this instruction I finally get my own “simple.c” compiled.&lt;/p&gt;

</description>
      <category>opengl</category>
      <category>graphics</category>
    </item>
  </channel>
</rss>
