An OpenGL Shader Example in Nim

It's been a while since I've posted something.  It's mainly because I've been busy for the past couple of weeks.  I've had to put my C# Networking Tutorials series on hiatus for the time being.  But I do have plans to finish it with about three more articles (they'll cover things like async).

In the meantime, I've been playing around with this language called "Nim."  I first heard about it from /r/programming.  It was popping up every so often so I decided to finally check it out about two or three weeks ago.  So far, I've found it very pleasant to work in despite being fairly young.  I've been reading through Peter Shirley's Raytracing Minibooks but writing the code in Nim instead of C++; it's been pretty good so far (I'll post about that on a later date).

You probably can guess that I really like graphics.  So one of the first things I looked for were OpenGL examples.  I had a lot of trouble trying to find a solid sample that showed you how to use shaders, so I made one myself.  You can find a link to it here, but the source for it is also included in this post.  I've tried to keep things as simple as possible, so there is duplicate code (e.g. the shader construction code), but it makes it easier to follow.  You can compile the program with nim c nim_opengl_shader_example.nim.  I built this on Linux so I also had to add in a {.passL: "-lGL".} call at the beginning.  Change it for the platform of your choice.   It also uses this GLFW binding, though it should be simple to change it out for SDL2.  It should show a colourful rotating hexagon.  You can change it's speed by passing in a non-negative float at the command line, e.g. "0.5," or "10".  Pass in "0" to make it not rotate.  If you want anti-aliasing, add "aa" to the command line arguments.  Here is what it looks like:

Edit: The official opengl package handles the linking for you.  If you're using a different OpenGL binding (like those from nimios,) you'll need to add the passL in manually for your OS.

 

OpenGL Shader Example in Nim

 

And the source:

 

© 16BPP.net – Made using & love.
Back to Top of Page
This site uses cookies.