Graphics Programming

Ray Tracer development and some more. By Pawel Bokota.

Posts Tagged ‘Programming

L-systems continued

leave a comment »

I have extended my previous L-systems implementation by adding more symbols, which allow me to create 3-D figures. This time I used OpenGL for rendering and QT for the GUI. Below is a screenshot of my program and the 3-D “tree” that it generated. Also below, is my code for the L-system class, the heart of the program. The class is responsible for rewriting strings based on the rules and symbols given.

LSystem::LSystem(string start, Rule rule) : rules(rule)
{
    startAxiom = start;
    lang       = "";
}

void LSystem::run(int iterations)
{
    string curr = startAxiom;

    for (int i = 0; i < iterations; i++)
    {
        for (int j = 0; j < curr.size(); j++)
        {
            string buff = rules.getRule(curr[j]);

            if (!buff.empty())
            {
                curr.replace(j, 1, buff);

                j += buff.size() - 1;
            }
        }
    }

    lang = curr;
}

string LSystem::getGenString()
{
    return lang;
}
Advertisements

Written by Pawel

December 30, 2009 at 4:42 pm

Exploring L-systems

leave a comment »

Some time ago I came across an interesting idea about using a variant of a formal grammar, an L-system, to draw fractals and plants. For those familiar with formal grammars an L-system grammar is defined as such:

G = (V, S, \omega, P)

where

V is a set of variables
S is a set of constants
\omega is the start axiom
P is a set of production rules

How does an L-system grammar differ from a formal grammar you ask? The difference lies in the way the production rules are applied. A formal grammar generates a language by applying its production rules one at a time. Whereas an L-system grammar generates an L-system by applying as many rules as it can per iteration. Wikipedia has much more on L-systems and formal grammars if you are interested.

What’s amazing about L-systems is that they’re simple concepts but they’re used to model complex natural phenomena such as the growth of plants. We can visually see what L-systems are capable of if we combine them with computer graphics. To accomplish this we give certain symbols of an L-system extra meaning. For example, if an L-System grammar contains a variable F and constants +, - , we can say that the F also means to draw forward, and the +, - also mean to draw left and right by some angle.

The screenshots below are my implementation of L-systems (developed with Java using Swing for GUI).

This one is called a dragon curve
Dragon Curve

An older screenshot of my program generating a plant:
Plant

Written by Pawel

August 25, 2009 at 6:14 pm

Math routines

leave a comment »

BadScreen

The above is an early screenshot when I had buffer overflow bug in my code.

In my last post I mentioned my top priority was to finish a 4×4 Matrix class to use for my ray tracer. Unfortunately, I have not finished it. However, I did start writing many other classes, such as Point and Transform. I also wrote transform functions for translations, Object-To-World and Camera-To-World. Of course I can’t show these things with a screenshot, so instead I included below the source code interface for my matrix class.

struct Matrix
    {
	public:
		double d[4][4];

		Matrix();
		Matrix(double initd[4][4]);
		Matrix(double d11, double d12, double d13, double d14,
			   double d21, double d22, double d23, double d24,
			   double d31, double d32, double d33, double d34,
			   double d41, double d42, double d43, double d44);

		Matrix inverse() const;

		Matrix operator+(const Matrix& m1) const;
		Matrix& operator+=(const Matrix& m1);
		Matrix operator-(const Matrix& m1) const;
		Matrix& operator-=(const Matrix& m1);
		Matrix operator*(const Matrix& m1) const;
		Matrix& operator*=(const Matrix& m1);

		bool operator==(const Matrix& m1) const;
		bool operator!=(const Matrix& m1) const;
};

I’m using doubles because there is some numerical instability in my ray tracer that I don’t feel like fixing just right now.

Written by Pawel

August 23, 2009 at 1:38 am

Posted in Ray Tracer

Tagged with , , , , ,