GLFW3 Basecode with FPS Camera Controls

Basecode is funny thing – when you start a new project, do you really start from scratch? A complete blank slate? Or do you make a copy of the last project you worked on which is similar and modify it? Often, you’re going to want to start from some pre-existing functional base, but what’s stable and functional enough? Do you really want to go with a framework like Cinder or Processing to hold your code? Or go with a full-on engine like Unity or Unreal Engine 4 or some other engine?

I’m going to write a game at some point in the future, and I want to go it (almost) alone – I don’t want to be locked into someone elses constructs and patterns, or drag-and-drop functionality in which I have absolutely no idea how it works – I want to think for myself and create what’s basically my own engine, where I understand how it fits together and how each piece works. This doesn’t necessarily mean that everything needs to be worked out from first principles, but it should be possible to make all the important architectural decisions. This means that I want precise control over:

  • At least one OpenGL window, with controllable context details (preferably multiple windows with a shared projection)
  • Painless keyboard and mouse handlers
  • File handling of common types (load and use this 3D model/sound file/settings file)
  • Help with prototyping via simple drawing calls

Which brings us back to basecode being a funny thing – you get to make the architectural decisions, and live with the consequences. If you decide to go with an engine, then you’re going to learn the engine – not the fundamental technologies or aspects of the code that make the engine work. So if you grab some fantastic engine and you go:

  1. Load this spaceship model, which is made of these different materials,
  2. There’s a light which is at (1000, 200, 300) in world space (and perhaps a dozen other lights),
  3. Draw the spaceship from my (i.e the camera’s) location.

But what does that actually teach you, as a developer? How do you load the model from file? How is the lighting model applied to the vertices? Where the hell is the spaceship in relation to you, let alone the surface normals of the spaceship with regard to the light-source(s) with regard to the camera? In an engine, you don’t care – you let the engine work it out for you, and you learn nothing. Or maybe you learn the engine – which means you learn to trust someone else to think instead of you having to think for yourself.

Which finally brings us back to basecode being a funny thing… I’ve been thinking about this for weeks, and below is the OpenGL/GLFW3 basecode I’ve written to open a window, draw some grids for orientation, and allow for ‘FPS-esque’ mouse and keyboard controls. The main.cpp is listed below, which shows you how the program itself runs – everything else you’ll need to look at for yourself – but I promise you this:

  • Every single piece of this code is clear in its use and serves a purpose.
  • Every single piece of this code performs its job in the simplest, most straight forward manner possible. If the option is to be clever or readable, then I pick readable every time. Saying that, I think I used an inline if-statement once i.e. “if (raining) ? putUpUmbrella() : keepUmbrellaDown();”. Honestly, when you see it, you’ll be okay.
  • Every single piece of this code is documented to explain not only WHAT the code is doing, but (where appropriate) WHY it is doing it. When I used to work as as Subsystem Integration and Test engineer, we would write software build instructions with the goal that your Mum should be able to build the software image from the simple, accurate, non-ambiguous instructions. If you didn’t think your Mum could build it, then you re-worked the instructions until you thought that she could.

I’ll add some additional utility classes to this over time, but for now, this basecode will get a window with FPS controls up and running and display some grids via shaders for orientation – and everything should be simple, straight-forward and clear. Enjoy!

Code::Blocks projects for both Windows and Linux (libraries included for Windows) can be found here: GLFW3_Basecode_Nov_2014.7z.

Update – Feb 2015: There were issues using this code in Visual Studio 2010 as it doesn’t support strongly typed enums or the R” notation (although VS2012 onwards does), and the libraries packaged were the Code::Blocks versions (which was intended – the above version is specifically for Code::Blocks) – so here’s a modified & fully working Visual Studio 2010 version: GLFW3-Basecode-VS2010.7z.

7 thoughts on “GLFW3 Basecode with FPS Camera Controls”

    1. I afraid I don’t own or have access to any Apple hardware so I can’t port it to XCode, however if you give me the exact error messages I might be able to advise the nature of the problem.

      Alternatively, perhaps ask if any of the nice people on StackOverflow would be willing to provide a configured XCode project version (as the code works fine on Windows and Linux it’s likely just a library linkage issue).

  1. When I try to compile the code in VS2010, I get the following errors:

    2>Grid.obj : error LNK2001: unresolved external symbol ___glewEnableVertexAttribArray
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewVertexAttribPointer
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewBufferData
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewBindBuffer
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGenBuffers
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewBindVertexArray
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGenVertexArrays
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewUseProgram
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewCreateProgram
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGetProgramiv
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewDetachShader
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewLinkProgram
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewAttachShader
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGetShaderiv
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewCompileShader
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewShaderSource
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewCreateShader
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGetAttribLocation
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewGetUniformLocation
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewDeleteProgram
    2>Grid.obj : error LNK2001: unresolved external symbol ___glewUniformMatrix4fv
    2>main.obj : error LNK2001: unresolved external symbol _glewExperimental
    2>C:\Users\User\Desktop\framework\SP2_Framework\SP2_Framework\Debug\Application.exe : fatal error LNK1120: 22 unresolved externals
    ========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Help would be much appreciated.

    1. Never mind, I got it to run. However, all I get is a white screen with the error message: “Could not add attribute: vertexLocation – location returned -1!”.

      1. If you used the provided Code::Blocks archive, did you replace the GLFW3 dll with the Visual Studio one? Compilers get fussy about using libraries compiled with other compilers, so trying to use a C::B compiled lib in VS is going to be problematic at best, it’s the same going VS to C::B.

        Trying grabbing a copy of GLFW3 from: http://www.glfw.org/download.html and replacing the file .\libs\Win32\glfw\lib\glfw3.dll with the one from the glfw3 zip .\lib-vc2010 folder.

        1. Looking into it more, if you’re using Visual Studio 2010 you need to make some modifications to the source – specifically, VS2010 doesn’t support strongly-typed enums or the R” notation of C++11/0x – I’ve just created a mostly working VC++ version, but the camera-look is being a dick… I’ll have a play and post the VS2010 version within a day or so.

Leave a Reply

Your email address will not be published.