Friday, September 30, 2011

Open CL first program

I struggled to find a really good program to getting started on openCL. so, in typical programmer fashion, I decided to make my own that fixed where others came short. I tried a couple places to post this, but no where had really good support for posting my code. What I ended up doing was realizing that I can host files from UbuntuOne. You can get the file from here.

The entire program is in one file for simplicity (first of my complaints against other examples). I go through the minimum steps to accomplish what is needed (another complaint) and explain what each step does and what the arguments that go into it are (my other big annoyance).

Remember, I'm learning this to as I blog, so I explain everything to the best of my understanding (by basically reading the openCL reference guide and rewording it). There are a couple things in that file that I out right said, I don't understand. But Considering the amount of comments I put in, if you read all of this, you should have a very good grasp on the basic steps. I might format it more down the road to make it prettier, easier to read, and more consistent. Any comments for improvements are welcome.

But from this point you have a code base to start from. And it is always always easier to start from a code base then to start from scratch. iterative development ftw!

Tuesday, September 27, 2011

Open Interfaces

So, it has been my intent to figure out how to program a game engine from the ground up for some time now. But I've never managed to get over the difficulty of the interfaces. Let me first define "ground" because this could be vauge. Ground in this case starts where driver interfaces end. Ground level stands on top of Open GL (for graphics), Open AL (for audio), and Open CL (for physics). With these 3 interfaces a professional level game engine can be made. Am I going to make a professional level game engine in this blog? lord no. But with my faithful copy of "Game engine Architecture" by my side, I'm certainly going to make something for the betterment of all young game developers just trying to learn how things work.

For me, one of the hardest steps has been simply trying to over come getting started. What I'm particularly refering to is getting those 3 open interfaces installed and working. Today, for the first time ever, I have succeeded at doing such. this took me months. don't be upset if it takes you some time, and if my information here doesn't help you, start asking on stack overflow. That being said, I'm running Ubuntu 11.04 right now. I will probably post later about how to do this in windows, but if you want to be serious about developing the c and c++ code that I will be talking about here, get yourself on linux. all sorts of options avaliable for doing such, if you are scared, try virtualisation, if you are confident duel boot. if you are bold, reinstall and put windows to rest once and for all.

With open GL and Open Cl the difficulty rested largely in figuring out why things weren't compiling. I read at least a dozen getting started guides. none helped. say for the last line of one in the opengl wiki ( "gcc -o example example.c -lX11 -lGL -lGLU" that is the line for compiling code for open GL in Ubuntu. The thing to know is that Ubuntu comes with OpenGL, stupid simple, but its true. When I had figured that out, things started falling into place. In fact, if you are on a computer w/o a graphics card (netbook for example) the example code from the wiki above should compile from a fresh install.

Open Cl takes a little more. I haven't succesfully implemented it on a computer with out a graphics card yet, so this assumes you have a graphics card. If you are an nVidia user, your graphics card needs to be cuda capable (if it has "GeForce" in it's name, you are good). if you are an ATI user, your card needs to be compatible with the ATI Stream SDK, which I believe is Radeon 5000+. For OpenCL, your graphics card driver should have come with the capacity, but it hasn't been "enabled" in a manner of speaking yet. For nVidia you need to get the most recent CUDA toolkit. It will include the headers needed to compile code. For ATI, the most recent Stream toolkit. With that you have the bare minimum of what you need to get working.

But if you try to compile (gcc -o example example.c -lOpenCL) it will fail, it will tell you that the .h file can't be found. what needs to happen is you need to make a symbolic link. For openCl it is a matter of pointing the includes to the right spot. gcc will lookin the folder /usr/include/ for .h files (or links to them. Create a symbolic link here that will point to where the .h files are:

sudo ln -s /usr/local/cuda/include/CL/ /usr/include/

this will create the link to the open CL libraries. If that doesn't work, then you can tell gcc to add the cuda include path to its search list. this is done with the flag -B. so type in "gcc -B /usr/local/cuda/include/CL/" and it will now look in that directory for the .h files and you should be able to compile.

Lastly there is openAL. It is important to note here that openGL and openCL have no affilation with openAL. GL and CL are open standards kept by khronos group. AL is a cross platform API that sits on top of drivers (such as ASLA, Pulse Audio, EAX, or Direct Audio on windows). Because audio doesn't take quite the effort that graphics and physics do, we can get away with the extra over head created here, for the benefit of having a system that is simple to cross platform. OpenAL is developed chiefly by Creative Audio. It was inspired by OpenGL, where OpenGL strived to create a cross platform, hardware agnostic, graphics card api, OpenAl sought to do the same for 3D audio in computer games. This is where OpenAL differs from khronos's OpenSL. OpenAL provides desktops sound, and is made entirely with games in mind, OpenSL is aimed chiefly at handleds.

Now, there isn't a lot of resources out there for openAL. But for installation in ubuntu, it is very simple "sudo apt-get install libopenal-dev" will install all you need. wa bam.

The next hardest thing for me has been each interface's hello world program. something that show that something has happened. the link i gave earlier for open GL was what finally worked for me. I haven't taken it all apart yet to figure out what does what, but i have something openGl specific running. that is one interface down.

I've never found a good openCL example. the simplest are all so long and do so much. So I'm going to have to create my own. that will be next post.