Doing without knowing – part 2

In the first part of this series we took a look at my weapons of choice for the Oculus/Indiecade VR Jam and in this part we’ll finally dive into some code! Also, please remember that I’m far from an expert in any of these areas so if your C++ expert friend tells you something else, listen! =)

Creating an OpenGL context

To be able to draw something in OpenGL we need a window to draw in. Creating a window that provide an OpenGL context as well as handle various keyboard and mouse inputs from scratch is hard, and it gets even harder when your time with C++ is measured in days and not years. So, what do we do when we realize that something is hard? We google!  And we soon realize that a lot of the window handling libraries are outdated (GLUT) while others are a bit too packed with features that using them would be a textbook example of overkill (SDL, SFML) and some are just boring (FreeGLUT).

There was, however, one that stood out, and that was a small library called GLFW. They had recently released a new major version (3.0) that stripped away even more of the already limited feature set and it seemed that it had everything that I needed.

And here comes the tricky part. To be able to use an external library you need to link it to your project. This is the part where I previously had lost it and just given up, but since that was not an option I decided to plow through this shit once and for all.

Building source archives

The first thing I needed was a project so I created a ”Win32 Console project”. The next step was to get GLFW up and running so I went ahead and downloaded the source archives. Source archives are not the most friendly way of getting GLFW up and running, but I had a feeling that I was going to be forced to build a few other libraries from scratch so I thought it was a good idea to bite the binary bullet an learn this as soon as possible.

The GLFW source contains cmakelists.txt and we’ll use this to build the libraries we use in Visual Studio.

  1. Install CMake and run it.
  2. Point the first path to the root of the GLFW project.
  3. Point the second path to wherever you want the generated files to end up (i chose cmakeroot/bin).
  4. Press configure.
  5. Use the checkboxes to select what you’d like to end up with. I chose GLWF_INSTALL and USE_MSVC_RUNTIME_INSTALL.
  6. Press configure and then generate.

CMake has now generated the makefiles needed for make to create a Visual Studio project in the bin folder. Build this and you’ll end up with a library that can be used on the windows platform.

Linking a library to your project

To use the library in a Visual Studio project we’ll need to add the include directory, the library directory and add the library to the input thingy.

  1. Right click on your project and choose properties.
  2. Add the GLFW include directory to Additional Include Directories in the C/C++ section.
  3. Add the path to the glfw3.lib you just build to the additional library directories in the Linker section.
  4. Add glfw3.lib and opengl32.lib to the Additional Dependencies in the Linker->Input section.

Using GLFW

We can now go ahead and include GLWF into our project by including the header.

#include <glfw3.h>

When the header is included we can go ahead and call a few GLFW functions to create our window.

	glfwInit();
	GLFWwindow* window = glfwCreateWindow(1280, 800, "V^Bert", NULL,NULL);
	glfwMakeContextCurrent(window);
	glClearColor(1,0,0,1); // red

As you can see the resolution is set to match the one used on the Oculus Rift, and if you would run the code it would create a window matching that resolution. The reason behind running in window-mode instead of fullscreen is that if you were to use the debugger and set a breakpoint you’d end up with a non responsive IDE and the agony of killing the process. Always use windowed mode unless you are testing your application on the rift.

The last piece of the puzzle is to add our main loop. The main loop is where the magic happens and right now it only clears the screen, swaps the buffers and displays the content we want (red) and then polls window and input events.

	while (!glfwWindowShouldClose(window))
	{
		glClear(GL_COLOR_BUFFER_BIT);
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

The complete program looks like this and the only thing it does is to create a new window with an OpenGL context for us to render our graphics in. I’ve also modified the code so that it enables you to press the ESC button to exit the program.

#include "stdafx.h"
#include <glfw3.h>

GLFWwindow* window;
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int _tmain(int argc, _TCHAR* argv[])
{
	glfwInit();
	window = glfwCreateWindow(1280, 800, "V^Bert", NULL,NULL);
	glfwMakeContextCurrent(window);
	glfwSetKeyCallback(window, KeyCallback);

	glClearColor(1,0,0,1);

	while (!glfwWindowShouldClose(window))
	{
		glClear(GL_COLOR_BUFFER_BIT);
		glfwSwapBuffers(window);
        	glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

One last thing (GLEW)

In order to use some functions in OpenGL we’ll need to use extensions. An extension manager handles all the fuzz for you and while it is not strictly needed it helps a LOT.

  1. Download the GLEW Win32/64 binaries
  2. Copy bin/glew32.dll to your debug/release directory
  3. Add the include path to additional include directories
  4. Add the lib path (where glew32.lib is located) to additional library directories
  5. Add glew32.lib to the linker input.
  6. Include it in your source with #include <GL/glew.h>

This is kind of a setup for the next tutorial where we’ll be using glew to generate buffers and such, but more on that later. =)

Summary

This pretty much sums it up for this part. Instead of taking huge steps I’ll take it one step at a time and I’ll try to improve on this post later on. In part 3 we’ll learn about shaders and render the mighty rectangle!

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Följande HTML-taggar och attribut är tillåtna: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>