L-Systems Code

WB01343_.gif (599 bytes)
#include "glaux.h"
#include <fstream.h>
#include <string.h>

const double PI=3.141592653;

double px=0, py=0, pz=0;
int p=90, r=0, h=0;

char axiom[8192];

struct { char from, to[20]; } rules[10];

int rc=0, it=0;

static void Init() {
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_FLAT);
}

static void Reshape(int w, int h) {
  glViewport(0, 0, GLint(w), GLint(h));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 300.0);
  glMatrixMode(GL_MODELVIEW);
}

static void key_esc() { auxQuit(); }

static void rleft() { r=(r+355)%360; }

static void rright() { r=(r+5)%360; }

static void pdown() { p=(p+355)%360; }

static void pup() { p=(p+5)%360; }

static void hleft() { h=(h+355)%360; }

static void hright() { h=(h+5)%360; }

static void forward() {
  px+=cos(h*PI/180)*sin(p*PI/180);
  pz+=sin(h*PI/180)*sin(p*PI/180);
  py+=cos(p*PI/180);
}

static void back() {
  px-=cos(h*PI/180)*sin(p*PI/180);
  pz-=sin(h*PI/180)*sin(p*PI/180);
  py-=cos(p*PI/180);
}

void show() {
  glLoadIdentity();
  cout << "(" << px << ", " << py << ", " << pz << ")" << endl
       << "H = " << h << "  P = " << p << "  R = " << r << endl;

  glRotated(r, 0.0, 0.0, 1.0);
  glRotated(p, 0.0, 1.0, 0.0);
  glRotated(h, 1.0, 0.0, 0.0);

  glTranslated(-px, -py, -pz);

  glColor3f(1.0, 1.0, 1.0);

  for(int x=0; x<10; x++) {
    glBegin(GL_LINE_STRIP);
    for(int y=0; y<10; y++) glVertex3f(x-5.0, 0, y-5.0);
    glEnd();
  }
  for(int y=0; y<10; y++) {
    glBegin(GL_LINE_STRIP);
    for(int x=0; x<10; x++) glVertex3f(x-5.0, 0, y-5.0);
    glEnd();
  } 

  /* for(int x=0; x<strlen(axiom); x++) {
    switch (axiom[x]) {
      case '*': glBegin(GL_LINES);
                glVertex3f(0.0,0.0,0.0);
                glVertex3f(1.0,0.0,0.0);
                glEnd();
                glTranslatef(1.0,0.0,0.0);
                break;
      case 'U': glRotatef(25.0, 1.0, 0.0, 0.0);
                break;
      case 'D': glRotatef(-25.0, 1.0, 0.0, 0.0);
                break;
      case 'L': glRotatef(25.0, 0.0, 1.0, 0.0);
                break;
      case 'R': glRotatef(-25.0, 0.0, 1.0, 0.0);
                break;      
      case '[': glPushMatrix();
                break;
      case ']': glPopMatrix();
                break;
      default:  break;
    }
  }*/
}   

static void display() {
  glClear(GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  show();
  glPopMatrix();
  glFlush();
  auxSwapBuffers();
}

void setrules(char* fn) {
  ifstream file(fn);
  char temp[10];
  file >> it >> rc;
  file.getline(temp, 10);
  file.getline(axiom, 8092);
  for(int x=0; x<rc; x++) {
    file.get(rules[x].from);
    file.ignore(1);
    file.getline(rules[x].to,20);
  }
  cout << "Iterations: " << it << endl;
  cout << "Rule count: " << rc << endl;
  cout << "Axiom: " << axiom << endl;
  for(int x=0; x<rc; x++)
    cout << rules[x].from << " -> " << rules[x].to << endl;
}
      
void iterate() {
  char temp[8192];
  strcpy(temp, axiom);
  strcpy(axiom, "");
  int x, y;
  for(x=0; x<strlen(temp); x++) {
    for(y=0; y<rc; y++)
      if (temp[x]==rules[y].from) { strcat(axiom, rules[y].to); break; }
    if (y==rc) { int q=strlen(axiom); axiom[q]=temp[x]; axiom[q+1]='\0'; }
  }
  cout << "Rule: " << axiom << endl;
}

void main(int argc, char* argv[]) {
  setrules(argv[1]);
  for (int x=0; x<it; x++) iterate();
  auxInitDisplayMode(AUX_RGBA);
  auxInitPosition(50, 50, 600, 600);
  if (auxInitWindow("3D Landscape")==GL_FALSE) auxQuit();
  Init();
  auxKeyFunc(AUX_LEFT, rleft);
  auxKeyFunc(AUX_RIGHT, rright);
  auxKeyFunc(AUX_UP, pup);
  auxKeyFunc(AUX_DOWN, pdown);
  auxKeyFunc(AUX_j, hleft);
  auxKeyFunc(AUX_l, hright);
  auxKeyFunc(AUX_i, forward);
  auxKeyFunc(AUX_k, back);
  auxExposeFunc(Reshape);
  auxReshapeFunc(Reshape);
  auxMainLoop(display);
}
WB01343_.gif (599 bytes)