Shadow Mapping on iPad

I’ve implemented shadow mapping on the MD2 Library using the Vortex Engine for iOS wrapper. Shadow mapping is a technique originally proposed in a paper called “Casting Curved Shadows on Curved Surfaces” [1], and it brought a whole new approach to implementing realtime shadows in 3D Apps.

Shadow mapping on iPad. The shadow is calculated in realtime and animates as the model moves.
Shadow mapping on iPad. The shadow is calculated in realtime and animates as the model moves.

Implementing shadow mapping on iOS is by nature a problem that spans several programming languages. Objective-C for creating the UI, C/C++ for interfacing with OpenGL and GLSL for implementing the technique in the GPU’s fragment shader.

The math involved in shadow mapping spans all of these languages, with different coordinate space transformations being implemented in the language appropriate to the pipeline stage we’re working on. This makes the technique a little tricky to implement the first time you attempt to.

Here is another screenshot of the technique running on an actual iPad. Notice how the shadow is cast on the floor as well as on top of the crate in the background.

Upfront capture of the shadow mapping technique running on the iPad simulator.
Upfront capture of the shadow mapping technique running on the iPad simulator.

Shadow mapping will be coming up in the next version of the MD2 Library app.

[1] – Lance Williams – Casting Curved Shadows on Curved Surfaces. http://artis.imag.fr/~Cyril.Soler/DEA/Ombres/Papers/William.Sig78.pdf

Fiddling with the HTML5 Canvas

I’ve been doing some experimenting with HTML5′s Canvas element. After some brushing up on Javascript, I started working on a couple of proof-of-concept scripts to see how well the platform can be used.

I have not been disappointed. Javascript, as a language, has evolved quite a bit since the old days of document.write(), with DOM editing as the one true way to manage dynamic content. Having said that, I find that Javascript is still quirky and flawed.

Here’s a short script I wrote (fewer than 160 lines) that animates a girl wearing the traditional Oktoberfest outfit. She walks back and forth within a given 500×500 area. This is not a video, your browser is doing the math to update her animations and move her around in realtime!

You should be able to see the demo just above this message, if you have a fairly recent browser that supports the HTML5 Canvas element. I was actually surprised about how mainstream Canvas support is: Chrome ≥ 22, Firefox ≥ 15, IE ≥ 9; Opera ≥ 12.1; Safari ≥ 5.1, Android ≥ 2.1 and Safari Mobile ≥ 3.2, all support this new element.

The animation atlas was taken from this cool website with resources free to use. The script “slices” the image, taking the appropriate frames to draw depending on the time simulation and the direction the character is walking towards (left or right).

Sprite animation atlas.
Sprite animation atlas.

Despite Javascript’s gotchas, with the addition of the HTML5 Canvas element, it’s become a nice tool that allows hacking together some easy 2D (or even 3D!) graphics and display them in a browser.

See you next time!

Function Objects using Variadic Templates in C++11

One of the biggest features introduced in C++11 are lambda expressions. Lamba expressions (or “lambdas”) are a powerful mechanism that allows defining functions “on-the-fly”. These can be stored in function objects and then be used by an object that doesn’t want to reveal its internal implementation, as well as to perform tasks on a parallel thread.

I was wondering how function objects could be implemented using other constructs of the language, mostly to see how much the language had to be changed in order to accomodate them. In this post I will show how to implement function objects using nothing but C++11′s variadic templates.

Variadic templates were introduced in our previous post in this C++11 series. This post will help pave the way for us to move into the more advanced topic of lambda expressions and std::function objects.

Function Pointers in C

As far as I can tell, C has always had functions as first-class citizens. Although their syntax is vastly different from the way one declares variables of other types, you could always define a variable that can be assigned a pointer to a function.

#include <stdio.h>
void print_number(int number)
{
    printf("Argument is: %d\n", number);
}

int main(int argc, char* argv[])
{
    void (*f)(int);  // f is a variable that can 
                     // store a pointer to a 
                     // function that receives 
                     // an int an returns nothing.
    f = print_number // legal.

    f(1024);         // calls print_number.  

    return 0;
}

Because C is a statically-typed language, we must be very careful with the types we declare f with, otherwise the assignment may be deemed invalid by the compiler.

Another problem is that the syntax can be cumbersome to remember and handle. Notice how the name of the variable is declared within the associated types.

Function Objects in C++

Unless you were using boost, C++ never had its own version of function objects until C++11. Programmers had to rely on the traditional C construct or write functors, classes that implement operator() and allow mimicking a function’s behavior.

Unlike C function pointers, functors are more readable, but we need to declare a class and implement operator() inside it. It’s much more verbose.

C++11 introduced the standard std::function type, which allows encapsulating a function declared by lamba expressions (or by other means). In production code, you will most certainly want to use std::functions. In this post, however, because of academic interest only, we’re going to exercise C++11′s variadic templates by implementing a generic function wrapper object called alg::function.

alg::function

Using variadic templates, it’s very easy to implement a simple function wrapper capable of handling any function type. I’m going to implement my solution using C function pointers. Remember: in C++11 production code, you will never do this.

namespace alg
{
    template <typename R, typename... T>
    class function
    {
        public:
            function(R (*f)(T...)) { _f = f; }
            R operator()(T... args) {return _f(args...);}
		
        private:
            R (*_f)(T...);

    };
}

And that’s all there is to it, really. Let’s go over the code.

I declare the class alg::function as a template that is to be instanciated by two parameters: a return type dubbed “R” and a list of types which I will refer to as “T”. The list of types can be empty, the return type is mandatory.

alg::function objects will have a single private member that is declared to be a pointer to a C function that returns a value of type R and receives the list of argument types T.

The only two additional methods that we need are: a public constructor that receives the function to wrap, storing it, and operator(), which will let us call the function. Notice how easily we can unpack the arguments when we call _f.

Instancing Examples

Let’s see it in action!

These samples illustrate the basic idea without complex data structures. It is certainly possible to pass alg::function objects around as the first-class citizens that they are, as well as passing-in and returning arbitrary C++ objects and structs from these wrappers.

The only limitation is the fact that we don’t support const parameters. Adding support for them would require tweaking the alg::function class a bit more. We leave this as an exercise to the reader.

A single function that receives an int and prints it:

void fun(int arg)
{
    std::cout << "hello from fun! (arg=" 
              << arg 
              << ")"
              << std::endl;
}

int main(int argc, char* argv[])
{
    alg::function<void, int> f(fun);

    f(0); // prints "hello from fun! (arg=0)"
}

A function that receives no parameters:

void fun2()
{
    std::cout << "hello from fun 2!" << std::endl;
}

int main(int argc, char* argv[])
{
    alg::function<void> f2(fun2);

    f2();  // prints "hello from fun 2!"
}

A function that receives two std::string instances and prints them together:

void fun4(std::string msg1, std::string msg2)
{
    std::cout << "hello from fun 4! (concat=" 
              << msg1 << msg2 
              << ")" 
              << std::endl;
}

int main(int argc, char* argv[])
{
    alg::function<void, std::string, std::string> f4(fun4);

    f4("hello, ", "world!"); // prints "hello from fun 4! (concat=hello, world!)"
}

Conclusion

Variadic templates offer a lot in terms of flexibility and allow us to extend the language even further than it was possible before. Remember, these examples are just food for thought. In C++11 production code, you will most certainly want to use std::function objects and lambda expressions.

See you next time!

Other articles in this C++11 series

libsdl-1.2 support for OpenGL 3.2

This week we take a break from the C++ saga to talk a little about OpenGL. I’ve forked libsdl-1.2 and added support for creating OpenGL 3.2 forward-compatible contexts. This is something that could be deemed helpful until libsdl 2.0 is released.

You can find the source code on my GitHub page, at: github.com/alesegovia. In so far, only the Mac platform is supported, as it’s the only Operating System I currently have access to. I’ll hopefully be able to add Linux support as soon as I can get hold of a Linux box with a suitable video card.

Creating an OpenGL 3.2 compatible context is very simple. Once you have downloaded, compiled and installed libsdl-1.2-gl, you just need to create your window using the new SDL_OPENGLCORE flag.

This sample program creates an OpenGL 3.2 context, displays the OpenGL version number and exits:

#include <SDL.h>
#include <stdio.h>
#include <OpenGL/gl.h>

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Surface* pSurface = SDL_SetVideoMode(600, 600, 32, SDL_OPENGL|SDL_OPENGLCORE);

    printf("GL Version:%s\n", glGetString(GL_VERSION));

    SDL_Quit();

    return 0;
}

You need to be running Mac OS X Lion or higher in order to be able to create OpenGL 3.2 contexts. If you are running Snow Leopard or your video card does not support OpenGL 3.2, you might get a Compatibility profile and your OpenGL version might be stuck on 2.1.

Also note that Mac OS X reports the OpenGL version to be 2.1 unless you specifically create forward-compatible OpenGL contexts, so if you need to know whether your Mac supports OpenGL 3.2, you can look your system configuration up in this great table maintained by Apple.

If you find this useful, let me know in the comments. Enjoy!

Variadic Templates in C++ 11 (Part 2 of 2)

Last week we started discussing C++11′s new Variadic Templates.

In that article I showed you how to declare a function initialized by a Variadic Template that returns the number of template arguments it was called with. Today, we build on those concepts to implement a printf-like function with the properties of being typesafe, don’t requiring parsing formatting characters and supporting non-POD data types. Let’s get started.

The first thing I want to do is rewrite the example from last week. This will ease introducing this week’s concepts. Let’s create a new function g that does the same that f did, but receives any number of arguments via template instantiation.

template <class... T>
size_t g(T... args)
{
  return sizeof...(args);
}

g does exactly the same that f, the only difference is that we can now call it using the following syntax:

int main(int argc, char* argv[])
{
  cout << g(); // prints "0"
  cout << g(1); // prints "1"
  cout << g(1,2,3,4,5,6,7) // prints "7"
}

It is, in a way, like a variadic function, but written in C++11 style instead of plain C.

It’s important that you understand how g works, if you don’t, please review the code again and try making changes to it.

Assuming that’s out of the way, let’s delve in today’s article.

We want to develop a print function that can receive any number of parameters of any type. So we can call:


A a; // Create some object...

print(1);
print(1, 2.0f);
print(1, 2.0f, "Hello");
print(1, 2.0f, "Hello", a);

// An so on...

Now, you might be guessing that one way to implement this function would be to have a “base case” that can print one argument and then have it called with each supplied argument.

template <class T>
void print(const T& msg)
{
  cout << msg << " ";
}

That’s a really good idea and, last time, we saw how to determine how many arguments a Variadic Template has been instanciated with (using the sizeof… operator).

Unfortunately, when it comes to Variadic Templates, we can’t iterate over the arguments based on the count. We need to find another way.

In an idea that I regard largely borrowed from functional programming, what if we could separate the list of arguments in the “head” element and the “rest” of the list?

Well, if that was the case, then we could call our base-case print function with the head and then call the general-case function with the rest of the list. Eventually, the list will be empty and we will have printed all elements!

We can do exactly that using C++11 new template syntax:


// This is our base-case for the print function:

template <class T>
void print(const T& msg)
{
  cout << msg << " ";
}


// And this is the recursive case:

template <class A, class... B>
void print(A head, B... tail)
{
  print(head);
  print(tail...);
}

Let’s try this with a simple program:

int main(int argc, char* argv[])
{

  print(1, "\n");
  print(1, 2.0f, "\n");
  print(1, 2.0f, "Hello", "\n");

  return 0;
}

The output should be something like:

 1 
 1 2 
 1 2 Hello

And that’s all there is to it. The C++ compiler will take care of generating the appropriate code to instanciate our functions and to split the parameter list so we can print the arguments one by one. Notice how this is much easier than having to mess with variadic functions, while being typesafe too.

The biggest challenge is assimilating the new syntax, but there are lots of good references around like this article from IBM, where I got the idea of writing a print function, or even the Variadic Template proposal for the ISO committee, another great resource.

Stay tuned for more C++11 goodness next week!

Variadic Templates in C++11 (Part 1 of 2)

This week we are continuing with the C++11 saga, moving on to a new feature called “Variadic Templates”.

Inspired by the concept of Variadic Functions in C, the idea behind Variadic Templates is to allow a template to be instanciated with any number of arguments.

In this article I’ll cover the new syntax and present a simple example that declares a function that uses a Variadic Template that prints the number of arguments it was intantiated with.

Previous parts of this saga included an overview of new iterators and lambda expressions as well as Move Semantics, which was treated in two parts: Part 1 and Part 2.

Let’s get started!

First of all, C++11 had to extend the C++ syntax for templates in order to support Variadic Templates. The new syntax allows annotating a parameter with an ellipsis (…) to denotate that we may expect receive zero or more parameters in the given place.

This means that the following declaration is now valid C++:

template <class... T>
void f()
{
}

f is a function that can be expanded with zero or more template parameters.

Now that we know how to declare a Variadic Template, let’s write a simple program that prints the number of arguments the function f template has been expanded with.

#include <iostream>
using std::cout;
using std::endl;

template <class... T>
size_t f()
{
    return sizeof...(T);
}

int main(int argc, char* argv[])
{
    cout << "sizeof f<int>" << f<int>() << "\n";
    cout << "sizeof f<int, float>" << f<int, float>() << "\n";
    cout << "sizeof f<int, float, char>" << f<int, float, char>() << endl;
    return 0;
}

Compiling this program with clang and running it produces the following output:

sizeof f<int>: 1
sizeof f<int, float>: 2
sizeof f<int, float, char>: 3

Here, each line of the output corresponds to the number of parameters the function template was instantiated with. As you probably noticed, the new sizeof… takes the packed template arguments and returns their count. If we just called f without instantiating its template, the number of arguments printed would be 0.

Now, you might be wondering what Variadic Templates might be used for. In next week’s article I will show you how to write a typesafe printf-like function in C++11 style. Stay tuned!

Move Semantics in C++11 (Part 2 of 2)

Last week we talked about the problem with passing objects around in the 2003 C++ standard, and mentioned how the new move semantics in C++11 could help us avoid expensive object copies.

In this article I continue where I left off last week and show an updated version of the code that implements move semantics.

Now, the new code might be generated by a C++11 compiler automatically, just like default copy and assignment operators are generated, but I will show you how to implement these yourself.

Normally, we would just go about adding the new functions to our program, and that’s the way it should be for new C++11 codebases. Chances are, however, that today you will probably be adding support to an existing project that might need to be built for a platform that doesn’t have a readily available C++11 compiler.

You wouldn’t want to break compatibility with that platform, so something you can do is to conditionally compile move semantics into your program. This way, you can have the best of both worlds: a speedy implementation for programs compiled with a state-of-the-art compiler and a (slower, but working) fallback for all other target platforms.

So, bearing this in mind, let’s add a new move constructor and a new move assignment operator to A, but protected by a MOVE_CTOR macro:

#include <iostream>
using std::cout;
using std::endl;

class A
{
	public:
		A();
		A(const A& other);
		A& operator=(const A& other);

#ifdef MOVE_CTOR
		A(A&& other);
		A& operator=(A&& other);
#endif //MOVE_CTOR

		float _a;
};

A::A() : _a(0.0f)
{
	cout << "Running A()" << endl;
}

A::A(const A& other)
{
	cout << "Running A(const A&)" << endl;
	_a = other._a;
}

A& A::operator=(const A& other)
{
	cout << "Running operator=(const A&)" << endl;
	_a = other._a;
	return *this;
}

#ifdef MOVE_CTOR

A::A(A&& other)
{
	cout << "Move-constructing A" << endl;
	_a = other._a;
	other._a = 0.0f;
}

A& A::operator=(A&& other)
{
	if (&other != this)
	{
		cout << "Move-assigning A" << endl;
		_a = other._a;
		other._a = 0.0f;
	}

	return *this;
}

#endif //MOVE_CTOR

A getAnA()
{
	A a;
	a._a = 1.0f;
	return a;
}

int main(int argc, char* argv[])
{
	cout << "==Declaring A a==" << endl;
	A a; 
	cout << "==Assigning a=getAnA()==" << endl;
	a = getAnA();
	cout << "==done==" << endl;
	cout << "a._a = "<< a._a << endl;
	return 0;
}

To build this source file with support for move semantics, use the following command. Older compilers can opt-out of building C++11-specific code.

clang++ -std=c++11 -stdlib=libc++ -DMOVE_CTOR main.cpp

If everything's alright, here's the output that should be produced from running this program:

:@~/devel/C++/move_ctor11$ ./a.out 
==Declaring A a==
Running A()
==Assigning a=getAnA()==
Running A()
Move-assigning A
==done==
a._a = 1

Here we can immediately notice how we are avoiding running the (expensive) copy assignment operator and we substitute it with a lightweight move assignment that takes the internal data of the source object.

Think how this could help you avoid deep-copying matrices, trees, memory pages, and even how it would help you code safer by preventing having to sacrifice const-correctness and immutable object state in your code as hacky ways to gain performance. C++11 really does feel like a new language.

Stay tuned for more C++11 goodness next week!

Move Semantics in C++11 (Part 1 of 2)

Continuing with the C++11 saga, this week I’ve been playing with move semantics. In this post I share my findings regarding using it on Apple’s Clang version 4.0.

Move semantics are a new mechanism built into C++ that allow us to “move” data from one object to another. They help avoid full object copies that, today, could occur under different scenarios.

The copy involved in passing in values to a function is a good example that can usually be avoided by receiving parameters as const references instead of as values. Other scenarios, like returning an object from a function, are, however, harder to avoid.

Move semantics let us write special functions that can “steal” a source object’s inner data and assign it to a new object. It could help you avoid an expensive copy operation when you know the source object is not going to be used anymore.

I’ve written a simple example to illustrate the problem. Suppose we have class A and we want to have a function that creates an A instance, then customizes it and finally returns it to the caller.

#include <iostream>
using std::cout;
using std::endl;

// A's interface:

class A
{
	public:
		// Default ctor:
		A();
		
		// Copy ctor:
		A(const A& other);

		// Copy-assignment operator:
		A& operator=(const A& other);

		// Inner data, should be private...
		float _a;
};

// A's implementation:

A::A() : _a(0.0f)
{
	cout << "Running A()" << endl;
}

A::A(const A& other)
{
	cout << "Running A(const A&)" << endl;
	_a = other._a;
}

A& A::operator=(const A& other)
{
	cout << "Running operator=(const A&)" << endl;
	_a = other._a;
	return *this;
}

// Helper function to create and customize an A instance:

A getAnA()
{
	A a;
	a._a = 1.0f;
	return a;
}

// Program entry point:

int main(int argc, char* argv[])
{
	cout << "==Declaring A a==" << endl;
	A a; 
	cout << "==Assigning a=getAnA()==" << endl;
	a = getAnA();
	cout << "==done==" << endl;
	cout << "a._a = "<< a._a << endl;
	return 0;
}

Running this code yields the following output:

:@~/devel/C++/move_ctor11$ ./a.out 
==Declaring A a==
Running A()
==Assigning a=getAnA()==
Running A()
Running operator=(const A&)
==done==
a._a = 1

The problem here lays in line 6. When the instance is to be returned from function getAnA(), it lives in the called function's stack, so we need to copy it into the caller's stack space in orde to return it. The code that does the copy is generated automatically by the C++ compiler and will incur in a performance penalty.

How slow is it? Well, it depends. For a big object (for instance a memory page, a huge matrix or other gigantic data structure) the performance penalty can be significant, specially when we really start to pass objects around.

This copy, however could completely be avoided by leveraging the fact that the source instance is going to go away. Move semantics allow us to have the destination A steal (or move) the source A's inner data.

Next week I'll show you how you can implement move semantics to speed up object passing. Stay tuned!.

C++11 Iterators and Lambdas (Revisited)

Last week I introduced an article where I tested C++11 support in Apple’s version of the clang compiler (Apple clang 4.0).

As you may remember, after conducting some tests, I was left with the impression that clang’s implementation was far behind of what I was expecting, with some notable features missing like initializer lists and some iterator constructs. This week I decided to revisit what had been done by studying the clang documentation.

After some reading, it turns out that C++11 support is beyond what I originally though. The problem is that (at this point) C++11 support is still an “opt-in” compiler option.

Language support is turned off by default and so is the standard library. That’s right, clang is shipping with not one but two C++ standard libraries: the C++ 2003 legacy library (libstdc++) and the new C++11 library (libc++). You need to tell clang that you want it to link against the new library, something I was not doing before!

With this in mind, let’s develop a new C++11 program:

#include <vector>
using std::vector;

#include <algorithm>
using std::for_each;

#include <iterator>
using std::begin;
using std::end;

#include <iostream>
using std::cout;
using std::endl;

int main(int argc, char* argv[])
{
	// Initializer List goodness : )
	vector<float> v = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f };

	// for_each loop Goodness using new iterators and a lambda:
	for_each(begin(v), end(v), [](float f) { cout << f << " "; });
	cout << endl;

	// New "auto" keyword semantics:
	auto f = v[v.size()-1];
	cout << "An element: " << f << endl;

	return 0;
}

Now, many things have changed since our last program. We can now access lots of goodies from the new standard, including (as noted in the code): initializer lists, std::for_each, std::begin, std::end, lambda expressions and the new “auto” keyword semantics. They are all supported and work as expected.

To build this program, assuming you’re using Apple’s clang 4.0 or the Open Source clang version >= 3.2, use the following command:

$ clang++ -std=c++11 -stdlib=libc++ main.cpp

Notice how I explicitly invoke clang++ as opposed to clang and how I must specify the standard library version to use. libc++ is the LLVM’s project implementation of the new C++11 standard library.

If you have been following with your text editor and compiler, the output generated by running this program should be something like:

$ ./a.out
0 1 2 3 4 
An element: 4

This now feels much better, much more in tune with what one would expect from a project as important as LLVM.

In my next article I will continue exploring C++11′s implementation in clang as I test more advanced features of the language such as shared pointers and threading. Stay tuned!

Playing with C++11 (using Clang)

I have been following the new C++ standard for quite some time (even before it was named C++11), so I’m very glad to see support for it reaching mainstream compilers such as clang, g++ and Visual C++.

Although this article here provides a good comparison of the state of the C++11 implementation in these three compilers, I wanted to give Clang on OSX a spin to see for myself how much of the new standard is actually supported in “real life” today.

I tried writing a short program that tested different features. I wasn’t expecting everything to work but, I was surprised to learn that some features that I was giving for granted are not widely available yet in clang 3.2 nor 4.0 .

Here’s my test program. It stores the numbers from 0 to 99 in a vector and then prints them to the console.


#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
	// Create and populate a container:
	
	const int n = 100;
	std::vector<float> v(n);
	for (auto i = 0; i < n; i++)
	{
		v[i] = i;
	}
	
	// Iterate over v, applying a lambda expression on
	// each element.
	
	std::for_each(v.begin(), v.end(), [](int f) {
				std::cout << f << " ";
			});

	std::cout << std::endl;

	return 0;
}

Some new C++11 features used in this program are:

  1. New std::for_each for iterating over a vector.
  2. New "auto" keyword semantics.
  3. Lambda Expressions (used here for printing elements).

Some features I wanted to try but are not yet supported in Clang 3.2, nor in Apple's Clang 4.0 are:

  1. Initializer Lists. Some people claim it is still not to be supported by Apple, but I couldn't get it to work on an open source build of Clang. Granted, I used 3.2. I should try building a newer clang perhaps even from trunk.
  2. std::begin() and std::end(), which should come with the updated Standard Library. It seems the STL implementation is not complete yet.

If I have time to try different compiler configurations, I might post the results here. All in all I'm happy with support reaching the mainstream and hope that I can start writing C++11 code in my next assignment.