RSS

Category Archives: C++

Herb Sutter’s “Why C++?” Talk Sum-up

This is a sum up of the points I believed to be the most interesting in Herb Sutter’s recent “Why C++?” talk. The video was published on Microsoft’s Channel9, but I have also embedded it here for convenience.



The talk starts with a recap on C++’s history. Then Herb Sutter talks about the rise of “Coffee-based” languages (i.e. managed languages) and how for about a decade (from 1999 to 2009) the industry seemed to accept trumping performance for productivity. Mr. Sutter clearly states he believes this is a perfect valid point, however, he notes that the opposite is also true: trumping productivity for performance is valid (and necessary) in many scenarios. The talk focuses on three such scenarios: Mobile, Datacenter and Desktop.

Managed Languages

Before moving into these scenarios, the talk focuses on the subject of managed languages. Mr. Sutter referenced Windows “Longhorn”, and its attempt to push managed languages as a first class citizen in Operating Systems. He evoked a quote of the time stating that “All new Windows APIs would be managed”.

However, as the OS was being developed, they realized that managed languages didn’t have the performance, deployability or serviceability (among others) required to develop an Operating System. That’s why Windows nowadays is all C and C++ and will probably stay that way.

Furthermore, demanding applications, like vision Apps running on mobile devices and Kinect applications, further emphasize the need for high performance in languages. According to Mr. Sutter, these applications are breathing new life into performance languages, and have been triggering a “C++ Renaissance”.

“Was .NET All a Mistake?” Of course it wasn’t, Mr. Sutter states. “But it can’t do everything C++ can, just like C++ can’t do all .NET can”. Managed languages were the hammer that made everything look like a nail during the last decade. Today, the focus is on high demanding applications that need performance. The “king” is Performance per Watt, per Cycle, per Transistor: Performance per Dollar.

Mr. Sutter takes some time to explain the drawbacks he sees on managed language performance. He quotes several reasons including: Garbage Collection, JIT services, lack or no control on caching and memory layout, etc. Mr. Sutter states that some of these drawbacks can be helped, but that programmers basically have to fight against the language in order to do so.

According to Mr. Sutter, except for the last decade, performance has always been the main reason to pick a programming language, and starting two years ago, it is now again.

Mobile

“Let’s talk about Mobile”. Mobile devices are all about battery, size and bigger experiences on smaller hardware.

Smart Phones are small computers. If we look at the main platforms today: iPhone is programable in Objective-C, C and C++. All native code. Android supports Java, but the Native Development Kit is essential when we need performance.

Mr. Sutter recalls it has not always been this way. The original iPhone preached Javascript development, but for its second version it went all native. Android started preaching Java development, and now supports native development as well. Windows Phone 7 started off with .NET, but by looking at the other two platforms, one can guess what will happen in the near future. The trend is to go all native.

What happens with Web Apps? According to Mr. Sutter they are and will still be there, but will be used as a backup when a native App does not exist for the task. We can look at Google+ for an example. Everyone uses the native App, and “default” to the Web App when there is no native App for the platform being used. Why? Mr. Sutter says it is because of their Performance.

Datacenter and Desktop

What happens in the Datacenter? Citing a study, Mr. Sutter claims that the biggest cost is Hardware and Power. These account for 88% of the total cost of running a Datacenter. Performance/Watt has a direct impact on this, and it can be improved by having more efficient programs.

Mr. Sutter states that if we could reduce Time and Space requirements, we could improve efficiency. Efficiency is “king” for managing the costs of the Datacenter.

Quoting Bjarne Stroustrup: “My biggest contribution to the fight against global warming is C++’s efficiency: Just think if Google had to have twice as many server farms! Each uses as much energy as a small town. And it’s not just a factor of two… Efficiency is not just running fast or running bigger programs, it’s also running using less resources.”

Regarding the Desktop Mr. Sutter notes how being efficient on power and heat also matters in the Desktop. I believe this case is particularly true for laptop devices.

C++11 and the next Decade

Mr. Sutter states: “C++11 is upon us”. Not only does C++11 have a lot of new features, but Mr. Sutter emphasizes how these will be available and also required soon. Programmers will be required to use the new Features in C++11 in order to take advantage of new frameworks (like C++ AMP). “C++ matters again as a first class system”.

The talk concludes by presenting view on what will come after the C++ Renaissance decade (2009-2019). Moore’s law is going to end. It will not happen over night, though, according to Mr. Sutter. The industry will be able to see the end coming when the end of we start obtaining diminishing returns.

Efficiency-oriented languages have a bright future, and they are going to stick around.

To end his presentation, Herb Sutter states that “The answer to the question <<Why C++?>> is <<Why, C++, of course!>>.”

 
Leave a comment

Posted by on September 8, 2011 in C++, Video

 

Support std::endl in custom C++ streams

I’ve been working on a custom Logging system for Vortex. After thinking about it for a while, I decided to go with a stream-based API for logging. I figured this would allow me to both: provide output capabilities for custom data types and also to format log messages “on the fly”.

This is where I started to play with the idea of being able to handle std::endl just like the standard streams.

Even though I’ve been a C++ programmer for a while (10 years since my first “hello world” program), I realized I actually didn’t know the type of std::endl. I looked around until I finally found something at Stack Overflow. Much to my surprise, it turns out std::endl is a function template! The correct way to handle it is, in the stream insertion operator (<<), call the passed-in endl function with the stream you are outputting to as the parameter.

// The code in Vortex is more involved,
// but this is the general idea
Logger& Logger::operator<<(std::ostream& (*f)(std::ostream&))
{
    f(std::cout);
    return *this;
}

For Vortex, it was enough just to support std::endl taking and receiving std::ostream instances, it might be different in your code, depending on the degree of customization you want to provide.

Now that everything’s in place, we can handle std::endl just like any other stream:

Logger debug;
debug << "running loggerTest() in file.cpp:12" << std::endl;
 
Leave a comment

Posted by on August 11, 2011 in C++, Vortex-Engine

 

Introducing the L+ Compiler (and it’s free!)

I’m glad to announce that Emilio Pombo and I have just released the full source code for a custom compiler we built back in 2006!

The compiler, built as a mandatory Compilers assignment at the Catholic University of Uruguay, translates programs written in a C-like programming language, called L+, into Java Bytecode, suitable for execution on any platform with a Java Virtual Machine.

L+ supports most of C constructs (variables, branching, loops, functions, recursion, I/O), with some limitations and some additions (like native string handling). Here’s the mandatory “hello world” program written in L+.

// hello.lp
void main()
{
	print("Hello, World!");
}

The full source code is available as of now at google code, at http://code.google.com/p/lpcompiler/.

We have licensed the L+ compiler under the GPLv3, so you are free to download, compile, study, modify and ultimately redistribute your changes to anyone. I encourage you to check out the code, build it and try the examples that come bundled, so you can see what our little language can do :)

L+ is written in pure ANSI/ISO C++ and supports GNU/Linux, Mac OS X and Microsoft Windows. Dependencies are minimal and should build on any UNIX-like environment.

I want to thank Emilio Pombo and Leonardo Val for subscribing to the idea of releasing this program as Free Software and for also encouraging me to do so.

I hope you find the project as interesting as we found it fun to write it!

 
2 Comments

Posted by on July 9, 2011 in C++, Education, L+

 

Improvements to Md2 in Vortex-Engine

These last few days I’ve been improving the Vortex-Engine support for Md2 Model files.


Vortex Instancing Test


Md2 models can now be “instanced” from the same data at the Engine level (not to be confused with GPU instancing) and different animations can be assigned to each model. The idea for the short-term is to be able to provide entities the ability to create a Md2 model instance and animate it however they like without causing conflicts with other entities that instantiate the same model.

There’s a video I created, where you can see the test, right after the jump.
Read the rest of this entry »

 
Leave a comment

Posted by on April 2, 2011 in C++, Quake, Vortex-Engine

 

Quake 2 Model Rendering (Part II)

In my last article, I provided some overall details on how to load Quake 2 model files (also known as md2 files) and described how these models would store its animations in their so called keyframes.

By the end of the post I discussed the fact that (as you could see on video) the model animation looks rather “choppy”. In this post I will explain what we can do to improve the animation rendering process for models animated through keyframes.

What we are going to do in order to smooth the animation is, instead of just rendering the keyframe data directly, take advantage of a factor that we haven’t completely explored yet: time.

If by the end of the last post you started working on rendering md2 models, chances are so far you have some sort of time control that allows you to determine which keyframe to render.

The rendering problem that we face is characterized by the fact that sometimes, for a given elapsed time, we determine that the actual keyframe we have to draw lays between two keyframes. Let’s call these the nth and nth+1 keyframes.

In order to draw the mode to the screen, we face two choices: select the nth keyframe or the nth+1 keyframe. What’s important to know here is that in either case our choice will be wrong. This is because the actual data that we need does not exist, and so, it’s impossible to render the model correctly. Forcing the choice to the nth or n+1 keyframe is what makes our model animation look choppy.

Not everything is lost though. Even if we don’t have the data we need, we can infer it.

Should our needed keyframe actually exist, we know it would lay between two other keyframes. Now, as we have some sort of time control mechanism, we can determine which ones these two actually are. Under these conditions, something we can do is determine the “distance” from the current time mark to each keyframe’s.

Using this information, we can guess the keyframe that’s supposed to lay in between by interpolating both keyframes in time. The idea is to perform a convex sum. The following C++ pseudocode depicts the concept:

Vertex guess[_numvertices];
for (size_t i = 0; i < _numvertices; i++)
{
        guess[i] = _keyframes[n] * (1-t) +
                              t * _keyframes[n+1];
}

Here, guess holds our guessed keyframe, Vertex is a struct composed of three floats and t is a parameter dependent on the elapsed time that has been adjusted to the [0,1] interval. When t=0, it means the keyframe to render is actually the nth keyframe. Conversely, t=1 indicates the keyframe to render is nth+1. Finally, notice that when t=0.5, the guessed frame will be halfway between keyframes n and n+1.

The following video shows the improved rendering process in action:



Notice how smooth the animation actually looks. This video was generated by running the animation at just 2 frames per second.

Conclusion

Interpolating between keyframes is certainly possible and, in our conducted tests, it really helped smooth the animation process.

This added visual quality does come at a cost, though. Every time we have to animate the model, we will have to iterate over the keyframe array, interpolating the values corresponding to two consecutive keyframes. The additional CPU overhead is well worth it nevertheless, dramatically improving the quality of our animation.

Many factors have been left out of this post. In particular, time control, elapsed time calculation and how to determine which keyframe should be rendered at a given time were all omitted. I believe these features are dependent on the kind of application being written and different formulae will or will not work in different situations. The best thing to do is to design a model that works for your application and produce a simple way to obtain the data needed to apply these concepts.

See you next time!

 
1 Comment

Posted by on March 18, 2011 in C++, Quake

 

Quake 2 Model Rendering

By mid June 2010 I worked on a personal project that consisted in rendering models from the classic Quake 2 game by Id Software. Here’s a video of the renderer.



Quake 2 was one of my favorite games when I was young and researching how to load and render its 3D models instantly sent me down the nostalgia path.

For those who might be interested in developing their own loader, Quake 2 models are very easy to read and parse from languages such as C and C++. This is mostly because .md2 files consist in a binary file format that we can easily read into C structs.

David Henry does a great job at explaining how the bytes are stored internally, so I’m not going to reproduce his work here. Nonetheless, here’s the md2 file header:

struct md2_header
{
     int id;
     int version;

     int skinwidth;
     int skinheight;

     int framesize;

     int numskins;
     int numvertices;
     int numtexcoords;
     int numtriangles;
     int numglcmds;
     int numframes;

     int skinDataOffset;
     int textureDataOffset;
     int triangleDataOffset;
     int frameDataOffset;
     int glcmdsDataOffset;
     int endOffset;
 };

Quake 2 model files organize vertices and triangles into keyframes: a series of poses that enable us to draw the model animated. The texture to use when rendering the model is referenced by the file too. Each file might point to zero or more textures in the PCX format. Textures are called the model’s “skin” in Quake 2′s terminology.

The video above were generated using the custom renderer that I wrote. It is using OpenGL and GLUT to create the window and draw the model.

Now, you’ve probably noticed that the animation in the video above looks rather “choppy”. It’s definitely not smooth. The reason is that keyframes provide a base for animating the model, but not enough frames are provided as to produce a “continuous” animation.

In my next post I’m going to show you how we can improve this situation. Stay tuned!

 
Leave a comment

Posted by on March 13, 2011 in C++, OpenGL, Quake

 

Overloading “operator bool” for fun and for profit

C++ is a great language when it comes to extensibility. I used to think most customizations were possible only because of the famous C preprocessor, however, other features such as operator overloading open the door for a whole new degree of extensibility too.

Operator overloading is a mechanism built right into C++ that allows classical operators such as +, *, /, – to be “overloaded” to operate on our objects. In some scenarios, operator overloading can greatly improve program readability, as it provides an infix notation for denoting operations on two objects.

This post is not about operator overloading in general, however (at least not about overloading the typical operators), but rather, about overloading the special operators we have in C++. In particular, and as you may have guessed from the post’s title, I’m interesting in talking about the “operator bool“.

operator bool is the operator that allows us to define if and how our objects can be implicitly cast into a boolean value. Why would you want to define an implicit conversion from an object into a boolean value? Well, it turns out there are a couple of cases where it can really help improve program readability.

Let’s consider the following example. Here, I want to write a program that reads a text file.

Now, there are a number of things that can go wrong when reading a file. The file might not exist. We might have read past it’s end. An error could’ve occurred. Etc. Wouldn’t it be great if we could test whether everything was OK without having to ask for each single possible error? Enter operator bool.

#include <fstream>
using std::ifstream;

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

#include <string>
using std::string;

int main(int argc, char* argv[])
{
	ifstream in("f.txt");
	while (in)
	{
		string word;
		in >> word;
		cout << word << " ";
	}
	cout << endl;
	return 0;
}

Notice the loop condition. ifstream instances can be implicitly cast into a boolean value to test for non-existing files, EOF, and other read errors.

Overloading operator bool is really easy. In the next snippet I'll show you how. Imagine we have a data provider class that, er, provides some data. We want to be able to test whether the stream is valid and has data just by dropping the object reference into a conditional.

class DataProvider
{
	public:
		operator bool() { return this->valid() && this->hasData(); }

// Rest of class omitted...

};

// somewhere down the road...

void process(const DataProvider& dp)
{
	while (dp)
	{
		unsigned char* pdata = dp.getData();
		// ...
	}
}

Easy, huh? One area where operator bool really shines is writing a custom smart pointer implementation. Operator bool can allow your library's users to test whether a smart pointer is null just by implicitly casting the smart pointer instance to bool.

Have a look at the following example. This is something we're doing today with Algorithmia's Vortex-Engine.

void renderQuad()
{
	ref_ptr<Texture> pTexture = getTexture("quad_texture.png");
	if (pTexture)
	{
             // Use texture for rendering a quad
	}
	else
	{
              // Error loading texture!
	}
}

In conclusion, C++ operator overloading is a powerful feature that adds a great deal of flexibility to the language. Nothing prevents you from redefining and completely changing the meaning of the operators that work on your objects. It's the programmer's responsibility to determine whether overloading an operator makes sense and actually improves readability, instead of tampering with it.

In general, use common sense and remember that with great power comes great responsibility.

 
Leave a comment

Posted by on March 9, 2011 in C++, Vortex-Engine

 

Quake 2 BSP Map Rendering

For the past month and a half I’ve been working on and off on a project that consists in the development of a custom Quake 2 BSP map renderer. The renderer was written from scratch using nothing but C++ and OpenGL 2.0.

Click on the image for more screenshots.

The project’s objective was twofold: to help develop the foundation for a custom object-oriented rendering engine and to test my skills at what seemed as such a daunting task as loading, rendering and navigating Quake 2 maps in realtime.

There is a lot of information on the Internet regarding Id Software’s BSP file format and the way polygons, texture data, lightmaps and any other aspects of the game are stored. In my experience, no website provides “the whole picture”, so if you’re planning on rolling your own renderer, be prepared to spend as much time doing research online and developing and testing ideas as writing code.

At its current status, I consider the renderer to be in a pretty good state, where it supports loading maps directly from Quake 2′s original PAK files, rendering its polygons, performing texture mapping, applying lightmaps, adding a skybox and leveraging the map’s visibility information to prevent large amounts of non-visible geometry from being sent to the Video Card. I’m glad I’ve been able to approximate the game’s look and feel in what I consider very close to the original.

There are still many open points that could be addressed, such as experimenting with blur effects to increase the sky’s realism and bring life into the skybox. All texture mapping and lighting is currently done using shaders, so the basic foundation for post production visual effects is laid and ready to be built upon.

At some point, I might provide executable versions of the renderer so you can try it yourself. In the mean time, if you’d like to view other rendered images, I’ve added more screenshots from this project at the Projects page of this site.

Here’s also a YouTube video showing an actual run of the Renderer:




Quake 2 and all of its art is Copyright (C) Id Software. Thanks to Id for creating such a great game.

 
Leave a comment

Posted by on December 5, 2010 in C++, OpenGL, Quake

 

Create Static Libraries using GCC

Static libraries provide a mechanism by which we can “pack” object code into reusable libraries. In this article I’m going to focus on creating static libraries of C (and C++) objects and functions. Let’s get started.

Creating a static library is actually a very easy process that consists in just two simple steps:

  1. Compiling source code into object code.
  2. Archiving all the generated object code into a single file that represents the library.

Assume we have the following C source files that declare a 3-element vector type and a dot function that calculates the dot product among two vectors:

/** algebra.h **/

#ifndef ALGEBRA_H
#define ALGEBRA_H

typedef struct
{
	float x, y, z;
} Vec3;

void vec3Init(Vec3* v, float x, float y, float z);

float dot(Vec3* a, Vec3* b);

#endif //ALGEBRA_H
/** algebra.c **/

#include "algebra.h"

void vec3Init(Vec3* v, float x, float y, float z)
{
	v->x = x;
	v->y = y;
	v->z = z;
}

float dot(Vec3* a, Vec3* b)
{
	return a->x * b->x + a->y * b->y + a->z * b->z;
}

We can use the following two commands to create a static library:

gcc -static -c algebra.c -o algebra.o
ar -rcs libalgebra.a algebra.o

Please notice we have named our static library “libalgebra.a”. This is not just a random choice as a static library’s name must begin with “lib” and end with “.a” in order to be able to have GCC link new programs against it.

Now that our library is ready, we can redistribute it with the header file “algebra.h” so other programmers can use it in their applications.

In order to illustrate this point further, let’s write a simple C program that includes the header and links against our library:

#include <stdio.h> /* printf */
#include "algebra.h" /* our library's header */

int main()
{
	Vec3 a;
	Vec3 b;
	vec3Init(&a, 1.0f, 2.0f, 3.0f);
	vec3Init(&b, 3.0f, 2.0f, 1.0f);
	printf("<a,b> = %.2fn", dot(&a,&b));
}

In the above example you can notice how we can liberally use data types and functions declared in our library as if they where part of this very same program.

Now, in order to compile, all we need is the “algebra.h” header, however, in order to have this program link, we have to use the “-l” (lowercase L) flag of the GCC compiler to specify our shared library, like so:

gcc main.c -L. -lalgebra
$ ./a.out
<a,b> = 10.00

Given the “-lalgebra” flag, GCC will look for a file called “libalgebra.a” (hence the importance in the library’s naming convention). The “-L” flag tells GCC where this file is located.

 
1 Comment

Posted by on May 22, 2010 in C++

 

On Polymorphism in C++

C++, as many other Object-Oriented languagues, provides many facilities for us to implement our Object Oriented Designs.

In this post I talk about a feature which is not frequently discussed, but must be taken into account when implementing a class hierarchy that leverages polymorphism as part of its design.

Let’s suppose we have a base class (conveniently named “Base”) and a derived class (conveniently called “Derived”) that extends “Base” through Public Inheritance.

If you don’t know what “Public Inheritance” means, all you really need to know for the purpose of this article is that there are three different kinds of inheritance in C++: public, protected and private. Public Inheritance is akin to inheritance in other programming languages such as Java, Python, Objective-C and C#.

Going back to our example, I’ve coded the Base and Derived classes’ interfaces in the following snippet:

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

class Base
{
	public:
		Base();
		virtual ~Base();
		virtual void printClassName() = 0; // =0 means "abstract"
};

class Derived : public Base
{
	public:
		Derived();
		virtual ~Derived();
		virtual void printClassName();

};

Now, for the implementation, I want to have every object whose class is derived from “Base” to have its classname printed upon creation. So, what I am going to do is add a call to printClassName() in Base’s constructor.

Base::Base()
{
	this->printClassName();
}

Base::~Base()
{
}

Derived::Derived() : Base()
{
}

Derived::~Derived()
{
}

void Derived::printClassName()
{
	cout << ""Derived"" << endl;
}

This is the program entry point:

// program entry point
int main()
{
	Derived* d = new Derived(); //print "Derived"
	delete d;
	return 0;
}

If you know your Patterns, by now you’ll probably have noticed by now that this example is just an implementation of the Factory Method pattern, where I’m relying in a virtual method for leveraging derived-class-specific behavior in the Base class.

The problem here is that, when we try to compile this program, we get the following error:

ale@syaoran factory]$ g++ main.cpp -Wall
Undefined symbols:
  "Base::printClassName()", referenced from:
      Base::Base()  in ccNxv98U.o
      Base::Base()  in ccNxv98U.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

GCC is warning us that there is no implementation for the abstract method “printClassName” in class “Base”, and it aborts!

Why does this happen? The compiler certainly should not be trying to find an implementation for an abstract method after all. The reason this happens is because of how inheritance works in C++.

When an object whose class is derived from another class is instantiated, what happens in C++ is that, in order to create this object, an inner instance of its base class is created first. In our example, when calling Derived’s constructor, Base’s constructor is called first.

This actually implies that the derived object does not exist until the the inner base object is created.

In turn, this means that the derived object’s vtable (used for implementing Polymorphism in C++) does not exist when the base class’ constructor is executing, so the compiler assumes that the base class’ implementation of the method has to be invoked, and that triggers the link error.

Even worse would be the case if the method was not abstract (but still virtual). In this case the program would build fine, but, at runtime you would find out that the base class’ implementation is being called instead of the derived class’.

How can you fix this problem?

Fortunately, the solution is simple, all you have to do is adopt the following gold rule:

Never call virtual methods on a partially-created object.

In order to fix our program, what I’m going to do first is to remove the printClassName call from Base’s constructor.

So, where should I place it now? Well, depending on the application it could be someplace or another. Since I would not like to change my design, what I’m going to do here is to separate the object construction into two phases: construction and initialization.

Construction will be carried out by the constructors, and initialization will be carried out by a special init() method that we can call once all the base objects have been created.

This separation will allow us to work safely, as all constructors will have executed when init() is called.

Applying these changes, the program will now look like this: the header for the Base class will include a new init() method:

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

class Base
{
	public:
		Base();
		virtual ~Base();
		virtual void init(); // new: two-phase initialization
		virtual void printClassName() = 0; // =0 means "abstract"
};

And the base class’ implementation will implement init() as to call our virtual method:

void Base::init()
{
    printClassName();
}

At this point the program should build fine and, when run, should produce the following result:

[ale@syaoran factory]$ ./a.out
"Derived"

As a final note, it would be possible to hide the call to init() into the Derived class’ constructor, but this will only work as long as Derived is the last class is the hierarchy.

As soon as we add a “Derived2″ class that inherits from Derived, we will have to refactor the call to init() into Derived2′s constructor, and so forth.

It would be interesting to see whether this problem arises in other programming languages. This will be left as an exercise for the reader.

You can find more information on abstract (pure virtual) methods and problems related to them here.

 
Leave a comment

Posted by on May 7, 2010 in C++