Friday, 6 February 2009

Logging

It's often useful to use logging messages or error messages in case something abnormal happens. Or just for fun. A useful trick is to send them straight to the visual studio output window. Using a small amount of code we can even override std::cout and std::cerr which is pretty cool. I'm just going to dive straight into some code:

class logbuf : public std::streambuf
{
public:
    virtual std::streamsize xsputn( const char *s, std::streamsize n )
    {
        // Output to visual studio output window
        OutputDebugStringW(widen(s).c_str());
        return n;
    }
    virtual int overflow(int c = EOF)
    {
        if(c != EOF) OutputDebugString(widen(std::string(1, c)).c_str());
        return 1;
    }
private:
    int indent_;
    char last_;
};
int main(int argc, const char* argv[])
{
    // Create the new stream to redirect cout and cerr output to vs.
    logbuf merr;
    std::ostream out(&merr);

    // Smart class that will swap streambufs and replace them
    // when object goes out of scope.
    class StreamBuf_Swapper
    {
    public:
        StreamBuf_Swapper( std::ostream& orig, std::ostream& replacement )
            : buf_(orig.rdbuf()), str_(orig)
        { orig.rdbuf( replacement.rdbuf() ); }
        ~StreamBuf_Swapper() { str_.rdbuf( buf_ ); }
    private:
        std::streambuf* buf_;
        std::ostream& str_;
    } cerrswap(std::cerr, out), coutswap(std::cout, out);

    try
    {
        // ... Lots of code
    }
    catch( const std::exception& e )
    {
        std::cerr << e.what();
    }

    return 0;
}

This code takes the streambuf associated with cout and cerr and simply swaps it with a streambuf derived class (vsstream) that prints anything to the visual studio output window. Simple and useful. One other thing to note is that if the output window contains a line formatted:

<filename>(<line number>) : <message>

then it will automatically display that file and line in the editor when double clicked. A few more things you could do with it would be to log errors to a file as well or maybe have something differentiate between normal log messages and error messages (in fact, I'll probably have to do this at some point).

Tuesday, 20 January 2009

Maths

Looking around the Internet, I was a little disappointed to find that blogger doesn't natively support Latex. Fortunately there's a nifty program that you can run on a web server that will output latex code as a png.

Let H \leq G and x, y \in G. Then either xH \cap yH = \emptyset or xH = yH.

The two dimentional gradient vector for a function u(x, y) is, \nabla u=\left(\frac{\partial u}{\partial x},\frac{\partial u}{\partial y}\right).

All made possible thanks to http://www.forkosh.com/mathtex.html - cheers!

Monday, 19 January 2009

Plans

This journal (I hate the word blog) is going to be a place for me to organise my thoughts. I intend to list projects I'd like to do and follow them through development with regular posts showing the problems I encounter. I hope maybe it can be of use to others as well, but that is not my primary motive. Anyway, if you're actually reading this I must have done something right.

The list of projects I'd like to complete:

  • Collada loader
  • Decent text rendering
  • Screen space ambient occlusion (SSAO)
  • A simple memory allocation tracker
  • Light perspective shadow maps
  • Self-shadowed normal mapping
  • Rain on glass shader
  • Efficient perlin noise generated terrain with dynamic LOD
  • Sky dome with vertex shader to interpolate given colours
    • Real time modification of shader inputs

Also my loan came through last night so I went on a shopping spree. I ended up buying:

I'll try and do a brief review of them when I'm done reading.