The Animal Farm

December 4th, 2007

Plasma

plasma.zip

There’s the source code for a simple plasma effect I created. It’s slow - all manner of slow - but it works, is heavily documented, and illustrates the concepts. At the suggestion of Matheney, I’ve been meaning to move the entire thing into a fragment shader (which shouldn’t be terribly difficult and should increase the speed considerably), so that may end up as a small side project during finals week.

Who you gonna call?

December 4th, 2007

C++ String Split

Here’s a utility function that spawned from my Neural Nets project today that needs to find its way into my codebase at some point. It takes a string and splits it based on a list of single-character delimiters, returning a vector of all the tokens (sans delimiters). There may exist something like it somewhere in the standard library, but I couldn’t find it.

[ftf def="js.xml"]
vector split(string str, const string &delims = ” “)
{
vector tokens;
string tok = “”;
for (int i = 0; i < str.length(); i++)
{
if (delims.find(str[i]) != string::npos)
{
if (tok != "")
{
tokens.push_back(tok);
tok = "";
}
}
else
{
tok = tok + str[i];
}
}

if (tok != "")
{
tokens.push_back(tok);
}

return tokens;
}
[/ftf]

Useful, neh?

December 4th, 2007

OpenGL Guide

openglguide.zip

For those interested, the above link contains the source code (no binaries) for 10 demos I gave to students working at the VEL while I was working there. The demos are:

(1)Window Creation
(2)Basic Input
(3)Basic Geometry
(4)Color
(5)Matrices and Transformations
(6)Texture Mapping
(7)Glut Shapes and Quadrics
(8)Lighting
(9)Terrain
(10)Particles

All the demos use glut and all should be cross-platform (this is untested). They’re all pretty basic, illustrating simple concepts. I tried to comment as much as I could and make everything straightforward, so hopefully even a beginner will be able to follow them.

glEnable(GL_DEPTH_TEST);

December 4th, 2007

lexical_cast

Last time I showed a BasicVariant for storing different value types in the same variable. Boost provides similar functionality in the form of a lexical_cast, which essentially converts from one type to another. Thus, if you wanted to, you could skip the whole BasicVariant and just have a string instead, and use the lexical_cast function to do your conversions. A bit simpler, although also a bit less readable.

This morning I had a hack at trying to create my own lexical_cast function. Here’s what I came up with:

[ftf def="js.xml"]
template
TO lexical_cast(const FROM& val)
{
TO ret;
stringstream stream(stringstream::in | stringstream::out);
stream << val;
stream >> ret;
return ret;
}
[/ftf]

Although there are two template parameters here, when calling you only need to provide one - the type of TO. The type of FROM will be inferred by the parameter you pass in. So calling the function would look something like this:

[ftf def="js.xml"]
float x = lexical_cast(”10.01″);
[/ftf]

I’m fairly certain Boost’s equivalent is faster (there was something mentioned about avoiding stringstream altogether), but my version gets the job done, and I like the way it looks.

That’s pretty good stuff, right there.

December 3rd, 2007

C++ Variant

In C++, it can sometimes be useful to have a single variable that can store something from any data type, and then do appropriate conversions when you actually go to use that variable. In Java and C#, everything derives from some base Object, so you can simply typecast if necessary. In C++, you can accomplish the same thing with a void*, but that is error pone and… icky. I’ve made a basic utility class, BasicVariant, to satisfy my needs:

[ftf def="js.xml"]
class BasicVariant
{
public:
BasicVariant()
{
}

template
BasicVariant(const T& val)
{
stringstream stream(stringstream::in | stringstream::out);
stream << val;
stream >> mVariant;
}

template
BasicVariant& operator=(const T& val)
{
stringstream stream(stringstream::in | stringstream::out);
stream << val;
stream >> mVariant;
return *this;
}

template
T GetValue()
{
T ret;
stringstream stream(mVariant, stringstream::in);
stream >> ret;
return ret;
}
private:
string mVariant;
};
[/ftf]

Note: for brevity’s sake, I’m using some bad practices (putting the entire class definition inside the .h, for instance), and I’m not showing the includes, but the general gist is there. The value to be stored is converted to/from a string using a stringstream, and thus anything which overloads the << operator can be stored in a BasicVariant. It's not as generic as having everything inherit from a base Object, but it has served most of my needs.

I should also note here that abusing this, just like abusing general typecasting, is probably a sign of bad design. But I've found some legitimate uses for the class, so I'm going to continue using it.

I like the word gist. Don’t you?