Xcode and OpenGL shaders

Started by
10 comments, last by Promit 8 years, 3 months ago
I am trying to turn this project intonam Xcode project.

https://www.dropbox.com/s/3wk7erfmlndzeyr/BenGraphicsTut8.zip?dl=0

I have everything working up until the program opens the shader files. It says it can't find the files. What is the proper way to Add and use shader files in Xcode?
Advertisement

You have to find out how the compiled program accesses the files. Where does it look for them? Are there absolute paths in use, or paths relative to the application, the install directory, the user's home directory, or whatever? Only if that is identified, you can work on a strategy to install the files where they are expected.

Just to echo what Haegarr posted; you need to find out what the 'working directory' is and you need to put the shader folder there. A quick search seems to imply that the working directory is the same as the directory Xcode is in so you could try putting the shader folder in there. Or you could try changing it to an absolute path. I am not familiar with MacOS so I don't really know what the 'correct' way to do things is there.

Look down a bit int his thread and it shows how to set a working directory for your project in XCode 4 (I don't know which version you are using).

http://stackoverflow.com/questions/13225500/how-to-change-the-working-directory-to-the-location-of-the-program

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

I found this tutorial

http://www.tomdalling.com/blog/modern-opengl/01-getting-started-in-xcode-and-visual-cpp/

His project has a .mm file that handles apples way of using file system and framework.

I was hoping that i could do it completely platform independent, but I guess not.

I assume that if i wamyed to build for Windows, I'd have to take my files into visual studio and write some code to check what operating system I'm using?

It says it uses GLFW for windows creation and input which Google tells me is cross platform so it should be good. I can't say you wouldn't have to do something platform specific at all but if GLFW is cross platform (window creation is usually the bit that requires the most platform specific code) then you shouldn't really have to do much different to get a simple OpenGL program to work on both MacOS/Windows. The same code will probably compile just fine on both platforms.

The issue you had originally is just where the program is being run from, that's not really a code/compilation issue. From a windows point of view; if your 'shaders' folder is int he same directory as your .exe then it will work. MacOS may be different in that respect (I just don't know) so you might have to have a tiny bit of platform specific code which you can deal with with a simple #ifdef

#ifdef WINDOWS

path = windowsPath;

#end

#ifdef MACOS

path = macPath

#endif

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.


I assume that if i wamyed to build for Windows, I'd have to take my files into visual studio and write some code to check what operating system I'm using?

No. You cannot run a Mac OS program on Windows or vice versa, so how should runtime OS checking help? Instead you build an own executable for each platform. Those for Windows has some Windows specific code, and those for Mac OS has some Mac OS specific code. If you have the need to call such code from the common code base, than wrap the specific code into functions / classes giving them a well defined single interface.

Mac and iOS use application bundles to package executables together with all of their supporting files. You need to copy the shaders into the application bundle, and then you need to find out where your application bundle is. For the first step, the easiest thing to do is simply to add the shaders to the XCode project - they will automatically be copied to sit alongside the executable. It will ignore folder structure in this case. To get a folder structure, add a folder reference to XCode.

Once you have the files copied in, the usual way is to use Cocoa system calls (typically NSBundle's resourcePath property) to get the path to your bundle and thus your files. This requires an objective C file, which is of course a real hassle. The trick to get around this is that you can write an Objective C implementation for a C function. So I have a header called "PlatformHelp" with a bunch of simple C styled functions inside a namespace, and then it's implemented in C++/Win32 for WIndows and ObjC/Cocoa for Mac and iOS.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
The shaders are added to the project. In fact, they were created in Xcode.

I copied the code from the example into my project.

Nothing seems to work.

Find where your compiled application bundle is, right click and select Show Package Contents. I forget the exact structure in there, there's another app folder or something but you should be able to find your binary and the shaders should be there too. Then once you have that, you have to load the shaders from that location using [[NSBundle mainBundle] resourcePath], which in turn means adding a C header and Objective C implementation to get the path. The code from the sample will NOT work as-is.

Nobody said cross platform was easy.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Well, I found the bundle, and the shaders aren't in there. They've been added to the project so, could this be some kind of build setting issue?

This topic is closed to new replies.

Advertisement